C++栈
来源:互联网 发布:郭德纲 人品 知乎 编辑:程序博客网 时间:2024/06/10 10:11
只能从一端访问的线性群体
特点:先进后出
例:栈模板(z.h)
#ifndef STACK_CLASS#define STACK_CLASS#include<iostream.h>#include<stdlib.h>const int MaxStackSize=50;//栈空间50template<class T>class Stack{private:T stacklist[MaxStackSize];int top;public:Stack(void);//构造函数vod Push(const T&item);//进栈T Pop(void);//出栈void ClearStack(void);//清空整个栈T Peek(void)const;int StackEmpty(void)const;//判断栈是否空int StackFull(void)const;//判断栈是否满};//构造函数template<class T>Stack<T>::Stack(void):top(-1)//栈顶初始化为-1{}//将元素item压入栈template<class T>void Stack<T>::Push(const T&item){//如果栈满,程序终止if(top==MaxStackSize-1){cerr<<"Stack overflow!"<<endl;exit(1);}top++;//栈顶指针增1stacklist[top]=item;//新元素压入栈顶}//讲栈顶元素弹出栈template<class T>T Stack<T>::Pop(void){T temp;if(top==-1){//如果栈空,程序终止cerr<<"Attempt to pop an empty stack!"<<endl;exit(1);}temp=stacklist[top];//取出栈顶元素top--;//栈顶指针自减return temp;//返回栈顶元素}//访问栈顶元素template<class T>T Stack<T>::Peek(void)const{if(top==-1)]{//如果栈空,程序终止cerr<<"Attempt to peek at an empty stack!"<<endl;exit(1);}return stacklist[top];//返回栈顶元素}//测试是否栈空template<class T>int Stack<T>::StackEmpty(void)const{//如果栈空则返回TRUE,否则返回FALSEreturn top==-1;}//测试是否栈满template<class T>int Stack<T>::StackFull(void)const{//如果栈满返回true,否则falsereturn top==MaxStackSize-1;}//清空栈template<class T>void Stack<T>::ClearStack(void){top=-1;}#endif //STACK_CLASS
例:实现一个简单计算器,能够进行加减乘除和乘方运算。使用时,算是采用后缀输入法,例如计算
“3+5”则输入“3 5 +”。乘方运算符用“^”表示。每次运算在前次结果基础上进行,若要将前次运算结果清
除,可键入“c”。当键入“q”时,程序结束。
jsq.h
//z2.h#include<iostream.h>#include<math.h>#include<stdlib.h>#include<string.h>enum Boolean{False,True};#include"z.h"class Calculator{private:Stack<int>S;//栈Svoid Enter(int num);Boolean GetTwoOperands(int& opnd1,int& opnd2);void Compute(char op);public:Calculator(void){}void Run(void);void Clear(void);};void Calculator::Enter(int num)//私有成员函数,输入入栈{S.Push(num);}Boolean Calculator::GetTwoOperands(int& opnd1,int&opnd2){//私有成员函数,判断两个操作数是否存在,栈中弹出if(S.StackEmpty()){cerr<<"Missing operand!"<<endl;return False;}opnd1=S.Pop();if(S.StackEmpty()){cerr<<"Missing operand!"<<endl;return False;}opnd2=S.Pop();return True;}//正整数计算函数void Calculator::Compute(char op){Boolean result;int operand1,operand2;result=GetTwoOperands(operand1,operand2);if(result==True){switch(op){case'+':S.Push(operand2+operand1);break;case'-':S.Push(operand2-operand1);break;case'*':S.Push(operand2*operand1);break;case'/':if(operand1==0){cerr<<"Divide by 0!"<<endl;S.ClearStack();}elseS.Push(operand2/operand1);break;case:'^':S.Push(pow(operand2,operand1));break;}cout<<'='<<S.Peek()<<' ';}elseS.ClearStack}//运算符,按键处理函数void Calculator::Run(void){char c[20];while(cin>>c,*c!='q')switch(*c){case'c':S.ClearStack();break;case'-':if(strlen(c)>1)//判断是否为负数Enter(atoi(c));//atoi:把字符串转成整形数else Compute(*c);break;//是正数就去compute那执行case'+':case'*':case'/':case'^':Compute(*c);break;default:Enter(atoi(c));break;}}void Calculator::Cear(void){S.ClearStack();}//清空栈
计算器.cpp
#include"jsq.h"void main(void){Calculator CALC;CALC.Run();}
0 0
- 栈(C#)
- C/C++:堆与栈
- [C/C++] 函数调用的栈分配
- 堆和栈的区别(C/C++)
- C++/C语言中链表队列与栈
- 【C/C++】堆和栈的区别
- 【C语言/C++】 栈和队列
- 【C/C++】堆和栈的区别
- 两个栈实现一个队列(C/C++)
- 数据结构(栈)c/c++/java实现
- 顺序栈(C#)
- NUnit--C#---栈---单元测试
- 栈(C语言)
- C数据结构 栈
- c实现顺序栈
- 四则运算 c/c++ 栈
- C语言算法:栈
- C语言--堆、栈
- ZOJ-3414-Trail Walk_计算几何
- Tomcat配置连接池
- POJ 3264 Balanced Lineup
- iPhone开发 屏幕适配(3.5英寸和4英寸屏幕适配)
- jquery tmpl 模版
- C++栈
- 用批处理删除txt中指定的行和替换指定的行,以及添加指定的行
- 获取本地IP
- 证书与成绩单
- java--jsp图片验证码
- phpinfo() 查看php信息
- 完整dm368打印信息
- GCC编译之如何控制共享文件导出符号
- Python学习中碰到的问题