用栈写的多括号的四则运算

辛苦原创,转载注明地址:https://blog.youkuaiyun.com/coder_what/article/details/86101524

之前在我的博文中写了一个用递归的小数四则运算:https://blog.youkuaiyun.com/coder_what/article/details/82682366
学了数据结构之后就用栈重新写了一个整数四则运算:
准备:

char OPE[7]    ={'+','-','*','/','(',')','#'};
char OPG[7][7]={{'>','>','<','<','<','>','>'},
				{'>','>','<','<','<','>','>'},
				{'>','>','>','>','<','>','>'},
				{'>','>','>','>','<','>','>'},
				{'<','<','<','<','<','=',' '},
				{'>','>','>','>',' ','>','>'},
				{'<','<','<','<','<',' ','='},};	

上面的两个数组可以确定每个符号的优先级关系。

悄悄告诉你们,合理的运用数组可以通过循环来有效减少if分支的使用!!!
https://blog.youkuaiyun.com/coder_what/article/details/86099579 这个里面就是一个肥肠好的例子!!

应该设定两个栈,即一个是存放数组的num[],另一个是存放符号位的ope[];

然后通过下面这两个函数再结合上面的数组就可以判断每个符号的优先级了~

int In(char ch)
{
	int i;
	for(i=0;i<7;i++)
		if(ch==OPE[i])
			return i;
	if(i>=7) return -1;
}
char Precede(char ch1,char ch2)  //判断运算的优先级,即进栈还是出栈
{
	int i=In(ch1);
	int j=In(ch2);
	return OPG[i][j];
}

下面奉上香喷喷的源码!

#include<stdio.h>
#include<stdlib.h>
char OPE[7]    ={'+','-','*','/','(',')','#'};
char OPG[7][7]={{'>','>','<','<','<','>','>'},
				{'>','>','<','<','<','>','>'},
				{'>','>','>','>','<','>','>'},
				{'>','>','>','>','<','>','>'},
				{'<','<','<','<','<','=',' '},
				{'>','>','>','>',' ','>','>'},
				{'<','<','<','<','<',' ','='},};
char s[200];//输入字符串 
char ope[100];//运算符号栈 
double num[100];//数字栈 
				
int In(char );
char Precede(char ,char );
double Operate(double ,double ,char );
double StackIO();

int main()
{
	printf("please input the number you wanna operate(ending by #):");
	printf("\nthe answer is:%.3lf",StackIO());
	return 0;
 } 
int In(char ch)
{
	int i;
	for(i=0;i<7;i++)
		if(ch==OPE[i])
			return i;
	if(i>=7) return -1;
}
char Precede(char ch1,char ch2)  //判断运算的优先级,即进栈还是出栈
{
	int i=In(ch1);
	int j=In(ch2);
	return OPG[i][j];
}
double Operate(double x1,double x2,char op)
{
	switch(op)
	{
		case '+': return (x1+x2); break;
		case '-': return (x1-x2); break;//为什么不是x2-x1? 
		case '*': return (x1*x2); break;
		case '/': return (x1/x2); break;
	}
}
double StackIO()
{
	int i,top_o,top_n;//栈顶 
	double x,y;
	i=x=y=top_o=top_n=0;
	ope[top_o++]='#';
	scanf("%s",s);   //如果用getchar的话不会成功? 
	do{
		//getchar(); 
		//printf("%c %d~%2.3lf %d~%c",s[i],top_n-1,num[top_n-1],top_o-1,ope[top_o-1]);
		if(s[i]>='0'&&s[i]<='9')
		{ 
			x=0;
			while(s[i]>='0'&&s[i]<='9')
			{
				x=s[i++]-'0'+x*10;
			}
			num[top_n++]=x;
		}
		else
		{
			switch(Precede(ope[top_o-1],s[i]))
			{
				case '<':
					ope[top_o++]=s[i++]; //进栈 
					break;
				case '=':
					top_o--;//此处不用-2 ,出栈 
					i++;
					break;
				case '>':
					y=Operate(num[--top_n],num[--top_n],ope[--top_o]);
					//top_o--; 因为switch函数中已有top_o-1,所以此处不用--; 
					num[top_n++]=y;
					break;
			}
		} 
	}while(s[i-1]!='#');
	return num[top_n-1];
 } 

GET IT!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值