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



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

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