hdu2424Gary's Calculator

來源:互聯網
上載者:User

Total Submission(s): 857    Accepted Submission(s): 188

For simplicity, you only need to consider two kinds of calculations in your program: addition and multiplication. It is guaranteed that all input numbers to the calculator are non-negative and without leading zeroes.
 
Input ends with End-of-File.
 
 
 
 


這道題是我第一次套用模板。

用了兩個模板。

高精度乘法,和高精度加法。。

本來這道題挺簡單的,但是讓我漏了幾個條件,wa了十幾次。。

還有看了一些前輩的blog,感到用java就能十分鐘做出來。因為java已經有高精度了。。

#include<stdio.h>#include<string.h>#include<malloc.h>int n;char ch[15];void mult(char a[],char b[],char s[]){    int i,j,k=0,alen,blen,sum=0,res[150][150]={0},flag=0;    char result[150];    alen=strlen(a);blen=strlen(b);     for (i=0;i<alen;i++)for (j=0;j<blen;j++) res[i][j]=(a[i]-'0')*(b[j]-'0');for (i=alen-1;i>=0;i--)        {            for (j=blen-1;j>=0;j--) sum=sum+res[i+blen-j-1][j];            result[k]=sum%10;            k=k+1;            sum=sum/10;        }for (i=blen-2;i>=0;i--)        {            for (j=0;j<=i;j++) sum=sum+res[i-j][j];            result[k]=sum%10;            k=k+1;            sum=sum/10;        }if (sum!=0) {result[k]=sum;k=k+1;}for (i=0;i<k;i++) result[i]+='0';for (i=k-1;i>=0;i--) s[i]=result[k-1-i];s[k]='\0';while(1)        {if (strlen(s)!=strlen(a)&&s[0]=='0') strcpy(s,s+1);elsebreak;        }}void add(char a[],char b[],char back[]){    int i,j,k,up,x,y,z,l;    char *c;    if (strlen(a)>strlen(b)) l=strlen(a)+2; else l=strlen(b)+2;    c=(char *) malloc(l*sizeof(char));    i=strlen(a)-1;    j=strlen(b)-1;    k=0;up=0;    while(i>=0||j>=0){if(i<0) x='0'; else x=a[i];if(j<0) y='0'; else y=b[j];z=x-'0'+y-'0';if(up) z+=1;if(z>9) {up=1;z%=10;} else up=0;c[k++]=z+'0';i--;j--;}    if(up) c[k++]='1';    i=0;    c[k]='\0';    for(k-=1;k>=0;k--)        back[i++]=c[k];    back[i]='\0';} int main(){int flag,flag2,ca = 1;char pre1[102],pre2[102],b[102];while(scanf("%d",&n)!=EOF){flag = 1,flag2 = 1;memset(pre1,'\0',sizeof(pre1));memset(pre2,'\0',sizeof(pre2));for(int i=1;i<=n;i++){scanf("%s",ch);if(n%2==0)    //一直漏了這個條件,wa呀waflag=0;if(flag){if(i%2==1 ){if(ch[0]=='+'||ch[0]=='*')//還有這個{flag = 0;continue;}if(flag2==1){memset(b,'\0',sizeof(b));add(pre1,pre2,b);strcpy(pre1,b);strcpy(pre2,ch);}else{memset(b,'\0',sizeof(b));mult(pre2,ch,b);strcpy(pre2,b);}}else{if((ch[0]!='+' && ch[0]!='*') || strlen(ch)!=1){flag = 0;continue;/*printf("Case %d: Invalid Expression!\n",ca++);break;*/}if(ch[0]=='+')flag2 = 1;  //1代表加法elseflag2 = 2;  //2代表乘法}}}add(pre1,pre2,b);if(!flag )printf("Case %d: Invalid Expression!\n",ca++);else{printf("Case %d: ",ca++);int i=0;while(b[i]=='0')i++;if(i==strlen(b))printf("0");for(;i<strlen(b);i++)printf("%c",b[i]);printf("\n");}}return 0;}




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.