作业:C++利用栈实现的计算器

通过把后缀表达式转为逆波兰式并压入栈中,再一一出栈完成运算,只支持整型数的运算//实现计算功能的模块(头文件Lunching.h)#include#include#include"Myblade.h"#include"MyRapier.h"using namespace std;//My target is a caculater that prove long numbers with space in them to caculate//At least can recognize two different prackets//ok then MOVEclass TNode{public:char Data;TNode *Next;};class Calculating{private:Stack OPER;NStack NUMB;public:float TheCaculater();int procede(char a, char b);float Operate(int a, int b, char Operater);void NmberAnalyze(char First, int &Number, char &Operater);void RPNTransfrom();void OutPut();void DestroyAll();};int Calculating::procede(char Top, char Ele){if(OPER.StackEmpty() == 1)return 1;int Fl = -1;int X = 0, Y = 0;int Priority[4][4] = { {1, 1, 0, 0},{1, 1, 0, 0},{1, 1, 1, 1},{1, 1, 1, 1}};if(Top == '+')X = 0;else if(Top == '-')X = 1;else if(Top == '*')X = 2;else if(Top == '/')X = 3;else{return 1;}if(Ele == '+')Y = 0;else if(Ele == '-')Y = 1;else if(Ele == '*')Y = 2;else if(Ele == '/')Y = 3;else if(Ele == '(')return 1;else{return 1;}Fl = Priority[X][Y];return Fl;};float Calculating::Operate(int a, int b, char Operater){float OP = 0;float OA = (float)a;float OB = (float)b;if(Operater == '+'){OP = OA + OB;}else if(Operater == '-'){OP = OA - OB;}else if(Operater == '*'){OP = OA * OB;}else if(Operater == '/'){OP = OA / OB;}else {cout << "Illegal Symbol" << endl;return -1;}return OP;};void Calculating::NmberAnalyze(char First, int &Number, char &Operater){char Accept = First;int Mi = 1, Sum = 0;TNode *Top = NULL, *Poi = NULL;Top = new TNode;Top->Data = Accept;Top->Next = NULL;while(( Accept <= '9' && Accept >= '0') || Accept == 32){cin >> Accept;if(Accept == 32)continue;else if(Accept > '9' || Accept < '0'){break;}else{Poi = new TNode;Poi->Data = Accept;Poi->Next = Top;Top = Poi;}}Operater = Accept;Poi = Top;while(Top){Sum = Sum + (Top->Data - 48) * Mi;Mi = Mi * 10;Top = Top->Next;}while(Top){Poi = Top->Next;delete Top;Top = Poi;}Number = Sum;};void Calculating::OutPut(){ NUMB.StackTreverse();};void Calculating::RPNTransfrom(){char Got = 0, Rec = 0, Acc = 0, Sym = 0;int Coun = 0, NO = 0, Id = 0;OPER.IniStack();NUMB.IniStack();Got = getchar();while(Got != '='){ if(Got == 32){Got = getchar();continue;}if( Got == 40 ||Got == 41){if(Got == 40){OPER.Push(Got);}else if(Got == 41){Rec = OPER.Pop();while(Rec != 40){NUMB.CPush(Rec);Rec = OPER.Pop();}}}else if(Got ==42 || Got == 43 || Got == 45 || Got==47){if(OPER.StackEmpty() == 1)OPER.Push(Got);else{if(procede(Got, OPER.GetTop()) == 1)OPER.Push(Got);else{while(procede(Got, OPER.GetTop()) != 1){if(OPER.StackEmpty() == 1){OPER.Push(Got);break;}Rec = OPER.Pop();NUMB.CPush(Rec);}OPER.Push(Got);}}}else if(Got >= 48 && Got <= 57){NmberAnalyze(Got, Coun, Acc);NUMB.IPush(Coun);if(Acc == '='){ break;}if( Acc == 40 ||Acc == 41){if(Acc == 40){OPER.Push(Acc);}else {Sym = OPER.Pop();while(Sym != 40){NUMB.CPush(Sym);Sym = OPER.Pop();}}}else if(Acc ==42 || Acc == 43 || Acc == 45 || Acc==47){if(OPER.StackEmpty() == 1)OPER.Push(Acc);else{if(procede(Acc, OPER.GetTop()) == 1)OPER.Push(Acc);else{while(procede(Acc, OPER.GetTop()) != 1){if(OPER.StackEmpty() == 1){OPER.Push(Acc);break;}Sym = OPER.Pop();NUMB.CPush(Sym);}OPER.Push(Acc);}}}else{cout << "Illigel operaterC" <next;delete Mark;Top = NULL;}}};;int Stack::StackEmpty(){if(Top == NULL){return 1;}else{return 0;}};int Stack::StackLenth(){if(Top == NULL){cout << "This Stack is empty" << endl;return 0;}else{int count = 0;Node *Op = Top;while(Op){count++;Op = Op->next;}return count;}};;char Stack::GetTop(){if(!Top){return 0;}else{return Top->Data;}};void Stack::Push(char e){Node *Ele = new Node;Ele->Data = e;Ele->next = Top;Top = Ele;};char Stack::Pop(){char e;if(!Top){cout << "This Stack is empty" << endl;}else{Node *Mark = Top;e = Top->Data;Top = Top->next;delete Mark;}return e;};void Stack::StackTreverse(){if(Top == NULL){cout << "This Stack is empty" << endl;}else{Node *Op = Top;while(Op){cout << Op->Data << endl;Op = Op->next;}}};void Stack::Reverse(){if(Top == NULL){cout << "This Stack is empty" << endl;}else{Node *Pre = NULL;Node *Poi = Top;Node *Go = Top->next;while(Poi){Go = Poi->next;Poi->next = Pre;Pre= Poi;Poi = Go;}Top = Pre;}};//可以存数字和运算符和栈(MyRapier.h)#includeusing namespace std;class NNode{public:int Figure;char Symbol;int Flag;NNode *next;};class NStack{private:NNode *Top;public:void IniStack();void Destroy();int StackEmpty();int StackLenth();int GetTop();void IPush(int e);void CPush(char e);void Pop(int &Reg, int &Jug);void StackTreverse();void Reverse();};void NStack::IniStack()//OK{Top = NULL;};void NStack::Destroy()//OK{if(Top == NULL){return;}else{NNode *Op = Top;NNode *Mark = Top;while(Op){Mark = Op;Op = Op->next;delete Mark;Top = NULL;}}};;int NStack::StackEmpty()//OK{if(Top == NULL){return 1;}else{return 0;}};int NStack::StackLenth()//OK{if(Top == NULL){cout << "This Stack is empty" << endl;return 0;}else{int count = 0;NNode *Op = Top;while(Op){count++;Op = Op->next;}return count;}};;int NStack::GetTop()//ok{if(!Top){cout << "This Stack is empty" << endl;}else{if(Top->Flag == 1)return Top->Figure;else if(Top->Flag == 0)return Top->Symbol;}return 0;};void NStack::IPush(int e)//ok{NNode *Ele = new NNode;Ele->Figure = e;Ele->Flag = 1;Ele->next = Top;Top = Ele;};//ATTENTION ATTENTION!!! 1 IS NUMBER AND 0 IS OPERATER;、、、、、、、、、、、、、、、、、、、、、、、、、、、、void NStack::CPush(char e)//ok{NNode *Ele = new NNode;Ele->Symbol = e;Ele->Flag = 0;Ele->next = Top;Top = Ele;};void NStack::Pop(int &Reg, int &Jug)//ok{if(!Top){cout << "This Stack is empty" << endl;return;}else{NNode *Mark = Top;if(Top->Flag == 1){Reg = Top->Figure;Jug = 1;}else if(Top->Flag == 0){Reg = Top->Symbol;Jug = 0;}Top = Top->next;delete Mark;}};void NStack::StackTreverse()//ok{if(Top == NULL){cout << "This Stack is empty" << endl;return;}else{NNode *Op = Top;while(Op){if(Op->Flag == 1)cout << Op->Figure << endl;else if(Op->Flag == 0)cout << Op->Symbol << endl;Op = Op->next;}}};void NStack::Reverse()//ok{if(Top == NULL){cout << "This Stack is empty" << endl;}else{NNode *Pre = NULL;NNode *Poi = Top;NNode *Go = Top->next;while(Poi){Go = Poi->next;Poi->next = Pre;Pre= Poi;Poi = Go;}Top = Pre;}};//主函数MySheild.cpp#include#include#include"Lunching.h"using namespace std;int main(){Calculating A;cout << A.TheCaculater() << endl;return 0;}

你可能感兴趣的:(作业:C++利用栈实现的计算器)