简单的整数计算器
http://dearymz.blog.163.com/blog/static/2056574200612393970/
//簡(jiǎn)單的整數(shù)計(jì)算器----堆棧的應(yīng)用
作者:秒大刀
完成日期:2004-10-07
這是一個(gè)將中序表達(dá)式變成后序表達(dá)式,并按照后序表達(dá)式進(jìn)行整數(shù)四則運(yùn)算是的程序
mystak.h ?????? 計(jì)算器中的一個(gè)特殊堆棧,其中push()函數(shù)可以保證中序表達(dá)式->后序表達(dá)式過程中運(yùn)算優(yōu)先級(jí)別的合理性
mystack.cpp???? 為以上類的實(shí)現(xiàn)文件
main.cpp ?????? 為測(cè)試驅(qū)動(dòng)文件
void M2B(char from[],char to[])??????????? 函數(shù)將中序計(jì)算成后序
int compvalue(char exp[],long int *n)????? 計(jì)算后序表達(dá)式的值
void main()??????????????????????????????? 為驅(qū)動(dòng)程序
?
//mystak.h
//計(jì)數(shù)器中的一個(gè)特殊堆棧,其中push()函數(shù)可以保證中序表達(dá)式->后序表達(dá)式過程中運(yùn)算優(yōu)先級(jí)別的合理性
#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)//關(guān)鍵函數(shù)
{
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<<"經(jīng)過了標(biāo)記"<<__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<<"經(jīng)過了標(biāo)記"<<__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 測(cè)試函數(shù)
#define DEBUG
#include<iostream>
#include"mystack.h"
//#include"run.cpp"
void M2B(char from[],char to[])
{
mystack stack;
char ch;//臨時(shí)緩沖區(qū)
int f(0),t(0);//分別為數(shù)組from和to的下標(biāo)
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<<"是數(shù)字"<<endl;
#endif
}
else
{
t+=stack.push(ch,&(to[t]));//?????
#ifdef DEBUG
cout<<"是符號(hào)"<<endl;
#endif
}
}
while(stack.empty()==false)
{
to[t++]=stack.top();stack.pop();
}
to[t]='\0';
return;
}
/
int compvalue(char exp[],long int *n)//計(jì)算后序表達(dá)式的值
{
stack<int> st;
long int d,b;
char ch;
int t(0);//t作為exp的下標(biāo)
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;//成功標(biāo)志
}
void main()
{
char str[64];
char exp[96];
long int ans;
while(1)
{
cout<<"請(qǐng)輸入計(jì)算表達(dá)"<<endl;
cin>>str;
M2B(str,exp);
cout<<"后序表達(dá)式為:"<<exp<<endl;
compvalue(exp,&ans);
cout<<"計(jì)算結(jié)果為: "<<ans<<endl;
cout<<"___________________________________"<<endl;
}
}
?
總結(jié)
- 上一篇: 几个经典算法的概念
- 下一篇: 把中缀表达式转化为后缀表达式