【算法】大数加减

超长的整型加减法可以用数组int来存储,每个单元可以存储1~9位数字,然后模拟手算过程

大数乘除法,稍复杂,(挖坑)续更..

====================分割线====================

  1 /*************************************************
  2 Copyright: CheerM
  3 Author: CheerM
  4 Date: 2016-11-02
  5 Description: 实现超长int型的加减运算。输入任意长度整数的简单计算式,e.g.A+B,A-B,可获得运算结果 
  6 **************************************************/  
  7 
  8 #ifndef _001_H_
  9 #define _001_H_
 10 
 11 #include <iostream>
 12 #include <string>
 13 #include <vector>
 14 #include <iomanip>
 15 
 16 using namespace std;
 17 
 18 const int max = 1000000000;
 19 
 20 /************************************************* 
 21 Function:       strToInt 
 22 Description:    将字符串表示的整形数字,转为用vector<int>数组存储,因为32位环境下,int最大为2147483647,则可用每一位数组来存储1,000,000,000内的9位数 
 23 Calls:          None
 24 Input:          一个任意长的字符串str,表示原始输入的数字
 25                 一个vector<int>类型的指针num,指向存储转变后的数字的数组
 26 Output:         None 
 27 Return:         int,数组num的长度
 28 *************************************************/ 
 29 int strToInt(const string str, vector<int>& num) {
 30     int digits[9] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};
 31     int sum = 0, len = str.size();
 32     for (int i = 0; i < len; i ++) {
 33         sum += (str[len - 1 - i] - '0') * digits[i % 9];
 34         if (i % 9 == 8) {
 35             num.push_back(sum % max);
 36             sum /= max;
 37         }
 38     }
 39 
 40     if (sum || num.empty()) num.push_back(sum % max);
 41 
 42     vector<int>::iterator it = num.end() - 1;
 43 
 44     /*当且仅当num==0时,最高位可以为0*/
 45     while (it != num.begin()) {
 46         if ((*it) == 0)num.erase(it--);
 47         else break;
 48     }
 49 
 50     return num.size();
 51 }
 52 
 53 
 54 /************************************************* 
 55 Function:       add 
 56 Description:    将字符串表示的整形数字,转为用vector<int>数组存储,模拟手算加法过程 
 57 Calls:          None
 58 Input:          2个任意长的字符串str,表示原始输入的数字
 59 Output:         输出相加结果 
 60 Return:         None
 61 *************************************************/ 
 62 void add(string a, string b) {
 63     vector<int> num1, num2;
 64     int len1 = strToInt(a, num1);
 65     int len2 = strToInt(b, num2);
 66 
 67     int carry = 0, temp = 0;
 68     vector<int> c;
 69     /*模拟手算加法过程*/
 70     for (int i = 0; i < len1 || i < len2; i ++) {
 71         if (i < len1 && i < len2) {
 72             temp = num1[i] + num2[i] + carry;
 73         }
 74         else if (i >= len1) {
 75             temp = num2[i] + carry;
 76         }
 77         else {
 78             temp = num1[i] + carry;
 79         }
 80 
 81         if (temp >= max) {
 82             carry = 1;
 83             temp -= max;
 84         }
 85         else {
 86             carry = 0;
 87         }
 88         c.push_back(temp);
 89     }
 90 
 91     /*输出结果,中间不足9位的数字要左补0来补足9位*/
 92     cout << " = ";
 93     cout << c[c.size() - 1];
 94     for (int i = c.size() - 2; i >= 0; i--)
 95     {
 96         cout << setfill('0') << setw(9);
 97         cout << c[i];
 98     }
 99     cout << endl;
100 }
101 
102 /*************************************************
103 Function:       subtract
104 Description:    将字符串表示的整形数字,转为用vector<int>数组存储,模拟手算减法过程
105 Calls:          None
106 Input:          2个任意长的字符串str,表示原始输入的数字
107 Output:         输出相减结果
108 Return:         None
109 *************************************************/
110 void subtract(string a, string b) {
111     bool negNum = false; /*标记结果是否为负数*/
112     if (a.size() < b.size() || (a.size() <= b.size() && a < b)) negNum = true;
113     vector<int> num1, num2;
114     int len1, len2;
115     if (!negNum) {
116         len1 = strToInt(a, num1);
117         len2 = strToInt(b, num2);
118     }
119     else {
120         len1 = strToInt(b, num1);
121         len2 = strToInt(a, num2);
122     }
123     
124     
125     int borrow = 0, temp = 0;/*借位初始化为0*/
126     vector<int> c;
127     /*模拟手算减法过程*/
128     for (int i = 0; i < len1; i ++) {
129         if (i < len1 && i < len2) {
130             temp = num1[i] - num2[i] - borrow;
131         }
132         else if (i >= len2) {
133             temp = num1[i] - borrow;
134         }
135 
136         if (temp < 0) {
137             borrow = 1;
138             temp += max;
139         }
140         else {
141             borrow = 0;
142         }
143         c.push_back(temp);
144     }
145 
146     /*输出结果,中间不足9位的数字要左补0来补足9位*/
147     cout << " = ";
148     if (negNum) cout << " - ";
149     cout << c[c.size() - 1];
150     for (int i = c.size() - 2; i >= 0; i--)
151     {
152         cout << setfill('0') << setw(9);
153         cout<< c[i];
154     }
155         
156     cout << endl;
157 }
158 
159 void LargeNumOperation(int t) {
160     string a, b;
161     char c;
162     while(t--) {
163         cin >> a >> c >> b;
164         switch (c)
165         {
166         case '+':
167             add(a, b);
168             break;
169         case '-':
170             subtract(a, b);
171             break;
172         default:
173             break;
174         }
175     }
176 }
177 
178 #endif
001.h

测试结果:

原文地址:https://www.cnblogs.com/cheermyang/p/6025075.html