Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
9.9 kB
2
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 detectcarry(int answer[],string base_k);
bool detectminus(int [201]);
void output_ans(int []);
void carry(int answer[],string base_k);
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(base_k.length()>2)
            {
                cout << "Error! Please try again" << endl;
                continue;
            }
            if(stoi(base_k)>16||stoi(base_k)<2)//wrong input
            {
                cout << "Error! Please try again" << endl;
                continue;
            }
            break;
        }while(true);//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 = ";
            output_ans(answer);
        }
        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 = ";
            output_ans(answer);

        }
        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 = ";
            output_ans(answer);
        }
        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)
{

    for(int i=0;i<201;i++)
    {
        a[i]=0;b[i]=0;
    }
    string inputa;string inputb;int c1=1;
    cout<<"Please enter two numbers."<<endl;
    do// input a start
    {
        c1=0;
        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]>=48&&inputa[x]<=57)//0 to 9
            {
                if(inputa[x]-'0'>=stoi(base_k))
                {
                    cout<<"Wrong input a! Please enter again!!!"<<endl;
                    c1=1;
                    break;
                }

            }
            else
            {
                cout<<"Wrong input a! Please enter again!!!"<<endl;
                c1=1;
                break;
            }
        }

    }while(c1!=0);
        for(int i=200 ,x=inputa.length()-1;i>=201-inputa.length();i--,x--)//input a value
        {
            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// input b start
    {
        c1=0;
        cout<<"b = ";
        getline(cin,inputb);
        lengthb=inputb.size();
        for(int i=200 , x=inputb.length()-1;i>=201-inputb.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]>=48&&inputb[x]<=57)//0 to 9
            {
                if(inputb[x]-'0'>=stoi(base_k))
                {
                    cout<<"Wrong input b! Please enter again!!!"<<endl;
                    c1=1;
                    break;
                }

            }
            else
            {
                cout<<"Wrong input b! Please enter again!!!"<<endl;
                c1=1;
                break;
            }
        }

    }while(c1!=0);
        for(int i=200 , x=inputb.length()-1;i>=201-inputb.length();i--,x--)//input b value
        {
            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)
{
    for(int i=0;i<201;i++)
    {
        answer[i]=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];
    }
    carry(answer,base_k);
}
void sub(int a[],int b[],int largenum,string base_k,int answer[201],int&lengtha,int&lengthb)
{
    int carry[201];
    for(int i=0;i<201;i++)
    {
        answer[i]=0;
    }
    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 start=200;int counter=0;
    for(int i=0;i<201;i++)
    {
        answer[i]=0;
    }
    for(int x=200;x>=201-lengthb;x--)
    {
        start=200-counter;
        for(int i=200;i>=201-lengtha;i--)
        {
            answer[start]+=a[i]*b[x];
            start--;
        }
        counter++;
    }
    carry(answer,base_k);


}
bool detectcarry(int answer[],string base_k)
{
    for(int i=0;i<201;i++)
    {
        if(answer[i]>=stoi(base_k))
        {
            return true;
        }
    }
    return 0;
}
int detectposition(int answer[])
{
    for(int i=0;i<201;i++)
    {
        if(answer[i]!=0)
        {
            return 201-i;
        }
    }
    return 1;
}
void output_ans(int answer[])
{
    for(int i=201-detectposition(answer);i<=200;i++)// output function need ABC (65,66,67)
        {
            if(answer[i]<=9)
            {
                cout<<answer[i];
            }
            else
            {
                cout<<static_cast<char>(answer[i]+55);
            }
        }
        cout<<"\n"<<endl;
}
void carry(int answer[],string base_k)
{
     int carry[201];
     do
    {

        for(int i=200;i>=0;i--)// carry out
        {
            carry[i]=answer[i]/stoi(base_k);
            answer[i]%=stoi(base_k);
        }
         for(int i=200;i>=0;i--)
         {
             answer[i-1]+=carry[i];
         }
    }while(detectcarry(answer,base_k));
}
Leave a Comment