#include<stdio.h>
#include<string.h>
int compute(int num1, int num2, char symbol)//主运算函数
{
int ans;
switch (symbol)
{
case '+':
ans = num1 + num2;
break;
case '-':
ans = num1 - num2;
break;
case '*':
ans = num1 * num2;
break;
case '/':
ans = num1 / num2;
break;
}
return ans;
}
int main(void)
{
char str[1000] = { 0 };
char symbol_stack[1000] = { 0 };//符号栈
int number_stack[1000] = { 0 };//数字栈
int s_size = 0, n_size = 0;
int i = 0;
int num1, num2;
scanf("%[^\n]", &str);
int len = strlen(str);
for (i = 0; i < len; i++)//遍历字符串
{
if (str[i] == '(' || str[i] == '[' || str[i] == '{')
{
symbol_stack[s_size++] = str[i];//左括号直接压入符号栈
}
if (str[i] == '-')
{
//先判断是符号还是减号
if (i == 0 || str[i - 1] == '(' || str[i - 1] == '[' || str[i - 1] == '{') // 代表符号
{
int temp = 0;
i++;
while (str[i] >= '0' && str[i] <= '9')
{
temp = 10 * temp + str[i] - '0';
i++;
}
number_stack[n_size++] = 0 - temp;
}
else//代表减号
{
while (s_size > 0 && (symbol_stack[s_size - 1] == '-' || symbol_stack[s_size - 1] == '*' || symbol_stack[s_size - 1] == '/'))
{
num2 = number_stack[--n_size];
num1 = number_stack[--n_size];
number_stack[n_size++] = compute(num1, num2, symbol_stack[--s_size]);
}
symbol_stack[s_size++] = '-';
}
}
if (str[i] == '+')
{
//先判断是符号还是加号
if (i == 0 || str[i - 1] == '(' || str[i - 1] == '[' || str[i - 1] == '{') // 代表符号
{
}
else//代表加号
{
while (s_size > 0 && (symbol_stack[s_size - 1] == '-' || symbol_stack[s_size - 1] == '*' || symbol_stack[s_size - 1] == '/'))
{
num2 = number_stack[--n_size];
num1 = number_stack[--n_size];
number_stack[n_size++] = compute(num1, num2, symbol_stack[--s_size]);
}
symbol_stack[s_size++] = '+';
}
}
if (str[i] == '*')
{
while (s_size > 0 && (symbol_stack[s_size - 1] == '/'))
{
num2 = number_stack[--n_size];
num1 = number_stack[--n_size];
number_stack[n_size++] = compute(num1, num2, symbol_stack[--s_size]);
}
symbol_stack[s_size++] = '*';
}
if (str[i] == '/')
{
while (s_size > 0 && (symbol_stack[s_size - 1] == '*' || symbol_stack[s_size - 1] == '/'))
{
num2 = number_stack[--n_size];
num1 = number_stack[--n_size];
number_stack[n_size++] = compute(num1, num2, symbol_stack[--s_size]);
}
symbol_stack[s_size++] = '/';
}
if (str[i] >= '0' && str[i] <= '9')
{
int temp = 0;
while (str[i] >= '0' && str[i] <= '9')
{
temp = temp * 10 + str[i] - '0';
i++;
}
if (!(str[i] >= '0' && str[i] <= '9'))
{
i--;
}
number_stack[n_size++] = temp;
}
if (str[i] == ')')
{
while (symbol_stack[s_size - 1] != '(')
{
num2 = number_stack[--n_size];
num1 = number_stack[--n_size];
number_stack[n_size++] = compute(num1, num2, symbol_stack[--s_size]);
}
s_size--;
}
if (str[i] == ']')
{
while (symbol_stack[s_size - 1] != '[')
{
num2 = number_stack[--n_size];
num1 = number_stack[--n_size];
number_stack[n_size++] = compute(num1, num2, symbol_stack[--s_size]);
}
s_size--;
}
if (str[i] == '}')
{
while (symbol_stack[s_size - 1] != '{')
{
num2 = number_stack[--n_size];
num1 = number_stack[--n_size];
number_stack[n_size++] = compute(num1, num2, symbol_stack[--s_size]);
}
s_size--;
}
}
while (s_size > 0)
{
num2 = number_stack[--n_size];
num1 = number_stack[--n_size];
number_stack[n_size++] = compute(num1, num2, symbol_stack[--s_size]);
}
printf("%d", number_stack[--n_size]);
return 0;
}
#include<string.h>
int compute(int num1, int num2, char symbol)//主运算函数
{
int ans;
switch (symbol)
{
case '+':
ans = num1 + num2;
break;
case '-':
ans = num1 - num2;
break;
case '*':
ans = num1 * num2;
break;
case '/':
ans = num1 / num2;
break;
}
return ans;
}
int main(void)
{
char str[1000] = { 0 };
char symbol_stack[1000] = { 0 };//符号栈
int number_stack[1000] = { 0 };//数字栈
int s_size = 0, n_size = 0;
int i = 0;
int num1, num2;
scanf("%[^\n]", &str);
int len = strlen(str);
for (i = 0; i < len; i++)//遍历字符串
{
if (str[i] == '(' || str[i] == '[' || str[i] == '{')
{
symbol_stack[s_size++] = str[i];//左括号直接压入符号栈
}
if (str[i] == '-')
{
//先判断是符号还是减号
if (i == 0 || str[i - 1] == '(' || str[i - 1] == '[' || str[i - 1] == '{') // 代表符号
{
int temp = 0;
i++;
while (str[i] >= '0' && str[i] <= '9')
{
temp = 10 * temp + str[i] - '0';
i++;
}
number_stack[n_size++] = 0 - temp;
}
else//代表减号
{
while (s_size > 0 && (symbol_stack[s_size - 1] == '-' || symbol_stack[s_size - 1] == '*' || symbol_stack[s_size - 1] == '/'))
{
num2 = number_stack[--n_size];
num1 = number_stack[--n_size];
number_stack[n_size++] = compute(num1, num2, symbol_stack[--s_size]);
}
symbol_stack[s_size++] = '-';
}
}
if (str[i] == '+')
{
//先判断是符号还是加号
if (i == 0 || str[i - 1] == '(' || str[i - 1] == '[' || str[i - 1] == '{') // 代表符号
{
}
else//代表加号
{
while (s_size > 0 && (symbol_stack[s_size - 1] == '-' || symbol_stack[s_size - 1] == '*' || symbol_stack[s_size - 1] == '/'))
{
num2 = number_stack[--n_size];
num1 = number_stack[--n_size];
number_stack[n_size++] = compute(num1, num2, symbol_stack[--s_size]);
}
symbol_stack[s_size++] = '+';
}
}
if (str[i] == '*')
{
while (s_size > 0 && (symbol_stack[s_size - 1] == '/'))
{
num2 = number_stack[--n_size];
num1 = number_stack[--n_size];
number_stack[n_size++] = compute(num1, num2, symbol_stack[--s_size]);
}
symbol_stack[s_size++] = '*';
}
if (str[i] == '/')
{
while (s_size > 0 && (symbol_stack[s_size - 1] == '*' || symbol_stack[s_size - 1] == '/'))
{
num2 = number_stack[--n_size];
num1 = number_stack[--n_size];
number_stack[n_size++] = compute(num1, num2, symbol_stack[--s_size]);
}
symbol_stack[s_size++] = '/';
}
if (str[i] >= '0' && str[i] <= '9')
{
int temp = 0;
while (str[i] >= '0' && str[i] <= '9')
{
temp = temp * 10 + str[i] - '0';
i++;
}
if (!(str[i] >= '0' && str[i] <= '9'))
{
i--;
}
number_stack[n_size++] = temp;
}
if (str[i] == ')')
{
while (symbol_stack[s_size - 1] != '(')
{
num2 = number_stack[--n_size];
num1 = number_stack[--n_size];
number_stack[n_size++] = compute(num1, num2, symbol_stack[--s_size]);
}
s_size--;
}
if (str[i] == ']')
{
while (symbol_stack[s_size - 1] != '[')
{
num2 = number_stack[--n_size];
num1 = number_stack[--n_size];
number_stack[n_size++] = compute(num1, num2, symbol_stack[--s_size]);
}
s_size--;
}
if (str[i] == '}')
{
while (symbol_stack[s_size - 1] != '{')
{
num2 = number_stack[--n_size];
num1 = number_stack[--n_size];
number_stack[n_size++] = compute(num1, num2, symbol_stack[--s_size]);
}
s_size--;
}
}
while (s_size > 0)
{
num2 = number_stack[--n_size];
num1 = number_stack[--n_size];
number_stack[n_size++] = compute(num1, num2, symbol_stack[--s_size]);
}
printf("%d", number_stack[--n_size]);
return 0;
}