欢迎来到天天文库
浏览记录
ID:40812532
大小:15.81 KB
页数:10页
时间:2019-08-08
《利用堆栈实现的计算器(代码)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、数据结构课程实验(利用堆栈实现简单计算器)代码共4个文件//main.cc文件#include#include"stack.h"#include"opretor.h"usingnamespacestd;voidmain(){Stack1stack1;Stack2staxk2;chars[50];stack1.Push('=');cout<<"请输入正确的算术式(正数计算,以‘=’结尾):"<>s;if(check(s)){Calculate(s,stack1,staxk2);}else{cout<<"输入错误!!"<2、("pause");}//stack.h文件#ifndefSTACK_H#defineSTACK_H#defineSTACK_INIT_SIZE100#defineSTACKINCREMENT10classStack1{private:char*top;char*base;intstacksize;intsize;public:Stack1();~Stack1();boolIsFull();boolIsEmpty();voidPush(chare);charPop();charGettop(charx);intgetsize();};classStack2{private:doub3、le*top;double*base;intstacksize;intsize;public:Stack2();~Stack2();boolIsFull();boolIsEmpty();voidPush(doublee);doublePop();doubleGettop(doublex);intgetsize();};#endif;//stack.cc文件#include"stack.h"#includeStack1::Stack1(){base=(char*)malloc(STACK_INIT_SIZE*sizeof(char));top=base;stack4、size=STACK_INIT_SIZE;size=0;}Stack1::~Stack1(){free(base);}boolStack1::IsFull(){if(top-base>=stacksize)returntrue;elsereturnfalse;}boolStack1::IsEmpty(){if(top==base)returntrue;elsereturnfalse;}voidStack1::Push(chare){if(top-base>=stacksize){base=(char*)realloc(base,(stacksize+STACKINCREMENT)*5、sizeof(char));top=base+stacksize;stacksize+=STACKINCREMENT;}*top++=e;size++;}charStack1::Pop(){charc;c=*(--top);size--;returnc;}charStack1::Gettop(charx){if(!IsEmpty()){x=*(top-1);returnx;}elsereturn'o';}intStack1::getsize(){returnsize;}Stack2::Stack2(){base=(double*)malloc(STACK_INIT_SIZE*siz6、eof(double));top=base;stacksize=STACK_INIT_SIZE;size=0;}Stack2::~Stack2(){free(base);}boolStack2::IsFull(){if(top-base>=stacksize)returntrue;elsereturnfalse;}boolStack2::IsEmpty(){if(top==base)returntrue;elsereturnfalse;}voidStack2::Push(doublee){if(top-base>=stacksize){base=(double*)realloc(b7、ase,(stacksize+STACKINCREMENT)*sizeof(double));top=base+stacksize;stacksize+=STACKINCREMENT;}*top++=e;size++;}doubleStack2::Pop(){doublec;c=*(--top);size--;returnc;}doubleStack2::Gettop(doublex){if(!IsEmpty()){x=*(top-1);returnx;}elsere
2、("pause");}//stack.h文件#ifndefSTACK_H#defineSTACK_H#defineSTACK_INIT_SIZE100#defineSTACKINCREMENT10classStack1{private:char*top;char*base;intstacksize;intsize;public:Stack1();~Stack1();boolIsFull();boolIsEmpty();voidPush(chare);charPop();charGettop(charx);intgetsize();};classStack2{private:doub
3、le*top;double*base;intstacksize;intsize;public:Stack2();~Stack2();boolIsFull();boolIsEmpty();voidPush(doublee);doublePop();doubleGettop(doublex);intgetsize();};#endif;//stack.cc文件#include"stack.h"#includeStack1::Stack1(){base=(char*)malloc(STACK_INIT_SIZE*sizeof(char));top=base;stack
4、size=STACK_INIT_SIZE;size=0;}Stack1::~Stack1(){free(base);}boolStack1::IsFull(){if(top-base>=stacksize)returntrue;elsereturnfalse;}boolStack1::IsEmpty(){if(top==base)returntrue;elsereturnfalse;}voidStack1::Push(chare){if(top-base>=stacksize){base=(char*)realloc(base,(stacksize+STACKINCREMENT)*
5、sizeof(char));top=base+stacksize;stacksize+=STACKINCREMENT;}*top++=e;size++;}charStack1::Pop(){charc;c=*(--top);size--;returnc;}charStack1::Gettop(charx){if(!IsEmpty()){x=*(top-1);returnx;}elsereturn'o';}intStack1::getsize(){returnsize;}Stack2::Stack2(){base=(double*)malloc(STACK_INIT_SIZE*siz
6、eof(double));top=base;stacksize=STACK_INIT_SIZE;size=0;}Stack2::~Stack2(){free(base);}boolStack2::IsFull(){if(top-base>=stacksize)returntrue;elsereturnfalse;}boolStack2::IsEmpty(){if(top==base)returntrue;elsereturnfalse;}voidStack2::Push(doublee){if(top-base>=stacksize){base=(double*)realloc(b
7、ase,(stacksize+STACKINCREMENT)*sizeof(double));top=base+stacksize;stacksize+=STACKINCREMENT;}*top++=e;size++;}doubleStack2::Pop(){doublec;c=*(--top);size--;returnc;}doubleStack2::Gettop(doublex){if(!IsEmpty()){x=*(top-1);returnx;}elsere
此文档下载收益归作者所有