计算:(4 + 2)*8 - 3
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
struct operand
{
int data[MAX];
int top;
};
struct operator_ch
{
int top;
char data[MAX];
};
typedef struct operand OPND;
typedef struct operator_ch OPCH;
void init_OPND_stack(OPND *stack)
{
stack->top = -1;
}
void init_OPCH_stack(OPCH *stack)
{
stack->top = -1;
}
int is_empty_OPND(OPND *stack)
{
if(stack->top == -1)
{
return -1;
}
return 0;
}
int is_empty_OPCH(OPCH *stack)
{
if(stack->top == -1)
{
return -1;
}
return 0;
}
int get_OPND_top(OPND *stack)
{
if(is_empty_OPND(stack) == -1)
{
return -1;
}
return stack->data[stack->top];
}
int get_OPCH_top(OPCH *stack)
{
if(is_empty_OPCH(stack) == -1)
{
return -1;
}
return stack->data[stack->top];
}
int push_OPND(OPND *stack, int num)
{
stack->data[++(stack->top)] = num;
}
int push_OPCH(OPCH *stack, char ch)
{
stack->data[++(stack->top)] = ch;
}
int pop_OPND(OPND *stack)
{
if(is_empty_OPND(stack) == -1)
{
return -1;
}
int num = stack->data[stack->top];
(stack->top)--;
return num;
}
char pop_OPCH(OPCH *stack)
{
if(is_empty_OPCH(stack) == -1)
{
return -1;
}
char ch = stack->data[stack->top];
(stack->top)--;
return ch;
}
int get_priority(char ch)
{
if(ch == '+' || ch == '-')
{
return 1;
}
if(ch == '*' || ch == '/')
{
return 2;
}
}
int compare_priority(char op_ch, char ch)
{
if(get_priority(op_ch) >= get_priority(ch))
{
return 1;
}
return -1;
}
int count(int num1, int num2, char ch)
{
int result;
int b = num1;
int a = num2;
switch(ch)
{
case '+':
{
result = a + b;
break;
}
case '-':
{
result = a - b;
break;
}
case '*':
{
result = a * b;
break;
}
case '/':
{
result = a / b;
break;
}
}
return result;
}
int main()
{
char ch;
char op_ch;
int a;
int b;
int result_val;
int op_num;
OPND * opnd_stack = NULL;
OPCH * opch_stack = NULL;
opnd_stack = (OPND *)malloc(sizeof(OPND));
opch_stack = (OPCH *)malloc(sizeof(OPCH));
init_OPND_stack(opnd_stack); //初始化
init_OPCH_stack(opch_stack); //初始化
while((ch = getchar()) != '\n')
{
if(ch >= '0' && ch <= '9')
{
//push_stack1();
op_num = ch - '0';
push_OPND(opnd_stack, op_num); //进栈
}
if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '(' || ch == ')')
{
if(ch == '(')
{
push_OPCH(opch_stack, ch);
continue;
}
if(ch == ')')
{
op_ch = pop_OPCH(opch_stack);
do{
a = pop_OPND(opnd_stack);
b = pop_OPND(opnd_stack);
result_val = count(a,b,op_ch);
push_OPND(opnd_stack, result_val); //进栈
}while((op_ch = pop_OPCH(opch_stack)) != '(');
continue;
}
do{
//op_ch = pop_stack2();
op_ch = get_OPCH_top(opch_stack);
if(op_ch == -1 || op_ch == '(')
{
push_OPCH(opch_stack, ch);
break;
}
else
{
if(compare_priority(op_ch,ch) < 0)
{
//push_stack2(ch);
push_OPCH(opch_stack, ch);
break;
}
else
{
a = pop_OPND(opnd_stack);
b = pop_OPND(opnd_stack);
op_ch = pop_OPCH(opch_stack);
result_val = count(a,b,op_ch);
push_OPND(opnd_stack, result_val); //进栈
}
}
}while(compare_priority(op_ch,ch) > 0);
}
}
do{
a = pop_OPND(opnd_stack);
b = pop_OPND(opnd_stack);
op_ch = pop_OPCH(opch_stack);
result_val = count(a,b,op_ch);
push_OPND(opnd_stack, result_val); //进栈
}while(op_ch != -1);
printf("result is %d\n", result_val);
free(opnd_stack);
free(opch_stack);
return 0;
}