【练习】c++用链栈实现计算器
生活随笔
收集整理的這篇文章主要介紹了
【练习】c++用链栈实现计算器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
棧有順序棧和鏈棧,其中順序棧相當于用數組表示,而鏈棧則用鏈表,在表現方式上用鏈棧當然更加復雜。
首先對于一個鏈棧,自己重寫類,需要寫的基本的函數:構造函數,析構函數,入棧操作,出棧操作,取棧頭,判斷棧空,(判斷棧滿的操作對于鏈棧沒有實際意義),其中的數據元素有指向下一個元素的結點指針和當前的元素個數。
上代碼:
定義模板類
#include<string> #include<iomanip> #define maxlen 1000template <class T> struct node {T data; // 元素字段 node* next; // 指針字段 };template <class T> class stack{ public:stack();~stack(); // 初始化bool empty() const {if (countt == 0) return true;else return false;}; // 判斷空bool full() const {if (countt == maxlen) return true;else return false;}; // 判斷滿T top() const;// 取棧頂元素void push(T x) {if (full())return;countt++;node<T>* p = new node<T>;p->data = x;// p->next = NULL;p->next = first;first=p;} //入棧void pop(); // 出棧// 棧的數據成員; private:int countt;node<T>* first = new node<T>;};其中要注意node* first,而不是node*< T>
(當模板類的數據成員中有模板結構體時的 處理方式)
成員函數的實現:
template <class T> stack<T> ::~stack() {// node<T>*p = first;node<T>* u = new node<T>;while (!empty()) {u = first;first = first->next;delete u;// pop();countt--;}};template <class T> stack<T> ::stack() {countt = 0;first = nullptr; }; template <class T> T stack<T>::top() const{int m = countt - 1;// node<T>* p = first->next;return first->data; } template <class T> void stack<T>::pop() {if (empty())return;node<T>* p = first;first= first->next;delete p;countt--;// first = first->next;}對于計算器的實現采用中綴轉后綴表達式:
class cal { public:double comppost();bool trans(string exp);void getfromclient(string& m){cin >> m;int si = m.size();exp = new char[si];post = new char[si];} private:char* post;char* exp;double result; };bool cal::trans(string expp) {stack<char>st;int k = 0, i = 0, j = 0;while (expp[j] != '\0'){if (expp[j] == 'C'){j++;i--;continue;}else if (expp[j] == 'A'){system("cls");return false;}else if (expp[j] == '='){exp[i++] = expp[j++];break;}else {exp[i++] = expp[j++];}}exp[i] = '\0';while (exp && (*exp != '=')){switch (*exp){case '(':st.push(*exp);exp++;break;case ')':while (!st.empty() && st.top() != '('){post[k++] = st.top();st.pop();}st.pop();//彈出“(”符號exp++;break;case '+':case '-':while (!st.empty()){if (st.top() != ')'){post[k++] = st.top();//若棧頂不是(則將棧頂放入post中st.pop();//出棧}elsebreak;//如果棧頂元素是'('時退出循環}st.push(*exp);exp++;break;case '*':case '/':while (!st.empty()){if (st.top() == '*' || st.top() == '/'){post[k++] = *exp;st.pop();}elsebreak;}st.push(*exp);exp++;break;default:while (*exp >= '0' && *exp <= '9'){post[k++] = *exp;exp++;}post[k++] = '#';//用#標識一個數字串結束 比如32這樣的多位數}}while (!st.empty())//將運算符放入post中{post[k++] = st.top();st.pop();}post[k] = '\0';//給post表達式添加結束符return true; } double cal::comppost()//計算后綴表達式的值 {double a, b, c;stack<double>st;while (*post){switch (*post){case '+':a = st.top(); st.pop();b = st.top(); st.pop();c = a + b;st.push(c);break;case '-':a = st.top(); st.pop();b = st.top(); st.pop();c = b - a;st.push(c);break;case '*':a = st.top(); st.pop();b = st.top(); st.pop();c = a * b;st.push(c);break;case '/':a = st.top(); st.pop();b = st.top(); st.pop();if (a == '0'){cout << "error!";exit(1);}elsec = b / a;st.push(c);break;default:double d = 0;while (*post >= '0' && *post <= '9'){d = d * 10 + *post - '0';post++;}st.push(d);break;}post++;}return st.top(); }int main() {string input;cal put;double res;put.getfromclient(input);//用戶接口if (put.trans(input))//計算引擎{res = put.comppost();cout << res;}elsecout << "已清零\n";}總結
以上是生活随笔為你收集整理的【练习】c++用链栈实现计算器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【笔记 】栈底层 循环队列的处理 链栈
- 下一篇: vue-springboot项目 myb