您的位置:首页 > 编程语言 > C语言/C++

在单链表的基础上用c++实现的链栈,并使用进制转换,表达式求值两个小程序来测试

2009-04-16 22:47 1041 查看
//MyList.h 需要用到上一篇文章里的List类

#ifndef MYSTACK_H
#define MYSTACK_H
#include "MyList.h"

template<class T>
class MyStack:private List<T>
{
public:
void DestoryStack();			//销毁栈
void ClearStack();				//清除栈元素
bool StackEmpty() const;		//判断栈是否为空
int StackLength() const;		//返回栈长度
T& GetTop() const;			//返回栈顶元素
bool Push(const T& val);				//进栈
bool Pop(T& val);						//出栈
void PrintStack() const;		//输出栈的元素
};

template<class T>
void MyStack<T>::DestoryStack()				//销毁栈
{
DestoryList();
}

template<class T>
void MyStack<T>::ClearStack()				//清除栈内元素
{
ClearList();
}

template<class T>
bool MyStack<T>::StackEmpty() const			//判断栈空
{
return ListEmpty();
}

template<class T>
int MyStack<T>::StackLength() const			//返回栈的长度
{
return ListLength();
}

template<class T>
T& MyStack<T>::GetTop() const				//返回栈顶元素
{
int i=ListLength();
return GetElem(i);
}

template<class T>
bool MyStack<T>::Push(const T& val)			//进栈
{
int i=ListLength();
if(ListInsert(i+1,val))
{
return true;
}
return false;
}

template<class T>
bool MyStack<T>::Pop(T& val)				//出栈
{
int i=ListLength();
if(ListDelete(i,val))
{
return true;
}
return false;
}

template<class T>
void MyStack<T>::PrintStack() const			//打印栈内元素
{
PrintList();
}
#endif


//利用栈实现的一个进制转换小程序

#include<iostream>
#include "MyStack.h"
using namespace std;

int main()
{
MyStack<int> stack;
int val;
cout<<"输入一个十进制数字:"<<endl;
cin>>val;
while(val)
{
stack.Push(val%2);
val=val/2;
}
int size=stack.StackLength();
cout<<"转换后的二进制数字:"<<endl;
for(int i=0;i<size;i++)
{
stack.Pop(val);
cout<<val<<" ";
}
cout<<endl;
return 0;
}


//利用栈实现的表达式求值程序,支持基本四则运算和括弧

#include <iostream>
#include <string>
#include "MyStack.h"
using namespace std;

bool precede(char x,char y);			//比较两个运算符的优先级
int make(int op1,char m,int op2);	//进行运算

int main()
{
MyStack<int> opnd;			//操作数
MyStack<char> oprt;			//运算符
MyStack<int> conver;		//将数字字符转换为数字
string express;				//表达式

cout<<"请输入表达式:";
cin>>express;
express.append("#");		//表达式尾部添加一个结束标志符
int exp_size=express.size();		//表达式的长度

int cnt=0;
char val=0;
int num=0;
while(cnt<exp_size)
{
val=express[cnt];
if(val=='(')
{
oprt.Push(val);
}
else if(val>='0' && val<='9')		//处理操作数
{
if(conver.StackEmpty())
conver.Push(val-48);	//val-48把字符转换成数字压栈
else
{
conver.Pop(num);
num=num*10+val-48;
conver.Push(num);
}
}
else							//处理运算符
{
if(!conver.StackEmpty())
{
conver.Pop(num);
opnd.Push(num);				//转换后的数字入栈
}
if(oprt.StackEmpty() || !precede(oprt.GetTop(),val) )	//空栈或者当前待插入运算符比栈顶运算符优先级高则入栈
{
oprt.Push(val);
}
else if(opnd.StackLength()>1)										//当前待插入运算符比栈顶运算符优先级低则先进行运算
{
int op1,op2;			//两个操作数
char m;					//运算符
opnd.Pop(op2);
opnd.Pop(op1);
oprt.Pop(m);
int ret=make(op1,m,op2);	//进行运算
opnd.Push(ret);			//运算结果入栈
if(val==')')
{
oprt.Pop(m);
cnt++;
continue;
}
if( !oprt.StackEmpty() && precede(oprt.GetTop(),val) )			//如果当前待插入运算符优先级小于栈顶优先级则继续等待插入
continue;
oprt.Push(val);			//运算符入栈
}
}
cnt++;
}
int result=0;
opnd.Pop(result);
cout<<result<<endl;
return 0;
}

bool precede(char x,char y)			//比较两个运算符的优先级
{
if( x=='*' || x=='/' )
return 1;
else if( (x=='+' || x=='-') && (y=='+' || y=='-' || y=='#' ||y==')') )
return 1;
return 0;
}

int make(int op1,char m,int op2)	//进行运算
{
switch(m)
{
case '+':
return op1+op2;
case '-':
return op1-op2;
case '*':
return op1*op2;
case '/':
if(op2==0)
return 0;
else
return op1/op2;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: