![]() |
#2
xg56992011-08-22 15:20
![]() #include<iostream> using std::cout; const int MAXSIZE=20; template<typename type> class STACK { private: type data[MAXSIZE]; int index; public: STACK(){index=0;} type pop(){--index;return data[index];} void push(type t){data[index]=t;++index;} bool empty(){return index==0;} }; class calc { private: char* expression;//指向要求解的中缀表达式 enum token{OPAREN,ADD,SUB,MULTI,DIV,CPAREN,VALUE,EOL}; void BinaryOp(token op,STACK<double>& dataStack); token getOp(double& value); public: calc(char* e){expression=e;} double result(); }; void calc::BinaryOp(calc::token op, STACK<double> &dataStack) { double num1,num2,result; num2=dataStack.pop(); num1=dataStack.pop(); switch(op) { case ADD:result=num1+num2;break; case SUB:result=num1-num2;break; case MULTI:result=num1*num2;break; case DIV:result=num1/num2;break;//忽略除数为0的情况 } dataStack.push(result); } calc::token calc::getOp(double &value) { while(*expression) { if(*expression<='9'&&*expression>='0') { value=0; while(*expression<='9'&&*expression>='0') { value=value*10+*expression-'0'; ++expression; } return VALUE; } switch(*expression) { case '(':++expression;return OPAREN; case ')':++expression;return CPAREN; case '+':++expression;return ADD; case '-':++expression;return SUB; case '*':++expression;return MULTI; case '/':++expression;return DIV; } } return EOL; } double calc::result() { STACK<token> opStack; STACK<double> dataStack; double value; token TOKEN,topOp; while(true) { TOKEN=getOp(value); if(TOKEN==EOL) break; switch(TOKEN) { case VALUE:dataStack.push(value);break; case OPAREN:opStack.push(OPAREN);break; case CPAREN:while((topOp=opStack.pop())!=OPAREN) BinaryOp(topOp,dataStack); break; case MULTI:case DIV:while((topOp=opStack.pop())==MULTI||topOp==DIV) BinaryOp(topOp,dataStack); opStack.push(topOp); opStack.push(TOKEN); break; case ADD:case SUB:while(!opStack.empty()&&(topOp=opStack.pop())!=OPAREN) BinaryOp(topOp,dataStack); opStack.push(topOp); opStack.push(TOKEN); break; } break;//退出while循环.switch语句中的break只是退出switch循环.少了我你无限循环下去吧不错才怪 } while(!opStack.empty()) BinaryOp(topOp,dataStack); return dataStack.pop(); } int main() { calc e("3+4*(5-3)"); cout<<e.result()<<'\n'; return 0; } [ 本帖最后由 xg5699 于 2011-8-22 15:22 编辑 ] |

#include<iostream>
using std::cout;
const int MAXSIZE=20;
template<typename type>
class STACK
{
private:
type data[MAXSIZE];
int index;
public:
STACK(){index=0;}
type pop(){--index;return data[index];}
void push(type t){data[index]=t;++index;}
bool empty(){return index==0;}
};
class calc
{
private:
char* expression;//指向要求解的中缀表达式
enum token{OPAREN,ADD,SUB,MULTI,DIV,CPAREN,VALUE,EOL};
void BinaryOp(token op,STACK<double>& dataStack);
token getOp(double& value);
public:
calc(char* e){expression=e;}
double result();
};
void calc::BinaryOp(calc::token op, STACK<double> &dataStack)
{
double num1,num2,result;
num2=dataStack.pop();
num1=dataStack.pop();
switch(op)
{
case ADD:result=num1+num2;break;
case SUB:result=num1-num2;break;
case MULTI:result=num1*num2;break;
case DIV:result=num1/num2;break;//忽略除数为0的情况
}
dataStack.push(result);
}
calc::token calc::getOp(double &value)
{
while(*expression)
{
if(*expression<='9'&&*expression>='0')
{
value=0;
while(*expression<='9'&&*expression>='0')
{
value=value*10+*expression-'0';
++expression;
}
return VALUE;
}
switch(*expression)
{
case '(':++expression;return OPAREN;
case ')':++expression;return CPAREN;
case '+':++expression;return ADD;
case '-':++expression;return SUB;
case '*':++expression;return MULTI;
case '/':++expression;return DIV;
}
}
return EOL;
}
double calc::result()
{
STACK<token> opStack;
STACK<double> dataStack;
double value;
token TOKEN,topOp;
while(true)
{
TOKEN=getOp(value);
if(TOKEN==EOL)
break;
switch(TOKEN)
{
case VALUE:dataStack.push(value);break;
case OPAREN:opStack.push(OPAREN);break;
case CPAREN:while((topOp=opStack.pop())!=OPAREN)
BinaryOp(topOp,dataStack);
break;
case MULTI:case DIV:while((topOp=opStack.pop())==MULTI||topOp==DIV)
BinaryOp(topOp,dataStack);
opStack.push(topOp);
opStack.push(TOKEN);
break;
case ADD:case SUB:while(!opStack.empty()&&(topOp=opStack.pop())!=OPAREN)
BinaryOp(topOp,dataStack);
opStack.push(topOp);//为何运行时此处会发生内存冲突? .........................................................
opStack.push(TOKEN);
}
}
while(!opStack.empty())
BinaryOp(topOp,dataStack);
return dataStack.pop();
}
int main()
{
calc e("3+4*(5-3)");
cout<<e.result()<<'\n';
return 0;
}
using std::cout;
const int MAXSIZE=20;
template<typename type>
class STACK
{
private:
type data[MAXSIZE];
int index;
public:
STACK(){index=0;}
type pop(){--index;return data[index];}
void push(type t){data[index]=t;++index;}
bool empty(){return index==0;}
};
class calc
{
private:
char* expression;//指向要求解的中缀表达式
enum token{OPAREN,ADD,SUB,MULTI,DIV,CPAREN,VALUE,EOL};
void BinaryOp(token op,STACK<double>& dataStack);
token getOp(double& value);
public:
calc(char* e){expression=e;}
double result();
};
void calc::BinaryOp(calc::token op, STACK<double> &dataStack)
{
double num1,num2,result;
num2=dataStack.pop();
num1=dataStack.pop();
switch(op)
{
case ADD:result=num1+num2;break;
case SUB:result=num1-num2;break;
case MULTI:result=num1*num2;break;
case DIV:result=num1/num2;break;//忽略除数为0的情况
}
dataStack.push(result);
}
calc::token calc::getOp(double &value)
{
while(*expression)
{
if(*expression<='9'&&*expression>='0')
{
value=0;
while(*expression<='9'&&*expression>='0')
{
value=value*10+*expression-'0';
++expression;
}
return VALUE;
}
switch(*expression)
{
case '(':++expression;return OPAREN;
case ')':++expression;return CPAREN;
case '+':++expression;return ADD;
case '-':++expression;return SUB;
case '*':++expression;return MULTI;
case '/':++expression;return DIV;
}
}
return EOL;
}
double calc::result()
{
STACK<token> opStack;
STACK<double> dataStack;
double value;
token TOKEN,topOp;
while(true)
{
TOKEN=getOp(value);
if(TOKEN==EOL)
break;
switch(TOKEN)
{
case VALUE:dataStack.push(value);break;
case OPAREN:opStack.push(OPAREN);break;
case CPAREN:while((topOp=opStack.pop())!=OPAREN)
BinaryOp(topOp,dataStack);
break;
case MULTI:case DIV:while((topOp=opStack.pop())==MULTI||topOp==DIV)
BinaryOp(topOp,dataStack);
opStack.push(topOp);
opStack.push(TOKEN);
break;
case ADD:case SUB:while(!opStack.empty()&&(topOp=opStack.pop())!=OPAREN)
BinaryOp(topOp,dataStack);
opStack.push(topOp);//为何运行时此处会发生内存冲突? .........................................................
opStack.push(TOKEN);
}
}
while(!opStack.empty())
BinaryOp(topOp,dataStack);
return dataStack.pop();
}
int main()
{
calc e("3+4*(5-3)");
cout<<e.result()<<'\n';
return 0;
}
[ 本帖最后由 紫凤双飞 于 2011-8-22 14:57 编辑 ]