How to deal with extremely long numbers in C++ (long long type integers cannot be stored)

How to deal with extremely long numbers in C++ (long long type integers cannot be stored)

In C++, if the number exceeds the range of the long long type, you can consider using strings or third-party libraries (such as Boost.Multiprecision) to represent and handle extremely long numbers. To use third-party libraries, you need to download and install the required third-party libraries, which will not be introduced here.

Here's an introduction to using strings to represent and handle very long numbers. This article will introduce the use of C++ strings to implement extremely long number addition, subtraction, multiplication and division operations.

1. Determine whether a non-negative very large number is odd or even. The source code is as follows:

#include <iostream>  
#include <string>
using namespace std;  

int main() {  
//string s = "123456789123456789123456789";
cout << "请输入一个正整数:" << endl;
    cin >> s;
    // s.size()是字符串的长度,即由多少字符组成的, 字符串的最后一位字符即s[s.size() - 1]
    char c = s[s.size() - 1];
    cout << "最后一位数字是" << c << endl;  
    // 将一个整数数字字符变成整数,我们只需要将它减字符'0'
    int i = c - '0';
    if (i % 2 == 0) {
        cout << s << "是偶数";
    } else {
        cout << s << "是奇数";
    }
        
    return 0;  
}

2. Determine the size of two very large positive integers

#include <iostream>  
#include <string>  
  
using namespace std;  
  
bool Lower(string str1, string str2){     
    //长度长的一定大(这里假设除了0以外,都没有前导0),长度相等则字典序小的数字更小
    return str1.size()<str2.size()||(str1.size()==str2.size()&&str1<str2);
} 
  
int main() {  
    cout << "请输入两个超大正整数:" << endl;
	string a, b; 
	cin >> a >> b; 
    if (Lower(a, b)){
    	cout << a << "小于" << b << endl;
	}
	else{
		cout << a << "大于" << b << endl;
	}
	  
    return 0;  
}

The implementation methods of addition, subtraction, multiplication and division operations are introduced below. In particular, addition and subtraction operations are limited to non-negative integer operations, because the addition and subtraction operations of negative numbers can be equivalent to some form of addition or subtraction operations, so they are not considered; multiplication and division operations only consider large integer operations, not decimals. Calculation; division only considers large integer operations, and the calculation result is accurate to 6 decimal places (it is directly discarded after 6 decimal places).

Reference https://blog.songjiahao.com/archives/382

3. Addition of non-negative large integers

The implementation of large number addition imitates our calculation method of vertical expressions. Starting from the ones digit, adding one by one, just consider the carry each time. Since the 0th position of the read string is the highest bit, we access the string in reverse order and then calculate each bit. Similarly, because each bit obtained during calculation is in reverse order, the final result must be reversed.

Addition may result in one more final digit (carry from the highest bit), so remember to handle it.

Finally, let's consider some special cases. For example, if both numbers are 0, or one of them is 0, we can get the result quickly and save the traversal process.

Implement code

#include <iostream>  
#include <string>  
#include <algorithm>

using namespace std; 

//判断是否为0,全部为0的数字就是0,比如00000
bool CheckZero(const string &str){      //检查是否等于0
    int size=str.size();                //如果全是0则为0,这里假设不会有带符号的+00000或者-00000作为输入
    for(int i=0;i<size;i++)
        if(str[i]!='0') return false;
    return true;
}

string Add(string str1, string str2){
    //关于0的处理
    if(CheckZero(str1)&&CheckZero(str2)) return "0";    //如果都是0
    else if(CheckZero(str1)) return str2;               //如果有个一为0
    else if(CheckZero(str2)) return str1;
    string ans;
    int i=str1.size()-1,j=str2.size()-1;
    int flag=0;                                         //进位标记
    while(i>=0||j>=0||flag){
        int numa=i>=0?str1[i--]-'0':0;                  //如果所有位都访问完毕,对应的位置用0代替
        int numb=j>=0?str2[j--]-'0':0;
        flag=numa+numb+flag;                            //计算两位的和
        ans+='0'+flag%10;                               //取个位保存在答案中
        flag/=10;                                       //计算进位
    }
    reverse(ans.begin(),ans.end());
    return ans;
}

int main(){
	cout << "请输入两个正整数:" << endl;
	string a, b;
	cin >> a >> b;
	string result = Add(a, b);  
    cout << "和:" << result << endl;  
    return 0;  
}

4. Subtraction of non-negative large integers

The implementation of large number subtraction also imitates our calculation method of vertical expressions. Starting from the ones digit, one digit is calculated each time. First, the borrowed digit is subtracted based on whether the next digit is borrowed, and then it is judged whether the current digit is subtracted enough. If a borrowed digit is needed, the previous digit is borrowed and then subtracted. until the operation is completed. Similarly, because we have to calculate from the ones digit, the calculated results must be in reverse order. Finally, we must remember to reverse the results.

During subtraction, leading 0s may appear. Remember to clear leading zeros.

Finally, let's consider some special cases, such as two numbers being the same or one number being 0. We can get the result directly, thus avoiding complicated processing.

Implement code

#include <iostream>  
#include <string>  
#include <algorithm>

using namespace std; 

//判断两个超大正整数的大小 
bool Lower(string str1, string str2){     
    //长度长的一定大(这里假设除了0以外,都没有前导0),长度相等则字典序小的数字更小
    return str1.size()<str2.size()||(str1.size()==str2.size()&&str1<str2);
} 

//判断是否为0,全部为0的数字就是0,比如00000
bool CheckZero(const string &str){      //检查是否等于0
    int size=str.size();                //如果全是0则为0,这里假设不会有带符号的+00000或者-00000作为输入
    for(int i=0;i<size;i++)
        if(str[i]!='0') return false;
    return true;
}

string Sub(string str1, string str2){
    //处理0的情况
    if(str1==str2||(CheckZero(str1)&&CheckZero(str2))) return "0";  //如果两数相等或者都是0
    else if(CheckZero(str1)) return "-"+str2;                       //如果第一个数字为0
    else if(CheckZero(str2)) return str1;                           //如果第二个数字为0
    //定正负
    int negative=0;                     //结果的正负号
    if(Lower(str1,str2)){
        swap(str1,str2);                //保证str1大于str2
        negative=1;                     //如果str1小于str2,则结果过为负值
    }
    string ans;
    int i=str1.size()-1,j=str2.size()-1;//逆序开始处理
    int flag=0;                         //借位标记
    while(i>=0||j>=0){
        int numa=i>=0?str1[i--]-'0':0;  //取每一位,因为长度可能不同所以当某一个已经读取完毕时对应位置取0
        int numb=j>=0?str2[j--]-'0':0;
        numa-=flag;                     //先减去借位
        if(numa<numb){                  //如果不够减则向上一位借位(只可能借一位)
            numa+=10;                   //借位并记录借位
            flag=1;
        }
        else flag=0;                    //如果不借位,则借位标记为0
        ans+='0'+numa-numb;             //计算当前位置并保存
    }
    i=ans.size()-1;
    while(ans[i]=='0') i--;
    ans=ans.substr(0,i+1);              //去除前导0,如111-110=1
    if(negative) ans+='-';              //如果计算结果是负数,添加负数符号
    reverse(ans.begin(),ans.end());     //因为是逆序计算得到的结果,所以需要翻转一下
    return ans;
}

int main(){
	cout << "请输入两个正整数:" << endl;
	string a, b;
	cin >> a >> b;
	string result = Sub(a, b);  
    cout << "差: " << result << endl;     
    return 0;  
}

5. Multiplication of large integers

The implementation of large number multiplication also uses our vertical calculation method. Starting from the ones digit, calculate the product of the multiplicand and the multiplier one digit each time, and then use the large number addition we wrote to achieve the accumulation of the final result. However, multiplication of large numbers needs to consider the issue of positive and negative, so the positive and negative signs need to be processed. Using XOR on the signs of two numbers can ultimately determine the positive or negative of the product result.

Multiplication, because each digit of the multiplier has a corresponding weight (one hundred million), so when we calculate the product of each digit of the multiplier, we must consider the weight of that position, and add the corresponding value after the product The number of zero is enough.

Finally, we consider some special cases. For example, as long as one of the two numbers is 0, the result is 0.

Implement code

#include <iostream>  
#include <string>  
#include <algorithm>

using namespace std; 

//判断两个超大正整数的大小 
bool Lower(string str1, string str2){     
    //长度长的一定大(这里假设除了0以外,都没有前导0),长度相等则字典序小的数字更小
    return str1.size()<str2.size()||(str1.size()==str2.size()&&str1<str2);
} 

//判断是否为负,只需要判断第一位是不是负号即可,这里不考虑正号的存在,即认为不使用正号
bool CheckNegative(const string &str){  //检查是否为负数
    return str[0]=='-';
}

//判断是否为0,全部为0的数字就是0,比如00000
bool CheckZero(const string &str){      //检查是否等于0
    int size=str.size();                //如果全是0则为0,这里假设不会有带符号的+00000或者-00000作为输入
    for(int i=0;i<size;i++)
        if(str[i]!='0') return false;
    return true;
}

string Add(string str1, string str2){
    //关于0的处理
    if(CheckZero(str1)&&CheckZero(str2)) return "0";    //如果都是0
    else if(CheckZero(str1)) return str2;               //如果有个一为0
    else if(CheckZero(str2)) return str1;
    string ans;
    int i=str1.size()-1,j=str2.size()-1;
    int flag=0;                                         //进位标记
    while(i>=0||j>=0||flag){
        int numa=i>=0?str1[i--]-'0':0;                  //如果所有位都访问完毕,对应的位置用0代替
        int numb=j>=0?str2[j--]-'0':0;
        flag=numa+numb+flag;                            //计算两位的和
        ans+='0'+flag%10;                               //取个位保存在答案中
        flag/=10;                                       //计算进位
    }
    reverse(ans.begin(),ans.end());
    return ans;
}

string Mul(string str1, string str2){
    if(CheckZero(str1)||CheckZero(str2)) return "0";    //如果有一个为0,则结果为0
 
    int negative=0,negastr1=0,negastr2=0;               //定正负
    if(CheckNegative(str1)){                            //确定正负号标记,并且去掉-字符
        negastr1=1; str1=str1.substr(1,str1.size()-1);
    }
    if(CheckNegative(str2)){
        negastr2=1; str2=str2.substr(1,str2.size()-1);
    }
    negative=negastr1^negastr2;                         //异或运算确定结果的正负号
 
    string ans;
    if(Lower(str1,str2)) swap(str1,str2);               //保证str1大于等于str2
    int size1=str1.size(),size2=str2.size();
    for(int i=size2-1;i>=0;i--){                        //遍历较小数字的每一位
        string temp(size2-1-i,'0');                     //temp为str1乘以str2[i]的积,根据str2[i]的权重(个十百千万,补充对应个数的0)
        int flag=0;                                     //进位标记
        for(int j=size1-1;j>=0;j--){                    //temp
            flag+=(str1[j]-'0')*(str2[i]-'0');
            temp.push_back('0'+(flag%10));
            flag/=10;
        }
        if(flag) temp.push_back('0'+flag);              //如果最高位还有进位
        reverse(temp.begin(),temp.end());
        ans=Add(ans,temp);                              //将计算结果累加到最终的结果上
    }
    if(negative) ans="-"+ans;                           //处理结果的正负号
    return ans;
}

int main(){
	cout << "请输入两个整数:" << endl;
	string a, b;
	cin >> a >> b;
	string result = Mul(a, b);  
    cout << "积:" << result << endl;     
    return 0;  
}

6. Division of large integers, the calculation result is accurate to 6 decimal places (directly discard after 6 decimal places)

Such as business 3.700014[5800933124]

The implementation of large number division is also calculated using our division formula method. First, use the XOR method to determine the sign of the result. When dividing two numbers, if the first number is greater than or equal to the second number, the result must be greater than or equal to 1, otherwise it is less than 1. Therefore, in order to achieve a result representation less than 1, we make the result accurate to 6 decimal places. The method used here is: determine in advance whether it is a pure decimal, then add 6 zeros at the end of the first number, and then use our division formula method to calculate. Starting from the head of the first number, find the first number whose length can be used for quotient calculation, calculate the quotient and add one digit to the temporary remainder to calculate the next quotient until the calculation is completed.

In division, you may encounter a situation where the divisor is 0, so you need to make a decision in advance during calculation. In addition, during the calculation process, the quotient calculation method uses the subtraction operation of large numbers, so you may encounter the situation of 0 accumulation (increased length), which will affect the comparison and determination of size, so you should pay attention to handling.

Finally, we consider some special cases. For example, when the dividend is 0, the result 0.000000 can be directly output.

Implement code

#include <iostream>  
#include <string>  
#include <algorithm>

using namespace std; 

//判断两个超大正整数的大小 
bool Lower(string str1, string str2){     
    //长度长的一定大(这里假设除了0以外,都没有前导0),长度相等则字典序小的数字更小
    return str1.size()<str2.size()||(str1.size()==str2.size()&&str1<str2);
} 

//判断是否为负,只需要判断第一位是不是负号即可,这里不考虑正号的存在,即认为不使用正号
bool CheckNegative(const string &str){  //检查是否为负数
    return str[0]=='-';
}

//判断是否为0,全部为0的数字就是0,比如00000
bool CheckZero(const string &str){      //检查是否等于0
    int size=str.size();                //如果全是0则为0,这里假设不会有带符号的+00000或者-00000作为输入
    for(int i=0;i<size;i++)
        if(str[i]!='0') return false;
    return true;
}

//大数减法
string Sub(string str1, string str2){
    //处理0的情况
    if(str1==str2||(CheckZero(str1)&&CheckZero(str2))) return "0";  //如果两数相等或者都是0
    else if(CheckZero(str1)) return "-"+str2;                       //如果第一个数字为0
    else if(CheckZero(str2)) return str1;                           //如果第二个数字为0
    //定正负
    int negative=0;                     //结果的正负号
    if(Lower(str1,str2)){
        swap(str1,str2);                //保证str1大于str2
        negative=1;                     //如果str1小于str2,则结果过为负值
    }
    string ans;
    int i=str1.size()-1,j=str2.size()-1;//逆序开始处理
    int flag=0;                         //借位标记
    while(i>=0||j>=0){
        int numa=i>=0?str1[i--]-'0':0;  //取每一位,因为长度可能不同所以当某一个已经读取完毕时对应位置取0
        int numb=j>=0?str2[j--]-'0':0;
        numa-=flag;                     //先减去借位
        if(numa<numb){                  //如果不够减则向上一位借位(只可能借一位)
            numa+=10;                   //借位并记录借位
            flag=1;
        }
        else flag=0;                    //如果不借位,则借位标记为0
        ans+='0'+numa-numb;             //计算当前位置并保存
    }
    i=ans.size()-1;
    while(ans[i]=='0') i--;
    ans=ans.substr(0,i+1);              //去除前导0,如111-110=1
    if(negative) ans+='-';              //如果计算结果是负数,添加负数符号
    reverse(ans.begin(),ans.end());     //因为是逆序计算得到的结果,所以需要翻转一下
    return ans;
}

string Div(string str1, string str2){
    //处理除数为0的情况和被除数为0的情况
    if(CheckZero(str2)) return "The divisor cannot be zero!";
    else if(CheckZero(str1)) return "0.000000";
 
    int negative=0,negastr1=0,negastr2=0;               //定正负
    if(CheckNegative(str1)){                            //确定正负号标记,并且去掉-
        negastr1=1; str1=str1.substr(1,str1.size()-1);
    }
    if(CheckNegative(str2)){
        negastr2=1; str2=str2.substr(1,str2.size()-1);
    }
    negative=negastr1^negastr2;                         //异或运算确定结果的正负号
 
    int point=0;                                        //结果是否为纯小数
    if(Lower(str1,str2)) point=1;                       //如果str1小于str2,则计算为纯小数
    string ans;                                         //计算结果
    str1+=string(6,'0');                                //补足6个0,用于计算小数位
 
    int size1=str1.size(),size2=str2.size();
    int i=size2-1;                                      //商第一位的位置
    string temp=str1.substr(0,i);                       //从str1上取size2-1个字符
    for(i;i<size1;i++){
        temp+=str1[i];                                  //从后边拿出一位,预先处理可以防止结尾处越界
        int cnt=0;                                      //当前位的商,也就是temp中包含了多少个str2,使用减法                                          //如果temp不为0,则计算商
        while(Lower(str2,temp)||temp==str2){            //如果当前位商不为0,则计算商
            temp=Sub(temp,str2);
            cnt++;
        }
        if(temp=="0") temp.clear();                     //如果某次计算结果为0,则清空,避免0的堆积,比如111000 111
        ans.push_back('0'+cnt);                         //保存商
    }
    i=0;
    while(ans[i]=='0') i++;
    ans=ans.substr(i,ans.size()-i);                     //去除前导0
    if(point){                                          //如果是纯小数,补足6位并添加小数点
        int len=6-ans.size();
        ans="0."+string(len,'0')+ans;
    }
    else ans.insert((ans.end()-6),'.');                 //如果不是小数,则只需要插入小数点       
    if(negative) ans="-"+ans;                           //最后一步骤,如果是负数带上负号
    return ans;
}

int main(){
	cout << "请输入两个整数:" << endl;
	string a, b;
	cin >> a >> b;
	string result = Div(a, b);  
    cout << "商(6位小数后直接舍去):" << result << endl;     
    return 0;  
}

7. Finally, integrate into the four arithmetic operations of large numbers

Addition and subtraction operations are limited to non-negative integer operations, because the addition and subtraction operations of negative numbers can be equivalent to some form of addition or subtraction operations, so they are not considered; multiplication and division operations only consider large integer operations, not decimal calculations; division Only large integer operations are considered, and the calculation results are accurate to 6 decimal places (directly discarding after 6 decimal places). The source code is as follows:

#include <iostream>  
#include <string>  
#include <algorithm>
  
using namespace std;  
  
//大数四则运算,两个参数都不能为空
string Add(string str1, string str2);       //大数加法
string Sub(string str1, string str2);       //大数减法
string Mul(string str1, string str2);       //大数乘法
string Div(string str1, string str2);       //大数除法
bool Lower(string str1, string str2);       //大数比较(小于)
bool CheckZero(const string &str);          //检查是不是0,比如0000认为是0
bool CheckNegative(const string &str);      //检查是不是负数
void ShowMenu();                            //提示菜单
void ShowMenu(char choice);                 //二级菜单
int main(){
    string a,b;
    char ch;
    ShowMenu();
    while(cin>>ch&&ch!='q'){                //循环打印菜单并提示用户输入
        ShowMenu(ch);
        cin>>a>>b;
        switch(ch){
            case 'a':cout<<a<<" + "<<b<<" = "<<Add(a,b)<<endl;break;
            case 'b':cout<<a<<" - "<<b<<" = "<<Sub(a,b)<<endl;break;
            case 'c':cout<<a<<" * "<<b<<" = "<<Mul(a,b)<<endl;break;
            case 'd':cout<<a<<" / "<<b<<" = "<<Div(a,b)<<endl;break;
        }
        ShowMenu();
    }
    return 0;
}
string Add(string str1, string str2){
    //关于0的处理
    if(CheckZero(str1)&&CheckZero(str2)) return "0";    //如果都是0
    else if(CheckZero(str1)) return str2;               //如果有个一为0
    else if(CheckZero(str2)) return str1;
    string ans;
    int i=str1.size()-1,j=str2.size()-1;
    int flag=0;                                         //进位标记
    while(i>=0||j>=0||flag){
        int numa=i>=0?str1[i--]-'0':0;                  //如果所有位都访问完毕,对应的位置用0代替
        int numb=j>=0?str2[j--]-'0':0;
        flag=numa+numb+flag;                            //计算两位的和
        ans+='0'+flag%10;                               //取个位保存在答案中
        flag/=10;                                       //计算进位
    }
    reverse(ans.begin(),ans.end());
    return ans;
}
string Sub(string str1, string str2){
    //处理0的情况
    if(str1==str2||(CheckZero(str1)&&CheckZero(str2))) return "0";  //如果两数相等或者都是0
    else if(CheckZero(str1)) return "-"+str2;                       //如果第一个数字为0
    else if(CheckZero(str2)) return str1;                           //如果第二个数字为0
    //定正负
    int negative=0;                     //结果的正负号
    if(Lower(str1,str2)){
        swap(str1,str2);                //保证str1大于str2
        negative=1;                     //如果str1小于str2,则结果过为负值
    }
    string ans;
    int i=str1.size()-1,j=str2.size()-1;//逆序开始处理
    int flag=0;                         //借位标记
    while(i>=0||j>=0){
        int numa=i>=0?str1[i--]-'0':0;  //取每一位,因为长度可能不同所以当某一个已经读取完毕时对应位置取0
        int numb=j>=0?str2[j--]-'0':0;
        numa-=flag;                     //先减去借位
        if(numa<numb){                  //如果不够减则向上一位借位(只可能借一位)
            numa+=10;                   //借位并记录借位
            flag=1;
        }
        else flag=0;                    //如果不借位,则借位标记为0
        ans+='0'+numa-numb;             //计算当前位置并保存
    }
    i=ans.size()-1;
    while(ans[i]=='0') i--;
    ans=ans.substr(0,i+1);              //去除前导0,如111-110=1
    if(negative) ans+='-';              //如果计算结果是负数,添加负数符号
    reverse(ans.begin(),ans.end());     //因为是逆序计算得到的结果,所以需要翻转一下
    return ans;
}
string Mul(string str1, string str2){
    if(CheckZero(str1)||CheckZero(str2)) return "0";    //如果有一个为0,则结果为0
 
    int negative=0,negastr1=0,negastr2=0;               //定正负
    if(CheckNegative(str1)){                            //确定正负号标记,并且去掉-字符
        negastr1=1; str1=str1.substr(1,str1.size()-1);
    }
    if(CheckNegative(str2)){
        negastr2=1; str2=str2.substr(1,str2.size()-1);
    }
    negative=negastr1^negastr2;                         //异或运算确定结果的正负号
 
    string ans;
    if(Lower(str1,str2)) swap(str1,str2);               //保证str1大于等于str2
    int size1=str1.size(),size2=str2.size();
    for(int i=size2-1;i>=0;i--){                        //遍历较小数字的每一位
        string temp(size2-1-i,'0');                     //temp为str1乘以str2[i]的积,根据str2[i]的权重(个十百千万,补充对应个数的0)
        int flag=0;                                     //进位标记
        for(int j=size1-1;j>=0;j--){                    //temp
            flag+=(str1[j]-'0')*(str2[i]-'0');
            temp.push_back('0'+(flag%10));
            flag/=10;
        }
        if(flag) temp.push_back('0'+flag);              //如果最高位还有进位
        reverse(temp.begin(),temp.end());
        ans=Add(ans,temp);                              //将计算结果累加到最终的结果上
    }
    if(negative) ans="-"+ans;                           //处理结果的正负号
    return ans;
}
string Div(string str1, string str2){
    //处理除数为0的情况和被除数为0的情况
    if(CheckZero(str2)) return "The divisor cannot be zero!";
    else if(CheckZero(str1)) return "0.000000";
 
    int negative=0,negastr1=0,negastr2=0;               //定正负
    if(CheckNegative(str1)){                            //确定正负号标记,并且去掉-
        negastr1=1; str1=str1.substr(1,str1.size()-1);
    }
    if(CheckNegative(str2)){
        negastr2=1; str2=str2.substr(1,str2.size()-1);
    }
    negative=negastr1^negastr2;                         //异或运算确定结果的正负号
 
    int point=0;                                        //结果是否为纯小数
    if(Lower(str1,str2)) point=1;                       //如果str1小于str2,则计算为纯小数
    string ans;                                         //计算结果
    str1+=string(6,'0');                                //补足6个0,用于计算小数位
 
    int size1=str1.size(),size2=str2.size();
    int i=size2-1;                                      //商第一位的位置
    string temp=str1.substr(0,i);                       //从str1上取size2-1个字符
    for(i;i<size1;i++){
        temp+=str1[i];                                  //从后边拿出一位,预先处理可以防止结尾处越界
        int cnt=0;                                      //当前位的商,也就是temp中包含了多少个str2,使用减法                                          //如果temp不为0,则计算商
        while(Lower(str2,temp)||temp==str2){            //如果当前位商不为0,则计算商
            temp=Sub(temp,str2);
            cnt++;
        }
        if(temp=="0") temp.clear();                     //如果某次计算结果为0,则清空,避免0的堆积,比如111000 111
        ans.push_back('0'+cnt);                         //保存商
    }
    i=0;
    while(ans[i]=='0') i++;
    ans=ans.substr(i,ans.size()-i);                     //去除前导0
    if(point){                                          //如果是纯小数,补足6位并添加小数点
        int len=6-ans.size();
        ans="0."+string(len,'0')+ans;
    }
    else ans.insert((ans.end()-6),'.');                 //如果不是小数,则只需要插入小数点       
    if(negative) ans="-"+ans;                           //最后一步骤,如果是负数带上负号
    return ans;
}
bool Lower(string str1, string str2){                   //长度长的一定大(这里假设除了0以外,都没有前导0),长度相等则字典序小的数字更小
    return str1.size()<str2.size()||(str1.size()==str2.size()&&str1<str2);
}
bool CheckZero(const string &str){      //检查是否等于0
    int size=str.size();                //如果全是0则为0,这里假设不会有带符号的+00000或者-00000作为输入
    for(int i=0;i<size;i++)
        if(str[i]!='0') return false;
    return true;
}
bool CheckNegative(const string &str){  //检查是否为负数
    return str[0]=='-';
}
void ShowMenu(){
    cout<<"请选择要进行的大数运算:\n"
        <<"a) 加法          b) 减法\n"
        <<"c) 乘法          d) 除法\n"
        <<"q) 退出\n"
        <<"请输入你的选择: ";
}
void ShowMenu(char choice){
    cout<<"请输入要计算的两个数字";
    switch(choice){
        case 'a':cout<<"(仅支持非负整数加法计算): "<<endl;break;
        case 'b':cout<<"(仅支持非负整数减法计算): "<<endl;break;
        case 'c':cout<<"(仅支持整数乘法计算): "<<endl;break;
        case 'd':cout<<"(仅支持整数除法计算,计算结果保留6位小数,之后的直接舍弃): "<<endl;break;
    }
}

Appendix, further study and understanding

https://blog.csdn.net/weixin_44668898/article/details/96763177

https://blog.csdn.net/wyqxii/article/details/131965735

Guess you like

Origin blog.csdn.net/cnds123/article/details/132858897