表达式求值
2015-06-09 14:26
281 查看
实验内容:
1、定义顺序栈;
2、用顺序栈实现表达式求值;
实验基本要求:
掌握栈的特性和栈结构的入栈和出栈等操作,掌握栈在表达式求值中的应用
#include<iostream>
#include<stdio.h>
using namespace std;
#define MaxSize 50
#define MaxOp 7
struct{
char ch;
int pri;
}lpri[]={{'=',0},{'(',1},{'*',5},{'/',5},{'+',3},{'-',3},{')',6}},
rpri[]={{'=',0},{'(',6},{'*',4},{'/',4},{'+',2},{'-',2},{')',1}};
int leftpri(char op){
int i;
for(i=0;i<MaxOp;i++)
if(lpri[i].ch==op)return lpri[i].pri;
}
int rightpri(char op){
int i;
for(i=0;i<MaxOp;i++)
if(rpri[i].ch==op)
return rpri[i].pri;
}
bool InOp(char ch){
if(ch=='('||ch==')'||ch=='+'||ch=='-'||ch=='*'||ch=='/')return true;
else return false;
}
int Precede(char op1,char op2){
if(leftpri(op1)==rightpri(op2))return 0;
else if(leftpri(op1)<rightpri(op2))return -1;
else return 1;
}
void trans(char *exp,char postexp[]){
struct{
char data[MaxSize];
int top;
}op;
int i=0;
op.top=-1;
op.top++;
op.data[op.top]='=';
while(*exp!='\0'){
if(!InOp(*exp)){
while(*exp>='0'&&*exp<='9'){
postexp[i++]=*exp;
exp++;
}
postexp[i++]='#';
}
else{
switch(Precede(op.data[op.top],*exp)){
case -1:
op.top++;op.data[op.top]=*exp;
exp++;
break;
case 0:
op.top--;
exp++;
break;
case 1:
postexp[i++]=op.data[op.top];
op.top--;
break;
}
}
}
while(op.data[op.top]!='='){
postexp[i++]=op.data[op.top];
op.top--;
}
postexp[i]='\0';
}
float compvalue(char *postexp){
struct{
float data[MaxSize];
int top;
}st;
float d,a,b,c;
st.top=-1;
while(*postexp!='\0'){
switch(*postexp){
case '+':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
c=a+b;
st.top++;
st.data[st.top]=c;
break;
case '-':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
c=b-a;
st.top++;
st.data[st.top]=c;
break;
case '*':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
c=a*b;
st.top++;
st.data[st.top]=c;
break;
case '/':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
if(a!=0){
c=b/a;
st.top++;
st.data[st.top]=c;
}
else{
printf("\n\t除零错误!\n");
exit(0);
}
break;
default:
d=0;
while(*postexp>='0'&&*postexp<='9'){
d=10*d+*postexp-'0';
postexp++;
}
st.top++;
st.data[st.top]=d;
break;
}
postexp++;
}
return(st.data[st.top]);
}
int main(){
char exp[]="(56*20)/(4-2)";
char postexp[MaxSize];
trans(exp,postexp);
printf("中缀表达式:%s\n",exp);
printf("后缀表达式:%s\n",postexp);
printf("表达式的值:%g\n",compvalue(postexp));
return 0;
}
1、定义顺序栈;
2、用顺序栈实现表达式求值;
实验基本要求:
掌握栈的特性和栈结构的入栈和出栈等操作,掌握栈在表达式求值中的应用
#include<iostream>
#include<stdio.h>
using namespace std;
#define MaxSize 50
#define MaxOp 7
struct{
char ch;
int pri;
}lpri[]={{'=',0},{'(',1},{'*',5},{'/',5},{'+',3},{'-',3},{')',6}},
rpri[]={{'=',0},{'(',6},{'*',4},{'/',4},{'+',2},{'-',2},{')',1}};
int leftpri(char op){
int i;
for(i=0;i<MaxOp;i++)
if(lpri[i].ch==op)return lpri[i].pri;
}
int rightpri(char op){
int i;
for(i=0;i<MaxOp;i++)
if(rpri[i].ch==op)
return rpri[i].pri;
}
bool InOp(char ch){
if(ch=='('||ch==')'||ch=='+'||ch=='-'||ch=='*'||ch=='/')return true;
else return false;
}
int Precede(char op1,char op2){
if(leftpri(op1)==rightpri(op2))return 0;
else if(leftpri(op1)<rightpri(op2))return -1;
else return 1;
}
void trans(char *exp,char postexp[]){
struct{
char data[MaxSize];
int top;
}op;
int i=0;
op.top=-1;
op.top++;
op.data[op.top]='=';
while(*exp!='\0'){
if(!InOp(*exp)){
while(*exp>='0'&&*exp<='9'){
postexp[i++]=*exp;
exp++;
}
postexp[i++]='#';
}
else{
switch(Precede(op.data[op.top],*exp)){
case -1:
op.top++;op.data[op.top]=*exp;
exp++;
break;
case 0:
op.top--;
exp++;
break;
case 1:
postexp[i++]=op.data[op.top];
op.top--;
break;
}
}
}
while(op.data[op.top]!='='){
postexp[i++]=op.data[op.top];
op.top--;
}
postexp[i]='\0';
}
float compvalue(char *postexp){
struct{
float data[MaxSize];
int top;
}st;
float d,a,b,c;
st.top=-1;
while(*postexp!='\0'){
switch(*postexp){
case '+':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
c=a+b;
st.top++;
st.data[st.top]=c;
break;
case '-':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
c=b-a;
st.top++;
st.data[st.top]=c;
break;
case '*':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
c=a*b;
st.top++;
st.data[st.top]=c;
break;
case '/':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
if(a!=0){
c=b/a;
st.top++;
st.data[st.top]=c;
}
else{
printf("\n\t除零错误!\n");
exit(0);
}
break;
default:
d=0;
while(*postexp>='0'&&*postexp<='9'){
d=10*d+*postexp-'0';
postexp++;
}
st.top++;
st.data[st.top]=d;
break;
}
postexp++;
}
return(st.data[st.top]);
}
int main(){
char exp[]="(56*20)/(4-2)";
char postexp[MaxSize];
trans(exp,postexp);
printf("中缀表达式:%s\n",exp);
printf("后缀表达式:%s\n",postexp);
printf("表达式的值:%g\n",compvalue(postexp));
return 0;
}
相关文章推荐
- linux 查看文件命令
- 初学 Java Web 开发,请远离各种框架,从 Servlet 开发
- 程序员的英雄泪-----第一章:小龙的前世今生。
- Android打包jar给第三方引用
- JAVA 语言如何进行异常处理,关键字:throws,throw,try,catch,finally 分别代表什么意义?在 try 块中可以抛出异常吗?
- 在VB.NET中,将数据库里的数据导出到Excel中
- C++数据结构 链表的基本操作
- webview和 内置浏览器的调用
- HTML5前世今生
- Mybatis中模糊查询的各种写法
- 创建尽可能小的 Docker 容器
- Oracle优化配置指南
- 黑马程序员——Java基础---File、Properties、序列流
- C++中this指针的用法详解
- Java合并两个有序数组
- android ListView之——ArrayAdapter使用
- Looksery Cup 2015 B. Looksery Party 暴力
- 【原创】kafka client源代码分析
- PHP底层的运行机制与原理
- CSRF攻击与防御