[关闭]
@xunuo 2017-01-18T13:16:34.000000Z 字数 3046 阅读 1091

Gary's Calculator


Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

来源:
HDU:HDU 2424 ary's Calculator
vjudge:B-ary's Calculator

高精度


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

题意

高精度的加法和乘法的连续计算

代码

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<algorithm>
  5. using namespace std;
  6. int a[105],b[105],c[105];
  7. char s[105],ss1[105],ss2[105],sum[105];
  8. void add(char s1[],char s2[],char sum1[])///高精度加法......模板
  9. {
  10. memset(a,0,sizeof(a));
  11. memset(b,0,sizeof(b));
  12. memset(c,0,sizeof(c));
  13. int l1=strlen(s1);
  14. int l2=strlen(s2);
  15. for(int i=0;i<l1;i++)
  16. a[i]=s1[l1-1-i]-'0';
  17. for(int i=0;i<l2;i++)
  18. b[i]=s2[l2-1-i]-'0';
  19. int l=(l1>l2)?l1:l2;
  20. for(int i=0;i<l;i++)
  21. {
  22. c[i]+=(a[i]+b[i]);
  23. if(c[i]>=10)
  24. {
  25. c[i]-=10;
  26. c[i+1]+=1;
  27. }
  28. }
  29. if(c[l]>0)
  30. l++;
  31. for(int i=l;i>0;i--)
  32. {
  33. if(c[i]==0)
  34. l--;
  35. else
  36. break;
  37. }
  38. for(int i=l;i>=0;i--)
  39. sum1[l-i]=c[i]+'0';
  40. }
  41. void mult(char s1[],char s2[],char mul[])///高精度乘法.....模板
  42. {
  43. memset(a,0,sizeof(a));
  44. memset(b,0,sizeof(b));
  45. memset(c,0,sizeof(c));
  46. int l1=strlen(s1);
  47. int l2=strlen(s2);
  48. for(int i=0;i<l1;i++)
  49. a[i]=s1[l1-1-i]-'0';
  50. for(int i=0;i<l2;i++)
  51. b[i]=s2[l2-1-i]-'0';
  52. for(int i=0;i<l1;i++)
  53. for(int j=0;j<l2;j++)
  54. {
  55. c[i+j]+=a[i]*b[j];
  56. if(c[i+j]>=10)
  57. {
  58. c[i+j+1]+=(c[i+j])/10;
  59. c[i+j]=c[i+j]%10;
  60. }
  61. }
  62. int l=l1+l2;
  63. for(int i=l;i>0;i--)
  64. {
  65. if(c[i]==0)
  66. l--;
  67. else
  68. break;
  69. }
  70. for(int i=l;i>=0;i--)
  71. mul[l-i]=c[i]+'0';
  72. }
  73. int main()
  74. {
  75. int n;
  76. int t=1;
  77. while(scanf("%d",&n)!=EOF)
  78. {
  79. memset(ss1,'\0',sizeof(ss1));
  80. memset(ss2,'\0',sizeof(ss2));
  81. int vis=1,flag=0;
  82. for(int i=1;i<=n;i++)
  83. {
  84. scanf("%s",s);
  85. if(n%2==0)///输入为偶数个时,不符合条件
  86. vis=0;
  87. else
  88. {
  89. if(i%2==1)///当该输入的是数字时
  90. {
  91. if(s[0]=='+'||s[0]=='*')///出现了'+'与'*',不符合条件
  92. {
  93. vis=0;
  94. continue;
  95. }
  96. if(flag==0)///当出现'+'号时
  97. {
  98. memset(sum,'\0',sizeof(sum));///注意每次清空
  99. add(ss1,ss2,sum);
  100. strcpy(ss1,sum);///手动跑一次就明白了
  101. strcpy(ss2,s);
  102. }
  103. else
  104. {
  105. /*当出现'*'号时*/
  106. memset(sum,'\0',sizeof(sum));
  107. mult(ss2,s,sum);
  108. strcpy(ss2,sum);
  109. }
  110. }
  111. /*为奇数,该输出符号时*/
  112. else
  113. {
  114. if(strlen(s)!=1&&(s[0]!='+'||s[0]!='*'))///在其间输出了其他的东西
  115. {
  116. vis=0;
  117. continue;
  118. }
  119. else
  120. {
  121. if(s[0]=='+')///+号
  122. flag=0;
  123. else
  124. flag=1;///*号
  125. }
  126. }
  127. }
  128. //printf("%s\n",sum);
  129. }
  130. add(ss1,ss2,sum);///最后还要加一次
  131. printf("Case %d: ",t);
  132. t++;
  133. if(vis==0)
  134. printf("Invalid Expression!\n");
  135. else
  136. printf("%s\n",sum);///顺序输出
  137. }
  138. return 0;
  139. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注