本文共 4880 字,大约阅读时间需要 16 分钟。
enum error_code{ success, overflow, underflow};error_code a;error_code b = success;
#includeusing namespace std;enum error_code{ success,overflow,underflow};class stack{ public: stack(); bool empty()const; bool full()const; error_code getTop(int &x)const; error_code push(const int x); error_code pop(); private: int count; int data[10];};stack::stack(){ count = 0;}bool stack::empty()const{ return (count == 0);}bool stack::full()const{ return (count == 10);}error_code stack::getTop(int &x)const{ if(empty()) return underflow; x = data[count-1]; return success;}error_code stack::push(const int x){ if(full()) return overflow; data[count] = x; count++; return success;}error_code stack::pop(){ if(empty()) return underflow; count--; return success;}bool ReferenceError(error_code a){ if(a == overflow){ cout << " overflow! " << endl; return false; } if(a == underflow){ cout<< " underflow " << endl; return false; } return true;}int main(){ stack s; int top; //栈顶 //入栈 for(int i = 0;i < 7;i++){ ReferenceError(s.push(i)); } //出栈 for(int i = 0;i < 5;i++){ ReferenceError(s.pop()); } //取出栈顶 ReferenceError(s.getTop(top)); cout << top << endl; return 0;}
#include#include using namespace std;#include enum error_code{ success, overflow, underflow};template class stack{ public: stack(); bool empty()const; bool full()const; error_code getTop(T& x)const; error_code push(const T x); error_code pop();private: int count; int data[100];}; template stack ::stack(){ count = 0;}template bool stack ::empty()const { return count == 0;}template bool stack ::full()const { return count == 10;}template error_code stack ::getTop(T& x)const { if (empty()) return underflow; x = data[count - 1]; return success;}template error_code stack ::push(const T x) { if (full()) return overflow; data[count] = x; count++; return success;}template error_code stack ::pop() { if (empty()) return underflow; count--; return success;}template bool ReferenceError(error_code a) { if (a == overflow) { cout << "overflow!" << endl; return false; } if (a == underflow) { cout << "underflow!" << endl; return false; } return true;}// 十进制转八进制 void transform(){ // 初始化题目条件 int num, result = 0; cout << "请输入要转化为八进制的十进制数:"; cin >> num; // 初始化栈结构 stack s; int top; // 开始运算 while(num != 0) { s.push(num % 8); num /= 8; } while(!s.empty()) { s.getTop(top); s.pop(); result = result * 10 + top; } cout << "转成八进制为:" << result << endl;}// 检验符号匹配string symbol(){ // 初始化题目条件 string s; cout << "请输入表达式S: "; cin >> s; int length; length = s.length(); // 初始化栈结构 stack a; char top; // 开始运算 for(int i = 0; i < length; i++) { if(s[i] == '(' || s[i] == '[' || s[i] == '{') a.push(s[i]); if(s[i] == ')') { a.getTop(top); if(top == '(') a.pop(); else return "不匹配!!"; } if(s[i] == ']') { a.getTop(top); if(top == '[') a.pop(); else return "不匹配!!"; } if(s[i] == '}') { a.getTop(top); if(top == '{') a.pop(); else return "不匹配!!"; } } return "匹配!!";}// 计算器 int getRes(int a, int b, char c) // 计算{ int result; switch(c){ case '+': result = a + b; break; case '-': result = a - b; break; case '*': result = a * b; break; case '/': result = a / b; break; } return result;}int getPri(char a) // 获得优先级 { if(a == ')') return 1; if(a == '+' || a == '-') return 2; if(a == '*' || a == '/') return 3; if(a == '(') return 4; else cout << "输入错误!!" << endl;}void calculator(){ // 初始化题目条件 string s; cout << "请输入计算式:"; cin >> s; int i = 0, k = 1, num1, num2; char sign; // 初始化栈结构 stack num; int top_n; stack oper; char top_o; // 开始计算 if(s[(s.length() - 1)] != '=') { cout << "算式请以 '=' 结尾"; return; } while(s[i] != '='){ if(s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' || s[i] == '(' || s[i] == ')') { l: if(!oper.empty()) { oper.getTop(top_o); if(getPri(s[i]) > getPri(top_o) || top_o == '(') oper.push(s[i]); else { oper.getTop(top_o); if(top_o != '(') { num.getTop(top_n); num1 = top_n; num.pop(); num.getTop(top_n); num.pop(); num.push(getRes(top_n, num1, top_o)); oper.pop(); oper.getTop(top_o); if(top_o == '(') { oper.pop(); i++; continue; } goto l; } } } if(oper.empty()) oper.push(s[i]); k = 1; } else { if(k == 0) { num.getTop(top_n); num.pop(); num.push(top_n * 10 + s[i] - 48); } if(k == 1) { num.push(s[i] - 48); k = 0; } } i++; } num.getTop(top_n); num1 = top_n; num.pop(); num.getTop(top_n); oper.getTop(top_o); cout << "计算结果为:" << getRes(top_n, num1, top_o) << endl;}int main(){ // 十进制转八进制 transform(); // 检验符号匹配 cout << symbol() << endl; // 计算器 calculator(); return 0;}
转载地址:http://rvnqi.baihongyu.com/