computations

Problem Description

ACM has designed its own computer system that can make some field computations and produce results in the form of mathematic expressions. Unfortunately, by generating the expression in several steps, there are always some unneeded parentheses inside the expression. Your task is to take these partial results and make them "nice" by removing all unnecessary parentheses.

Input

There is a single positive integer T on the first line of input. It stands for the number of expressions to follow. Each expression consists of a single line containing only lowercase letters, operators (+, -, *, /) and parentheses (( and )). The letters are variables that can have any value, operators and parentheses have their usual meaning. Multiplication and division have higher priority then subtraction and addition. All operations with the same priority are computed from left to right (operators are left-associative). There are no spaces inside the expressions. No input line contains more than 250 characters.

Output

Print a single line for every expression. The line must contain the same expression with unneeded parentheses removed. You must remove as many parentheses as possible without changing the semantics of the expression. The semantics of the expression is considered the same if and only if any of the following conditions hold:
The ordering of operations remains the same. That means "(a+b)+c" is the same as "a+b+c", and "a+(b/c)" is the same as "a+b/c".
The order of some operations is swapped but the result remains unchanged with respect to the addition and multiplication associativity. That means "a+(b+c)" and "(a+b)+c" are the same. We can also combine addition with subtraction and multiplication with division, if the subtraction or division is the second operation. For example, "a+(b-c)" is the same as "a+b-c".
You cannot use any other laws, namely you cannot swap left and right operands and you cannot replace "a-(b-c)" with "a-b+c".

Sample Input

8
(a+(b*c))
((a+b)*c)
(a*(b*c))
(a*(b/c)*d)
((a/(b/c))/d)
((x))
(a+b)-(c-d)-(e/f)
(a+b)+(c-d)-(e+f)

Sample Output

a+b*c
(a+b)*c
a*b*c
a*b/c*d
a/(b/c)/d
x
a+b-(c-d)-e/f
a+b+c-d-(e+f)
/*
      解题报告:先把中缀表达式转化为后缀表达式,在还原,需要加括号的加括号。
*/
//标程:
#include<stdio.h>
#include<string>
#include<iostream>
#include<stack>
using namespace std;
char s[300],a[300];
struct ss
{
	string s;
	int yxj;
};
int yx(char ch)
{
	if(ch=='+' || ch=='-') return 1;
	if(ch=='*' || ch=='/') return 2;
	if(ch=='(')   return 0;
}
void f(char* x)
{
    int i;
	ss temp,ans,bstr;       
	stack<ss> st;
	for(i=0;x[i]!='\0';i++)
	{
		if(x[i]>='a' && x[i]<='z')
		{
			temp.s=x[i], temp.yxj=100;
			st.push(temp);
		}
		else 
		{
			bstr=st.top(), st.pop();
			ans=st.top(), st.pop();
			temp.s=x[i];
			if(yx(x[i])>ans.yxj)
			{
                ans.s="("+ans.s+")";
				ans.yxj=yx(x[i]);
			}
			if(yx(x[i])>bstr.yxj || (yx(x[i])==bstr.yxj && x[i]=='-') || (yx(x[i])==bstr.yxj && x[i]=='/'))
			{
				bstr.s="("+bstr.s+")";
				bstr.yxj=yx(x[i]);
			}
			temp.s=ans.s+temp.s+bstr.s;
			int d=200;
            if(d>ans.yxj) d=ans.yxj;
			if(d>bstr.yxj) d=ans.yxj;
			if(d>yx(x[i]))  d=yx(x[i]);
			temp.yxj=d;
			st.push(temp);
		}
	}
	cout<<st.top().s<<endl;
}
int main()
{
	//freopen("a.txt","r",stdin);
    int t;
	scanf("%d\n",&t);
	while(t--)
	{
		int i,j,k=0;
        stack<char> sc;
		gets(s);
		for(i=0;s[i]!='\0';i++)
		{
			if(s[i]>='a' && s[i]<='z') a[k++]=s[i];
            else 
			{
				if(s[i]=='(') { sc.push('(');  continue;}
				if(s[i]==')') 
				{
					while(!sc.empty() && sc.top()!='(')
					{
						a[k++]=sc.top();
						sc.pop();
					}
					sc.pop();
					continue;
				}
				if(sc.empty()) sc.push(s[i]);
				else 
				{
					while(!sc.empty() && yx(sc.top())>=yx(s[i]))
						a[k++]=sc.top(),  sc.pop();
					sc.push(s[i]);
				}
			}
		}
		while(!sc.empty()) a[k++]=sc.top(), sc.pop();
		a[k]='\0';
		f(a);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值