太多了

請原諒我不解釋

 

 

#include <iostream>
#include <cstring>

#define N 1001

using namespace std;

void charToInt();
void offset();
void calc(char oper);
void carry_or_borrow(char type);
void print();

string input1, input2;
char oper;
int output[N];

int main()
{
    while(cin >> input1 >> oper >> input2)
    {
        offset();
        charToInt();
        calc(oper);
        print();
    }
}

void charToInt()
{
    for(int i = 0; i<input1.length(); i++)
        input1[i] -= '0';

    for(int i = 0; i<input2.length(); i++)
        input2[i] -= '0';
}

void offset()
{
    if(oper == '/') return;

    if(input1.length() > input2.length())
        for(int i = input2.length(); i<input1.length(); i++)
            input2 = "0" + input2;

    else if(input1.length() < input2.length())
        for(int i = input1.length(); i<input2.length(); i++)
            input1 = "0" + input1;
}

void calc(char oper)
{
    memset(output, 0, sizeof(output));

    bool negative;
    switch(oper)
    {
        case '+':

            //digit一開始為最低位所在位置
            for(int i = input1.length() - 1, digit = N-1; i >= 0; i--, digit--)
                output[digit] += input1[i] + input2[i];

            carry_or_borrow('c');

            return;

        case '-':

            //考慮正負
            negative = false;

            for(int i = 0; i<input1.length(); i--)
                if(input1[i] < input2[i])
                {
                    negative = true;
                    break;
                }

            //正的
            if(!negative)
                for(int i = input1.length() - 1, digit = N-1 ; i >= 0; i--, digit--)
                    output[digit] += input1[i] - input2[i];

 

 

            //負的
            else
            {
                cout << "-";
                for(int i = input1.length() - 1, digit = N-1 ; i >= 0; i--, digit--)
                    output[digit] += input2[i] - input1[i];
            }

            carry_or_borrow('b');

            return;

        case '*':

            for(int i = input2.length() - 1, digit = N-1; i >= 0; i--, digit--)
                for(int j = input1.length() - 1, digit2 = 0; j>=0; j--, digit2++)
                    output[digit - digit2] += input2[i] * input1[j];

            carry_or_borrow('c');

            return;

        case '/':

            if(input1.length() < input2.length())
                return;
            else if(input1.length() == input2.length())
            {
                for(int i = 0; i<input1.length(); i++)
                    if(input1[i] > input2[i])
                        break;
                    else if(input1[1] < input2[i])
                        return;
            }

            if(input2.length() > 1)
                for(int times = input1.length() - input2.length(), i = 0, quotient; i<input1.length() && times>=0; times--, i++)
                {
                    //找尋商數
                    for(quotient = 1; ; quotient++)
                        if(input1[i] <= input2[0]*quotient + (input2[1]*quotient)/10)
                            break;

                    //保守估計
                    if(input1[i] == input2[0]*(quotient-1) + input2[1]*(quotient-1)/10 && times > 0) quotient--;

                    if(quotient>1)
                        for(int k = i, l = 0; l<input2.length() && k<input1.length(); k++, l++)
                        {
                            input1[k] -= input2[l]*(quotient-1);
                            while(input1[k] < 0)
                            {
                                input1[k] += 10;
                                input1[k-1] --;
                            }
                        }

                    input1[i+1] += input1[i]*10;

                    if(times)
                        output[N-1-times] = quotient-1;
                    else
                        output[N-1-times] = quotient;
                }
            else
                for(int times = input1.length() - 1, i = 0 , j; i+1<input1.length() && times>=0; times--, i++)
                {
                    j = input1[i]/input2[0];
                    input1[i] -= input2[0]*j;
                    input1[i+1] += input1[i]*10;
                    output[N-1-times] = j;
                }
                carry_or_borrow('c');

            return;
    }
}

void carry_or_borrow(char type)
{
    if(type == 'c')
        for(int i = 0, digit = N-1; i<2*input1.length()-1; i++, digit--)
        {
            output[digit-1] += output[digit]/10;
            output[digit] %= 10;
        }
    else
        for(int i = 0, digit = N-1; i<input1.length(); i++, digit--)
            if(output[digit] < 0)
            {
                output[digit] += 10;
                output[digit-1]--;
            }

}

void print()
{
    int i;

    //找出最高位的位置
    for(i = 0; i <= N-1 && output[i] == 0; i++);

    if(i >= N){cout << "0" << endl;return;}

    for(; i<=N-1; i++)
        cout << output[i];

    cout << endl;
}

 
arrow
arrow
    全站熱搜

    大神(偽) 發表在 痞客邦 留言(0) 人氣()