2014
02-10

# Simple Desk Calculator

Your task are to write a program that imitates a simple desk calculator. Your calculator must be able to accept an infix expression which at least includes (, ), +, -, *, /,% . If the expression is legal, output its value, else ouput an error message.

There are several test cases, each occupies one line that contains an infix expression. Proceed until the end of the file.

There are several test cases, each occupies one line that contains an infix expression. Proceed until the end of the file.

4.99+5.99+6.99*1.06
(3*5*(4*8)%2)
1+2(
2/0
2.5%2
2%2.5

18.39
0.00
ERROR IN INFIX NOTATION
ERROR IN INFIX NOTATION
ERROR IN INFIX NOTATION
ERROR IN INFIX NOTATION

#include<iostream>
#include<cstring>
#include<cctype>
#include<stack>
using std::stack;
const int L=1001;

stack<double> OPND;
stack<char> OPTR;

bool IsLegalChar(char c)
{
if(isdigit(c))
return true;
else if(c=='+' || c=='-' || c=='*' || c=='/' || c=='%' || c=='(' || c==')' || c=='.')
return true;
else
return false;
}

double ToOperand(char t[],int length,int count,int point)
{
if(count>1 || t[0]=='.' || t[length-1]=='.')
return -1.0;
double result=0.0,v=1.0;
if(count==0)
{
for(int i=length-1;i>=0;i--)
{
result+=(double)(t[i]-'0')*v;
v*=10;
}
return result;
}
else
{
for(int i=point-1;i>=0;i--)
{
result+=(double)(t[i]-'0')*v;
v*=10;
}
v=0.1;
for(int i=point+1;i<length;i++)
{
result+=(double)(t[i]-'0')*v;
v/=10.0;
}
return result;
}
}

char OperatorJudgement(char optr1,char optr2)
{
if(optr1=='+' || optr1=='-')
{
if(optr2=='+' || optr2=='-' || optr2==')' || optr2=='#')
return '>';
if(optr2=='*' || optr2=='/' || optr2=='%' || optr2=='(')
return '<';
}
if(optr1=='*' || optr1=='/' || optr1=='%')
{
if(optr2=='+' || optr2=='-' || optr2=='*' || optr2=='/' || optr2=='%' || optr2==')' || optr2=='#')
return '>';
if(optr2=='(')
return '<';
}
if(optr1=='(')
{
if(optr2==')')
return '=';
if(optr2=='#')
return 'E';
if(optr2=='+' || optr2=='-' || optr2=='*' || optr2=='/' || optr2=='%' || optr2=='(')
return '<';
}
if(optr1==')')
{
if(optr2=='(')
return 'E';
else
return '>';
}
if(optr1=='#')
{
if(optr2==')')
return 'E';
if(optr2=='#')
return '=';
else
return '<';
}
}

bool IsLegalMod(double opnd1,double opnd2)
{
if(opnd1-(int)opnd1!=0.0 || opnd2-(int)opnd2!=0.0 || opnd2==0.0)
return false;
return true;
}

double Calculate(double opnd1,char optr,double opnd2)
{
if(optr=='+')
return opnd1+opnd2;
if(optr=='-')
return opnd1-opnd2;
if(optr=='*')
return opnd1*opnd2;
if(optr=='/')
return opnd1/opnd2;
if(optr=='%')
return (double)((int)opnd1%(int)opnd2);
}

int main()
{
int i,j;
bool isLegalInfixNonation;
char s[L],t[L];

while(gets(s))
{
isLegalInfixNonation=true;
for(i=0;s[i]!='\0';i++)
{
if(IsLegalChar(s[i])==false)
{
isLegalInfixNonation=false;
break;
}
}
strcat(s,"#");
while(OPND.empty()==false)
OPND.pop();
while(OPTR.empty()==false)
OPTR.pop();

i=0;
OPTR.push('#');
while((s[i]!='#' || OPTR.top()!='#') && isLegalInfixNonation==true)
{
if(isdigit(s[i]) || s[i]=='.')
{
int point=0,count=0;
for(j=0;isdigit(s[i]) || s[i]=='.';i++,j++)
{
t[j]=s[i];
if(s[i]=='.')
count++,point=j;
}
double opnd=ToOperand(t,j,count,point);
if(opnd==-1.0)
{
isLegalInfixNonation=false;
break;
}
else
{
OPND.push(opnd);
}
}
else
{
char optr1=OPTR.top(),optr2=s[i];
char judgement=OperatorJudgement(optr1,optr2);
if(judgement=='E')
isLegalInfixNonation=false;
else
{
if(judgement=='<')
{
OPTR.push(optr2);
i++;
}
else if(judgement=='=')
{
OPTR.pop();
i++;
}
else if(judgement=='>')
{
OPTR.pop();
if(OPND.empty()==true)
{
isLegalInfixNonation=false;
break;
}
double opnd2=OPND.top();
OPND.pop();
if(OPND.empty()==true)
{
isLegalInfixNonation=false;
break;
}
double opnd1=OPND.top();
OPND.pop();
if(optr1=='%')
{
if(IsLegalMod(opnd1,opnd2)==false)
{
isLegalInfixNonation=false;
break;
}
}
if(optr1=='/')
{
if(opnd2==0.0)
{
isLegalInfixNonation=false;
break;
}
}
double result=Calculate(opnd1,optr1,opnd2);
OPND.push(result);
}
}
}
}
if(isLegalInfixNonation==false)
puts("ERROR IN INFIX NOTATION");
else
{
if(OPND.size()!=1)
puts("ERROR IN INFIX NOTATION");
else
printf("%.2lf\n",OPND.top());
}
}
return 0;
}

,
1. Pingback: Cheap NFL Jerseys Wholesale

2. Pingback: Cheap Jerseys Wholesale

3. Pingback: Wholesale Baseball Jerseys China

4. Pingback: Cheap NFL Jerseys China

5. Pingback: Cheap Jerseys

6. Pingback: Cheap Jerseys Online

7. Pingback: Cheap Nike NFL Jerseys Online

8. Pingback: Cheap Jerseys China

9. Pingback: Cheap Jerseys China Free Shipping

10. Pingback: Wholesale Jerseys Cheap

11. Pingback: Wholesale China Jerseys

12. Pingback: Wholesale Jerseys From China

13. Pingback: Wholesale Jerseys

14. Pingback: Cheap NFL Jerseys

15. Pingback: Wholesale Jerseys China Wholesale

16. Pingback: Cheap Jerseys China

17. Pingback: Cheap NFL Jerseys China Online

18. Pingback: Wholesale MLB Jerseys

19. Pingback: Cheap Jerseys

20. Pingback: Wholesale Jerseys From China

21. Pingback: Cheap Jerseys Free Shipping

22. Pingback: Wholesale China Jerseys

23. Pingback: Wholesale NFL Jerseys

24. Pingback: Cheap NFL Jerseys Clearance

25. Pingback: Wholesale Jerseys

26. Pingback: Cheap Jerseys

27. Pingback: Cheap Jerseys Wholesale

28. Pingback: Cheap NFL Jerseys Sale

29. Pingback: Wholesale NFL Jerseys From China

30. Pingback: Cheap Jerseys

31. Pingback: Cheap Jerseys 17\$

32. Pingback: Cheap Soccer Jerseys Wholesale

33. Pingback: Wholesale Jerseys

34. Pingback: Wholesale Jerseys China

35. Pingback: Cheap China Jerseys

36. Pingback: Wholesale Soccer Jerseys Cheap

37. Pingback: Wholesale NFL Jerseys

38. Pingback: Wholesale NFL Jerseys China

39. Pingback: Wholesale Jerseys

40. Pingback: Wholesale NFL Jerseys China

41. Pingback: Cheap Jerseys From China

42. Pingback: Cheap NFL Jerseys China

43. Pingback: Cheap NFL Jerseys China

44. Pingback: Wholesale Jerseys Free Shipping

45. Pingback: Cheap Jerseys Wholesale

46. Pingback: Cheap NFL Jerseys Wholesale

47. Pingback: Wholesale NFL Jerseys

48. Pingback: human hair weave

49. Pingback: Cheap Jerseys From China

50. Pingback: Cheap Jerseys

51. Pingback: Wholesale Jerseys

52. Pingback: Cheap Jerseys From China

53. Pingback: Wholesale Jerseys Cheap

54. Pingback: Cheap NHL Jerseys

55. Pingback: Cheap Jerseys

56. Pingback: Wholesale Jerseys

57. Pingback: Cheap NFL Jerseys From China

58. Pingback: Cheap NFL Jerseys Free Shipping

59. Pingback: Wholesale Jerseys China

60. Pingback: Cheap Jerseys

61. Pingback: Wholesale Jerseys China

62. Pingback: Cheap Jerseys

63. Pingback: Cheap NFL Jerseys China

64. Pingback: Wholesale Authentic NFL Jerseys

65. Pingback: Wholesale NFL Jerseys

66. Pingback: Cheap Jerseys

67. Pingback: Wholesale Jerseys China

68. Pingback: Cheap Jerseys From China

69. Pingback: Cheap Jerseys From China

70. Pingback: Cheap NFL Jerseys China

71. Pingback: Wholesale NFL Jerseys From China

72. Pingback: Cheap Jerseys Wholesale

73. Pingback: Cheap NFL Jerseys

74. Pingback: Cheap NHL Jerseys

75. Pingback: Wholesale NFL Jerseys Free Shipping

76. Pingback: Wholesale Jerseys

77. Pingback: Wholesale NFL Jerseys Supply

78. Pingback: Cheap NFL Jerseys China

79. Pingback: Wholesale NFL Jerseys

80. Pingback: Cheap NFL Jerseys

81. Pingback: Cheap Jerseys Wholesale

82. Pingback: Wholesale Jerseys

83. Pingback: Cheap Jerseys Free Shipping

84. Pingback: Wholesale NFL Jerseys

85. Pingback: Wholesale Jerseys

86. for(int i=1; i<=m; i++){
for(int j=1; j<=n; j++){
dp = dp [j-1] + 1;
if(s1.charAt(i-1) == s3.charAt(i+j-1))
dp = dp[i-1] + 1;
if(s2.charAt(j-1) == s3.charAt(i+j-1))
dp = Math.max(dp [j - 1] + 1, dp );
}
}
这里的代码似乎有点问题？ dp(i)(j) = dp(i)(j-1) + 1;这个例子System.out.println(ils.isInterleave("aa","dbbca", "aadbbcb"));返回的应该是false