zoukankan      html  css  js  c++  java
  • 大数加法

    #include<iostream>
    
    #include<stdio.h>
    
    #include<string.h>
    
    using namespace std;
    
    char * add(char *a, char*b) {
    
      int la = strlen(a);
    
      int lb = strlen(b);
    
      char result[la+lb+2];
    
      int p_r = 0;
    
      memset(result, 0, sizeof(result));
    
      int crease = 0;
    
      while (la-- && lb--) {
    
        int tmp = int(a[la] - '0' + b[lb] - '0') + crease;
    
        result[p_r] = (tmp % 10) + '0';
    
        crease = tmp / 10;
    
        p_r++;
    
      }
    
      while (la>=0) {
    
        int tmp = crease + int(a[la] - '0');
    
        result[p_r] = (tmp%10) + '0';
    
        crease = tmp/ 10;
    
        p_r++;
    
        la--;
    
      }
    
      while (lb>=0) {
    
        int tmp = crease + int(b[lb] - '0');
    
        result[p_r] = (tmp%10) + '0';
    
        crease = tmp/ 10;
    
        p_r++;
    
        lb--;
    
      }
    
      if ( crease > 0 ){
    
        result[p_r] = crease + '0';
    
        p_r++;
    
      }
    
      result[p_r]='';
    
      for (int i=0; i<strlen(result)/2; ++i) {
    
        char tmp = result[strlen(result)-i-1];
    
        result[strlen(result)-i-1] = result[i];
    
        result[i] = tmp;
    
      }
    
      return result;
    
    }
    int main(int argc, char** argv){
      char *a = "123456";
      char *b = "9999";
      char * result = add(a,b);
      printf ("%s+%s=%s", a, b, result);
      return 0;
    }

    我的博客地址:http://www.yancey.info/?p=129

  • 相关阅读:
    2020软件工程作业00
    2020软件工程作业03
    2020软件工程作业02
    软件工程作业01
    2020软件工程作业06
    2020软件工程作业05
    问题清单
    2020软件工程作业04
    2020软件工程作业02
    2020软件工程作业1
  • 原文地址:https://www.cnblogs.com/yancey/p/3404543.html
Copyright © 2011-2022 走看看