四则运算(用栈实现)

main.h

1 #pragma once 2 #include <iostream> 3 #include <malloc.h> 4 using namespace std; 5 int GET(int const &a,char const &ch,int const &b); 6 template<typename T>//用模板,因为我要使用一个字符栈,保存运算符号,,一个数字栈,保存要计算的数字。 7 class Stack 8 { 9 public: 10 Stack() 11 { 12 STACK = 10; 13 data = (T *)malloc(sizeof(T)*STACK); 14 size = 0; 15 } 16 bool IS_FULL()const; 17 void push(T x)throw(); 18 void NEW(); 19 T pop()throw(); 20 void view()const throw(); 21 int GetTop()const throw(); 22 private: 23 T *data; 24 int size; 25 int STACK; 26 }; 27 template<typename T> 28 void Stack<T> :: NEW() 29 { 30 STACK+=3; 31 data = (T *)realloc(data,sizeof(T)*STACK); 32 } 33 template<typename T> 34 bool Stack<T> :: IS_FULL()const 35 { 36 if(size == STACK) 37return true; 38 else return false;39 } 40 template<typename T> 41 int Stack<T> :: GetTop()const throw() 42 { 43 if(size == 0)cout<<"no member!" ; 44 else 45 return data[size-1]; 46 } 47 template<typename T> 48 void Stack<T> :: push(T x)throw() 49 { 50 if(this->IS_FULL()) 51 { 52 this->NEW(); 53 } 54 data[size++] = x; 55 } 56 template<typename T> 57 T Stack<T> :: pop()throw() 58 { 59 if(size == 0)cout<<"no member!"<<endl; 60 else 61 return data[–size]; 62 } 63 template<typename T> 64 void Stack<T> :: view()const throw() 65 { 66 for(int i=0;i<size;i++) 67 { 68cout<<"—>"<<data[i]<<endl; 69 } 70 } 71 char CompChar(char const &a,char const &b)//判断运算符号的优先等级。 72 { 73 int i = 0;74 int j = 0; 75 for(;i<5;i++) 76 { 77if(a == "#+-*/"[i]) 78{ 79break; 80} 81 } 82 for(;j<5;j++) 83 { 84if(b == "#+-*/"[j]) 85{ 86break; 87} 88 } 89 char p[][5] ={'=','<','<','<','<', 90'>','=','=','<','<', 91'>','=','=','<','<', 92'>','>','>','=','=', 93'>','>','>','=','='};//用数组实现,能使选择迅速。 94 return p[i][j]; 95 } 96 bool IS_CHAR(char const &a) 97 { 98 if(a == '+' || a == '-' || a== '*' || a== '/' || a=='#') 99 return true;100 else return false;101 }102 int Calculate(char *str,Stack<int> &stack1,Stack<char> &stack2)103 {104 105 char ch1;106 int a1;107 int b1;108 int result;109 while(*str == ' ')110 {111str++;112 }113 stack2.push('#');114 int a = 0;115 int b = 0;116 while(!IS_CHAR(*str))117 {118a = a*10+(*str-'0');119str++;120 }121 stack1.push(a);122 stack2.push(*str);123 str++;124 while(!IS_CHAR(*str))125 {126b = b*10+(*str-'0');127str++;128 }129 stack1.push(b);130 while(stack2.GetTop() != '#' || (*str) != '#')131 {132 133 switch(CompChar(stack2.GetTop(),*str))134 {135case '>':136ch1 = stack2.pop();137a1 = stack1.pop();138b1 = stack1.pop();139result = GET(b1,ch1,a1);140stack1.push(result);141break;142case '=':143ch1 = stack2.pop();144a1 = stack1.pop();145b1 = stack1.pop();146result = GET(b1,ch1,a1);147stack1.push(result);148break;149case '<':150stack2.push(*str);151str++;152a = 0;153while(!IS_CHAR(*str))154{155a = a*10+(*str-'0');156str++;157}158stack1.push(a);159break;160}161 }162 return stack1.pop();163 }164 int GET(int const &a,char const &ch,int const &b)165 {166 switch(ch)167 {168 case '-':return (a-b);break;169 case '+':return (a+b);break;170 case '*':return (a*b);break;171 case '/':return (a/b);break;172 }173 }174 main.cpp

1 #include <iostream> 2 #include <string.h> 3 #include "main.h" 4 using namespace std; 5 int main() 6 { 7 Stack<int> mystack1; 8 Stack<char> mystack2; 9 char *p = new char [100]; 10 cout<<"please input:"<<endl; 11 while(1) 12 { 13 cin>>p; 14 if(!strcmp(p,"end")) 15{ 16return 0; 17} 18 strcat(p,"#"); 19 cout.width(40); 20 cout<<"运算结果是:"<<Calculate(p,mystack1,mystack2)<<endl; 21 } 22 }

我先将’#’压入字符栈,最后通过判断是否到达最后一个元素‘#’来判断结束,+-*/的优先级我使用数组来实现,可是102-163比较不好,不优雅。

其实生命无论长短,只要我们能努力绽放出生命的光彩,便会拥有精彩的人生。

四则运算(用栈实现)

相关文章:

你感兴趣的文章:

标签云: