#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("结果是: %s", num); } //遍历队列 void PrintQueue(Queue *q) { QueueNode *pNode; if (!QueueEmpty(q)) { printf(" Queue:"); pNode = q->front; while (pNode) { printf("%s,", pNode->item); pNode = pNode->next; } } } //遍历栈 void PrintStack(Stack *s) { Node *pNode; if (!StackEmpty(s)) { printf(" Stack:"); 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("输入表达式: "); gets(sExpression); printf("%s", sExpression); stack_opr = InitStack(); queue_exp = InitQueue(); i = 0; while (sExpression[i] != '