用栈实现十以内四则运算

mac2025-03-30  7

用栈实现十以内四则运算

main.cpp

#include<iostream> #include"Stack.h" using namespace std; int getResult(int a,int b,char c){ switch(c){ case'*': return a*b; case'+': return a+b; case'-': return b-a; case'/': return a/b; } } int formulate(string formulas){ pNode number; pNode cal; Stack *stack_num = new Stack(number, 0); stack_num->Pop(); Stack *stack_cal = new Stack(cal, 0); stack_cal->Pop(); int size = formulas.size(); for (int i = 0; i < size; i++) { if (formulas[i] > 47 && formulas[i] < 58) { stack_num->Push(number, int(formulas[i]) - 48); } else if (formulas[i] == '+')//运算符 { while ((!stack_cal->IsEmpty()) && stack_cal->top->data != '(') { int num1 = stack_num->GetTopdata(); stack_num->Pop(); int num2 = stack_num->GetTopdata(); stack_num->Pop(); char x = stack_cal->GetTopdata(); stack_num->Push(stack_num->top, getResult(num1, num2, x)); stack_cal->Pop(); } stack_cal->Push(stack_cal->top, '+'); } else if (formulas[i] == '-')//运算符 { while ((!stack_cal->IsEmpty()) && stack_cal->top->data != '(') { int num1 = stack_num->GetTopdata(); stack_num->Pop(); int num2 = stack_num->GetTopdata(); stack_num->Pop(); char x = stack_cal->GetTopdata(); stack_num->Push(stack_num->top, getResult(num1, num2, x)); stack_cal->Pop(); } stack_cal->Push(stack_cal->top, '-'); } else if (formulas[i] == '*') { if (stack_cal->IsEmpty() || stack_cal->top->data == '+' || stack_cal->top->data == '-') { stack_cal->Push(stack_cal->top, '*'); } else { while (stack_cal->top->data == '*' || stack_cal->top->data == '/') { int num1 = stack_num->GetTopdata(); stack_num->Pop(); int num2 = stack_num->GetTopdata(); stack_num->Pop(); char x = stack_cal->GetTopdata(); stack_num->Push(stack_num->top, getResult(num1, num2, x)); stack_cal->Pop(); } stack_cal->Push(stack_cal->top, '*'); } } else if (formulas[i] == '/') { if (stack_cal->IsEmpty() || stack_cal->top->data == '+' || stack_cal->top->data == '-') { stack_cal->Push(stack_cal->top, '/'); } else { while (stack_cal->top->data == '*' || stack_cal->top->data == '/') { int num1 = stack_num->GetTopdata(); stack_num->Pop(); int num2 = stack_num->GetTopdata(); stack_num->Pop(); char x = stack_cal->GetTopdata(); stack_num->Push(stack_num->top, getResult(num1, num2, x)); stack_cal->Pop(); } stack_cal->Push(stack_cal->top, '/'); } } else if (formulas[i] == '(') { stack_cal->Push(stack_cal->top, '('); } else if (formulas[i] == ')') { while (stack_cal->top->data != '(') { int num1 = stack_num->GetTopdata(); stack_num->Pop(); int num2 = stack_num->GetTopdata(); stack_num->Pop(); char x = stack_cal->GetTopdata(); stack_num->Push(stack_num->top, getResult(num1, num2, x)); stack_cal->Pop(); } stack_cal->Pop(); } else return 0; } while (!stack_cal->IsEmpty()) { int num1 = stack_num->GetTopdata(); stack_num->Pop(); int num2 = stack_num->GetTopdata(); stack_num->Pop(); char x = stack_cal->GetTopdata(); stack_num->Push(stack_num->top, getResult(num1, num2, x)); stack_cal->Pop(); } if (stack_cal->IsEmpty()) { cout<< stack_num->top->data << endl; } else { cout << "无解" << endl; } return 0; } int main() { string formulas;//存式子的地方 //把表达式存入formulas cin >> formulas; formulate(formulas); system("PAUSE"); return 0; }

Stack.h

#include<iostream> #ifndef STACK_H #define STACK_H using namespace std; typedef int Item; typedef struct node{ Item data; struct node *pNext; }Node ,*pNode; class Stack//栈类 { public: int size;//大小 pNode top;//栈顶 pNode Buttom;//栈底 Stack(pNode node, Item data); //产生一个栈 bool Push(pNode node, Item data);//进栈 bool Pop();//出栈 bool IsEmpty();//求是否空栈 int Stack_size();//求栈大小 Item GetTopdata();//获得栈顶元素 ~Stack(); protected: }; #endif

Stack.cpp

#include "Stack.h" int size; pNode top;//栈顶 pNode Buttom;//栈底 //产生一个栈 Stack::Stack(pNode node, Item data) { node = new Node; if (node == 0) { cout << "new 失败" << endl; return; } this->top = node; this->Buttom = node; this->size = 1; node->pNext = NULL; node->data = data; return; } //进栈 bool Stack::Push(pNode node, Item data) { node = new Node; if (node == 0) { cout << "new 失败" << endl; return false; } node->pNext = this->top; node->data = data; this->size++; this->top = node; return true; } //出栈 bool Stack::Pop() { if (!this->size) { cout << "栈空" << endl; this->top = NULL; return false; } pNode node; Item data; data = this->top->data; node = this->top; this->size--; this->top = this->top->pNext; delete node; return true; } //求是否空栈 bool Stack::IsEmpty() { if (this->size == 0) return true; else return false; } //求栈大小 int Stack::Stack_size() { return this->size; } //获得栈顶元素 Item Stack::GetTopdata() { return this->top->data; } Stack::~Stack() { }
最新回复(0)