zoukankan      html  css  js  c++  java
  • 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 ", fun(s1,s2) ); }
  • 相关阅读:
    找水王
    哈利波特图书购买问题
    中序线索化二叉树[C语言实现及注释]
    第一篇随文。
    理解Python函数中的的return
    记录一款实时同步的软件——Lsyncd
    for循环
    while循环
    文件操作
    我的第一个博客
  • 原文地址:https://www.cnblogs.com/zhangzimu/p/6123099.html
Copyright © 2011-2022 走看看