发布时间:2019-07-31 11:20:30
#include <iostream>
#include <cstdlib>
using namespace std;
#define MAX_OP 5
#define operator(c) ((c=='+')||(c=='-')||(c=='*')||(c=='/'))?1:0
#define operands(c) ((c)>='a' && (c)<='z')?1:0
#define MAX_ITEM 100
typedef struct tagSTACK{
int item[MAX_ITEM];
int top;
}STACK;
STACK S;
char op[MAX_OP]={'(','+','-','*','/'};
char prio[MAX_OP]={0,1,1,2,2};
int op_value[26]={10,30,6,9,8,11,12,13,7,8,22,56,77,76,55,56,43,40,13,19,18,16,46,52,61,63};
int preprocessing(char *infix1, char *infix){
char *pstr, c;
int *pa, i, j, k, m, n, e10, digit, c_asc=97, ndigit=0;
pstr=infix1;
pa=op_value;
ndigit=0;
i=0; j=0;n=0;
while(*(pstr+i)!='\0'){
if((*(pstr+i)>='0') && (*(pstr+i)<='9'))
j++;
else {
if (j>0){
digit=*(pstr+i-1)-48;
k=1;
while (k<j) {
e10=1;
for (m=1;m<=k;m++)
e10*=10;
digit+=(*(pstr+i-1-k)-48)*e10;
k++;
}//while
*(infix+n)=c_asc++;
n++;
*pa=digit;
ndigit++;
pa++;
j=0;
}//if
*(infix+n)=*(pstr+i);
n++;
}//else
i++;
}//while
if (j>0) {
digit=*(pstr+i-1)-48;
k=1;
while (k<j){
e10=1;
for (m=1;m<=k;m++)
e10*=10;
digit+=(*(pstr+i-1-k)-48)*e10;
k++;
}//while
*pa=digit;
ndigit++;
j=0;
*(infix+n)=c_asc++;
n++;
}//
*(infix+n)='\0';
return ndigit;
}//preprocessing
void Push(int x){
if (S.top < MAX_ITEM-1) {
S.item[++S.top]=x;
}//if
}//Push
void Pop(int *x){
if (S.top >= 0) {
*x=S.item[S.top--];
}//if
}//Pop
int priority(int c){
int i;
for(i=0;i<MAX_OP;i++)
if(op[i] == c) return(prio[i]);
return(-1);
}//priority
int evaluate(char optr,int op1,int op2){
int result;
switch(optr){
case '+':result=op1+op2;
break;
case '-':result=op1-op2;
break;
case '*':result=op1*op2;
break;
case '/':result=op1/op2;
break;
}//switch
return(result);
}//evaluate
int post_evaluate(char *postfix){
char token;
int op1,op2,result,i=0;
while((token=postfix[i]) != '\0') {
if(operands(token))Push(op_value[token-'a']);
else if(operator(token)){
Pop(&op2);
Pop(&op1);
result=evaluate(token,op1,op2);
Push(result);
}//else if(operator(token))
i=i+1;
}//while
Pop (&result);
return(result);
}//post_evaluate
void in_to_post(char *infix,char *postfix){
int i,j,element;
char token;
i=j=0;
while((token=infix[i]) != '\0') {
if(operands(token))
postfix[j++]=token;
else if(token == '(')
Push(token);
else if(token == ')')
while (S.top >= 0){
Pop(&element);
if(element == '(') break;
postfix[j++]=element;
}//while
else if(operator(token)){
while(S.top >= 0){
element=S.item[S.top];
if(priority(token) <= priority(element)){
Pop(&element);
postfix[j++]=element;
}//if
else break;
}//while
Push(token);
}//else if(operator(token))
i=i+1;
}//while ((token=infix[i]) != '\0')
while(S.top >= 0){
Pop(& element);
postfix[j++]=element;
}//while(S.top >= 0)
postfix[j]='\0';
}//in_to_post
int main(){
int i, count=0;
char infix[MAX_ITEM],infix_c[MAX_ITEM];
char postfix[MAX_ITEM];
S.top=-1;
cout << "Please enter infix expression, e.g. (a+b)*c-d/e : ";
cin >> infix_c;
count = preprocessing(infix_c, infix);
in_to_post(infix,postfix);
cout << "Infix expression => " << infix << "\nPostfix expression=> " << postfix;
cout << "\nAnd the values of a ~ z are => ";
for(i=0;i <count;i++)
cout << op_value[i] << " " ;
cout << "\nThe evaluation of infix expression = " << post_evaluate(postfix) << "\n";
system("pause");
return 0;
}//main
这个程序是完成表达式运算的,实现的大概方法是用栈(很多数据结构教程上有这类算法的)
而个人一向不建议是初学者研究分析别人的程序 ,因为这么做对你学习语言基本没有帮助,首先它不会增加你的语法的理解 ,其次,即使看懂它每行程序,也不一定能理解它的具体算法过程,所有程序员都会认为,看懂别人的程序要比自己写一个难很多的。所以,学习语言初期的最好方法是先掌握所有语法,然后是自己试着写程序。到能看懂别人程序时,再去进一步研究