棧(一)利用棧計算表示式
阿新 • • 發佈:2019-02-12
整理了一下使用資料結構這本書上的內容
順序棧類 sq_Stack.h
#pragma once #include<iostream> using namespace std; template<class T> class sq_Stack { private: int mm; int top; T*s; public: sq_Stack(int);//建構函式 ~sq_Stack();//解構函式 void prt_sq_Stack();//順序輸出棧頂指標與棧中元素 int flag_sq_Stack();//檢測順序棧狀態 void ins_sq_Stack(T);//入棧 T del_sq_Stack();//退棧 T read_sq_Stack();//讀取棧頂元素 }; //建立容量為mm的空棧 template<class T> sq_Stack<T>::sq_Stack(int m) { mm = m; s = new T[mm]; top = 0; return; } //解構函式 template<class T > sq_Stack<T>::~sq_Stack() { delete[]s; top = 0; mm = 0; return; } //順序輸出棧頂指標與棧中元素 template<class T> void sq_Stack<T>::prt_sq_Stack() { cout << "top=" << top << endl; for (int i = top - 1; i >= 0; i--) { cout << s[i] << " "; } return; } //檢測順序棧狀態 template<class T> int sq_Stack<T>::flag_sq_Stack() { if (top == mm)return -1;//滿棧 if (top == 0)return 0;//空棧 return 1; } //入棧 template<class T> void sq_Stack<T>::ins_sq_Stack(T data) { if (top == mm) { cout << "overflow"; return; } s[top] = data; top++; return; } //退棧 template<class T> T sq_Stack<T>::del_sq_Stack() { if (top == 0) { cout << "underflow"; return(0); } top--; return s[top]; } //讀取棧頂元素 template<class T> T sq_Stack<T>::read_sq_Stack() { if (top == 0) { cout << "empty"; return(0); } return s[top - 1]; } //int main() //{ // sq_Stack<int> s(10); // s.ins_sq_Stack(50); s.ins_sq_Stack(60); s.ins_sq_Stack(70); s.ins_sq_Stack(80); s.ins_sq_Stack(90); s.ins_sq_Stack(100); // s.prt_sq_Stack(); // cout << endl; // cout << s.read_sq_Stack(); // cout << endl; // cout << s.del_sq_Stack(); // cout << s.del_sq_Stack(); // cout << s.del_sq_Stack(); // cout << endl; // s.prt_sq_Stack(); //}
利用棧來實現表示式
#include"sq_Stack.h" double shishu(char*s, int *k) { double x = 0.0, y = 1.0; int flag = 1; char c; c = s[*k]; while (c >= '0'&&c <= '9' || c == '.') { *k = *k + 1; if (c >= '0'&&c <= '9') { if (flag == 0)//處理小數點以後的資料 { y *= 0.1; x += (c - '0')*y; } else//處理小數點之前的資料 { x *= 10; x += c - '0'; } } else flag = 0; c = s[*k]; } return x; } //計算運算子的優先順序 int pp(char c) { int k; switch (c) { case'*':k = 2; break; case'/':k = 2; break; case'+':k = 1; break; case'-':k = 1; break; case'(':k = 0; break; case')':k = 0; break; case 0:k = -1; break; } return k; } int main() { sq_Stack<double> sv(50); sq_Stack<char> sp(20); char s[60], c; int flag = 1, k; double x, y; sp.ins_sq_Stack(0);//表示式結束符進運算子棧 cout << "input s:" << endl; cin >> s; k = 0;//掃描指標初始化 c = s[k]; while (flag) { if (c >= '0'&&c <= '9' || c == '.')//當前字元為數字字元或小數點 sv.ins_sq_Stack(shishu(s, &k)); else if (c == '(' || pp(c) > pp(sp.read_sq_Stack())) { sp.ins_sq_Stack(c); k++; } else if (c == 0&&sp.read_sq_Stack() == 0) flag = 0; else if (c == ')' && (sp.read_sq_Stack() == '(')) { sp.del_sq_Stack(); k++; } else if (pp(c) <=pp(sp.read_sq_Stack())) { y = sv.del_sq_Stack(); x = sv.del_sq_Stack(); c = sp.del_sq_Stack(); switch (c) { case'*':x = x*y; break; case'/':x = x / y; break; case'+':x = x + y; break; case'-':x = x - y; break; } sv.ins_sq_Stack(x); } c = s[k]; } cout << s << "=" << sv.read_sq_Stack() << endl; }