首页 > 代码库 > 4进制加法-C++实现-分类讨论

4进制加法-C++实现-分类讨论

思路:

1. 分四类讨论

2. 得到加和减计算方法

3. 前导0删除与符号删除

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
// 输入4进制数格式判断
bool judge(string a)
{
    int i = 0;
    if(a[i] == '-' || a[i] == '+')
        i++;
    if(a[i] == '0' && a.size()-1>i)
        return false;
    for(;i<a.size();i++)
    {
        switch(a[i])
        {
            case '0':
            case '1':
            case '2':
            case '3':break;
            default:
                return false;
        }
    }
    return true;
}
// 字符串翻转
void reverse(string &a)
{
    char x;
    unsigned len = a.size();
    if(len == 1)
        return;
    for(int i=0;i<len/2;i++)
    {
        x = a[i];
        a[i] = a[len-1-i];
        a[len-1-i] = x;
    }
}
// 正数,相加,翻转为低地址低位运算,后翻转回来
string strplus(string a,string b)
{
    reverse(a);
    reverse(b);
    int lena = a.size();
    int lenb = b.size();
    int lmin = lena < lenb ? lena : lenb;
    int lmax = lena > lenb ? lena : lenb;
 
    string res = "";
    int i;
    for(i=0;i<lmin;i++)
    {
        res += a[i] - '0' + b[i];
    }
    for(;i<lmax;i++)
    {
        if(lena > lenb)
            res += a[i];
        else
            res += b[i];
    }
    int c = 0;
    for(i=0;i<lmax;i++)
    {
        res[i] = c + res[i];
        if(res[i]>'3'){
            res[i] -= 4;
            c = 1;
        }else
            c = 0;
    }
    if(c == 1)
        res += '1';
    reverse(res);
    return res;
}
// 删除前导0
void deletefrontzero(string &res)
{
    string temp;
    bool s = true;
    int len = res.size();
    for(int i=0;i<len;++i)
        if(res[i] == '0' && s)
            continue;
        else
        {
            s = false;
            temp += res[i];
        }
    res = temp;
}
// 大数-小数,无符号
string strminus(string a,string b)
{
    reverse(a);
    reverse(b);
    bool abigb = true;
    int lena = a.size();
    int lenb = b.size();
    string res("");
    if(lena < lenb)
        abigb = false;
    if(lena == lenb){
        int i = lena - 1;
        while(i>=0 && a[i]==b[i])
            i--;
        if(a[i]<b[i])
            abigb = false;
        if(i == -1){
            return "0";
        }
    }
    if(!abigb){
        reverse(a);
        reverse(b);
        return "-"+strminus(b,a);
    }
    int i;
    for(i = 0;i < lenb;i++)
    {
        res += a[i] - b[i] + '0';
    }
    for(;i < lena;i++)
        res += a[i];
    int c = 0;
    for(int i = 0;i < lena;i++)
    {
        res[i] = res[i] - c;
        if(res[i] < '0'){
            res[i] += 4;
            c = 1;
        }else
            c = 0;
    }
    reverse(res);
    deletefrontzero(res);
    return res;
}
// 删除符号
string deletesymbol(string &a)
{
    string b;
    if(a[0] == '+'||a[0] == '-'){
        for(int i=0;i<a.size()-1;i++)
            b += a[i+1];
        return b;
    }else{
        return a;
    }
}
string myplus(string a,string b)
{
    // 正正
    if(a[0] != '-' && b[0] != '-')
        return strplus(deletesymbol(a),deletesymbol(b));
    // 正负
    if(a[0] != '-' && b[0] == '-')
        return strminus(deletesymbol(a),deletesymbol(b));
    // 负负
    if(a[0] == '-' && b[0] == '-')
        return "-"+strplus(deletesymbol(a),deletesymbol(b));
    // 负正
    if(a[0] == '-' && b[0] != '-')
        return strminus(deletesymbol(b),deletesymbol(a));
}
int main()
{
    string a("123"),b("323");
    string c("-123"),d("-23423");
    string x1,x2;
 
    cout<<setw(6)<<a<<" + "<<setw(6)<<b<<" = ";
    cout<<setw(6)<<myplus(a,b)<<endl;
 
    cout<<setw(6)<<a<<" + "<<setw(6)<<c<<" = ";
    cout<<setw(6)<<myplus(a,c)<<endl;
 
    cout<<setw(6)<<c<<" + "<<setw(6)<<d<<" = ";
    cout<<setw(6)<<myplus(c,d)<<endl;
 
    cout<<setw(6)<<d<<" + "<<setw(6)<<b<<" = ";
    cout<<setw(6)<<myplus(d,b)<<endl;
 
    do{
        cout<<"输入两个正确的四进制的数"<<endl;
        cout<<"数1:";
        cin>>x1;
        cout<<"数2:";
        cin>>x2;
    }while(!judge(x1)||!judge(x2));
    cout<<setw(6)<<x1<<" + "<<setw(6)<<x2<<" = ";
    cout<<setw(6)<<myplus(x1,x2)<<endl;
 
    return 0;
}


运行结果如下图所示:


4进制加法-C++实现-分类讨论