登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

秒大刀 博客

好好学习 天天向上

 
 
 

日志

 
 
 
 

简单的整数计算器  

2006-02-23 21:39:07|  分类: C/C++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

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

作者:秒大刀

完成日期: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;

}

}

 

  评论这张
 
阅读(1175)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018