hdu2424Gary's Calculator

Gary's Calculator

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 857    Accepted Submission(s): 188


Problem Description
Gary has finally decided to find a calculator to avoid making simple calculational mistakes in his math exam. Unable to find a suitable calculator in the market with enough precision, Gary has designed a high-precision calculator himself. Can you help him to write the necessary program that will make his design possible?
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
There are multiple test cases in the input file. Each test case starts with one positive integer N (N < 20), followed by a line containing N strings, describing the expression which Gary's calculator should evaluate. Each of the N strings might be a string representing a non-negative integer, a "*", or a "+". No integer in the input will exceed 109.
Input ends with End-of-File.
 

Output
For each test case, please output one single integer (with no leading zeros), the answer to Gary's expression. If Gary's expression is invalid, output "Invalid Expression!" instead. Please use the format indicated in the sample output. 
 

Sample Input
3 100 + 600 3 20 * 4 2 + 500 5 20 + 300 * 20
 

Sample Output
Case 1: 700 Case 2: 80 Case 3: Invalid Expression! Case 4: 6020
 

Source
 

Recommend
lcy
 

Statistic | Submit | Discuss | Note


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

用了兩個模板。

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

本來這道題挺簡單的,但是讓我漏了幾個條件,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);
			else
				break;
        }
}

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呀wa
				flag=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代表加法
					else
						flag2 = 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;
}




發佈了58 篇原創文章 · 獲贊 89 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章