转化逆波兰式为常规表达式

题目来自《程序设计导引及在线实践》9.4思考题 转化逆波兰式为正常的表达式

#include <stdio.h>
#include <string.h>

//输入样例:
//* + 11.0 12.0 + 24.0 35.0

//输出样例
//((11.0+12.0)*(24.0+35.0))

char* exp2()
{
	char a[100];
	char buff1[256];
	char buff2[256];
	char buff_sum[256];

	scanf("%s",a);
	switch (a[0])
	{
	case '+':
		strcpy(buff1,exp2());
		strcpy(buff2,exp2());
		sprintf(buff_sum,"(%s+%s)",buff1,buff2);
		return buff_sum;
		
	case '-': 
		strcpy(buff1,exp2());
		strcpy(buff2,exp2());
		sprintf(buff_sum,"(%s-%s)",buff1,buff2);
		return buff_sum;
		
	case '*':
		strcpy(buff1,exp2());
		strcpy(buff2,exp2());
		sprintf(buff_sum,"(%s*%s)",buff1,buff2);
		return buff_sum;
		
	case '/':
		strcpy(buff1,exp2());
		strcpy(buff2,exp2());
		sprintf(buff_sum,"(%s/%s)",buff1,buff2);
		return buff_sum;
		
	default:
		return (a);
	}
}

int main(int argc, char* argv[])
{
	char res[256];
	strcpy(res,exp2());
	printf("%s",res);
	return 0;
}


 

你可能感兴趣的:(转化逆波兰式为常规表达式)