您的位置:首页 > 编程语言 > C语言/C++

C++ 计算器小程序

2011-01-20 16:08 127 查看
#include<iostream>
#include<string>
using namespace std;

class Str_Num{
public:
Str_Num(const string& s=""):str(s){}
int toNum(int begin,int& end)const;
private:
string str;
};

int Str_Num::toNum(int begin,int& end)const{
int length=str.size();
int result=0;
for(end=begin;end<length&&str[end]>='0'&&str[end]<='9';){
result=10*result+str[end]-'0';
++end;
}
return result;
}

class Calculater{
public:
Calculater(const string& str=""):eval(str),str_num(str){}
int value()const;
private:
int evalExpression(int& pos,bool& ok)const;
int evalTerm(int& pos,bool& ok)const;
int evalFactor(int& pos,bool& ok)const;
string eval;
Str_Num str_num;
};

int Calculater::value()const{
int begin=0;
bool ok=true;
int result=evalExpression(begin,ok);
if(!ok)
throw exception("the format of the calculate string is wrong!!/n");
return result;
}

int Calculater::evalExpression(int& pos,bool& ok)const{
int result=evalTerm(pos,ok);
int length=eval.size();
while(pos<length&&ok){
char op=eval[pos];
if(op=='+'||op=='-'){
++pos;
int term=evalTerm(pos,ok);

switch (op){
case '+':
result+=term;
break;
case '-':
result-=term;
break;
}
}
else return result;

}
return result;
}

int Calculater::evalTerm(int& pos,bool& ok)const{
int result=evalFactor(pos,ok);
int length=eval.size();
while(pos<length&&ok){
char op=eval[pos];
if(op=='*'||op=='/'){
++pos;
int term=evalFactor(pos,ok);
switch (op){
case '*':
result*=term;
break;
case '/':
if(term==0){
ok=false;
cout<<"number zero error!!"<<endl;
return 0;
}
result/=term;
break;
}
}
else return result;
}
return result;
}

int Calculater::evalFactor(int& pos,bool& ok)const{
bool negtive=false;
int result=0;
if(eval[pos]=='-'){
++pos;
negtive=true;
}
if(eval[pos]=='('){
++pos;
result=evalExpression(pos,ok);
if(eval[pos++]!=')'){
ok=false;
return 0;
}
}
else{
result=str_num.toNum(pos,pos);
}
if(negtive)
result*=-1;
return result;
}

int main(){
string calStr="-(89*(-100))*(90/45)+(67-7)";
Calculater calculater(calStr);
try{
cout<<calculater.value()<<endl;
}catch(exception e){
cout<<e.what()<<endl;
}
return 0;
}


稍微改进了一下,可以处理更多的错误,匹配更加严格了

#include<iostream>
#include<string.h>
using namespace std;

class Str_Num{
public:
Str_Num(const string &s):str(s),len(str.length()){}

int val(int &pos,bool &ok)const{
int val=0,t=pos;
ok=true;
for(;pos<len&&str[pos]>='0'&&str[pos]<='9';++pos)
val=val*10+str[pos]-'0';
if(pos==t)
ok=false;
return val;
}

private:
string str;
int len;
};

class Calculator{
public:
Calculator(const string& s):str_n(s),eval(s),len(eval.length()){}

int value()const{
int val=0,pos=0;
bool ok=true;
val=term1(pos,ok);
if(!ok||pos!=len)
throw exception("Format of calculate string is invalid,or divisor is zero!!");
return val;
}

private:
int term1(int &pos,bool &ok)const{
int a=term2(pos,ok),b=0;
while(pos<len&&ok){
if(eval[pos]=='+'||eval[pos]=='-'){
char op=eval[pos];
++pos;
b=term2(pos,ok);
if(op=='+')
a+=b;
else a-=b;
}
else return a;
}
return a;
}
int term2(int &pos,bool &ok)const{
int a=term3(pos,ok),b;
while(pos<len&&ok){
if(eval[pos]=='*'||eval[pos]=='/'){
char op=eval[pos];
++pos;
b=term3(pos,ok);
if(op=='*')
a*=b;
else{
if(b==0){
ok=false;
return a;
}
a/=b;
}
}
else return a;
}
return a;
}

int term3(int &pos,bool &ok)const{
bool negative=false;
int result;
if(eval[pos]=='-'){
negative=true;
++pos;
}
if(eval[pos]=='('){
++pos;
result=term1(pos,ok);
if(eval[pos]!=')'){
ok=false;
return result;
}
++pos;
}
else result=str_n.val(pos,ok);
if(negative)
result*=-1;
return result;

}
Str_Num str_n;
string eval;
int len;
};

int main(){
string test="2*((11-1)-12*(6/(6-3)))";
Calculator cal(test);
try{
cout<<cal.value()<<endl;
}catch(exception e){
cout<<e.what()<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: