大整数加法

来源:互联网 发布:设计师导航源码 编辑:程序博客网 时间:2024/05/04 10:47
方法:这里用了数据结构栈,实际上栈更方便实现高精度加法。

步骤:1、第一个数据加数按输入顺序(高位到低位)入栈1。此时栈顶为最低位

            2、‍第二个数据加数按输入顺序(高位到低位)入栈2。此时栈顶为最低位

            3、将栈1、栈2均pop出栈顶做加法,并考虑进位,结果入栈3,这时栈3正好是低位入栈。

            4、处理多余的栈1、栈2。

            5、直接pop出栈3,即正好的从高位到低位的结果。

 完整的实现代码如下:

[cpp] view plaincopy
  1. #include "iostream"  
  2. #include "stack"  
  3. using namespace std;  
  4.   
  5. stack<int>s1;  
  6. stack<int>s2;  
  7. stack<int>s3;  
  8. char c1[100];  
  9. char c2[100];  
  10.   
  11. int main(void)  
  12. {  
  13.     printf("请输入第一个加数:");  
  14.     cin>>c1;  
  15.     printf("请输入第二个加数:");  
  16.     cin>>c2;  
  17.     int len1=strlen(c1);  
  18.     int len2=strlen(c2);  
  19.     for(int i=0;i<len1;i++)  
  20.     {  
  21.         s1.push(c1[i]-'0');            //按输入顺序(高位到低位)入栈1,此时栈顶为最低位  
  22.     }  
  23.     for(int i=0;i<len2;i++)  
  24.     {  
  25.         s2.push(c2[i]-'0');            //按输入顺序(高位到低位)入栈2,此时栈顶为最低位  
  26.     }  
  27.   
  28.     int tmp=0;  
  29.     while(!s1.empty() && !s2.empty())  
  30.     {  
  31.         tmp += s1.top()+s2.top();   // 将栈1、栈2均pop出栈顶做加法,并考虑进位,结果入栈3,这时栈3正好是低位入栈  
  32.         s1.pop();  
  33.         s2.pop();  
  34.         s3.push(tmp%10);  
  35.         tmp = tmp/10;  
  36.     }  
  37.     while(!s1.empty())   //处理多余的栈1  
  38.     {  
  39.         tmp += s1.top();  
  40.         s1.pop();  
  41.         s3.push(tmp%10);  
  42.         tmp = tmp/10;  
  43.     }  
  44.     while(!s2.empty())   //处理多余的栈2  
  45.     {  
  46.         tmp += s2.top();  
  47.         s2.pop();  
  48.         s3.push(tmp%10);  
  49.         tmp = tmp/10;  
  50.     }  
  51.     if(tmp)        //处理多余的进位  
  52.     {  
  53.         s3.push(tmp);  
  54.     }  
  55.   
  56.     printf("两个数相加的结果为:");  
  57.     while(!s3.empty())    //直接pop出栈3,即正好的从高位到低位的结果  
  58.     {  
  59.         cout<<s3.top();  
  60.         s3.pop();  
  61.     }  
  62.     cout<<endl;  
  63.     system("pause");  
  64.     return 0;  
  65. }  

运行效果图如下:


转自:http://blog.csdn.net/hackbuteer1/article/details/6709101

// addMethord.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include <stack>
#include "iostream"
using namespace std;  


stack<int> s1;
stack<int> s2;
stack<int> s3;
char c1[9000000000];
char c2[9000000000];


int main(int argc, char* argv[])
{
printf("input first !\n");
cin >> c1;
printf("input second !\n");
cin >> c2;


int len1 = strlen(c1);
int len2 = strlen(c2);


printf("len1 = %d\n", len1);
printf("len2 = %d\n", len2);
for (int i = 0; i < len1; i++) {
s1.push(c1[i] - '0');
}


for (int j = 0; j < len2; j++) {
s2.push(c2[j] - '0');
}


int tmp = 0;
while(!s1.empty() && !s2.empty()) {
tmp += s1.top() + s2.top();
s1.pop();
s2.pop();
s3.push(tmp % 10);
tmp = tmp / 10;
}
while(!s1.empty()) {
tmp += s1.top();
s1.pop();
s3.push(tmp % 10);
tmp = tmp / 10;
}
while(!s2.empty()) {
tmp += s2.top();
s2.pop();
s3.push(tmp % 10);
tmp = tmp / 10;
}
if(tmp) {
s3.push(tmp);
}
printf("The result length = %d\n", s3.size());
printf("The add result = ");
while(!s3.empty()) {
cout << s3.top();
s3.pop();
}
cout << endl;
system("pause");
return 0;
}



原创粉丝点击