题目:将a和b所指的两个字符串转换成面值相同的整数,并进行相加,将结果作为函数值返回,规定字符串中只含9个的以下数字字符。
例如,主函数中输入字符串:32486和12345,在主函数中输出的函数值为:44831。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define N 9
// 将字符串转换成long型数据
long ctod(char *s){
long d = 0;
while (*s) // 判断是否结束
if (isdigit(*s)){ // 判断是否为数字
d = d * 10 + *s - '0';
s++; // 指针后移一位
}
return d; // 返回转换后的数字
}
// 计算两个字符串的和
long fun(char *a, char *b){
return ctod(a) + ctod(b);
}
int main(){
char s1[N], s2[N];
do{
printf("Input string s1 : ");
gets(s1);
} while (strlen(s1) > N);
do{
printf("Input string s2 : ");
gets(s2);
} while (strlen(s2) > N);
printf("The result is: %ld\n", fun(s1, s2));
}