#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
const int N = 1e3 + 10;
stack<int> op, br;
stack<double> num;
char str[N];
void initStack() {
while (!op.empty()) op.pop();
while (!br.empty()) br.pop();
while (!num.empty()) num.pop();
}
int getType(char c) {
if (c >= '0' && c <= '9')
return 0;
else if (c == '+' || c == '-')
return 1;
else if (c == '*' || c == '/')
return 1;
else if (c == '(' || c == ')')
return 2;
else if (c == '[' || c == ']')
return 2;
else if (c == '{' || c == '}')
return 2;
else
return -1;
}
int transOp(char c) {
if (c == '+')
return 0;
else if (c == '-')
return 1;
else if (c == '*')
return 2;
else if (c == '/')
return 3;
return -1;
}
int transBr(char c) {
if (c == '(')
return 0;
else if (c == ')')
return 1;
else if (c == '[')
return 2;
else if (c == ']')
return 3;
else if (c == '{')
return 4;
else if (c == '}')
return 5;
else
return -1;
}
bool cal(int fp) {
double num2, num1;
if (!num.empty()) { // get second number
num2 = num.top();
num.pop();
}
else {
printf("Wrong position of operator\n");
return false;
}
if (!num.empty()) { // get first number
num1 = num.top();
num.pop();
}
else {
printf("Wrong position of operator\n");
return false;
}
double ans = 0;
if (fp % 4 == 0) {
ans = num1 + num2;
}
else if (fp % 4 == 1) {
ans = num1 - num2;
}
else if (fp % 4 == 2) {
ans = num1 * num2;
}
else if (fp % 4 == 3) {
if (num2 == 0) {
printf("Divide zero\n");
return false;
}
ans = num1 / num2;
}
num.push(ans);
return true;
}
bool solve(double &res) {
initStack();
int len = strlen(str);
int plus = 0; // operator addition
for (int i = 0; i < len; i++) {
int type = getType(str[i]);
if (type == 0) { // number
double sum = str[i++] - '0';
double pl = 10;
while (i < len && (type = getType(str[i])) == 0) { // before decimal
sum = sum * pl + str[i] - '0';
i++;
}
if (i < len && str[i] == '.') { // decimal
pl = 0.1;
i++;
while (i < len && (type = getType(str[i])) == 0) { // after decimal
sum = sum + (str[i] - '0') * pl;
i++;
pl *= 0.1;
}
}
else if (i < len && type == -1) { // illegal character
printf("Illegal character: %c\n", str[i]);
return false;
}
num.push(sum);
i--;
}
else if (type == 1) { // operator
int p = transOp(str[i]) + plus;
while (!op.empty()) {
int fp = op.top();
if (fp >= p) { // now operator is lower
op.pop();
if (!cal(fp))
return false;
}
else { // now operator is higher
break;
}
}
op.push(p);
}
else if (type == 2) { // bracket
int t = transBr(str[i]);
if (t % 2) { // right bracket
if (br.empty()) {
printf("Brackets are not mactch\n");
return false;
}
else {
int ft = br.top();
br.pop();
if (t - 1 != ft) {
printf("Brackets are not mactch\n");
return false;
}
}
plus -= 4;
}
else { // left bracket
plus += 4;
br.push(t);
}
}
else { // illegal character
printf("Illegal character: %c\n", str[i]);
return false;
}
}
if (!br.empty()) { // check bracket
printf("Brackets are not mactch\n");
return false;
}
while (!op.empty()) { // calculate rest
int fp = op.top();
op.pop();
if (!cal(fp))
return false;
}
if (num.empty()) { // no number left
printf("No Number\n");
return false;
}
else if (num.size() > 1) { // too many numbers
printf("Too many numbers\n");
return false;
}
res = num.top();
return true;
}
int main() {
scanf("%s", str);
double res = -1;
if (solve(res))
printf("%.3lf\n", res);
return 0;
}
计算表达式值
最新推荐文章于 2023-03-15 16:12:14 发布