简单表达式求值
2005-04-18 21:29
369 查看
#include "stack.h" #include "stdio.h" #include "conio.h" #include "stack.h" #include "stdio.h" #include "conio.h" main() { char c,theta,array[20]; int a,b,t,i=0,j=0; SqStack OPTR; SqStack2 OPND; clrscr(); main() { char c,theta,array[20]; int a,b,t,i=0,j=0; SqStack OPTR; SqStack2 OPND; clrscr(); InitStack(&OPTR); Push(&OPTR,'#'); InitStack2(&OPND); gets(array); while(array[i]) i++; InitStack(&OPTR); Push(&OPTR,'#'); InitStack2(&OPND); gets(array); while(array[i]) i++; if(array[i-1]!='=') printf("You must put a character make this procedure to the end!!!"); else{ i=0; while(array[i]!='='||GetTop(OPTR)!='#'){ while(In(array[i])){i++;j++;} if(j){ t=atoi(&array[i-j]); Push2(&OPND,t); j=0; } if(!In(array[i])){ switch(Precede(GetTop(OPTR),array[i])){ case '<': Push(&OPTR,array[i]);i++; break; case '=': Pop(&OPTR,&c);i++; break; case '>': Pop(&OPTR,&theta); Pop2(&OPND,&a);Pop2(&OPND,&b); Push2(&OPND,Operate(a,theta,b)); break; default:printf("Warning:Data Error!"); } /*switch*/ } /*if*/ } /*swicth*/ } /*else*/ printf("%d",GetTop2(OPND)); sleep(2); } /*main*/ if(array[i-1]!='=') printf("You must put a character make this procedure to the end!!!"); else{ i=0; while(array[i]!='='||GetTop(OPTR)!='#'){ while(In(array[i])){i++;j++;} if(j){ t=atoi(&array[i-j]); Push2(&OPND,t); j=0; } if(!In(array[i])){ switch(Precede(GetTop(OPTR),array[i])){ case '<': Push(&OPTR,array[i]);i++; break; case '=': Pop(&OPTR,&c);i++; break; case '>': Pop(&OPTR,&theta); Pop2(&OPND,&a);Pop2(&OPND,&b); Push2(&OPND,Operate(a,theta,b)); break; default:printf("Warning:Data Error!"); } /*switch*/ } /*if*/ } /*swicth*/ } /*else*/ printf("%d",GetTop2(OPND)); sleep(2); } /*main*/ Precede(char a,char b){ if(((a=='(')&&(b==')')) ||((a=='#')&&(b=='='))) return '='; else if(((a=='('||a=='#')&&(b=='+'||b=='-'||b=='*'||b=='/'||b=='(')) ||((a=='+'||a=='-')&&(b=='*'||b=='/'||b=='(')) ||((a=='*'||a=='/')&&b=='(')) return '<'; else if(((a=='+'||a=='-'||a=='*'||a=='/'||a==')')&&(b=='+'||b=='-'||b==')'||b=='='))||((a=='*'||a=='/')&&(b=='*'||b=='/'))) return '>' ; else return '@'; Precede(char a,char b){ if(((a=='(')&&(b==')')) ||((a=='#')&&(b=='='))) return '='; else if(((a=='('||a=='#')&&(b=='+'||b=='-'||b=='*'||b=='/'||b=='(')) ||((a=='+'||a=='-')&&(b=='*'||b=='/'||b=='(')) ||((a=='*'||a=='/')&&b=='(')) return '<'; else if(((a=='+'||a=='-'||a=='*'||a=='/'||a==')')&&(b=='+'||b=='-'||b==')'||b=='='))||((a=='*'||a=='/')&&(b=='*'||b=='/'))) return '>' ; else return '@'; } } int Operate(int a,char theta,int b){ int t; switch(theta){ case '+': t=(a+b);break; case '-': t=(b-a);break; case '*': t=(a*b);break; case '/': t=(b/a);break; }; return t; } int In(char c){ if(c>='0'&&c<='9') return 1; else return 0; } int Operate(int a,char theta,int b){ int t; switch(theta){ case '+': t=(a+b);break; case '-': t=(b-a);break; case '*': t=(a*b);break; case '/': t=(b/a);break; }; return t; } int In(char c){ if(c>='0'&&c<='9') return 1; else return 0; } |
相关文章推荐
- hdu1237简单表达式求值(我多判断了())
- 简单表达式求值(用栈实现)
- 百度的科学计算器(简单) Python 库函数实现表达式求值
- NYOJ 305 表达式求值 (简单栈应用)
- 简单表达式求值
- 利用栈实现简单算术表达式求值
- Openjudge-计算概论(A)-简单算术表达式求值
- 编程练习:简单表达式求值
- 包含简单科学运算的表达式求值
- Openjudge-NOI题库-简单算术表达式求值
- [计蒜客 15504 百度的科学计算器(简单)]表达式求值
- OpenJudge计算概论-简单算术表达式求值
- 简单算术表达式求值
- 问题 H: 简单表达式求值
- 简单算术表达式求值
- 简单算术表达式求值
- VB.NET编程技巧与实例集粹 Module5_输入简单表达式并求值
- 栈的应用_简单表达式求值
- 二叉树显示(图形界面,控制台字符),简单表达式求值,pyqt,swig初试....
- 实现简单的表达式求值