主函数
/**************************************************************
> File Name: calculator.c
> Author: chengfeiyan
> Mail: 1493789272@qq.com
> Created Time: 2018年08月08日 星期三 14时41分41秒
**************************************************************/
#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, (num2 + num1));
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, (num2 * num1));
break;
case '/':
num1 = pop(&s_num);
num2 = pop(&s_num);
push(&s_num, (num2 / num1));
break;
}
}
}
}
printf("%d\n", GetTop(s_num));
return 0;
}
头文件
/**************************************************************
> File Name: LinkStack.h
> Author: chengfeiyan
> Mail: 1493789272@qq.com
> Created Time: 2018年08月08日 星期三 09时37分16秒
**************************************************************/
#ifndef _LINKSTACK_H
#define _LINKSTACK_H
#define SUCCESS 10000
#define FAILURE 10001
#define TRUE 10002
#define FALSE 10003
typedef int ElemType;
struct node
{
ElemType data;
struct node *next;
};
typedef struct node Node;
struct stack
{
Node *top;
int count;
};
typedef struct stack Stack;
int StackInit(Stack **s);
int StackEmpty(Stack *s);
int push(Stack **s, ElemType e);
int GetTop(Stack *s);
int pop(Stack **s);
int StackClear(Stack **s);
int StackDestroy(Stack **s);
#endif
自定义函数
#include <stdio.h>
#include "LinkStack.h"
#include <stdlib.h>
int StackInit(Stack **s)
{
if(NULL == s)
{
return FAILURE;
}
(*s) = (Stack *)malloc(sizeof(Stack) * 1);
if(NULL == *s)
{
return FAILURE;
}
(*s)->top = NULL;
(*s)->count = 0;
return SUCCESS;
}
int push(Stack **s, ElemType e)
{
if(NULL == *s || NULL == s)
{
return FAILURE;
}
Node *p = (Node *)malloc(sizeof(Node));
if(NULL == p)
{
return FAILURE;
}
p->data = e;
p->next = (*s)->top;
(*s)->top = p;
(*s)->count++;
return SUCCESS;
}
int GetTop(Stack *s)
{
if(NULL == s || s->top == NULL) //s->top == NULL
{
return FAILURE;
}
return s->top->data;
}
int pop(Stack **s)
{
if(NULL == s || NULL == *s)
{
return FAILURE;
}