一个堆栈类模板,和用到此模板的一个表达式计算器。
输入四则运算表达式(可含括号),输出计算结果,暂未提供盛放运算和浮点数功能。
Stack.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | template <class T> class Stack { public: Stack(int stack_size = 30); ~Stack(); void Push(T value); T Pop(); T Top(); bool IsEmpty(); bool IsFull(); void ClearStack(); private: int top; T* stack; int size; }; template<class T> Stack<T>::Stack(int stack_size) { size = stack_size; Stack::stack = new T[size]; Stack::top = -1; } template<class T> Stack<T>::~Stack() { delete []stack; } template<class T> void Stack<T>::Push(T value) { stack[++top] = value; } template<class T> T Stack<T>::Pop() { return stack[top--]; } template<class T> T Stack<T>::Top() { return Stack[top]; } template<class T> bool Stack<T>::IsEmpty() { return(top==-1?true:false); } template<class T> bool Stack<T>::IsFull() { return(top==(size-1)?true:false); } template<class T> void Stack<T>::ClearStack() { top = -1; } |
Calculater.h:
#include <iostream> #include <string> #include <sstream> #include <vector> #include "Stack.h" using namespace std; class Calculate { public: Calculate(); ~Calculate(); int Cal(string express); private: string ParseString(string express); vector<string> MidToBehind(string express); int Compute(int a,int b,char operater); Stack<int> stack_int; Stack<char> stack_char; }; |
Calculater.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | Calculate::Calculate() { } Calculate::~Calculate() { } vector<string> Calculate::MidToBehind(string express) { stringstream strstrm(express); char temp_char; string temp_str; vector<string> result_vec; while (strstrm>>temp_char) { if (temp_char!='('&&temp_char!=')'&&temp_char!='+'&&temp_char!='-'&&temp_char!='*'&&temp_char!='/'&&temp_char!='=') { strstrm.putback(temp_char); strstrm>>temp_str; result_vec.push_back(temp_str); } else if(temp_char == '(') { stack_char.Push(temp_char); } else if(temp_char == ')') { if(stack_char.IsEmpty()) { cout<<"stack is empty when push ')'"<<endl; stack_char.ClearStack(); } else { while (temp_char = stack_char.Pop(),temp_char != '(') { result_vec.push_back(string(1,temp_char)); if (stack_char.IsEmpty()) { cout<<"stack is empty when pop,not found the '('"<<endl; stack_char.ClearStack(); } } } } else if(temp_char == '+'||temp_char == '-'||temp_char == '*'||temp_char == '/') { switch (temp_char) { char temp_char_2; case '+': while (!stack_char.IsEmpty()) { temp_char_2 = stack_char.Pop(); if (temp_char_2 == '(') { stack_char.Push(temp_char_2); break; } else result_vec.push_back(string(1,temp_char_2)); } stack_char.Push(temp_char); break; case '-': while (!stack_char.IsEmpty()) { temp_char_2 = stack_char.Pop(); if (temp_char_2 == '(') { stack_char.Push(temp_char_2); break; } else result_vec.push_back(string(1,temp_char_2)); } stack_char.Push(temp_char); break; case '*': while (!stack_char.IsEmpty()) { temp_char_2 = stack_char.Pop(); if (temp_char_2 == '('||temp_char_2 == '+'||temp_char_2 == '-') { stack_char.Push(temp_char_2); break; } else result_vec.push_back(string(1,temp_char_2)); } stack_char.Push(temp_char); break; case '/': while (!stack_char.IsEmpty()) { temp_char_2 = stack_char.Pop(); if (temp_char_2 == '('||temp_char_2 == '+'||temp_char_2 == '-') { stack_char.Push(temp_char_2); break; } else result_vec.push_back(string(1,temp_char_2)); } stack_char.Push(temp_char); break; default: break; } } else if (temp_char == '=') { while (!stack_char.IsEmpty()) { result_vec.push_back(string(1,stack_char.Pop())); } } else cout<<"wrong express"<<endl; } return result_vec; } int Calculate::Compute(int a,int b,char operater) { switch(operater) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': if(b == 0) { cout<<"divided by 0!wrong!"<<endl; return -1; } else return a / b; default: break; } cout<<"Not return"<<endl; return -1; } int Calculate::Cal(string express) { string new_exp = ParseString(express); string str_temp; vector<string> vec_exp = MidToBehind(new_exp); for (vector<string>::iterator it = vec_exp.begin();it != vec_exp.end();it++) { str_temp = *it; if (str_temp!="+"&&str_temp!="-"&&str_temp!="*"&&str_temp!="/") { int int_temp = 0; int time = 1; for (string::iterator itr = str_temp.begin();itr != str_temp.end();itr++) { int_temp = int_temp*10 + (*itr - '0'); } stack_int.Push(int_temp); } else { int a,b; b = stack_int.Pop(); a = stack_int.Pop(); char ch = str_temp[0]; int c = Compute(a,b,ch); stack_int.Push(c); } } return stack_int.Pop(); } string Calculate::ParseString(string express) { string new_exp; bool flag = false; for (string::iterator it = express.begin();it != express.end();it++) { if (*it>='0'&&*it<='9') { if(flag) new_exp.push_back(' '); flag = false; new_exp.push_back(*it); } else { flag = true; if(it != express.begin()) new_exp.push_back(' '); new_exp.push_back(*it); } } return new_exp; } |
你好!除了代码,此处没有多少原创之物,皆为本人搜集、整理、总结之记录与心得,欢迎转载分享!转载时请尽量注明出处,将不胜感激。祝你健康、快乐!
#include
…
std::stack s;
s.push(1);
cout << s.top();
s.pop();
…
纯属练习……
噢。。虽然不能动态管理内存(考虑用vector做底层实现很不错),不过比起stl的stack,有了clear和full判断,很赞。。。
记得你这样写过一个,当时还有些地方不明白。一会再找找看