#include "Cacluate.h"
void Calculate::setString(string str)
{
clear();
infixString=str;
existString=true;
}
bool Calculate::checkEqual()
{
string::size_type pos=infixString.size();
if(infixString[pos-1]!='=')//检查等于号
{
return false;
}
char temp_pre=infixString[pos-2];
if((temp_pre<=43&&temp_pre>=42)||temp_pre==45||temp_pre==47)//检查倒数第二个符号是否为运算符
{
return false;
}
return true;
}
bool Calculate::run(double& answer)
{
if(!existString)
{
return false;
}
if(!analysis())
{
return false;
}
if(!convertInfixToPostfix())
{
return false;
}
if(!calculate(answer))
{
return false;
}
return true;
}
void Calculate::clear()
{
existString=false;
infixString.clear();
infix.clear();
postfix.clear();
}
void Calculate::getPostfix(string& postfixStr)
{
for(vector<string>::iterator itr=postfix.begin();itr!=postfix.end();itr++)
{
postfixStr+=(*itr);
}
}
bool Calculate::analysis()
{
string matchN("0123456789.");
char temp_pre;
string::size_type pre=0;
string::size_type pos=0;
string::size_type size=infixString.size();
while(pre<size)
{
temp_pre=infixString[pre];
if((temp_pre<=57&&temp_pre>=48)||temp_pre==46)
{
string numStr;
pos=infixString.find_first_not_of(matchN,pre);
numStr=infixString.substr(pre,pos-pre);
pre=pos;
infix.push_back(numStr);
}
else if((temp_pre<=43&&temp_pre>=40)||temp_pre==45||temp_pre==47)
{
string symbolStr=infixString.substr(pre,1);
pre++;
infix.push_back(symbolStr);
}
else
{
if(temp_pre!=61)
{
return false;
}
pre++;
}
}
return true;
}
bool Calculate::checkBrackets()
{
string l_brackrt="(";
string r_bracket=")";
string ::size_type size=infix.size();
string::size_type pos(0);
while(pos!=size)
{
if(infix[pos]==l_brackrt)
{
cac_stack.push(infix[pos]);
}
else if(infix[pos]==r_bracket)
{
if(cac_stack.empty())
{
return false;
}
if(cac_stack.top()!=l_brackrt)
{
return false;
}
cac_stack.pop();
}
}
if(!cac_stack.empty())
{
return false;
}
return true;
}
bool Calculate::convertInfixToPostfix()
{
string num("0123456789.");
string symbol("+-*/");
string ::size_type size=infix.size();
string::size_type pos(0);
while(pos!=size)
{
if(infix[pos]=="(")
{
cac_stack.push(infix[pos]);
}
if((infix[pos].find_first_of(symbol))!=string::npos)
{
if(cac_stack.empty())
{
cac_stack.push(infix[pos]);
}
else
{
switch(compareSymbol(infix[pos],cac_stack.top()))
{
case gt:
cac_stack.push(infix[pos]);
break;
case le:
case eq:
postfix.push_back(cac_stack.top());
cac_stack.pop();
while(!cac_stack.empty())
{
switch(compareSymbol(infix[pos],cac_stack.top()))
{
case le:
case eq:
postfix.push_back(cac_stack.top());
cac_stack.pop();
continue;
case er:
break;
}
break;
}
cac_stack.push(infix[pos]);
break;
case er:
cac_stack.push(infix[pos]);
break;
}
}
}
if(infix[pos]==")")
{
while(cac_stack.top()!="(")
{
postfix.push_back(cac_stack.top());
cac_stack.pop();
}
cac_stack.pop();
}
if((infix[pos].find_first_of(num))!=string::npos)
{
postfix.push_back(infix[pos]);
}
pos++;
}
while(!cac_stack.empty())
{
postfix.push_back(cac_stack.top());
cac_stack.pop();
}
return true;
}
bool Calculate::calculate(double& answer)
{
while(!cac_stack.empty())
{
return false;
}
string num("0123456789.");
string symbol("+-*/");
string ::size_type size=postfix.size();
string::size_type pos(0);
while(pos!=size)
{
if((postfix[pos].find_first_of(num))!=string::npos)
{
cac_stack.push(postfix[pos]);
}
if((postfix[pos].find_first_of(symbol))!=string::npos)
{
double retNum(0);
double rightNum=atof(cac_stack.top().c_str());
cac_stack.pop();
double leftNum=atof(cac_stack.top().c_str());
cac_stack.pop();
if(postfix[pos]=="+")
{
retNum=leftNum+rightNum;
}
else if(postfix[pos]=="-")
{
retNum=leftNum-rightNum;
}
else if(postfix[pos]=="*")
{
retNum=leftNum*rightNum;
}
else if(postfix[pos]=="/")
{
retNum=leftNum/rightNum;
}
char tmp[15];
sprintf_s(tmp,"%5.5f",retNum);
string tmpRet(tmp);
cac_stack.push(tmpRet);
}
pos++;
}
answer=atof(cac_stack.top().c_str());
}
compret Calculate::compareSymbol(string&left,string &right)
{
if(left=="+"||left=="-")
{
if(right=="*"||right=="/")
{
return le;
}
else if(right=="+"||right=="-")
{
return eq;
}
}
if(left=="*"||left=="/")
{
if(right=="+"||right=="-")
{
return gt;
}
else if(right=="*"||right=="/")
{
return eq;
}
}
return er;
}
- 1
- 2
前往页