C语言四则( ’ + ’ , ’ - ’ , ’ * ’ , ’ / ')计算器,包括括号
1、思路:
(1)、当遇到数字字符时
所有数字字符存到snum[]栈中,但是要考虑多位数字的问题。在这里可以用标志位进行解决,value = value * 10 + str[i] - ‘0’; ,‘0’—的ASCII码为48。
(2)、当我们遇到运算符(包括左括号)时:(get_pri()_运算符优先级比较函数) 若该运算符的优先级比我们sop[]栈栈顶的优先级高时,直接入栈。 若该运算符的优先级没有sop[]栈栈顶的优先级大高时,则不断出栈并开始compute(snum,old_op)运算,直到该运算符的优先级比目前**sop[]栈栈顶的运算符高为止。 (3)、当遇到右括号时,则不断出栈,符号栈出一个就开始计算一个,直至遇到左括号时停止。
2、代码
2.1 主函数_main.c
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
int main()
{
char str[] = "1+2*3";
STACK *snum,*sop;
int i = 0,value = 0,flag = 0;
int old_op;
snum = stack_create();
sop = stack_create();
for(; str[i] != '\0' ; i++)
{
if(str[i] >= '0' && str[i] <= '9')
{
value = value * 10 + str[i] - '0';
flag = 1;
}
else
{
if(flag)
{
stack_push(snum,&value);
value = 0;
flag = 0;
}
if(str[i] == ')')
deal_bracket(snum,sop);
else
deal_op(snum,sop,str[i]);
}
}
if(flag)
{
stack_push(snum,&value);
}
while(!stack_isempty(sop))
{
stack_pop(sop,&old_op);
compute(snum,old_op);
}
stack_pop(snum,&value);
printf("%s = %d\n",str,value);
stack_destroy(snum);
stack_destroy(sop);
exit(0);
}
2.2 没有括号的运算函数_deal_op(snum,sop,str[i])
void deal_op(STACK *snum, STACK *sop, int op)
{
int old_op;
if(stack_isempty(sop) || op == '(')
{
stack_push(sop,&op);
return ;
}
stack_top(sop,&old_op);
if(get_pri(op) > get_pri(old_op))
{
stack_push(sop,&op);
return ;
}
while(get_pri(op) <= get_pri(old_op))
{
stack_pop(sop,&old_op);
compute(snum,old_op);
if(stack_isempty(sop))
break;
stack_top(sop,&old_op);
}
stack_push(sop,&op);
}
2.3 遇到右括号时运算的函数_deal_bracket(STACK *snum, STACK *sop)
void deal_bracket(STACK *snum, STACK *sop)
{
int old_op;
stack_top(sop, &old_op);
while(old_op != '(')
{
stack_pop(sop,&old_op);
compute(snum,old_op);
stack_top(sop,&old_op);
}
stack_pop(sop,&old_op);
}
2.4 结果运算函数_compute(STACK *snum, int op)
void compute(STACK *snum, int op)
{
int n1,n2,n;
stack_pop(snum,&n2);
stack_pop(snum,&n1);
switch(op)
{
case '+':
n = n1 + n2;
break;
case '-':
n = n1 - n2;
break;
case '*':
n = n1 * n2;
break;
case '/':
n = n1 / n2;
break;
default:
break;
}
stack_push(snum,&n);
}
2.5 设置运算符优先级函数_get_pri(int op)
int get_pri(int op)
{
switch(op)
{
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
}
}
2.6 static.c 的所有函数
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
STACK *stack_create()
{
STACK *new;
new = malloc(sizeof(*new));
if(new == NULL)
return NULL;
new->top = -1;
return new;
}
void stack_create2(STACK **ptr)
{
*ptr = malloc(sizeof(STACK));
if(*ptr == NULL)
return ;
(*ptr)->top = -1;
return ;
}
int stack_isempty(STACK *ptr)
{
if(ptr->top == -1)
return 1;
return 0;
}
int stack_isfull(STACK *ptr)
{
if(ptr->top == MAXSIZE-1)
return 1;
return 0;
}
int stack_push(STACK *ptr, const int *x)
{
if(stack_isfull(ptr))
return -1;
ptr->data[++ptr->top] = *x;
return 0;
}
int stack_pop(STACK *ptr, int *x)
{
if(stack_isempty(ptr))
return -1;
*x = ptr->data[ptr->top--];
return 0;
}
int stack_top(STACK *ptr,int *x)
{
if(stack_isempty(ptr))
return -1;
*x = ptr->data[ptr->top];
return 0;
}
void stack_display(STACK *ptr)
{
int i;
if(stack_isempty(ptr))
return ;
for(i = 0 ; i <= ptr->top ; i++)
printf("%d ",ptr->data[i]);
printf("\n");
}
void stack_destroy(STACK *ptr)
{
free(ptr);
ptr = NULL;
}
|