大整数的加法

数据结构设计

Struct BigIngter{

  int digit[1000];

  int size;

Void init() //清零,全为0

Void change(char str[]) {

  // 输入的数倒放在digit[]

}

BigIngter operator+ (cosnt BigIngter &A) const{

   //重载 注意进位

   //要判断最后一个进位是否大于0 digit[key++] = cry;

}

关键是要模拟加法的计算将数倒序放在数组中可将最后是否进位模拟出来,也可正放数组,不过要在digit[0]留出一位,来填最后的进位

#include <stdio.h>
#include <string.h>
using namespace std;
struct BigIngter{
  int digit[1000];
  int size;
  void init(){
      for(int i = 0; i < size; i++){
          
          digit[i] = 0;
      }
}
   void change(char str[])
   {
       init();
        int l = strlen(str);
        size = l;
        int lg = 0;
        for(int i = size-1; i >= 0; i--){
            
               digit[lg] = str[i] - '0';
               lg++;
               printf("%d  ",digit[i]);
        }
       
       
       
       
   }
      
      BigIngter operator+ (const BigIngter &A) const{
        BigIngter tmp;
        tmp.init();
      
        int key,cry = 0;
        if(size >= A.size) key = size;
        else key = A.size;
        for(int i = 0; i < key; i++){
       int t = digit[i] + A.digit[i] + cry;
            tmp.digit[i] = t % 10;
            cry = t / 10;
     }
        if(cry > 0) tmp.digit[key++] = cry;
       tmp.size = key;
        return tmp;
  }
  void show(){
      for(int i = size -1; i >= 0; i--){
          printf("%d",digit[i]);
      
      }
          printf("\n");
  }
    
}a,b,c;
int main(){
  char str1[1000];
  char str2[1000];
  scanf("%s",str1);
  scanf("%s",str2);
  a.init();
  b.init();
  c.init();
  a.change(str1);
  b.change(str2);
  a.show();
  b.show();
  c = a + b;
  c.show();
 
}
 
 

转载于:https://www.cnblogs.com/jerrmylei/p/4355386.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值