表达式计算器

1+2/3*(4-6)*6/8+9*2 = ?

#include <stdio.h>
#include <stdlib.h>

#define 	MAXSIZE		32
typedef struct{
	int data[MAXSIZE];//数据段
	int top;//栈指针
}sqstack;

sqstack *sqstack_create()
{
	sqstack *sq;
	sq = malloc(sizeof(*sq));
	if(sq == NULL )
	{
		return NULL;
	}
	sq->top = -1;
	return sq;
}

int sqstack_push(sqstack *sq, int *data)//入栈
{
	if(sq->top == MAXSIZE-1)//full
	{
		return -1;
	}
	else
	{
		sq->data[++sq->top] = *data;
	}
	return 0;
}

int sqstack_top(sqstack *sq, int *data)//取得栈顶数据
{
	if(sq->top == -1)//empty
	{
		return -1;
	}
	*data = sq->data[sq->top];
	return 0;
}

int sqstack_pop(sqstack *sq, int *data)//出栈
{
	if(sq->top == -1)//empty
	{
		return -1;
	}
	*data = sq->data[sq->top--];
	return 0;
}

int compute(sqstack *snum, int ope)
{
	int n1, n2, n;
	sqstack_pop(snum, &n1);
	sqstack_pop(snum, &n2);
	switch(ope)
	{
		case '+':	n=n1+n2; printf("%d+%d=%d\n", n1, n2, n); break;
		case '-':	n=n2-n1; printf("%d-%d=%d\n", n2, n1, n);break;
		case '*':	n=n1*n2; printf("%d*%d=%d\n", n1, n2, n);break;
		case '/':	n=n2/n1; printf("%d/%d=%d\n", n2, n1, n);break;
		default:
			return -1;//break;
	}
	sqstack_push(snum, &n);//将运算结果压入数字栈
	return 0;
}

void deal_bracket(sqstack *snum, sqstack *sope)//处理括号
{
	int old_ope;
	sqstack_top(sope, &old_ope);//取得栈顶运算符
	while(old_ope != '(')
	{
		sqstack_pop(sope, &old_ope);
		compute(snum, old_ope);
		sqstack_top(sope, &old_ope);//取得栈顶运算符
	}
	sqstack_pop(sope, &old_ope);
}

int sqstack_is_empty(sqstack *sq)
{
	return (sq->top == -1);
}

int get_pri(int ope)
{
	switch(ope)
	{
		case '(':	return 0;
		case '+':
		case '-':	return 1;
		case '*':
		case '/':	return 2;
	}
}
void deal_ope(sqstack *snum, sqstack *sope, int ope)//处理运算符
{
	int old_ope;
	if(ope == '('||sqstack_is_empty(sope))
	{
		sqstack_push(sope, &ope);
		return ;
	}
	sqstack_top(sope, &old_ope);
	if(get_pri(ope) > get_pri(old_ope))
	{
		sqstack_push(sope, &ope);
		return ;
	}
	while(get_pri(ope) <= get_pri(old_ope))// * +
	{
		sqstack_pop(sope,&old_ope);
		compute(snum,old_ope);
		if(sqstack_is_empty(sope))
			break;
		sqstack_top(sope,&old_ope);
	}
	sqstack_push(sope,&ope);
}

int main()
{
	sqstack *snum;//运算数字栈
	sqstack *sope;//运算符栈
	int i = 0, value = 0, flag = 0, old_ope;
	
	char str[] = "1+3-(2*5)*(8-6)+5";//所要计算的表达式
	//初始化
	snum = sqstack_create();
	sope = sqstack_create();
	
	while(str[i] != '\0')
	{
		if(str[i] >= '0' && str[i] <= '9')//数字
		{
			value = value*10 + str[i]-'0';
			flag = 1;
		}
		else//运算符
		{
			if(flag == 1)//取得了运算的数字
			{
				sqstack_push(snum, &value);//数字入 运算数字栈
				value = 0;
				flag = 0;
			}
			if(str[i] == ')')
			{
				deal_bracket(snum,sope);
			}
			else//(+-*/
			{
				deal_ope(snum,sope,str[i]);
			}
		}
		i++;
	}
	if(flag)
	{
		sqstack_push(snum,&value);
	}
	while(!sqstack_is_empty(sope))
	{
		sqstack_pop(sope, &old_ope);
		compute(snum, old_ope);
	}
	sqstack_pop(snum, &value);
	
	printf("%s = %d\n", str, value);
	return 0;
}


上一篇:Hadoop默认支持集成OSS,作为Hadoop兼容的文件系统


下一篇:C#动态编译计算表达式的值