#include<stdio.h> #include<stdlib.h> #include<stdlib.h> //鏈棧 typedef struct node { char item[30]; struct node* next; }Node; typedef struct stack { Node *top; }Stack; //隊列 typedef struct queueNode { char item[30]; struct queueNode* next; }QueueNode; typedef struct queue { QueueNode *front; QueueNode *rear; }Queue; //初始化棧 Stack* InitStack() { Stack *s = (Stack *)malloc(sizeof(Stack)); s->top = NULL; return s; } //判斷棧是否為空 int StackEmpty(Stack *s) { if (s->top == NULL) return 1; else return 0; } //創建節點,入棧時用 Node *MakeNode(char *data) { Node *pNode; pNode = (Node *)malloc(sizeof(Node)); strcpy(pNode->item, data); 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); } } //創建鏈隊列的節點 QueueNode* MakeQueueNode(char *item) { QueueNode *pNode; pNode = (QueueNode *)malloc(sizeof(QueueNode)); strcpy(pNode->item, item); pNode->next = NULL; return pNode; } //初始化隊列 Queue* InitQueue() { Queue *q = (Queue *)malloc(sizeof(Queue)); q->front = q->rear = NULL; return q; } //隊列是否空 int QueueEmpty(Queue *q) { if (!q->front) return 1; else return 0; } //入隊操作 void Append(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 Serve(char *item, Queue *q) { QueueNode *pNode; pNode = q->front; strcpy(item, pNode->item); q->front = pNode->next; free(pNode); } //判斷運算符優先級 int Priority(char opr) { switch (opr) { case '(': return 0; case '-': case '+': return 1; case '*': case '/': return 2; } } //計算 void Caculate(Queue *q) { char temp[20], opr[20], num[20]; double fa, fb; Stack *stack_num = NULL; stack_num = InitStack(); while (!QueueEmpty(q)) { Serve(opr, q); if ((opr[0] >= '0'&&opr[0] <= '9') || (opr[0] == '.')) Push(opr, stack_num); else { Pop(num, stack_num); fb = atof(num); //atof將字符轉成數字 Pop(num, stack_num); fa = atof(num); switch (opr[0]) { case '+': fa += fb; break; case '-': fa -= fb; break; case '*': fa *= fb; break; case '/': fa /= fb; break; } sprintf(num, "%f", fa); //將數字再轉成字符 Push(num, stack_num); } } Pop(num, stack_num); printf("結果是:\n%s", num); } //遍歷隊列 void PrintQueue(Queue *q) { QueueNode *pNode; if (!QueueEmpty(q)) { printf("\nQueue:"); pNode = q->front; while (pNode) { printf("%s,", pNode->item); pNode = pNode->next; } } } //遍歷棧 void PrintStack(Stack *s) { Node *pNode; if (!StackEmpty(s)) { printf("\nStack:"); pNode = s->top; while (pNode) { printf("%s,", pNode->item); pNode = pNode->next; } } } int main() { //下面是把輸入的表達式轉成后綴表達式,輸入3-5*2則轉成352*- char sExpression[100], temp[20], opr[20]; int i, j, isNum; Stack *stack_opr = NULL; //用來存運算符 Queue *queue_exp = NULL; //隊列用來存后綴表達式 printf("輸入表達式:\n"); gets(sExpression); printf("%s", sExpression); stack_opr = InitStack(); queue_exp = InitQueue(); i = 0; while (sExpression[i] != '\0') { isNum = j = 0; while (sExpression[i] >= '0'&&sExpression[i] <= '9')//如果輸入的是數字 { isNum = 1; temp[j] = sExpression[i]; //將數字放入臨時的temp i++; j++; } if (isNum) { temp[j] = '\0'; Append(temp, queue_exp); //入隊列,轉后綴表達式時,如果是數字直接進隊列 } else //是操作符的話 { temp[0] = sExpression[i++]; temp[1] = '\0'; //截斷字符串 switch (temp[0]) //根據操作符,比較和棧內的操作符優先級,進行入棧還是出棧操作 { case '(': Push(temp, stack_opr); break; case '+': case '-': case '*': case '/': if (!StackEmpty(stack_opr)) while (Priority(temp[0]) <= Priority(stack_opr->top->item[0]))//小於或等於棧內的操作符 { Pop(opr, stack_opr); Append(opr, queue_exp); if (StackEmpty(stack_opr)) break; } Push(temp, stack_opr); break; case ')': while (stack_opr->top->item[0] != '(') { Pop(opr, stack_opr); Append(opr, queue_exp); } Pop(opr, stack_opr); break; } } // PrintStack(stack_opr); // PrintQueue(queue_exp); } while (!StackEmpty(stack_opr)) //將表達式剩下的入隊列 { Pop(opr, stack_opr); Append(opr, queue_exp); } Caculate(queue_exp); //計算后綴表達式 // PrintStack(stack_opr); // PrintQueue(queue_exp); system("pause"); }