作者在 2010-04-17 22:44:22 发布以下内容
【将中缀表达式转化为后缀】
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
typedef struct node
{
char data; int code; int pri;
struct node *link;
}NODE;
struct Tb1
{
char data; int code; int pri;
}opchTb1[]={{'*',1,4},{'/',2,4},{'+',3,2},{'-',4,2},{'(',5,5},{')',6,1},{'\0',7,0},{'#',-1,0}};
NODE *optop;
char num[200], *numtop;
char expStr[200];
void push(char x,int c,int p,NODE **toppt)
{
NODE *q=(NODE *)malloc(sizeof(NODE));
q->data=x;
q->code=c;
q->pri=p;
q->link=*toppt;
*toppt=q;
}
int pop(char *op,int *cp, NODE **toppt)
{
NODE *q=*toppt;
if(*toppt==NULL) return 1;
*op=q->data;
*cp=q->code;
*toppt=q->link;
free(q);
return 0;
}
int expr(char *pos)
{
struct Tb1 *op;
char sop;
int type,code,n,m,i,c;
optop=NULL;
numtop=num;
n=m=0;
c=' ';
push('#',0,0,*optop);
while(1){
while(c==' '||c=='\t') c=*pos++;
if(isalpha(c)){
*numtop++=' ';
while(isalpha(c)||isdigit(c)) {*numtop++=c;c=*pos++;}
if(m) return 1;
m=1;
continue;
}
else {
for(i=0;opchTb1[i].code!=-1&&opchTb1[i].data!=c;i++)
if(opchTb1[i].code==-1) return 3;
op=&opchTb1.[i];
type=opchTb1.[i].code;
c=*pos++;
}
if(type<5){
if(m!=1) return 1;
m=0;
}
if(type==5) n++;
if(type==6){
if(n--==0) return 2;
if(op->pri>optop->pri)
if(op->data=='(') push(op->code,1,*optop);
else push(op->data,op->code,op->pri,*optop);
else{
while(optop!=NULL&&op->pri<=optop->pri) {
pop(&sop,&code,&optop);
if(code<5&&code>0) {
*numtop++=' ';
*numtop++=sop;
}
}
if(op->data=='\0') return(n!=0||(m!=1&&numtop>num))?4:(*numtop='\0');
else if(op->data!=')') push(op->data,op->code,op->pri,&optop);
}
}
}
void main()
{
int d;
printf("please input the string!\n");
gets(expStr);
if((d=expr(expStr))==0) printf("the postfix string is:%s\n",num);
else printf("The string error! the error is:%d\n",d);
getch();
}
【后缀表达式的计算】
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXCOLS 80
#define TRUE 1
#define FLASE 0
double eval(char[]);
double pop(struct stack *ps);
void push(struct stack *ps,double x);
int empty(struct stack *ps);
int isdigit(char);
double oper(int,double,double);
void main()
{
char expr[MAXCOLS];
int position=0;
printf("\nPlease input the string:");
while((expr[position++]=getchar())!='\n');
expr[--position]='\0';
printf("%s%s","the original postfix expression is",expr);
printf("\n%f",eval(expr));
getch();
} /*end main*/
/*程序的主要部分eval函数,这个函数只是计算算法的C语言实现,同时考虑了特定的环境和数据的输入*/
/*输出格式。eval调用了一个isdigit函数,它用来判断其参数是不是一个操作数。在函数eval及其调用的*/
/*pop和push例程中都使用了下面的堆栈说明。eval函数在声明后给出*/
struct stack{
int top;
double items[MAXCOLS];
};
double eval(char expr[])
{
int c,position;
double opnd1,opnd2,value;
struct stack opndstk;
opndstk.top=-1;
for(position=0;(c=expr[position])!='\0';position++)
if(isdigit(c)) /*operand--convert the character representation of the digit into double and*/
/*push it onto the stack*/
push(&opndstk,(double)(c-'0'));
else{ /*operator*/
opnd2=pop(&opndstk);
opnd1=pop(&opndstk);
value=oper(c,opnd1,opnd2);
push(&opndstk,value);
} /*end else*/
return(pop(&opndstk));
}/*end eval*/
/*下面的函数在许多C系统中都被预定义为一个宏*/
int isdigit(char symb)
{
return(symb>='0'&&symb<='9');
}
/*函数oper首先检查它的第一个参数是不是一个合法的运算符,如果是,则用另外两个参数来决定运算结果*/
/*对于求幂运算,使用了math.h中定义的函数pow(op1,op2)。*/
double oper(int symb,double op1,double op2)
{
switch(symb){
case '+' : return(op1+op2);
case '-' : return(op1-op2);
case '*' : return(op1*op2);
case '/' : return(op1/op2);
case '$' : return(pow(op1,op2));
default:printf("%s","illegal operation");
exit(1);
}/*end switch*/
}/*end oper*/
double pop(struct stack *ps)
{
if(empty(ps)){
printf("%s","stack underflow");
exit(1);
} /*end if*/
return(ps->items[ps->top--]);
}/*end pop*/
void push(struct stack *ps,double x)
{
ps->items[++(ps->top)]=x;
return;
} /*end push*/
int empty(struct stack *ps)
{
return(ps->top==-1);
}/*end empty*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
typedef struct node
{
char data; int code; int pri;
struct node *link;
}NODE;
struct Tb1
{
char data; int code; int pri;
}opchTb1[]={{'*',1,4},{'/',2,4},{'+',3,2},{'-',4,2},{'(',5,5},{')',6,1},{'\0',7,0},{'#',-1,0}};
NODE *optop;
char num[200], *numtop;
char expStr[200];
void push(char x,int c,int p,NODE **toppt)
{
NODE *q=(NODE *)malloc(sizeof(NODE));
q->data=x;
q->code=c;
q->pri=p;
q->link=*toppt;
*toppt=q;
}
int pop(char *op,int *cp, NODE **toppt)
{
NODE *q=*toppt;
if(*toppt==NULL) return 1;
*op=q->data;
*cp=q->code;
*toppt=q->link;
free(q);
return 0;
}
int expr(char *pos)
{
struct Tb1 *op;
char sop;
int type,code,n,m,i,c;
optop=NULL;
numtop=num;
n=m=0;
c=' ';
push('#',0,0,*optop);
while(1){
while(c==' '||c=='\t') c=*pos++;
if(isalpha(c)){
*numtop++=' ';
while(isalpha(c)||isdigit(c)) {*numtop++=c;c=*pos++;}
if(m) return 1;
m=1;
continue;
}
else {
for(i=0;opchTb1[i].code!=-1&&opchTb1[i].data!=c;i++)
if(opchTb1[i].code==-1) return 3;
op=&opchTb1.[i];
type=opchTb1.[i].code;
c=*pos++;
}
if(type<5){
if(m!=1) return 1;
m=0;
}
if(type==5) n++;
if(type==6){
if(n--==0) return 2;
if(op->pri>optop->pri)
if(op->data=='(') push(op->code,1,*optop);
else push(op->data,op->code,op->pri,*optop);
else{
while(optop!=NULL&&op->pri<=optop->pri) {
pop(&sop,&code,&optop);
if(code<5&&code>0) {
*numtop++=' ';
*numtop++=sop;
}
}
if(op->data=='\0') return(n!=0||(m!=1&&numtop>num))?4:(*numtop='\0');
else if(op->data!=')') push(op->data,op->code,op->pri,&optop);
}
}
}
void main()
{
int d;
printf("please input the string!\n");
gets(expStr);
if((d=expr(expStr))==0) printf("the postfix string is:%s\n",num);
else printf("The string error! the error is:%d\n",d);
getch();
}
【后缀表达式的计算】
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXCOLS 80
#define TRUE 1
#define FLASE 0
double eval(char[]);
double pop(struct stack *ps);
void push(struct stack *ps,double x);
int empty(struct stack *ps);
int isdigit(char);
double oper(int,double,double);
void main()
{
char expr[MAXCOLS];
int position=0;
printf("\nPlease input the string:");
while((expr[position++]=getchar())!='\n');
expr[--position]='\0';
printf("%s%s","the original postfix expression is",expr);
printf("\n%f",eval(expr));
getch();
} /*end main*/
/*程序的主要部分eval函数,这个函数只是计算算法的C语言实现,同时考虑了特定的环境和数据的输入*/
/*输出格式。eval调用了一个isdigit函数,它用来判断其参数是不是一个操作数。在函数eval及其调用的*/
/*pop和push例程中都使用了下面的堆栈说明。eval函数在声明后给出*/
struct stack{
int top;
double items[MAXCOLS];
};
double eval(char expr[])
{
int c,position;
double opnd1,opnd2,value;
struct stack opndstk;
opndstk.top=-1;
for(position=0;(c=expr[position])!='\0';position++)
if(isdigit(c)) /*operand--convert the character representation of the digit into double and*/
/*push it onto the stack*/
push(&opndstk,(double)(c-'0'));
else{ /*operator*/
opnd2=pop(&opndstk);
opnd1=pop(&opndstk);
value=oper(c,opnd1,opnd2);
push(&opndstk,value);
} /*end else*/
return(pop(&opndstk));
}/*end eval*/
/*下面的函数在许多C系统中都被预定义为一个宏*/
int isdigit(char symb)
{
return(symb>='0'&&symb<='9');
}
/*函数oper首先检查它的第一个参数是不是一个合法的运算符,如果是,则用另外两个参数来决定运算结果*/
/*对于求幂运算,使用了math.h中定义的函数pow(op1,op2)。*/
double oper(int symb,double op1,double op2)
{
switch(symb){
case '+' : return(op1+op2);
case '-' : return(op1-op2);
case '*' : return(op1*op2);
case '/' : return(op1/op2);
case '$' : return(pow(op1,op2));
default:printf("%s","illegal operation");
exit(1);
}/*end switch*/
}/*end oper*/
double pop(struct stack *ps)
{
if(empty(ps)){
printf("%s","stack underflow");
exit(1);
} /*end if*/
return(ps->items[ps->top--]);
}/*end pop*/
void push(struct stack *ps,double x)
{
ps->items[++(ps->top)]=x;
return;
} /*end push*/
int empty(struct stack *ps)
{
return(ps->top==-1);
}/*end empty*/