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;
}


運行結果如下圖所示:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章