C语言 字符串中数字的运算

主函数中输入字符串"32486"和"12345",在主函数中输出的函数值为44831。

#include  <stdio.h>
#include  <string.h>
#include  <ctype.h>
#define  N  9
long  ctod( char  *s )
{ long  d=0;
  while(*s)
    if(isdigit( *s))  {      // 此出的“isdigit”是“ctype”中的一个函数,用于检查字符串中的字符是否为数字,若是返回1,不是则返回0

      d=d*10+*s-'0';        //  d用于存放字符串中的数字的和(c语言中用于存放和差的先将其置0,乘除的置为1) 因为d的取值是从字符串的高位开始,故每次计数都要×10,以调和位权

      s++;    //    可能会被写成  *s++
     }
  return  d;
}
long fun( char *a, char *b ) { return ctod(a)+ctod(b); a,b是数组名 } 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 );                do..while...句型不常用,do中存放任务,在while中存放条件的取反 printf("The result is: %ld\n", fun(s1,s2) ); }

 

posted @ 2016-12-01 18:32  张子木  阅读(6716)  评论(0编辑  收藏  举报