实现一个基本的计算器来计算一个简单的字符串表达式的值。
字符串表达式可以包含左括号 ( ,右括号 ),加号 + ,减号 -,非负整数和空格 。
示例 1:
输入: "1 + 1"
输出: 2
示例 2:
输入: " 2-1 + 2 "
输出: 3
示例 3:
输入: "(1+(4+5+2)-3)+(6+8)"
输出: 23
说明:
你可以假设所给定的表达式都是有效的。
请不要使用内置的库函数 eval。
#define true 1
#define false 0
int isDigit(char ch)
{
if (ch >= '0' && ch <= '9') {
return true;
}
return false;
}
int calculate(char *s)
{
int len = strlen(s);
int *stackDigit = (int*)malloc(sizeof(int) * len);
char *stackOperator = (char*)malloc(sizeof(char) * len);
int stackDigitTop = -1;
int stackOperatorTop = -1;
int i = 0;
int tempDigit = 0;
int num1 = 0;
int num2 = 0;
int temp = 0;
for (i = 0; i < len; i++) {
while (s[i] == ' ') {
i++;
}
if (i == len) {
break;
}
if (isDigit(s[i]) == true) {
tempDigit = s[i] - '0';
i++;
while (isDigit(s[i]) == true) {
tempDigit = tempDigit * 10 + (s[i] - '0');
i++;
}
i--;
stackDigitTop++;
stackDigit[stackDigitTop] = tempDigit;
while (stackOperatorTop != -1) {
if (stackOperator[stackOperatorTop] == '+') {
num2 = stackDigit[stackDigitTop];
stackDigitTop--;
num1 = stackDigit[stackDigitTop];
stackDigitTop--;
temp = num1 + num2;
stackDigitTop++;
stackDigit[stackDigitTop] = temp;
} else if (stackOperator[stackOperatorTop] == '-') {
num2 = stackDigit[stackDigitTop];
stackDigitTop--;
num1 = stackDigit[stackDigitTop];
stackDigitTop--;
temp = num1 - num2;
stackDigitTop++;
stackDigit[stackDigitTop] = temp;
} else {
break;
}
stackOperatorTop--;
}
continue;
} else if (s[i] == '(') {
stackOperatorTop++;
stackOperator[stackOperatorTop] = s[i];
} else if (s[i] == ')'){
while (stackOperator[stackOperatorTop] != '(') {
num2 = stackDigit[stackDigitTop];
stackDigitTop--;
num1 = stackDigit[stackDigitTop];
stackDigitTop--;
if (stackOperator[stackOperatorTop] == '+') {
temp = num1 + num2;
stackDigitTop++;
stackDigit[stackDigitTop] = temp;
} else if (stackOperator[stackOperatorTop] == '-') {
temp = num1 - num2;
stackDigitTop++;
stackDigit[stackDigitTop] = temp;
}
stackOperatorTop--;
}
stackOperatorTop--;
} else {
if (stackOperatorTop != -1) {
while (stackOperatorTop != -1) {
if (stackOperator[stackOperatorTop] == '+') {
num2 = stackDigit[stackDigitTop];
stackDigitTop--;
num1 = stackDigit[stackDigitTop];
stackDigitTop--;
temp = num1 + num2;
stackDigitTop++;
stackDigit[stackDigitTop] = temp;
} else if (stackOperator[stackOperatorTop] == '-') {
num2 = stackDigit[stackDigitTop];
stackDigitTop--;
num1 = stackDigit[stackDigitTop];
stackDigitTop--;
temp = num1 - num2;
stackDigitTop++;
stackDigit[stackDigitTop] = temp;
} else {
break;
}
stackOperatorTop--;
}
}
stackOperatorTop++;
stackOperator[stackOperatorTop] = s[i];
}
}
while (stackOperatorTop != -1) {
num2 = stackDigit[stackDigitTop];
stackDigitTop--;
num1 = stackDigit[stackDigitTop];
stackDigitTop--;
if (stackOperator[stackOperatorTop] == '+') {
temp = num1 + num2;
stackDigitTop++;
stackDigit[stackDigitTop] = temp;
} else if (stackOperator[stackOperatorTop] == '-') {
temp = num1 - num2;
stackDigitTop++;
stackDigit[stackDigitTop] = temp;
}
stackOperatorTop--;
}
return stackDigit[0];
}
来源:CSDN
作者:段刘昌
链接:https://blog.csdn.net/DuanLiuchang/article/details/104156804