表达式求值
#include "stdio.h" #include "conio.h" #include "string.h" #include "stdlib.h" #define null 0 typedef struct node {char item[10]; struct node *next;}node;//定义链式堆栈元素类型 typedef struct stack {node *top;}stack; //定义链式堆栈类型 typedef struct queuenode {char item[10]; struct queuenode *next;}queuenode;//定义链式队列元素类型 typedef struct queue {queuenode *front; queuenode *rear;}queue;//定义链式队列类型 stack *stack_opr=null; queue *queue_exp=null; char sexpression[200],temp[20],opr[20]; int i,j,isnum; stack *createstack() //创建一个空堆栈(不带头结点的链栈) {stack *s=(stack *)malloc(sizeof(stack)); if(s==null) exit(0); else s->top=null; return s;} int stackempty(stack *s) //推断堆栈是否为空 { if(!s) { printf("\nStack if not exit!\n"); exit(0);} if(s->top==null) return 1; else return 0;} node *makenode(char *item) //创建一个堆栈结点,并使其数据域等于item { node *pnode; pnode=(node *)malloc(sizeof(node)); if(!pnode) exit(0); else {strcpy(pnode->item,item); pnode->next=null;} return pnode;} void push(char *item,stack *s)//压栈 { node *pnode=makenode(item); pnode->next=s->top; s->top=pnode;} void pop(char *item,stack *s)//出栈 { node *pnode; if(!stackempty(s)) {pnode=s->top; strcpy(item,pnode->item); s->top=pnode->next; free(pnode);} } void clearstack(stack *s)//清空堆栈 { node *pnode; while(!stackempty(s)) {pnode=s->top; s->top=pnode->next; free(pnode);} } int stacksize(stack *s)//计算栈中结点的个数 {int i=0; node *pnode; if(!stackempty(s)) {for(pnode=s->top;!pnode; pnode=pnode->next) i++;} return i; } void stacktop(char *item,stack *s)//取栈顶元素,但不把栈顶元素出栈 { if(!stackempty(s)) { strcpy(item,s->top->item);} } queuenode *makequeuenode(char *item)//创建一个链队列结点。并使其数据域等于item { queuenode *pnode; pnode=(queuenode *)malloc(sizeof(queuenode)); if(pnode==null) exit(0); else { strcpy(pnode->item,item); pnode->next=null;} return pnode; } queue *createqueue() //创建一个空队列。不带头结点 { queue *q=(queue *)malloc(sizeof(queue)); if(q==null) exit(0); else { q->front=q->rear=null;} return q; } int queueempty(queue *q)//推断一个队列是否为空 { if(!q->front) return 1; else return 0; } void queueinsert(char *item,queue *q)//入队 { queuenode *pnode=makequeuenode(item); if(queueempty(q)) q->front=q->rear=pnode; else { q->rear->next=pnode; q->rear=pnode;} } void queuedel(char *item,queue *q)//出队 { queuenode *pnode; if(queueempty(q)) printf("出队错误,队列为空!\n"); else { pnode=q->front;strcpy(item,pnode->item);q->front=pnode->next;free(pnode);} } int queuesize(queue *q)//求队列长度 {int i=0; queuenode *pnode; if(!queueempty(q)) {for(pnode=q->front;!pnode; pnode=pnode->next) i++;} return i; } void clearqueue(queue *q)//清空队列 { queuenode *pnode; while(!queueempty(q)) {pnode=q->front;q->front=pnode->next; free(pnode);} } void queuefront(char *item,queue *q)//取队头元素,但并不出队 { if(!queueempty(q)) { strcpy(item,q->front->item);} } void printqueue(queue *q)//输出队列中的元素 { queuenode *pnode; if(!queueempty(q)) {printf("\n队列元素:");pnode=q->front; while(pnode){printf("%s ,",pnode->item); pnode=pnode->next;} } } void printstack(stack *s)//输出堆栈中的元素 { node *pnode; if(!stackempty(s)) {printf("\n堆栈元素:");pnode=s->top; while(pnode){printf("%s ,",pnode->item); pnode=pnode->next;} } } int priorty(char opr)//求运算符号优先级 {switch(opr) {case '(': return 0; case '-': return 1; case '+': return 1; case '*': return 2; case '/': return 2; } } void caculate(queue *q)//计算后缀表达式的数值,要求最初參与运算的数值是整数 { char temp[20],opr[20],num[20]; double fa,fb; stack *stack_num=null; stack_num=createstack(); while(!queueempty(q)) { queuedel(opr,q); if((opr[0]>='0'&&opr[0]<='9')||(opr[0]=='.')) push(opr,stack_num); else { pop(num,stack_num); fb=atof(num); pop(num,stack_num); fa=atof(num); switch(opr[0]) {case '+':fa+=fb;break; case '-':fa-=fb;break; case '*': fa*=fb;break; case '/': if(fb==0){printf("\n除0出错!");exit(0);} else {fa/=fb;break;} } sprintf(num,"%f",fa); push(num,stack_num); } }pop(num,stack_num); printf("\n运算结果是:%s",num); } void translate(char sexpression[200]) { //printf("%s",sexpression); stack_opr=createstack(); queue_exp=createqueue(); i=0; while(sexpression[i]!='\0') { isnum=j=0; while(sexpression[i]>='0'&&sexpression[i]<='9') {isnum=1;temp[j++]=sexpression[i++];} if(isnum) { temp[j]='\0';queueinsert(temp,queue_exp);} else { temp[0]=sexpression[i++];temp[1]='\0'; switch(temp[0]) { case '('6 case '+': case '-': case '*': case '/': if(!stackempty(stack_opr)) while(priorty(temp[0])<=priorty(stack_opr->top->item[0])) {pop(opr,stack_opr);queueinsert(opr,queue_exp);if(stackempty(stack_opr)) break;} push(temp,stack_opr); break; case ')': while(stack_opr->top->item[0]!='(') {pop(opr,stack_opr); queueinsert(opr,queue_exp);} pop(opr,stack_opr); break; } } printstack(stack_opr); printqueue(queue_exp); } while(!stackempty(stack_opr)) {pop(opr,stack_opr);queueinsert(opr,queue_exp);} } int main() { //char sexpression[200],temp[20],opr[20]; //int i,j,isnum; // stack *stack_opr=null; // queue *queue_exp=null; printf("\n请输入待计算的表达式(中缀式):\n"); gets(sexpression); translate(sexpression); caculate(queue_exp); printstack(stack_opr); printqueue(queue_exp); getch(); }