Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
8.6 kB
0
Indexable
Never
#include <iostream>
#include<vector>
#include<string>
using namespace std;
void add(int[],int[],int,string base_k,int a[],int&lengtha,int&lengthb);
void sub(int[],int[],int,string base_k,int a[],int&lengtha,int&lengthb);
void mult(int[],int[],int,string base_k,int a[],int&lengtha,int&lengthb);
int inputnum(int[],int[],string,int&lengtha,int&lengthb);
int detectposition(int answer[]);
bool detectminus(int [201]);
int main()
{
    string base_k;string option; int a[201]={0};int b[201]={0};int largenum;int answer[201]={0};int lengtha;int lengthb;
    do
    {
        cout << "----Welcome to big number calculator----" << endl;
        cout << "------------------------------------------------" << endl;
        do//find base
        {

            cout << "Please enter the base-k of the number (2<=k<=16)" << endl;
            cout << "base-k = ";
            getline(cin,base_k);
            if(stoi(base_k)>16||stoi(base_k)<2)//wrong input
            {
                cout << "Error! Please try again." << endl;
            }
        }while(stoi(base_k)>16||stoi(base_k)<2);//wrong input
        cout << "------------------------------------------------" << endl;

        do
        {

            cout << "Please choose the function" << endl;
            cout << "1.Addition" << endl;
            cout << "2.Subtraction" << endl;
            cout << "3.Multiplication" << endl;
            cout << "0.Exit" << endl;
            cout << "------------------------------------------------" << endl;
            getline(cin,option);
            if(!(option=="1"||option=="0"||option=="2"||option=="3"))
            {
                cout<<"Error! Please try again."<<endl;
            }

        }while(!(option=="1"||option=="0"||option=="2"||option=="3"));//wrong  input
        if(option=="1")
        {
            cout<<"----Now for Addition----"<<endl;

            largenum=inputnum(a,b,base_k,lengtha,lengthb);
            answer[201]={0};
            add(a,b,largenum,base_k,answer,lengtha,lengthb);
            cout<<"Result a+b = ";
            for(int i=201-detectposition(answer);i<=200;i++)
            {
                cout<<answer[i];
            }
            cout<<"\n"<<endl;
        }
        if(option=="2")
        {
            cout<<"----Now for Subtraction----"<<endl;
            largenum=inputnum(a,b,base_k,lengtha,lengthb);
            answer[201]={0};
            sub(a,b,largenum,base_k,answer,lengtha,lengthb);
            cout<<"Result a-b = ";
            for(int i=201-detectposition(answer);i<=200;i++)
            {
                cout<<answer[i];
            }
            cout<<"\n"<<endl;
        }
        if(option=="3")
        {
            cout<<"----Now for Multiplication----"<<endl;
            largenum=inputnum(a,b,base_k,lengtha,lengthb);
            answer[201]={0};
            mult(a,b,largenum,base_k,answer,lengtha,lengthb);
            cout<<"Result a*b = ";
            for(int i=201-detectposition(answer);i<=200;i++)
            {
                cout<<answer[i];
            }
            cout<<"\n"<<endl;
        }
        if(option=="0")
        {
            cout<<"Good bye!";
            return 0;
        }

    }while(true);
    return 0;
}
int inputnum(int a[201],int b[201],string base_k,int&lengtha,int&lengthb)
{

    string inputa;string inputb;int c1=1;
    cout<<"Please enter two numbers."<<endl;
    do
    {
        c1=1;
        cout<<"a = ";
        getline(cin,inputa);
        lengtha=inputa.size();
        for(int i=200, x=inputa.length()-1;i>=201-inputa.length();i--,x--)
        {
            if(inputa[x]>96)//a=97
            {
                if(inputa[x]-87>=stoi(base_k))//wrong
                {
                    cout<<"Wrong input a! Please enter again!!!"<<endl;
                    c1=1;
                    break;
                }
            }
            else if(inputa[x]>64&&inputa[x]<91)//Upper letter A=65
            {
                if(inputa[x]-55>=stoi(base_k))//wrong
                {
                    cout<<"Wrong input a! Please enter again!!!"<<endl;
                    c1=1;
                    break;
                }
            }
            else if(inputa[x]-'0'>=stoi(base_k))//0 to 9
            {
                cout<<"Wrong input a! Please enter again!!!"<<endl;
                c1=1;
                break;
            }
        }
        c1=0;

    }while(c1!=0);//input a
        for(int i=200 ,x=inputa.length()-1;i>=201-inputa.length();i--,x--)//give num a
        {
            if(inputa[x]>96)
            {
                a[i]=(static_cast<int>(inputa[x]-87));

            }
            else if(inputa[x]>64&&inputa[x]<91)
            {
                a[i]=(static_cast<int>(inputa[x]-55));

            }
            else //0 to 9
            {
                a[i]=(static_cast<int>(inputa[x]-'0'));

            }

        }


    do
    {
        c1=1;
        cout<<"b = ";
        getline(cin,inputb);
        lengthb=inputb.size();
        for(int i=200 , x=inputa.length()-1;i>=201-inputa.length();i--,x--)
        {
            if(inputb[x]>96)//a=97
            {
                if(inputb[x]-87>=stoi(base_k))//wrong
                {
                    cout<<"Wrong input b! Please enter again!!!"<<endl;
                    c1=1;
                    break;
                }
            }
            else if(inputb[x]>64&&inputa[x]<91)//Upper letter A=65
            {
                if(inputb[x]-55>=stoi(base_k))//wrong
                {
                    cout<<"Wrong input b! Please enter again!!!"<<endl;
                    c1=1;
                    break;
                }
            }
            else if(inputb[x]-'0'>=stoi(base_k))//0 to 9 wrong
            {
                cout<<"Wrong input b! Please enter again!!!"<<endl;
                c1=1;
                break;
            }
        }
        c1=0;
    }while(c1!=0);//input b
        for(int i=200 , x=inputa.length()-1;i>=201-inputa.length();i--,x--)//give num b
        {
            if(inputb[x]>96)
            {
                b[i]=(static_cast<int>(inputb[x]-87));
            }
            else if(inputa[x]>64&&inputb[x]<91)
            {
                b[i]=(static_cast<int>(inputb[x]-55));
            }
            else //0 to 9
            {
                b[i]=(static_cast<int>(inputb[x]-'0'));
            }
        }

        if(lengtha>lengthb)
        {
            return lengtha;
        }
        else
            return lengthb;


}
void add(int a[],int b[],int largenum,string base_k,int answer[201],int &lengtha,int &lengthb)
{
    int carry[201]={0};
    for(int i=200;i>=201-lengtha;i--)
    {
        answer[i]=a[i];
    }
    for(int i=200;i>=201-lengthb;i--)
    {
        answer[i]+=b[i];
    }

    for(int i=200;i>=201-detectposition(answer);i--)
    {
        carry[i]=answer[i]/stoi(base_k);
        answer[i]%=stoi(base_k);
    }
    for(int i=200;i>=201-detectposition(answer)+1;i--)
    {
        answer[i-1]+=carry[i];
    }


}
void sub(int a[],int b[],int largenum,string base_k,int answer[201],int&lengtha,int&lengthb)
{
    int carry[201];
    for(int i=200;i>=201-largenum;i--)//minus
    {
        answer[i]=a[i]-b[i];
    }
    do
    {
        for(int i=200;i>=0;i--)// borrow
        {
            if(answer[i]<0)
            {
                answer[i-1]-=1;
                answer[i]+=stoi(base_k);
            }
        }
    }while(detectminus(answer));
}
bool detectminus(int answer[])
{
    for(int i=0;i<201;i++)
    {
        if(answer[i]<0)
            {
                return true;
            }

    }
    return false;
}
void mult(int a[],int b[],int largenum,string base_k,int answer[201],int&lengtha,int&lengthb)
{
    int carry[201];
    for(int x=0;x<201;x++)
    {
        for(int i=200;i>=201-largenum;i--)
        {
            answer[i-x]+=a[i]*b[200-x];
        }
    }
    for(int i=200;i>=201-largenum;i--)
    {
        carry[i-1]=answer[i]/stoi(base_k);
        answer[i]%=stoi(base_k);
    }
    for(int i=200;i>=201-detectposition(answer);i--)
    {
        answer[i]+=carry[i];
    }

}
int detectposition(int answer[])
{
    for(int i=0;i<201;i++)
    {
        if(answer[i]!=0)
        {
            return 201-i;
        }
    }
    return 0;
}
Leave a Comment