hdu2424,高精度

高精度加法和乘法

这东西,写起来实在纠结

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define INF 2000000000
char stack[2555],nstack[2555];
char s[255][2555],re[2555],temp[2555];
int tt[2555];
int min(int a,int b)
{
    return a>b?b:a;
}
int max(int a,int b)
{
    return a>b?a:b;
}
void mul(int x,int y)
{
    //printf("%d %d %s %s\n",x,y,s[x],s[y]);
    int len1,len2,i,j,k,l,c,d;
    len1=strlen(s[x]);
    len2=strlen(s[y]);
    memset(tt,0,sizeof(tt));
    for(i=0;i<len1;i++)
        for(j=0;j<len2;j++)
    {
        tt[(i+1)+(j+1)-1]+=((s[x][i]-'0')*(s[y][j]-'0'));
    }
    for(i=0,c=0;i<(len1)+(len2)-1;i++)
    {
        d=c;
        c=(tt[i+1]+c)/10;
        s[x][i]=(tt[i+1]+d)%10+'0';
    }
    if(c>0)
    {
        s[x][i++]=c+'0';
        s[x][i]='\0';
    }
    else
        s[x][i]='\0';
}
void add(int x,int y)
{
    //printf("%d %d %s %s\n",x,y,s[x],s[y]);
    int len1,len2,i,j,k,l,c,d;
    len1=strlen(s[x]);
    len2=strlen(s[y]);
    for(i=0,c=0;i<min(len1,len2);i++)
    {
        d=c;
        c=(s[x][i]+s[y][i]-'0'-'0'+c)/10;
        s[x][i]=(s[x][i]+s[y][i]-'0'-'0'+d)%10+'0';
    }
    for(;i<max(len1,len2);i++)
    {
        if(len1>len2)
        {
            d=c;
            c=(s[x][i]-'0'+c)/10;
            s[x][i]=(s[x][i]-'0'+d)%10+'0';
        }
        else
        {
            d=c;
            c=(s[y][i]-'0'+c)/10;
            s[x][i]=(s[y][i]-'0'+d)%10+'0';
        }
    }
    if(c>0)
    {
        s[x][i++]=c+'0';
        s[x][i]='\0';
    }
    else
        s[x][i]='\0';
}
int main()
{
    int n,i,j,k,l,t=0,flag,len,top,ntop,x;
    while(scanf("%d",&n)!=EOF)
    {
        flag=1;
        for(i=1;i<=n;i++)
        {
            scanf("%s",s[i]);
            if((i%2==1&&(s[i][0]=='*'||s[i][0]=='+'))||(i%2==0&&s[i][0]!='*'&&s[i][0]!='+'))
                flag=0;
            if(s[i][0]!='*'&&s[i][0]!='+')
            {
                len=strlen(s[i]);
                strcpy(temp,s[i]);
                for(j=0;j<len;j++)
                    s[i][j]=temp[len-1-j];
            }
        }
        if(n%2==0||!flag)
        {
            printf("Case %d: Invalid Expression!\n",++t);
            continue;
        }
        stack[0]='.';
        for(i=1,top=0,ntop=0;i<=n;i++)
        {
            if(s[i][0]=='*'||s[i][0]=='+')
                stack[top++]=i;
            else
            {
                if(s[stack[top-1]][0]=='*')
                {
                    x=nstack[ntop-1];
                    mul(x,i);
                }
                else
                    nstack[ntop++]=i;
            }
        }
        for(i=ntop-2;i>=0;i--)
        {
            add(nstack[i],nstack[i+1]);
        }
        x=nstack[0];
        len=strlen(s[x]);
        strcpy(temp,s[x]);
        for(j=0;j<len;j++)
            s[x][j]=temp[len-1-j];
        printf("Case %d: %s\n",++t,s[x]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值