#include <stdio.h>
#include "LinkStack.h"
int Priority(char ch)
{
switch(ch)
{
case '(':
return 3;
case '*':
case '/':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
}
}
int main()
{
Stack *s_opt, *s_num;
char opt[1024] = {0}; //存放表达式
int i = 0, tmp = 0, num1 = 0, num2 = 0;
if (StackInit(&s_opt) != SUCCESS || StackInit(&s_num) != SUCCESS)
{
printf("Init Failure!\n");
}
printf("Please input : \n");
scanf("%s", opt);
while (opt[i] != '\0' || StackEmpty(s_opt) != TRUE) //表达式没结束 或者 操作符栈不为空
{
if (opt[i] >= '0' && opt[i] <= '9') //操作数
{
tmp = tmp * 10 + opt[i] - '0';
i++;
if (opt[i] > '9' || opt[i] < '0') //操作符
{
push(&s_num, tmp);
tmp = 0;
}
}
else //操作符
{
if (opt[i] == ')' && GetTop(s_opt) == '(') //出栈不计算
{
pop(&s_opt);
i++;
continue;
}
if (StackEmpty(s_opt) == TRUE || (Priority(opt[i]) > Priority(GetTop(s_opt)))
|| (GetTop(s_opt) == '(' && opt[i] != ')')) //进栈
{
push(&s_opt, opt[i]);
i++;
continue;
}
if ((opt[i] == '\0' && StackEmpty(s_opt) != TRUE) ||
(opt[i] == ')' && GetTop(s_opt) != '(') ||
(Priority(opt[i]) <= Priority(GetTop(s_opt)))) //出栈计算
{
switch(pop(&s_opt))
{
case '+':
num1 = pop(&s_num);
num2 = pop(&s_num);
push(&s_num, (num1 + num2));
break;
case '-':
num1 = pop(&s_num);
num2 = pop(&s_num);
push(&s_num, (num2 - num1));
break;
case '*':
num1 = pop(&s_num);
num2 = pop(&s_num);
push(&s_num, (num1 * num2));
break;
case '/':
num1 = pop(&s_num);
num2 = pop(&s_num);
push(&s_num, (num2 / num1));
break;
}
}
}
}
printf("%d\n", GetTop(s_num));
return 0;
}
#ifndef _SEQUENCEQUEUE_H
#define _SEQUENCEQUEUE_H
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
#define SUCCESS 10000
#define FAILURE 10001
#define TRUE 10002
#define FALSE 10003
struct queue
{
int data[SIZE];
int front; //队头指针(下标)
int rear; //队尾指针
};
typedef struct queue Queue;
int InitQueue(Queue *q);
int EnterQueue(Queue *q, int e);
int EmptyQueue(Queue q);
int GetFront(Queue q);
int LengthQueue(Queue q);
int DeleteQueue(Queue *q);
int ClearQueue(Queue *q);
#endif
#include "SequenceQueue.h"
int main()
{
int ret, i;
Queue queue;
ret = InitQueue(&queue);
if (ret == SUCCESS)
{
printf("Init Success!\n");
}
else
{
printf("Init Failure!\n");
}
ret = EmptyQueue(queue);
if (ret == TRUE)
{
printf("Queue Is Empty!\n");
}
else
{
printf("Queue is not Empty!\n");
}
for(i = 0; i < 10; i++)
{
ret = EnterQueue(&queue, i);
if (FAILURE == ret)
{
printf("Enter %d Failure!\n", i);
}
else
{
printf("Enter %d Success!\n", i);
}
}
ret = GetFront(queue);
if (ret == FAILURE)
{
printf("Get Front Failure!\n");
}
else
{
printf("Front is %d\n", ret);
}
ret = LengthQueue(queue);
printf("length is %d\n", ret);
for (i = 0; i < 5; i++)
{
ret = DeleteQueue(&queue);
if (ret == FAILURE)
{
printf("Delete Failure!\n");
}
else
{
printf("Delete %d Success!\n", ret);
}
}
ret = ClearQueue(&queue);
if (ret == SUCCESS)
{
printf("Clear Success!\n");
}
else
{
printf("Clear Failure!\n");
}
ret = GetFront(queue);
if (ret == FAILURE)
{
printf("Get Front Failure!\n");
}
else
{
printf("Front is %d\n", ret);
}
return 0;
}
#include "SequenceQueue.h"
int InitQueue(Queue *q)
{
if (NULL == q)
{
return FAILURE;
}
q->rear = q->front = 0; //初始化空队
return SUCCESS;
}
int EmptyQueue(Queue q)
{
return (q.front == q.rear) ? TRUE : FALSE;
}
int EnterQueue(Queue *q, int e)
{
if (NULL == q)
{
return FAILURE;
}
if ((q->rear + 1) % SIZE == q->front) //队满
{
return FAILURE;
}
q->data[q->rear] = e;
q->rear = (q->rear + 1) % SIZE;
return SUCCESS;
}
int GetFront(Queue q)
{
if (q.rear == q.front)
{
return FAILURE;
}
return q.data[q.front];
}
int LengthQueue(Queue q)
{
return (q.rear - q.front + SIZE) % SIZE;
}
int DeleteQueue(Queue *q)
{
if (NULL == q)
{
return FAILURE;
}
if (q->rear == q->front)
{
return FAILURE;
}
int e = q->data[q->front];
q->front = (q->front + 1) % SIZE;
return e;
}
int ClearQueue(Queue *q)
{
if (NULL == q)
{
return FAILURE;
}
q->front = q->rear;
return SUCCESS;
}