C二级题,改下呢?

来源:百度知道 编辑:UC知道 时间:2024/05/14 13:08:10
磁盘上有一个名为test1-1.c的C程序文件,其功能是:输入一个字符串,将其中的每一个连续的数字序列看作一个整数,将这些整数检索出来后依次放入一个long int型数组中。请改正程序中的错误,使程序能得出正确的结果。
注意:不要增加或删除行,不要修改程序结构。
源程序如下:*/
#include <stdio.h>
#include <string.h>
main()
{
char str[100], *p=str, help[15];
static long int num[50];
int i=0, k=0, m, sign=0;
printf("\ninput the string:");
scanf("%s", p);
do
{ if(*p>=0 && *p<=9)
{ sign=1;
help[k++]=*p;
}
else if(sign=1)
{ int n=0;
while(--k>=0)
num[i]+=(help[k]-48)*((long int)pow(n++,10));
i++;
k=0;
sign=0;
}
else
p++;
}while(*(p-1)!='\0');
for(m=0; m<i; m++)
printf("%d,", num[m]);
}

#include <stdio.h>
#include <string.h>
main()
{
char str[100], *p=str, help[15];
static long int num[50];
int i=0, k=0, m, sign=0;
printf("\ninput the string:");
scanf("%s", p);
do
{ if(*p>=0 && *p<=9) /*改为: *p>='0' && *p<='9'*/
{ sign=1;
help[k++]=*p; /*改为: help[k++]=*p++ */
}
else if(sign=1) /*改为: sign == 1*/
{ int n=0;
while(--k>=0)
num[i]+=(help[k]-48)*((long int)pow(n++,10));
/*改为: num[i]+=(help[k]-48)*((long int)pow(10, n++))*/
i++;
k=0;
sign=0;
}
else
p++;
}while(*(p-1)!='\0');
for(m=0; m<i; m++)
printf("%d,", num[m]);
}

不用添加其他语句

明天回答吧,睡觉了

#include <stdio.h>
#include <string.h>
main()
{
char str[100], *p=str, help[15];
static long int num[50];
int i=0, k=0, m, sign=0;
prin