| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1034 人关注过本帖
标题:请看这个计算器程序
只看楼主 加入收藏
凡星
Rank: 1
等 级:新手上路
帖 子:61
专家分:0
注 册:2006-6-14
收藏
 问题点数:0 回复次数:4 
请看这个计算器程序

#include<iostream>
#include<stdlib.h>
#include<ctype.h>

using namespace std;

int add();
int mul();
int number();
void error();

static char exp[81]; //全局变量,垃圾!
static int pos; //又来一个,靠!


int main(){
int ans;
do
{
pos=0;

cout<<"\n请输入表达式"<<endl;
cin>>exp;

ans = add();
if(exp[pos]!='\0')
error();
if(ans!=0)
cout<<ans<<endl;
}while(ans!=0);
return 0;
}
int add()
{
int rtn=mul();
while(exp[pos]=='+'||exp[pos]=='-')
{
int op=exp[pos++];
int opr2=mul();
if(op=='+')
rtn+=opr2;
else
rtn-=opr2;
}
return rtn;
}
int mul()
{
int rtn=number();
while(exp[pos]=='*'||exp[pos]=='/')
{
int op=exp[pos++];
int opr2=number();
if(op=='*')
rtn*=opr2;
else
rtn/=opr2;
}
return rtn;
}
int number()
{
int rtn;
if(exp[pos]=='(')
{
pos++;
rtn=add();
if(exp[pos++]!=')')
error();
return rtn;
}
if(!isdigit(exp[pos]))
error();
rtn=atoi(exp+pos);
while(isdigit(exp[pos]))
pos++;
return rtn;
}
void error()
{
cout<<'\r';
while(pos++)
cout<<' ';
cout<<"error"<<endl<<'\a';
exit(-1);
}

这是我在网上下的一个程序,是个计算器程序,但是那笔没有写注释,真他妈的sb,我看不懂,请问哪位大哥能帮我解释一下呢!

搜索更多相关主题的帖子: 计算器 ans int exp 
2006-06-14 15:41
lxlgod
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-8-9
收藏
得分:0 

#include<iostream>
#include<stdlib.h>
#include<ctype.h>

using namespace std;

int add();
int mul();
int number();
void error();

static char exp[81]; //全局变量,垃圾!
static int pos; //又来一个,靠!


int main(){
int ans;
do
{
pos=0;

cout<<"\n请输入表达式"<<endl;
cin>>exp;

ans = add(); 将add赋植给ans;
if(exp[pos]!='\0')
error();
if(ans!=0)
cout<<ans<<endl; 判断add的值是否为0?不是!输出
}while(ans!=0);
return 0;
}
int add() //加法 减法
{
int rtn=mul(); //mul()返回值为rtn故等效于调用number() 函数处理* / 前面的数给rtn;

while(exp[pos]=='+'||exp[pos]=='-')
{
int op=exp[pos++]; //将+或-前面的字符赋给op
int opr2=mul(); //
if(op=='+')
rtn+=opr2;
else
rtn-=opr2;
}
return rtn;
}
int mul()
{
int rtn=number(); ////调用number() 函数处理* / 前面的数
while(exp[pos]=='*'||exp[pos]=='/')
{
int op=exp[pos++];
int opr2=number(); //再次调用number() 函数处理* / 后面的数

if(op=='*')
rtn*=opr2;
else
rtn/=opr2;
}
return rtn;
}
int number()
{
int rtn;
if(exp[pos]=='(')
{
pos++;
rtn=add();
if(exp[pos++]!=')') //"()"优先
error();
return rtn;
}
if(!isdigit(exp[pos]))
error();
rtn=atoi(exp+pos); //将char 转换成 int
while(isdigit(exp[pos])) //int isdigit(int ch); exp[pos] 是否是数是
pos++; //返回1,不是返回0, 包含文件 ctype.h
return rtn;
}
void error()
{
cout<<'\r';
while(pos++)
cout<<' ';
cout<<"error"<<endl<<'\a';
exit(-1);
}
我没运行但估计还是能运行就是不太理想 有点垃圾 不能处理小数

2006-08-09 18:17
funnyt
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-8-15
收藏
得分:0 

计算器程序到底是计算什么的

例如写加法计算器是不是应该不能出现

int++

或者+= + 等等

那已经是用到了计算机本来的资源了

也就没什么意思了

2006-08-16 23:30
lundon
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2006-5-7
收藏
得分:0 
这位仁兄说话好没道德,你不会认真看看吗?你输入表达式后再加个空格就有结果了!
做人要……

还想好好学习
2006-08-19 20:21
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 

写的不错
后缀表达式输入
在 cfree 下运行通过
可以对 () 处理


~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2006-08-20 00:55
快速回复:请看这个计算器程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011228 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved