題目連接html
後綴表達式又稱逆波蘭表示法,不含括號,運算符放在兩個參與運算的語法成分的後面。ios
後綴表達式運算求值c++
自左向右順序掃描後綴表達式。最後棧中的數字就是答案。post
(1)若是是數字,則壓入棧中。spa
(2)若是是運算符,就從棧中彈出兩個數字進行運算,將運算結果壓入棧中。.net
中綴表達式轉後綴表達式code
從左向右掃描中綴表達式。htm
(1)當輸入爲數字時,直接輸出到後續表達式序列中。blog
(2)當遇到開括號時,將其入棧。(表明一個子域)get
(3)當遇到閉括號時,先判斷棧是否爲空,若爲空,則表示括號不匹配,報告異常並退出。若非空,則將棧中元素依次彈出,直到遇到第一個開括號爲止(將開括號也彈出)。將彈出的元素輸出到後綴表達式序列中。若沒有遇到開括號,則報告異常並退出。
(4)當輸入爲運算符時(四則運算+ - * /之一)
(5)中綴表達式所有掃描完畢,清棧,所有彈出放入後綴表達式序列中。若彈出元素中有開括號,報告異常並退出。
模板-中綴表達式轉後綴表達式
注意:這個模板提醒了我一個有意思的點。char[]={0}就是在結尾加上了換行符,int[]={1}僅僅是將首元素變爲1,其他爲0,若是int[]則默認全部元素爲0。
#include<cstdio> #include<cstring> #include<iostream> #include<stack> using namespace std; int priority(char c) { if (c == '+' || c == '-')return 0; else if (c == '*' || c == '/')return 1; } int main() { char in[205]; char post[205] = { 0 }; stack<char> s; scanf("%s", in); int l = strlen(in); int size = 0; for (int i = 0; i < l; i++) { if (in[i] >= 'a'&&in[i] <= 'z')post[size++] = in[i]; else if (in[i] == '(')s.push(in[i]); else if (in[i] == ')') { if (s.empty()) { printf("Wrong!\n"); return 0; } while (s.top() != '(' && !s.empty()) { post[size++] = s.top(); s.pop(); } if (s.empty())printf("Wrong!\n"); else s.pop(); } else if (in[i] == '*' || in[i] == '/' || in[i] == '+' || in[i] == '-') { while (!s.empty() && priority(s.top()) >= priority(in[i]) && s.top() != '(') { post[size++] = s.top(); s.pop(); } s.push(in[i]); } } while (!s.empty()) { post[size++] = s.top(); s.pop(); } printf("%s\n", post); system("pause"); return 0; }
簡單計算器題解(北理工10年第二題)
#include<cstdio> #include<cstring> #include<iostream> #include<stack> using namespace std; int priority(char c) { if (c == '+' || c == '-')return 0; else if (c == '*' || c == '/')return 1; } int main() { char in[205]; char post[205] = { 0 }; stack<char> s; scanf("%s", in); int l = strlen(in); int size = 0; for (int i = 0; i < l; i++) { if (in[i] >= '0'&&in[i] <= '9')post[size++] = in[i]; else if (in[i] == '(')s.push(in[i]); else if (in[i] == ')') { if (s.empty()) { printf("Wrong!\n"); return 0; } while (s.top() != '(' && !s.empty()) { post[size++] = s.top(); s.pop(); } if (s.empty())printf("Wrong!\n"); else s.pop(); } else if (in[i] == '*' || in[i] == '/' || in[i] == '+' || in[i] == '-') { while (!s.empty() && priority(s.top()) >= priority(in[i]) && s.top() != '(') { post[size++] = s.top(); s.pop(); } s.push(in[i]); } } while(!s.empty()) { post[size++] = s.top(); s.pop(); } cout << post << endl; stack<int> ans; for (int i = 0; i < size; i++) { if (post[i] >= '0'&&post[i] <= '9')ans.push(post[i] - '0'); else { int b = ans.top(); ans.pop(); int a = ans.top(); ans.pop(); int c; if (post[i] == '+')c = a + b; else if (post[i] == '-')c = a - b; else if (post[i] == '*')c = a * b; else if (post[i] == '/')c = a / b; ans.push(c); } } printf("%d\n", ans.top()); system("pause"); return 0; }
後綴表達式轉二叉樹及前序遍歷獲得前綴表達式
將後綴表達式轉化成二叉樹:
首先準備一個二叉樹節點棧s.
一、從左開始向右遍歷逆波蘭式的元素。
二、每遍歷一個元素都新建一個樹節點p,char值爲當前字符(無論是操做數仍是運算符)。
若是取到的元素是操做數,直接把p入棧s;
若是是運算符,從棧中彈出2個節點,把第一個彈出的節點做爲p的右子樹,第二個彈出的節點做爲p的左子樹,而後把p入棧。
當遍歷完逆波蘭式時,樹的根節點就保存在棧裏了。
例:輸入:a+b*(c-d)-e/f 輸出:-+a*b-cd/ef (北理工13年第三題)
#include<cstdio> #include<cstring> #include<iostream> #include<stack> using namespace std; struct Node { Node* l; Node* r; char c; }t[50]; int loc = 0; Node* create() { t[loc].l = t[loc].r = NULL; return &t[loc++]; } void preorder(Node* t) { cout << t->c; if (t->l != NULL)preorder(t->l); if (t->r != NULL)preorder(t->r); } int priority(char c) { if (c == '+' || c == '-')return 0; else if (c == '*' || c == '/')return 1; } int main() { char in[205]; char post[205] = { 0 }; stack<char> s; scanf("%s", in); int l = strlen(in); int size = 0; for (int i = 0; i < l; i++) { if (in[i] >= 'a'&&in[i] <= 'z')post[size++] = in[i]; else if (in[i] == '(')s.push(in[i]); else if (in[i] == ')') { if (s.empty()) { printf("Wrong!\n"); return 0; } while (s.top() != '(' && !s.empty()) { post[size++] = s.top(); s.pop(); } if (s.empty())printf("Wrong!\n"); else s.pop(); } else if (in[i] == '*' || in[i] == '/' || in[i] == '+' || in[i] == '-') { while (!s.empty() && priority(s.top()) >= priority(in[i]) && s.top() != '(') { post[size++] = s.top(); s.pop(); } s.push(in[i]); } } while(!s.empty()) { post[size++] = s.top(); s.pop(); } stack<Node*>tr; for (int i = 0; i < size; i++) { Node* n = create(); n->c = post[i]; if (n->c >= 'a'&&n->c <= 'z')tr.push(n); else if (n->c == '*' || n->c == '/' || n->c == '+' || n->c == '-') { Node* a = tr.top(); tr.pop(); Node* b = tr.top(); tr.pop(); n->r = a; n->l = b; tr.push(n); } } Node* root = tr.top(); preorder(root); cout << endl; return 0; }