大数加法乘法

大数加法乘法:

  1 /*
  2 2015.4
  3 大数加法,乘法
  4 
  5 */
  6 #include <iostream>
  7 #include <string>
  8 #include <vector>
  9 
 10 using namespace std;
 11 #define MAX        99
 12 #define MAXM    200
 13 
 14 void BigNumAdd()
 15 {
 16     char strA[MAX], strB[MAX];
 17     cout << "输入大数A,B: " << endl;
 18     cin >> strA;
 19     cin >> strB;
 20 
 21     int num1[MAX], num2[MAX], k;
 22     int i, j, len1, len2;
 23     for (k = 0; k<MAX; k++)
 24     {
 25         num1[k] = 0;
 26         num2[k] = 0;
 27     }
 28     len1 = strlen(strA);
 29     len2 = strlen(strB);
 30     for (i = len1 - 1, j = 0; i >= 0; i--)     //num[0]保存的是低位
 31         num1[j++] = strA[i] - '0';
 32     for (i = len2 - 1, j = 0; i >= 0; i--)
 33         num2[j++] = strB[i] - '0';
 34     for (i = 0; i<MAX; i++)
 35     {
 36         num1[i] += num2[i];
 37         if (num1[i]>9)
 38         {
 39             num1[i] -= 10;
 40             num1[i + 1]++;
 41         }
 42     }
 43 
 44     for (i = (MAX - 1); (i >= 0) && num1[i] == 0; i--); //使第一位不为0
 45     if (i >= 0)
 46     for (; i >= 0; i--)
 47         cout << num1[i];
 48     else
 49         cout << "0" << endl;
 50     cout << endl;
 51 }
 52 
 53 void BigNumMultiply()
 54 {
 55     int i;
 56     string s;
 57     vector<int> a, b;
 58     cout << "输入大数A,B: " << endl;
 59     cin >> s;
 60 
 61     // 容量不足就扩大
 62     a.reserve(s.size());
 63     for (i = 0; i < (int)s.size(); ++i)
 64     {
 65         // 在尾部依次添加
 66         a.push_back(s[i] - '0');
 67     }
 68     cin >> s;
 69     b.reserve(s.size());
 70     for (i = 0; i < (int)s.size(); ++i)
 71     {
 72         b.push_back(s[i] - '0');
 73     }
 74 
 75     // 存放结果的容器, 初始值为0
 76     vector<int> c(a.size() + b.size() - 1, 0);
 77     int j, k, temp;
 78 
 79     for (i = 0; i < (int)a.size(); ++i)
 80     {// 通过该i,j循环,从高位开始相乘,结果同位相加
 81         k = i;
 82         for (j = 0; j < (int)b.size(); ++j)
 83         {
 84             c[k++] += a[i] * b[j];
 85         }
 86     }
 87 
 88     for (k = c.size() - 1; k >= 0; --k)
 89     {
 90         if (c[k] > 9)
 91         {
 92             if (k != 0)
 93             {
 94                 c[k - 1] += c[k] / 10;
 95                 c[k] %= 10;
 96             }
 97             else
 98             {
 99                 temp = c[k] / 10;
100                 c[k] %= 10;
101                 // 添加到最前面
102                 c.insert(c.begin(), temp);
103             }
104         }
105     }
106 
107     for (i = 0; i < (int)c.size(); ++i)
108     { // 输出
109         cout << c[i];
110     }
111     cout << endl;
112 }
113 
114 int main()
115 {
116     cout << "大数加法,乘法:" << endl;
117     cout << "选择: 1——加法  2——乘法  3——退出" << endl;
118     int select = 0;
119 
120     while (1)
121     {
122         cout << "输入选择: ";
123         cin >> select;
124         if (select == 1)
125             BigNumAdd();
126         else if (select == 2)
127             BigNumMultiply();
128         else if (select == 3)
129             break;
130         cout << endl;
131     }
132 
133     return 0;
134 }

 

posted @ 2015-05-11 18:36  ht-beyond  阅读(243)  评论(0编辑  收藏  举报