简单的整数计算器

http://dearymz.blog.163.com/blog/static/2056574200612393970/

//简单的整数计算器----堆栈的应用

作者:秒大刀

完成日期:2004-10-07

这是一个将中序表达式变成后序表达式,并按照后序表达式进行整数四则运算是的程序

mystak.h        计算器中的一个特殊堆栈,其中push()函数可以保证中序表达式->后序表达式过程中运算优先级别的合理性
mystack.cpp     为以上类的实现文件
main.cpp        为测试驱动文件


void M2B(char from[],char to[])            函数将中序计算成后序
int compvalue(char exp[],long int *n)      计算后序表达式的值
void main()                                为驱动程序


 

//mystak.h
//计数器中的一个特殊堆栈,其中push()函数可以保证中序表达式->后序表达式过程中运算优先级别的合理性



#include<stack>
using namespace std ;

class mystack{
public:
mystack();
~mystack();
public:
bool empty()const;
char top()const;
void pop();
int push(char ch,char *mubiao);
private:
stack<char> sta;
};


 

//mystack.cpp
#include"mystack.h"

mystack::mystack(){}
mystack::~mystack(){}
char mystack::top()const
{
return sta.top();
}
void mystack::pop()
{
sta.pop();
}
int mystack::push(char ch,char *mubiao)//关键函数
{
int num(0);
if(ch=='+'||ch=='-')
{
while((empty()==false)&&(sta.top()!='('))
{
*mubiao=sta.top();sta.pop();mubiao++;
++num;
}
//todo:
//sta.push('#');
sta.push(ch);
}
else if(ch=='*'||ch=='/')//此处有BUG!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
{
#ifdef DEBUG
cout<<"经过了标记"<<__LINE__<<'\t';
#endif
while((sta.empty()==false)&&(sta.top()!='-'&&sta.top()!='+'&&sta.top()!='('))
{
*mubiao=sta.top();sta.pop();mubiao++;
++num;
}
//todo:
//sta.push('#');
sta.push(ch);
}
else if(ch==')')
{
#ifdef DEBUG
cout<<"经过了标记"<<__LINE__<<'\t';
#endif
while((sta.empty()==false)&&(sta.top()!='('))
{
*mubiao=sta.top();sta.pop();mubiao++;
++num;
}
//todo:
//sta.push('#');
sta.pop();
}
else//'('
{
//todo:
sta.push(ch);
}
return num;
}
bool mystack::empty()const
{
if(sta.empty()==false)return false;
else return true;
}


 

//main.cpp 测试函数
#define DEBUG
#include<iostream>
#include"mystack.h"
//#include"run.cpp"

void M2B(char from[],char to[])
{
mystack stack;
char ch;//临时缓冲区
int f(0),t(0);//分别为数组from和to的下标
while((ch=from[f++])!='\0')
{
#ifdef DEBUG
cout<<'('<<"ch="<<ch<<")\t";
#endif
if(ch>='0'&&ch<='9')
{
to[t++]=ch;//
while((ch=from[f++])!='\0'&&(ch>='0'&&ch<='9')/*||ch=='.'*/)
{
to[t++]=ch;
}
f--;
to[t++]='#';
#ifdef DEBUG
cout<<"是数字"<<endl;
#endif
}
else
{
t+=stack.push(ch,&(to[t]));//?????
#ifdef DEBUG
cout<<"是符号"<<endl;
#endif
}
}
while(stack.empty()==false)
{
to[t++]=stack.top();stack.pop();
}
to[t]='\0';
return;
}
/
int compvalue(char exp[],long int *n)//计算后序表达式的值
{
stack<int> st;
long int d,b;
char ch;
int t(0);//t作为exp的下标
while((ch=exp[t++])!='\0')
{
if(ch>='0'&&ch<='9')
{
d=0;
do{d=10*d+ch-'0';}
while((ch=exp[t++])!='#');
st.push(d);
}
else
{
d=st.top();st.pop();
b=st.top();st.pop();
switch(ch)
{
case '+':st.push(b+d);break;
case '-':st.push(b-d);break;
case '*':st.push(b*d);break;
case '/':st.push(b/d);break;
}
}
}
(*n)=st.top();
return 1;//成功标志
}
void main()
{
char str[64];
char exp[96];
long int ans;
while(1)
{
cout<<"请输入计算表达"<<endl;
cin>>str;
M2B(str,exp);
cout<<"后序表达式为:"<<exp<<endl;
compvalue(exp,&ans);
cout<<"计算结果为: "<<ans<<endl;
cout<<"___________________________________"<<endl;

}

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值