您的位置:首页 > 其它

栈的运用! 表达式求值。

2015-05-20 20:15 295 查看
初级版本
http://acm.hdu.edu.cn/showproblem.php?pid=1237
拿杭电的这个初级版本练练手!

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<queue>
#include<stack>
using namespace std;
#define maxn 17

stack <char> opS;//符号栈
stack <double> numS;//数字栈
char str[300];
int pos[5] = {'+','-', '*', '/', '#' };
int cmps[5][5] = { {1,1,2,2,1},
{1,1,2,2,1},
{1,1,1,1,1},
{1,1,1,1,1},
{2,2,2,2,3} };

char* getNum(char *p,double &num)
{
num = 0;
while(*p != ' ' && *p)
{
num = num*10 + *p - '0';
p ++;
}

if(*p == ' ')
p ++;

return p;
}
int getOp(char ch)
{
for(int i=0; i<5; i++)
{
if(ch == pos[i])
return i;
}
}

int cmp(char op2,char op)
{
int a, b;
a = getOp(op2);
b = getOp(op);

return cmps[a][b];
}

double cal(double a, double b,char op)
{
switch(op)
{
case '+':return a+b;
case '-':return b-a;
case '*':return a*b;
case '/':return b/a;
}

}

double solve()
{
double num1, num2;
char op, *p = str, op2;

opS.push('#');

while(*p)
{
p = getNum(p,num1);

if(*p)
{
op = *p;
p += 2;
}
else
op = '#';

numS.push(num1);

while(1)
{
op2 = opS.top();

if(cmp(op2,op) == 2)///如果栈内运算符的优先级 比栈外的优先级要低 操作符都入栈
{
opS.push(op);
break;
}
else if(cmp(op2, op) == 3)
{
return numS.top();
}
else
{
num1 = numS.top();
numS.pop();
num2 = numS.top();
numS.pop();
opS.pop();

num1 = cal(num1, num2, op2);
numS.push(num1);
}
}

}

}

int main()
{
while(gets(str), strcmp(str, "0") )
{
while(opS.size() )
opS.pop();
while(numS.size() )
numS.pop();

double ans = solve();

printf("%.2lf\n", ans);
}
return 0;
}


下面是南阳理工的一个题目属于加强版,第一个写出来后第二个写起来就轻松多了

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<queue>
#include<stack>
using namespace std;
#define maxn 17

char pos[7] = { '+', '-', '*', '/', '(', ')', '=' };
///运算符的优先级比较 cmp[栈中符号][栈外符号比较] 1 >, 2<, 3 =, 4 不可比
int cmp[7][7] = { {1,1,2,2,2,1,1},
{1,1,2,2,2,1,1},
{1,1,1,1,2,1,1},
{1,1,1,1,2,1,1},
{2,2,2,2,2,3,4},
{4,4,4,4,4,4,4},
{2,2,2,2,2,4,3} };

double getNum(char* &p)
{
double num;
sscanf(p, "%lf", &num);

while( (*p >='0' && *p <= '9') || *p == '.' )
p ++;

return num;
}

char getOp(char* &p)
{
char ch;
ch = *p;
p ++;
return ch;
}

double cal(double a, double b,char op)
{
switch(op)
{
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
}
}

int cmps(char op1,char op2)
{
int a, b;

for(int i=0; i<7; i++)
{
if(pos[i] == op1)
a = i;
if(pos[i] == op2)
b = i;
}
return cmp[a][b];
}

double solve(char *p)
{
stack <char> opS;///符号栈
stack <double> numS;///数字栈

double num1, num2;
char op1, op2;

opS.push('=');
while(1)
{
if(*p >= '0' && *p <= '9')
numS.push( getNum(p) );

op2 = getOp(p);

while(1)
{
op1 = opS.top();

if( cmps(op1,op2) == 2)///栈中元素的优先级小,将栈外符号入栈
{
opS.push(op2);
break;
}

else if( cmps(op1,op2) == 1)///栈中元素的优先级大,将栈中符号拿出来运算
{
opS.pop();

num1 = numS.top();
numS.pop();

num2 = numS.top();
numS.pop();

numS.push( cal(num2, num1, op1) );
}

else if( cmps(op1, op2) == 3 )
{
opS.pop();
if(op1 == '(' )
break;
if(op1 == '=')
return numS.top();

}
}
}
}

int main()
{
int T;
char str[1350];
scanf("%d ", &T);

while(T--)
{
scanf("%s", str);

double ans = solve(str);

printf("%.2lf\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: