Header file:
#pragma once#include <iostream> #include <assert.h> #include <string>using namespace Std;template <class type>class seqstack{public:seqstack (size_t sz = INIT_SZ) ~seqstack ();p ublic:bool empty () Const;bool full ( ) const;void Show () Const;bool push (const Type &x); bool Pop (); void GetTop (Type &x); int length () const;void Clear () void Destory (), void Quit_system (Type &x);p rivate:enum{INIT_SZ = 64}; Type *base;int capacity;int top;}; Template<class type>seqstack<type>::seqstack (size_t sz = INIT_SZ) {capacity = SZ > INIT_SZ? sz:init_sz;ba SE = new Type[capacity];assert (base! = NULL); top = 0;} Template<class Type>seqstack<type>::~seqstack () {destory ();} Determine if the stack is full template<class type>bool seqstack<type>::full () Const{return (top >= capacity);} Determine if the empty stack template<class type>bool seqstack<type>::empty () const{return (top = = 0);} Show Template<class type>void seqstack<type>::show () const{if (top = = 0) {cout << "The stack is empty!" << Endl;return;} for (int i = top-1; I >= 0; i.) {cout << base[i] << Endl;}} Into the stack template<class type>bool seqstack<type>::p ush (const Type &x) {if (full ()) {cout << "the stack is Full,can not enter! "<< Endl;return false;} Else{base[top] = X;top++;return true;}} Out of Stack template<class type>bool seqstack<type>::p op () {if (empty ()) {cout << "the stack is Empty,can not PO p! "<< Endl;return false;} Else{top--;return true;}} Get stack top element template<class type>void seqstack<type>::gettop (Type &x) {x = base[top-1];} Find the stack length template<class type>int seqstack<type>::length () Const{return top;} Empty stack template<class type>void seqstack<type>::clear () {top = 0;} Destroy Stack Template<class type>void seqstack<type>::d estory () {delete[]base;base = null;capacity = top = 0;} Exit system Template<class type>void Seqstack<type>::quit_system (Type &x) {x = 0;}
Main function:
#include "exp.h" #include "exp.h"//Check the priority between the symbols, 1 means that the >,0 means =,-1 represents the operation within the <,C1 stack, c2 the operation int check (char C1, char c2) {int a1, A2;if (C1 = = ' + ' | | c1 = = '-') a1 = 3;if (C1 = = ' * ' | | c1 = = '/') a1 = 5;if (C1 = = ' (') a1 = 1;if (C1 = = ') ') a1 = 7;if (C1 = = ' # ') a1 = 0;if (C2 = = ' + ' | | c2 = = '-') a2 = 2;if (C2 = = ' * ' | | c2 = = '/') a2 = 4;if (C2 = = ' (') a2 = 6;if (C2 = = ') ') a1 = 1; if (C2 = = ' # ') a2 = 0;if (A1 > A2) return 1;if (a1 = A2) return 0;if (A1 < A2) return-1;} Symbolic operation function Double sum (char C, double D1, double D2) {switch (c) {case ' + ': return D1 + d2;break;case '-': return D1-D2;BREAK;CA Se ' * ': return d1 * d2;break;case '/': Return D1/d2;break;default:break;}} int main () {char *op = "+-*/() #"; string str;cin >> str;//to the expression string str add ' # ' end marker str.append (1, ' # '); seqstack<char> optr;//operator Stack seqstack<double> opnd;//operand stack int a = -1;//first ' # ' into the Stack Optr.push (' # '); while (true) { int B = a + 1;a = str.find_first_of (OP, a + 1), if (a = = String::npos) break;if (A! = b) {string SS (str, B, a);d ouble d = Atof (ss.c_sTR ());//data first into stack opnd.push (d);} Operator precedence Comparison of Char Val;optr.gettop (val), int che = check (val, str[a]), if ( -1 = = che)//stack outside priority large direct into stack {Optr.push (str[a]);} if (0 = = che)//stack and inside and outside the priority is equal to the stack {optr.pop ();} if (1 = = che)//Stack priority is large, out of the stack operation {char val;optr.gettop (val);d ouble d1;opnd.gettop (D1); Opnd.pop ();d ouble d2;opnd.gettop (D2 ); Opnd.pop ();d 1 = SUM (val, D2, D1);//operation result into stack opnd.push (D1); Optr.pop (); a--;}} Double S;opnd.gettop (s); cout << s << endl;return 0;}
Application of "Data structure" stack--infix expression Evaluation (C + +)