大數加減運算

這裏本身利用STL模板中的容器和鏈表實現字符串大數加減運算.ios

  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 }

結果:函數

相關文章
相關標籤/搜索