给定程序中,函数 fun 的功能是将 a 和 b 所指的两个字符串分别转换成面值相同的整数,并进行相加作为函数值返回,规定字符串中只含 9 个以下数字字符。 例如,主函数中输入字符串 "32486" 和 "12345" ,在主函数中输出的函数值为 44831
1 #include<stdio.h> 2 int fun(char *p,char *p1) 3 { 4 char *ch,*ch1; 5 int sum=0,temp=0; 6 ch=p,ch1=p1; 7 while(*ch) //抓取每个位置上的字符转换成数字 8 { 9 temp=temp*10+(*ch-'0'); //每次累加一个数字后乘十移位 10 ch++; 11 } 12 sum+=temp; //将第一个字符串的数先累加到sum 13 temp=0; //归零进行下一次运算 14 while(*ch1) 15 { 16 temp=temp*10+(*ch1-'0'); 17 ch1++; 18 } 19 return (sum+=temp); 20 } 21 int main() 22 { 23 char a[100],b[100]; 24 gets(a); 25 gets(b); 26 printf("%d",fun(a,b)); 27 return 0; 28 }