大数加减运算

这里自己利用STL模板中的容器和链表实现字符串大数加减运算.

  1 #include<iostream>
  2 #include<vector>
  3 #include<list>
  4 using namespace std;
  5 
  6 list<char> Input_Number(list<char> ilist)//输入链表字符串,以‘#’作为结束符
  7 {
  8     cout<<"please Input string ,end up with the '#'!"<<endl;
  9     char s;
 10     cin>>s;
 11     while(s!='#')
 12     {
 13         ilist.push_back(s);
 14         cin>>s;
 15     }
 16     return ilist;
 17 }
 18 
 19 void print(list<char> ilist)//打印链表
 20 {
 21     list<char>::iterator ite;
 22     for(ite=ilist.begin();ite!=ilist.end();ite++)
 23         cout<<*ite;
 24     cout<<endl;
 25 }
 26 
 27 void Add(list<char>& ilist1,list<char>& ilist2)//字符串数字‘+’运算
 28 {
 29     int small_size=ilist1.size();//较小链表长度
 30     int big_size=ilist2.size();//较长链表长度
 31     list<char> ilist=ilist2;//ilist始终为最长链表
 32     list<char>::iterator ivlist1=ilist1.end();//链表迭代器指向ilist链尾
 33     list<char>::iterator ivlist2=ilist2.end();
 34 
 35     if(small_size>big_size)
 36     {
 37         ilist=ilist1;
 38         small_size=ilist2.size();
 39         big_size=ilist1.size();
 40     }
 41 
 42     vector<char> iv(big_size+1,'0');//给容器开辟big_size+1个空间,并初始化;
 43     for(int i=0;i<small_size;i++)//先遍历较小链表长度
 44     {
 45         iv[big_size-i] +=*(--ivlist1)+*(--ivlist2)-2*'0';
 46         ilist.pop_back();
 47         if(iv[big_size-i]>'9')
 48         {
 49             iv[big_size-i] -=10;
 50             iv[big_size-i-1]='1';
 51         }
 52     }
 53     for(i=small_size;i<big_size;i++)//从上次断开处,继续遍历较长链表长度
 54     {
 55         iv[big_size-i] +=ilist.back()-'0';
 56         ilist.pop_back();
 57         if(iv[big_size-i]>'9')
 58         {
 59             iv[big_size-i] -=10;
 60             iv[big_size-i-1]='1';
 61         }
 62 
 63     }
 64         if(iv[0]=='0')//如果首字符没有进位,进行首字符删除工作
 65             iv.erase(iv.begin());
 66         vector<char>::iterator ite;//容器iv结果输出
 67         for(ite=iv.begin();ite!=iv.end();ite++)
 68             cout<<*ite;
 69             cout<<endl;
 70 }
 71 
 72 int Cmp(list<char>& ilist1,list<char>& ilist2)//字符串大小比较函数
 73 {
 74     list<char>::iterator ite1=ilist1.begin();
 75     list<char>::iterator ite2=ilist2.begin();
 76     bool flag;
 77     if(ilist1.size()>ilist2.size())
 78         flag=true;
 79     if(ilist1.size()<ilist2.size())
 80         flag=false;
 81     if(ilist1.size()==ilist2.size())
 82     {
 83        while(*ite1==*ite2)
 84        {
 85          *ite1++;
 86          *ite2++;
 87          if(ite1==ilist1.end())
 88          break;
 89        }
 90           flag=(*ite1>=*ite2) ? true : false;
 91 
 92     }
 93     return flag;
 94 }     
 95 
 96 
 97 void Sub(list<char>ilist1,list<char> ilist2)
 98 {
 99     int flag;
100     int big_size=ilist1.size();
101     int small_size=ilist2.size();
102     list<char> ilist_big;//最大链表
103     list<char> ilist_small;//最小链表
104 
105     if(Cmp(ilist1,ilist2))
106     {
107         ilist_big=ilist1;
108         ilist_small=ilist2;
109         flag=1;//输出结果为'+'标志
110     }
111     else
112     {
113         ilist_big=ilist2;
114         ilist_small=ilist1;
115         big_size=ilist2.size();
116         small_size=ilist1.size();
117         flag=0;//输出结果为'-'标志
118 
119     }
120     list<char>::iterator ivlist1=ilist_big.end();
121     list<char>::iterator ivlist2=ilist_small.end();
122     vector<char> iv(big_size,'0');//分配big_size个内存大小,初始都为'0';
123 
124     //计算差值的绝对值
125     for(int i=0;i<small_size;i++)
126     {
127         iv[big_size-i-1] +=(*(--ivlist1)-*(--ivlist2));
128         if(iv[big_size-i-1]<'0')
129         {
130             iv[big_size-i-1] +=10;
131             iv[big_size-i-2] -=1;
132         }
133     }
134     list<char>::iterator ito=ivlist1;
135         for(i=small_size;i<big_size;i++)
136     {
137         iv[big_size-i-1] +=*(--ivlist1)-'0';
138         if(iv[big_size-i-1]<'0')
139         {
140             iv[big_size-i-1] +=10;
141             iv[big_size-i-2] -=1;
142         }
143 
144     }
145         while(*iv.begin()=='0'&&iv.size()>1)
146         {iv.erase(iv.begin());}
147         if(flag==0)
148         iv.insert(iv.begin(),1,'-');
149         vector<char>::iterator ite;
150         for(ite=iv.begin();ite!=iv.end();ite++)
151             cout<<*ite;
152         cout<<endl;
153 }
154 
155 
157 void main()
158 {
159     list<char> ilist1;
160     list<char> ilist2;
161     list<char> list1=Input_Number(ilist1);
162     list<char> list2=Input_Number(ilist2);
163     print(list1);
164     cout<<"+"<<endl;
165     print(list2);
166     Add(list1,list2);
167     cout<<endl;
168     print(list1);
169     cout<<"-"<<endl;
170     print(list2);
171     Sub(list1,list2);
172 }

结果:

转载于:https://www.cnblogs.com/wxdjss/p/5559556.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值