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

DOS窗口的多项式计算器,看不懂给谢写一些注释!
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cstring>
using namespace std;

enum types { DELIMITER = 1, VARIABLE, NUMBER};

class parser {
char *exp_ptr; // points to the expression
char token[80]; // holds current token
char tok_type; // holds token's type

void eval_exp2(double &result);
void eval_exp3(double &result);
void eval_exp4(double &result);
void eval_exp5(double &result);
void eval_exp6(double &result);
void atom(double &result);
void get_token();
void serror(int error);
int isdelim(char c);
public:
parser();
double eval_exp(char *exp);
};

// parser constructor
parser::parser()
{
exp_ptr = NULL;
}

// Parser entry point.
double parser::eval_exp(char *exp)
{
double result;

exp_ptr = exp;

get_token();
if(!*token) {
serror(2); // no expression present
return 0.0;
}
eval_exp2(result);
if(*token) serror(0); // last token must be null
return result;
}

// Add or subtract two terms.
void parser::eval_exp2(double &result)
{
register char op;
double temp;

eval_exp3(result);
while((op = *token) == '+' || op == '-') {
get_token();
eval_exp3(temp);
switch(op) {
case '-':
result = result - temp;
break;
case '+':
result = result + temp;
break;
}
}
}

// Multiply or divide two factors.
void parser::eval_exp3(double &result)
{
register char op;
double temp;

eval_exp4(result);
while((op = *token) == '*' || op == '/' || op == '%') {
get_token();
eval_exp4(temp);
switch(op) {
case '*':
result = result * temp;
break;
case '/':
result = result / temp;
break;
case '%':
result = (int) result % (int) temp;
break;
}
}
}

// Process an exponent
void parser::eval_exp4(double &result)
{
double temp, ex;
register int t;

eval_exp5(result);
if(*token== '^') {
get_token();
eval_exp4(temp);
ex = result;
if(temp==0.0) {
result = 1.0;
return;
}
for(t=(int)temp-1; t>0; --t) result = result * (double)ex;
}
}

// Evaluate a unary + or -.
void parser::eval_exp5(double &result)
{
register char op;

op = 0;
if((tok_type == DELIMITER) && *token=='+' || *token == '-') {
op = *token;
get_token();
}
eval_exp6(result);
if(op=='-') result = -result;
}

// Process a parenthesized expression.
void parser::eval_exp6(double &result)
{
if((*token == '(')) {
get_token();
eval_exp2(result);
if(*token != ')')
serror(1);
get_token();
}
else atom(result);
}

// Get the value of a number.
void parser::atom(double &result)
{
switch(tok_type) {
case NUMBER:
result = atof(token);
get_token();
return;
default:
serror(0);
}
}

// Display a syntax error.
void parser::serror(int error)
{
static char *e[]= {
"Syntax Error",
"Unbalanced Parentheses",
"No expression Present"
};
cout << e[error] << endl;
}

// Obtain the next token.
void parser::get_token()
{
register char *temp;

tok_type = 0;
temp = token;
*temp = '\0';

if(!*exp_ptr) return; // at end of expression

while(isspace(*exp_ptr)) ++exp_ptr; // skip over white space

if(strchr("+-*/%^=()", *exp_ptr)){
tok_type = DELIMITER;
// advance to next char
*temp++ = *exp_ptr++;
}
else if(isalpha(*exp_ptr)) {
while(!isdelim(*exp_ptr)) *temp++ = *exp_ptr++;
tok_type = VARIABLE;
}
else if(isdigit(*exp_ptr)) {
while(!isdelim(*exp_ptr)) *temp++ = *exp_ptr++;
tok_type = NUMBER;
}

*temp = '\0';
}

// Return true if c is a delimiter.
int parser::isdelim(char c)
{
if(strchr(" +-/*%^=()", c) || c==9 || c=='\r' || c==0)
return 1;
return 0;
}
int main()
{
char expstr[80];

cout << "Enter a period to stop.\n";

parser ob; // instantiate a parser

for(;;) {
cout << "Enter expression: ";
cin.getline(expstr, 79);
if(*expstr=='.') break;
cout << "Answer is: " << ob.eval_exp(expstr) << "\n\n";
};

return 0;
}

各位看看这个计算器,有人能写出注释吗?

搜索更多相关主题的帖子: 计算器 注释 
2007-01-22 13:33
smartwind
Rank: 1
等 级:新手上路
威 望:1
帖 子:277
专家分:0
注 册:2006-11-13
收藏
得分:0 
已经有注释了,只不过是英文的

2007-01-23 13:21
快速回复:C++计算器程序请写个注释
数据加载中...
 
   



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

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