Simple arithmetic operation
Problem description:
Enter a string of four simple arithmetic expressions that only contain a single digit to calculate the value of this expression.
Note:
3.1 The expression contains only the +,-, *, And/operators, excluding parentheses.
3.2 expression values only contain single-digit integers (0-9) and do not use 0 as the divisor.
3.3 consider the computing priority specified by the four arithmetic operations for addition, subtraction, multiplication, division
3.4 Division: use an integer division. That is, only the integer part of the division result is retained. For example, 8/3 = 2. The input expression ensures that no 0 is used as the divisor.
3.5 The input string must be an expression that matches the valid meaning of the question. It only contains digits and four operator characters. It does not include any other characters and does not cause computing overflow.
Required implementation functions:
Int calculate (INT Len, char * expstr)
[Input] int Len: String Length;
Char * expstr: expression string;
[Output] None
[Return] Calculation Result
Example:
1) input: char * expstr = "1 + 4*5-8/3"
Function return: 19
2) input: char * expstr = "8/3*3"
Function return: 6
1 #include<stdio.h> 2 template <typename T> 3 class MyStack{ 4 int top; 5 int sum; 6 T a[100]; 7 public: 8 MyStack(){top = -1; sum = 0;} 9 T getTop(){return a[top];} 10 int getSize(){return sum;} 11 bool pop() 12 {if(top >=0) 13 { 14 top--; 15 sum--; 16 return true; 17 } 18 else return false; 19 } 20 bool push(T value) 21 { 22 if (top >= 99) 23 return false; 24 else 25 { 26 a[++top] = value; 27 sum++; 28 } 29 } 30 31 }; 32 int switchCal(char c) 33 { 34 switch(c) 35 { 36 case ‘+‘: 37 case ‘-‘: 38 return 1; 39 case ‘*‘: 40 case ‘/‘: 41 return 2; 42 default: 43 return 0; 44 } 45 } 46 int calc(char c,int i,int j) 47 { 48 switch(c) 49 { 50 case ‘+‘: return i+j; 51 case ‘-‘: return i-j; 52 case ‘*‘: return i*j; 53 case ‘/‘: return i/j; 54 default: 55 return 0; 56 } 57 } 58 59 int calculate(int len, char *expStr) 60 { 61 char *p = expStr; 62 int i,j; 63 MyStack<int> num; 64 MyStack<char> cal; 65 while (*p != ‘\0‘) 66 { 67 if (cal.getSize() > 0 && switchCal(cal.getTop()) == 2 ) 68 { 69 i = num.getTop(); 70 printf("\n"); 71 printf("pop: %d ",i); 72 num.pop(); 73 j = *p - ‘0‘; 74 p++; 75 printf("jump: %d ",j); 76 printf(" calculate %c ",cal.getTop()); 77 i = calc(cal.getTop(),i,j); 78 cal.pop(); 79 num.push(i); 80 printf("push: %d ",i); 81 } 82 if (*p == ‘\0‘) 83 break; 84 if (*p >= ‘0‘ && *p <= ‘9‘) 85 { 86 i = *p - ‘0‘; 87 printf("push: %d ",i); 88 num.push(i); 89 } 90 else 91 { 92 cal.push(*p); 93 printf("push: %c ",*p); 94 } 95 p++; 96 } 97 while (cal.getSize()) 98 { 99 i = num.getTop();100 num.pop();101 j = num.getTop();102 num.pop();103 i = calc(cal.getTop(),j,i);104 cal.pop();105 num.push(i);106 }107 printf("\n");108 return num.getTop();109 }110 int main()111 {112 int len;113 char str[100];114 scanf("%d%s",&len,str);115 printf("%d\n",calculate(len,str));116 }
This question is a bit troublesome. I forgot how to change the infix to the prefix ~ Ah ....
It can only be calculated with an infix, but the input is perfect.
Simple arithmetic operation