c语言程序求救 1.字符串程序设计: 写程序将用户输入的由数字字符和非数字字符组成的字符串中数字提取输

2024-10-31 13:27:40
有4个网友回答
网友(1):

不用给分了缓神,我没时间给你写程序了,只能把大体算法告诉你:::数字在ASC2中是连续的。是048到057,可以写一个循环程序在字符串中把在这个范围内的字符复制到一个数组中,然后输出数组就可以了。如果还不行可以找则哪弯我,我孙闷试试帮你。

网友(2):

完全按你的要求做的。
(1)===============
#include
#include
#include
#define MAX 512

int main()
{
int i,j,len,start,end;
char buff[MAX];
char digit[MAX];
printf("Input a string:");
gets(buff);

j=0;
start=0;
end=-1;
len=strlen(buff);
for (i=0;i if (buff[i]>='0' && buff[i]<轮念衡='9')
end=i;
else if (i>0 && buff[i-1]>='0' && buff[i-1]<='9')
{
for (j=start;j<=end;j++)
printf("%c",buff[j]);
printf("\n");
start=i+1;
}
else
start=i+1;
for (j=start;j<高谈len;j++)
printf("%c",buff[j]);
printf("\n");

getch();
}

(2)==============
#include
#include

int main()
{
int i;
char *day[10]={
{""},
{"Monday"},
{"Tuesday"},
{"Wednesday"},
{"Thursday"},
{"Friday"},
{"Saturday"},
{"Sunday"}
};

do
{
printf("input a num between 1 and 7:");
scanf("%d",&i);
}while( !(i>=1 && i<=7));
printf("%s",day[i]);

getch();
}

(3)============
#include
#include
#define MAXSTU 100

struct student
{
char no[5],name[10];
int score[4];
double average;
};

int main()
{
int numstu=0;
struct student stu[MAXSTU];
int readrec(struct student *stu,int numstu);
int writerec(struct student *stu,int numstu);

numstu=2;
readrec(stu,numstu);
writerec(stu,numstu);

getch();
}

int readrec(struct student *stu,int numstu)
{
int i,j,sum=0;
for (i=0;i {
fflush(stdin);
printf("NO=");
gets(stu[i].no);
printf(" name=");
gets(stu[i].name);
sum=0;
for (j=0;j<4;j++)
{
printf(" score[%d]=",j);
scanf("%d",&stu[i].score[j]);
sum+=stu[i].score[j];
}
stu[i].average=1.0*sum/4+0.5;
}
return 1;
}

int writerec(struct student *stu,int numstu)
{
int i,j,sum=0;
for (i=0;i {
printf("student %d\n",i);
printf("\tNO=%s\n",stu[i].no);
printf("\tname=%s\n",stu[i].name);
for (j=0;j<腊做4;j++)
printf("\t\tscore[%d]=%d\n",j,stu[i].score[j]);
printf("\taverage=%.2f\n",stu[i].average);
}
return 1;
}

网友(3):

#include
#define STR_LENGTH 300
void main()
{
char buff[STR_LENGTH];
int i = 0;
printf("请输入一个字符串:(长迅消度小于%d):\n", STR_LENGTH);
gets(buff);
printf("字符串中的所有数字如下:\n");
while (buff[i] != '\0')
{
if (buff[i] >= '0' && buff[i] <= '9')
printf("%c", buff[i]);
++i;
}
printf("\n");
}
ps:由于你的题目要求不全,我姑且理解为把所有的数字输出了,如果你是别的要求,请hi我一下,如果只是哪昌余输出数字的话,完全没必要像楼上那样多定义一个数组来来浪费电脑内存和李滚增加赋值运算浪费效率

网友(4):

#include "stdafx.h"

#include
#include
#include "string.h"

void main(void)
{
char src[100]={0};
int len=0;
int i;
printf("please input a string:");
scanf("%s",&src);
len=strlen(src);

printf("output:");
for(i=0;i {
if(src[i]>=0x30 &&src[i]<=0x39)
printf("%c",src[i]);
}
printf("\n");
}

out put:
please input a string:18skdjfkawi81928fsd
output:1881928
Press any key to continue

void main(void)
{
char *weekname[7]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
int week=0;
printf("please input 1-7:");
scanf("%d",&week);
switch(week)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
printf("%s\n",weekname[week-1]);
break;
default:
printf("Error input date. \n");
break;
}

}

out put:
please input 1-7:1
Monday
------------------
please input 1-7:3
Wednesday

#include
#include
#include "string.h"

#define STU_NUM 2

typedef struct
{ char no[5];
char name[10];
int score[4];
double average;
}STUDENT_STRU;

void readrec(STUDENT_STRU stu[])
{
int i;

for (i=0;i {
printf("please input %d studens info\n",i);
printf("note: code,name,sc1,sc2,sc3,sc4.\n");
scanf("%s %s %d %d %d %d",&stu[i].no,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2],&stu[i].score[3]);
stu[i].average=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2]+stu[i].score[3])/4;
}
}

void writerec(STUDENT_STRU stu[])
{
int i;

for(i=0;i {
printf("the %d sudent: %5s %10s %4d %4d %4d %4d; avg:%5f\n",i+1,stu[i].no,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].average);
//%3d %3d %3d %3d; avg:%f\n
}

}

void main(void)
{
STUDENT_STRU stud[STU_NUM]={0};
readrec(stud);
writerec(stud);

}

out put : 为了方便输只输了两个人的成绩,你可以改上面的宏变为5个。
please input 0 studens info
note: code,name,sc1,sc2,sc3,sc4.
1 sfdk 100 10 100 20
please input 1 studens info
note: code,name,sc1,sc2,sc3,sc4.
2 asdbsdf 30 80 80 30
the 1 sudent: 1 sfdk 100 10 100 20; avg:57.000000
the 2 sudent: 2 asdbsdf 30 80 80 30; avg:55.000000
Press any key to continue

//代写作业应该结贴快吧.