Original works reproduced please indicate the source
Welcome to copy, for the benefit of you hand party
/************************************************************************* > File Name:evaluator.cpp > Author : Acvcla > Version 1.01 > qq:[email protected] > Mail: [email protected] > Created time : November 17, 2014 Monday 23:34 13 seconds > This calculator supports subtraction four operations, support floating point, enter expression, press ENTER to calculate results > Exit Please enter EOF (Win press Ctrl+z,linux under Ctrl+d) > code Compiled in g++ environment, recommended to use Codeblocks compile run, strongly not recommended vc++6.0 > if not codeblocks, you can use dev C + + or VS2010 version *************************** /#include <iostream> #include <string> #include < cctype>using namespace Std;const int init_size=10;const int maxn=100005; #define Debugtemplate <typename T> struct stack{string name; T *base,*top;int size; Stack (const string s= "None"): Name (s)/* Initialize stack */{top=base=new t[size=init_size];} void Expansion (); void push (T e)/* into Stack */{expansion (); #ifdef debugcout<<name<< "Push" <<e<<endl; #endif *top++=e;} T & Top () const/* fetch stack Top reference */{return * (top-1);} bool Empty () const/* determines whether the stack is empty */{return base==top;} T pop ()/* Returns the top element of the stack and stacks */{if (Empty ()) {cout<<name<< "is empty" <<endl;return T (NULL);} #ifdef debugcout<<name<< "Pop" <<* (top-1) <<endl; #endifreturn * (--top);}; Template<typename t>void stack<t>::expansion ()/* Expansion function */{if (top-base>=size) {T *newbase= new T[size< <=1];/* multiplied to reduce the averaging complexity, averaging o (1) */int i=0;while (base+i!=top) * (newbase+i) =* (base+i);/* Copy to newly opened space */delete base;/* release old space */base=newbase;}} void Readnum (char *&s,stack<double>&f)/* Reads a number from S and points s to the first operator after the number */{double Tmp=0,c=1;int found=0;while ( IsDigit (*s) | | *s== '. ') {if (*s== '. ') {found=1; S++;continue;} if (found) c*=10;tmp=tmp*10+ (*s)-' 0 '; s++;} F.push (TMP/C);} Double Calcu (double A,char op,double B)/* Calculate a op b*/{switch (OP) {case ' + ': return a+b;case '-': return a-b;case ' * ': return a*b ; case '/': return a/b;}} int Optoi (const char OP)/* Converts the operator to the number */{switch (OP) {case ' + ': return 0;case '-': return 1;case ' * ': return 2;case '/': Return 3;c Ase ' (': Return 4;cAse ') ': Return 5;case ': Return 6;}} Char oderbetween (const char a,const char b)/* judgment operator precedence */{const int op_kind=10;const char pri[op_kind][op_kind]={"> ><<<>> "," >><<<>> "," >>>><>> "," >>>>< >> "," <<<<<= "," "", "<<<<< ="};return pri[optoi (a)][optoi (b)];} Double Evaluate (char *s)/* Algorithm main process */{STACK<DOUBLE>OPND ("OPND");/* Operand stack */stack<char>optr ("optr");/* Operation Fu Yi */ Optr.push (' + '); while (!optr.empty ()) {if (IsDigit (*s) | | *s== '. ') Readnum (S,OPND);/* Reads the digital */else{if (*s== ") Continue;switch (Oderbetween (optr). Top (), *s)) {/* calculated according to priority */case ' < ': Optr.push (*s); S++;break;case ' = ': Optr.pop (); S++;break;case ' > ': {char op=optr.pop ();d ouble opnd2=opnd.pop (), Opnd1=opnd.pop (); Opnd.push (CALCU (opnd1,op,opnd2 )); break;}}} return Opnd.pop ();} Char exptr[maxn];/* expression array */int main (int argc, char const *argv[]) {while (cin>>exptr) {cout<<evaluate (exptr) <<endl;} return 0;}
Data structure experiment: Stack implementation calculator (expression calculation)