OJ_1037 Powerful Calculator

本文介绍了一个C++程序,用于实现大数的加减乘运算。该程序能够处理长度高达400位的大整数,通过自定义的数据结构和算法实现了精确的计算结果,避免了使用浮点数或科学计数法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using std::max;
 
const int MAXLEN = (400 + 2)*2;
 
char A[MAXLEN];
char B[MAXLEN];
char C[MAXLEN];
 
int arrA[MAXLEN];
int arrB[MAXLEN];
int arrC[MAXLEN];
int arrTmp[MAXLEN];
 
//int max(int a, int b)
//{
//  return a > b ? a : b;
//}
 
void StrToBigInt(char a[], int arr[], int& len, bool& positive)
{
    int i, j, k;
 
    if(a[0]=='-')
    {
        positive = false;
        i=1;
    }
    else
    {
        positive = true;
        i=0;
    }
 
    while(a[i])
    {
        arr[i] = a[i] - '0';
        i++;
    }
 
    len = max(i, 1);
 
    j=0;k=len-1;
     
    while(j<k)
    {
        std::swap(arr[j], arr[k]);
        j++;
        k--;
    }
}
 
void BigIntToStr(int arr[], int len, bool positive, char a[])
{
    int i;
 
    if(positive)
    {
        i=0;
    }
    else
    {
        a[0]='-';
        i=1;
    }
 
    int j = len - 1;
 
    while(j>=0)
    {
        a[i] = '0' + arr[j];
        j--;
        i++;
    }
 
    a[i] = '\0';
}
 
 
 
int BigCmp(int a[], int b[], int lenA, int lenB)
{
    if(lenA<lenB)return -1;
    if(lenA>lenB) return 1;
 
    int i;
 
    for(i=lenA-1;i>=0&&a[i]==b[i];i--);
 
    if(i>=0)
    {
        if(a[i]<b[i])
        {
            return -1;
        }
        else
        {
            return 1;
        }
    }
    else
    {
        return 0;
    }
}
 
int BigAdd(int a[], int b[], int c[], int lenA, int lenB, int M)
{
    int i;
    int advance = 0;
 
    int maxLen = max(lenA, lenB)+1;
 
    for(i=lenA;i<=maxLen;i++)a[i]= 0;
    for(i=lenB;i<=maxLen;i++)b[i]= 0;
     
    for(i=0;i<maxLen;i++)
    {
        int tmp = a[i] + b[i] + advance;
        if(tmp >= M)
        {
            advance = 1;
            c[i] = tmp - M;
        }
        else
        {
            advance = 0;
            c[i] = tmp;
        }
    }
     
    i--;
 
    while(i>=0 &&!c[i])i--;
 
    return max(i+1, 1);
}
 
int BigSub(int a[], int b[], int c[], int lenA, int lenB, int M)
{
    int i;
    int additional = 0;
 
    int maxLen = max(lenA, lenB);
 
    for(i=lenA;i<=maxLen;i++)a[i]= 0;
    for(i=lenB;i<=maxLen;i++)b[i]= 0;
 
    for(i=0;i<maxLen;i++)
    {
        int tmp = a[i] + additional - b[i];
 
        if(tmp>=0)
        {
            additional = 0;
            c[i] = tmp;
        }
        else
        {
            additional = -1;
            c[i] = tmp + M;
        }
    }
 
    i--;
 
    while(i>=0 &&!c[i])i--;
 
    return max(i+1, 1);
}
 
int BigMult(int a[], int b, int n, int c[], int lenA, int M)
{
    int i;
    int advance = 0;
    a[lenA]=0;
 
    for(i=0;i<=lenA;i++)
    {
        int tmp = a[i] * b + advance;
        advance = tmp / M;
        c[i] = tmp % M;
    }
 
    for(i=lenA;i>=0;i--)
    {
        c[i+n] = c[i];
    }
 
    for(i=0;i<n;i++)c[i]=0;
     
    i=lenA + n;
 
    while(i>=0 &&!c[i])i--;
 
    return max(i+1, 1);
}
 
int BigMult(int a[], int b[], int c[], int lenA, int lenB, int M)
{
    int i;
    int lenC=1;
    c[0] = 0;
 
    for(i=0;i<lenB;i++)
    {
        int lenTmp = BigMult(a, b[i], i, arrTmp, lenA, M);
        lenC = BigAdd(c, arrTmp, c, lenC, lenTmp, M); 
    }
 
    return lenC;
}
 
int main()
{
    while(scanf("%s%s", &A, &B)!=EOF)
    {
        int lenA; bool aPositive;
        int lenB; bool bPositive;
        int lenC; bool cPositive;
 
        StrToBigInt(A, arrA, lenA, aPositive);
        StrToBigInt(B, arrB, lenB, bPositive);
 
        // Add
 
        if(aPositive && bPositive)
        {
            cPositive = aPositive;
            lenC = BigAdd(arrA, arrB, arrC, lenA, lenB, 10);
        }
        else
        {
            int cmp = BigCmp(arrA, arrB, lenA, lenB);
             
            switch(cmp)
            {
            case 0:
                arrC[0] = 0;
                lenC = 1;
                break;
            case 1:
                cPositive = aPositive;
                lenC = BigSub(arrA, arrB, arrC, lenA, lenB, 10);
                break;
            case -1:
                cPositive = bPositive;
                lenC = BigSub(arrB, arrA, arrC, lenB, lenA, 10);
                break;
            }
        }
 
        BigIntToStr(arrC, lenC, cPositive, C);
 
        printf("%s\n", C);
 
        // Subsctract
 
        if(lenB==1&&arrB[0]==0)
        {
            bPositive = true;
        }
        else
        {
            bPositive = !bPositive;
        }
 
 
        if(aPositive && bPositive)
        {
            cPositive = aPositive;
            lenC = BigAdd(arrA, arrB, arrC, lenA, lenB, 10);
        }
        else
        {
            int cmp = BigCmp(arrA, arrB, lenA, lenB);
             
            switch(cmp)
            {
            case 0:
                arrC[0] = 0;
                lenC = 1;
                break;
            case 1:
                cPositive = aPositive;
                lenC = BigSub(arrA, arrB, arrC, lenA, lenB, 10);
                break;
            case -1:
                cPositive = bPositive;
                lenC = BigSub(arrB, arrA, arrC, lenB, lenA, 10);
                break;
            }
        }
 
        BigIntToStr(arrC, lenC, cPositive, C);
 
        printf("%s\n", C);
 
        if(lenB==1&&arrB[0]==0)
        {
            bPositive = true;
        }
        else
        {
            bPositive = !bPositive;
        }
 
        // Multiple
 
        if( (lenA==1 && arrA[0]==0 )|| (lenB==1 && arrB[0]==0))
        {
            cPositive = true;
        }
        else
        {
        cPositive = (aPositive == bPositive);
        }
 
        lenC = BigMult(arrA, arrB, arrC, lenA, lenB, 10);
 
        BigIntToStr(arrC, lenC, cPositive, C);
 
        printf("%s\n", C);
    }
 
    return 0;
}

大数加减乘,没做

题目描述:

    Today, facing the rapid development of business, SJTU recognizes that more powerful calculator should be studied, developed and appeared in future market shortly. SJTU now invites you attending such amazing research and development work.
    In most business applications, the top three useful calculation operators are Addition (+), Subtraction (-) and Multiplication (×) between two given integers. Normally, you may think it is just a piece of cake. However, since some integers for calculation in business application may be very big, such as the GDP of the whole world, the calculator becomes harder to develop.
    For example, if we have two integers 20 000 000 000 000 000 and 4 000 000 000 000 000, the exact results of addition, subtraction and multiplication are:
    20000000000000000 + 4000000000000000 = 24 000 000 000 000 000
    20000000000000000 - 4000000000000000 = 16 000 000 000 000 000
    20000000000000000 × 4000000000000000 = 80 000 000 000 000 000 000 000 000 000 000
    Note: SJTU prefers the exact format of the results rather than the float format or scientific remark format. For instance, we need "24000000000000000" rather than 2.4×10^16.
    As a programmer in SJTU, your current task is to develop a program to obtain the exact results of the addition (a + b), subtraction (a - b) and multiplication (a × b) between two given integers a and b.

输入:

   Each case consists of two separate lines where the first line gives the integer a and the second gives b (|a| <10^400 and |b| < 10^400).

输出:

    For each case, output three separate lines showing the exact results of addition (a + b), subtraction (a - b) and multiplication (a × b) of that case, one result per lines.

样例输入:
20000000000000000
4000000000000000
样例输出:
24000000000000000
16000000000000000
80000000000000000000000000000000

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值