简单的整数计算器
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;
}
}
?
總結
- 上一篇: 几个经典算法的概念
- 下一篇: 把中缀表达式转化为后缀表达式