#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<algorithm>
#include<string>
using namespace std;
const int maxsize = 105;
struct Stack{
char Data[maxsize];
int ptr;
Stack() {
memset(Data, '\0', sizeof(Data));
ptr = -1;
}
int empty() {
if (ptr == -1) return 1;
else return 0;
}
int full() {
if (ptr == maxsize - 1) return 1;
else return 0;
}
void push(char ch) {
if (!full()) {
Data[++ptr] = ch;
}
else printf("栈满");
}
void pop() {
if (!empty()) {
Data[ptr] = '\0';
ptr--;
}
}
char top() {
if (!empty()) return Data[ptr];
}
}s1,s2;
map<char, int>mpp;
int calcul(char ch, int num1, int num2) {
if (ch == '+') return num1 + num2;
else if (ch == '-') return num1 - num2;
else if (ch == '*') return num1 * num2;
else if (ch == '/' && num2 != 0) return num1 / num2;
}
int cal(string ans) {
int len = ans.length();
for (int i = 0; i < len; i++) {
if (isdigit(ans[i])) s2.push(ans[i]);
else {
int num1 = s2.top() - '0'; s2.pop();
int num2 = s2.top() - '0'; s2.pop();
s2.push(calcul(ans[i], num2, num1) + '0');
}
}
return s2.top() - '0';
}
int main()
{
string buf, ans = "";
getline(cin, buf);
mpp['+'] = 1; mpp['-'] = 1; mpp['*'] = 2; mpp['/'] = 2;
for (int i = 0; i < buf.length(); i++) {
if (isdigit(buf[i])) ans += buf[i];
else if (buf[i] == ' ') continue;
else {
char ch = buf[i];
if (s1.empty() || s1.top() == '(' || ch == '(') s1.push(ch);
else if (ch == ')') {
while (ch != '(') {
ch = s1.top(); s1.pop();
if (ch != '(') ans += ch;
}
}
else {
if (mpp[ch] > mpp[s1.top()]) s1.push(ch);
else {
while (!s1.empty()&&mpp[ch] <= mpp[s1.top()]) {
ans += s1.top();
s1.pop();
}
s1.push(ch);
}
}
}
}
while (!s1.empty()) {
ans += s1.top();
s1.pop();
}
cout << cal(ans);
}
#include "pch.h"
#include<iostream>
#include<queue>
#include<map>
#include<sstream>
#include<stack>
#include<algorithm>
#include<string>
using namespace std;
const int TreeSize = 105;
typedef int ElementType;
typedef struct PTNode {
ElementType data;
int parent;
};
typedef struct PTree* TreePtr;
typedef struct PTree {
PTNode nodes[tree_size];
int root, num_of_ndoes;
};
TreePtr InitTree() {
return new PTree();
}
void CreatTree(TreePtr& T) {
printf("输入树的节点个数:");
int nodes_num;
scanf("%d", &nodes_num);
T->num_of_ndoes = nodes_num;
for (int i = 0; i < nodes_num; i++) {
scanf("%d %d", &T->nodes[i].data, &T->nodes[i].parent);
}
}
typedef struct Node {
ElementType data;
Node * Next;
}* ChildPtr;
typedef struct {
ElementType Data;
ChildPtr Next;
}HeadNode;
typedef struct {
HeadNode Nodes[TreeSize];
int NumOfNodes, Root;
}Tree;
int main() {
}