大数加法,减法,乘法

方法:利用string储存一个数字,然后利用加减法的规则进行运算。
加法:各数位对齐,然后进行加减,若相加大于’9’则进位。
减法:方法同加法,但是要处理被减数与减数的大小问题,所以需要多加一个判断条件。
乘法:利用多项式的思想,例如 123 123 = 123 1 1 0 2 + 123 2 1 0 1 + 123 3 1 0 0 = 12300 1 + 1230 2 + 123 3 123*123=123*1*10^2+123*2*10^1+123*3*10^0=12300*1+1230*2+123*3
除法:暂无

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
string result;
// 比较两个string类型数的大小
bool compare(string a, string b) {
 if (a.size() < b.size()) {
  return 1;
 }
 else if (a.size() == b.size()) {
  if (a < b) {
   return 1;
  }
 }
 return 0;
}
// 加法
void add(string a, string b) {
 if (a.size() < b.size()) swap(a, b);
 result = "0" + a;// 前面的0进位用,如果没有进位,后面会删掉这个0
 for (int i = 0; i < b.size(); i++) {
  result.at(a.size() - i) += b.at(b.size() - 1 - i) - '0';
 }
 for (int i = result.size() - 1; i >= 0; i--) {
  if (result.at(i) > '9') {
   result.at(i) = result.at(i) - 10;
   result.at(i - 1) += 1;
  }
 }
 if (result.at(0) == '0')
  result.erase(0, 1);
}
// 减法
void substract(string a, string b) {
 if (a == b) {
  result = "0";
 }
 else if (b.at(0) == '-') {
  b.erase(0, 1);
  add(a, b);
 }
 else {
  bool fuhao = 0;
  if (compare(a, b)) {
   swap(a, b);
   fuhao = 1;
  }
  result = "1" + a;
  for (int i = 0; i < b.size(); i++) {
   result.at(a.size() - i) = result.at(a.size() - i) - b.at(b.size() - 1 - i) + '0';
  }
  for (int i = result.size() - 1; i >= 1; i--) {
   if (result.at(i) < '0') {
    result.at(i) = result.at(i) + 10;
    result.at(i - 1) -= 1;
   }
  }
  result.erase(0, 1);
  if (result[0] == '0') {
   result.erase(0, result.find_first_not_of('0'));// 把多余的0删除
  }
  if (fuhao) {
   swap(a, b);
   result.insert(0, "-");// 负数前面要加个负号“-”
  }
 }
}
// 乘法
void mutiple(string a, string b) {
 result = "";
 string temp;// 用来计算
 string result_ = a;
 for (int i = b.size() - 1; i >= 0; i--) {
  temp = result_;
  for (int j = 0; j < temp.size(); j++) {
   temp.at(j) = (temp.at(j) - '0') * (b.at(i) - '0'); // 记录0到81而不是从'0'开始,因为'81'的ascii码为129 > 128 会溢出
  }
  temp = "0" + temp;// 前面补一位用来进位
  for (int j = temp.size() - 1; j >= 1; j--) {
   if (temp.at(j) > 9) { // 大于9而不是'9'的理由同上
    temp.at(j - 1) = temp.at(j - 1) + (temp.at(j)) / 10;
    temp.at(j) = (temp.at(j)) % 10;
   }
  }
  // 因为result的数字是'0'到'9',所以要还原(此时不会有溢出问题)
  for (int i = 1; i < temp.size(); i++) {
   temp.at(i) += '0';
  }
  add(result, temp);
  result_ += "0";// 原因看原理
 }
 if (result.at(0) == '0') result.erase(0, 1);
}
int main() {
 string a, b;
 cout << "please input num 1 and num2:\n";
 cin >> a >> b;
 int size = a.size() > b.size() ? a.size() : b.size();
 add(a, b);
 cout << " " << setw(size + 1) << a << endl;
 cout << "+" << setw(size + 1) << b << endl;
 for (int i = 0; i <= size + 1; i++) {
  cout << "-";
 }
 cout << endl;
 cout << " " << setw(size + 1) << result << endl;
 cout << endl;
 substract(a, b);
 cout << " " << setw(size + 1) << a << endl;
 cout << "-" << setw(size + 1) << b << endl;
 for (int i = 0; i <= size + 1; i++) {
  cout << "-";
 }
 cout << endl;
 cout << " " << setw(size + 1) << result << endl;
 cout << endl;
 mutiple(a, b);
 size = result.size();
 cout << " " << setw(size + 1) << a << endl;
 cout << "*" << setw(size + 1) << b << endl;
 for (int i = 0; i <= size + 1; i++) {
  cout << "-";
 }
 cout << endl;
 cout << " " << setw(size + 1) << result << endl;
 system("pause");
 return 0;
}

猜你喜欢

转载自blog.csdn.net/CoderMaximum/article/details/86554074
今日推荐