看看这个代码,case那一块,return3、4、2、??为什么这样操作?
程序代码:
#include<stdio.h> #ifndef STACK_H_INCLUDED #define STACK_H_INCLUDED typedef struct Node { char Date; struct Node *Next; } LinkStack;//定义结构体 LinkStack *CreateStack()//创建栈 { LinkStack *S; S = (LinkStack*)malloc(sizeof(struct Node));//分配空间 S->Next = NULL;//初始化,即栈为空栈 return S; } int IsEmpty(LinkStack *S)//IsEmpty的意思是说明这个链栈已经初始化 { return(S->Next == NULL); } void Push(LinkStack *S, char item) { LinkStack *TmpCell; TmpCell = (LinkStack*)malloc(sizeof(struct Node)); TmpCell->Date = item;/*为栈分配数据域和指针域,数据域就是前面定义过的字符item,指针域TmpCell是什么?TmpCell是人员自定的一个变量名*/ TmpCell->Next = S->Next; S->Next = TmpCell;//插入TmpCell } char Pop(LinkStack *S)//Pop即出栈操作 { LinkStack *FirstCell; char TopElem;//栈顶元素 if(IsEmpty(S)) { return NULL; } else { FirstCell = S->Next; S->Next = FirstCell->Next; TopElem = FirstCell->Date; free(FirstCell); return TopElem; } } #endif // STACK_H_INCLUDED #include <stdio.h> #include <stdlib.h> #define MaxSize 1000 int Judge(int result,char op) {//result只是一个用来判断的标志,只是一个变量名 if(result) switch(op) {//operater操作符 case '+': return 3; case '-': return 3; case '*': return 5; case '/': return 5; case '(': return 1; case ')': return 6; } else switch(op) { case '+': return 2; case '-': return 2; case '*': return 4; case '/': return 4; case '(': return 6; case ')': return 1; } } void SUFF(char *a) {//定义一个操作,操作名为SUFF int i=0, j=0; char b[MaxSize]; LinkStack *Stack; Stack = CreateStack(); while(a[i]) { if(a[i]>='0'&& a[i]<='9') { b[j++] = a[i]; i++; continue; } while( !IsEmpty(Stack) && Judge(0,a[i])<Judge(1,Stack->Next->Date)) b[j++] = Pop(Stack); if(IsEmpty(Stack)) { Push(Stack,a[i]);//Push是入栈操作 i++; continue; } else if(Judge(0,a[i])==Judge(1,Stack->Next->Date)) Pop(Stack); else if(Judge(0,a[i])>Judge(1,Stack->Next->Date)) Push(Stack,a[i]); i++; } while(!IsEmpty(Stack)) b[j++] = Pop(Stack); b[j]='\0'; puts(b); } int main() { char a[MaxSize]; gets(a); SUFF(a); return 0; }