+-*/ which is the biggest

 

 

 

 

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <math.h>

#include <assert.h>

#include <ctype.h>

#include <map>

#include <string>

#include <set>

#include <bitset>

#include <utility>

#include <algorithm>

#include <vector>

#include <stack>

#include <queue>

#include <iostream>

#include <fstream>

#include <list>



using namespace  std;



const int MAXL = 200;

struct BigNum

{

    int num[MAXL];

    int len;

};



//高精度比较 a > b return 1, a == b return 0; a < b return -1;

int Comp(BigNum &a, BigNum &b)

{

    int i;

    if(a.len != b.len) return (a.len > b.len) ? 1 : -1;

    for(i = a.len-1; i >= 0; i--)

        if(a.num[i] != b.num[i]) return (a.num[i] > b.num[i]) ? 1 : -1;

    return 0;

}



//高精度加法

BigNum Add(BigNum &a, BigNum &b)

{

    BigNum c;

    int  i, len;

    len = (a.len > b.len) ? a.len : b.len;

    memset(c.num, 0, sizeof(c.num));

    for(i = 0; i < len; i++)

    {

        c.num[i] += (a.num[i]+b.num[i]);

        if(c.num[i] >= 10)

        {

            c.num[i+1]++;

            c.num[i] -= 10;

        }

    }

    if(c.num[len]) len++;

    c.len = len;

    return  c;

}



//高精度减法,保证a >= b

BigNum Sub(BigNum &a, BigNum &b)

{

    BigNum  c;

    int  i, len;

    len = (a.len > b.len) ? a.len : b.len;

    memset(c.num, 0, sizeof(c.num));

    for(i = 0; i < len; i++)

    {

        c.num[i] += (a.num[i]-b.num[i]);

        if(c.num[i] < 0)

        {

            c.num[i] += 10;

            c.num[i+1]--;

        }

    }

    while(c.num[len] == 0 && len > 1) len--;

    if(c.num[len]) len++;

    c.len = len;

    return  c;

}



//高精度乘以高精度

BigNum  Mul(BigNum &a, BigNum &b)

{

    int i, j, len = 0;

    BigNum  c;

    memset(c.num, 0, sizeof(c.num));

    for(i = 0; i < a.len; i++)

        for(j = 0; j < b.len; j++)

        {

            c.num[i+j] += (a.num[i]*b.num[j]);

            if(c.num[i+j] >= 10)

            {

                c.num[i+j+1] += c.num[i+j]/10;

                c.num[i+j] %= 10;

            }

        }

        len = a.len+b.len-1;

        while(c.num[len-1] == 0 && len > 1) len--;

        if(c.num[len]) len++;

        c.len = len;

        return  c;

}



//高精度*10

void  Mul10(BigNum &a)

{

    int  i, len = a.len;

    for(i = len; i >= 1; i--)

        a.num[i] = a.num[i-1];

    a.num[i] = 0;

    len++;

    //if a == 0

    while(len > 1 && a.num[len-1] == 0)  len--;

    if(a.num[len]) len++;

    a.len = len;

}



//高精度除以高精度,除的结果为c,余数为f

void Div(BigNum &a, BigNum &b, BigNum &c, BigNum &f)

{

    int  i, len = a.len;

    memset(c.num, 0, sizeof(c.num));

    memset(f.num, 0, sizeof(f.num));

    f.len = 1;

    for(i = len-1;i >= 0;i--)

    {

        Mul10(f);

        //余数每次乘10

        f.num[0] = a.num[i];

        //然后余数加上下一位

        ///利用减法替换除法

        while(Comp(f, b) >= 0)

        {

            f = Sub(f, b);

            c.num[i]++;

        }

    }

    while(len > 1 && c.num[len-1] == 0)len--;

    if(c.num[len]) len++;

    c.len = len;

}



void print(BigNum &a, int tag)

{

    if(a.len == 1 && a.num[0] == 0)

    {

        puts("0");

    }

    else

    {

        if(tag < 0) putchar('-');

        int  i;

        for(i = a.len-1; i >= 0; i--)

            printf("%d", a.num[i]);

        puts("");

    }

}

// 输出商和余数

void print(BigNum &c, BigNum &f, int tag)

{

    if(c.len == 1 && c.num[0] == 0)

    {

        printf("0");

    }

    else

    {

        if(tag < 0) putchar('-');

        int  i;

        for(i = c.len-1; i >= 0; i--)

            printf("%d", c.num[i]);

    }

    printf(" ");

    print(f,1);

}



void Init(BigNum &a, char *s, int &tag)

{

    memset(a.num,0,sizeof(a.num));

    int  i = 0, j = strlen(s);

    if(s[0] == '-') {j--; i++; tag *= -1;}

    a.len = j;

    for(; s[i] != '\0'; i++, j--)

        a.num[j-1] = s[i]-'0';

    //print(a);

}



void work(BigNum &a,int &atag,BigNum &b,int &btag)

{

    BigNum tmp1;

    memset(tmp1.num,0,sizeof(tmp1.num));

    if(atag == 1 && btag == 1)// ++

    {

        tmp1 = Add(a, b);

        a = Mul(a, b);

        if (Comp(tmp1, a) == 1)

        {

            a = tmp1;

        }

        print(a, 1);

    }else if(atag == 1 && btag == -1)// +-

    {

        a = Add(a, b);

        print(a, 1);

    }else if(atag == -1 && btag == 1)// -+

    {

        if(b.len == 1 && b.num[0] == 0)

        {

            a.len = 1;

            a.num[0] = 0;

            print(a, 1);

        }

        else

        {

            if (Comp(b, a) >= 0)

            {

                a = Sub(b, a);

                print(a, 1);

            }

            else

            {

                tmp1 = Sub(a, b);

                BigNum c, f;

                Div(a, b, c, f);

                if (Comp(tmp1, c) == -1)

                {

                    print(tmp1, -1);

                }

                else

                {

                    print(c, f, -1);

                }

            }

        }

    }else// --

    {

        a = Mul(a, b);

        print(a, 1);

    }

}



int  main()

{



    BigNum a, b;

    char  s1[100], s2[100];

    int n;

    scanf("%d", &n);

    for(int i=0;i<n;i++)

    {

        if(scanf("%s %s", s1, s2) != EOF)

        {

            int atag = 1;

            int btag = 1;// 两个数的符号

            Init(a, s1, atag);

            Init(b, s2, btag);

            work(a, atag, b, btag);

        }

    }

    return 0;

}

 

 

 

posted @ 2018-05-09 16:10  RomanticChopin  阅读(103)  评论(0编辑  收藏  举报
-->
Live2D