(Your)((Term)((Project)))

Description

You have typed the report of your term project in your personal computer. There are several one line arithmetic expressions in your report. There is no redundant parentheses in the expressions (omitting a pair of redundant matching parentheses does not change the value of the expression). In your absence, your little brother inserts some redundant matching parentheses in the expressions of your report. Assume that the expressions remain syntactically correct and evaluate to their original value (the value before inserting redundant parentheses). To restore your report to its original form, you are to write a program to omit all redundant parentheses. 
To make life easier, consider the following simplifying assumptions: 
  1. The input file contains a number of expressions, each in one separate line. 
  2. Variables in the expressions are only single uppercase letters. 
  3. Operators in the expressions are only binary '+' and binary '-'.

Note that the only transformation allowed is omission of redundant parentheses, and no algebraic simplification is allowed.

Input

The input consists of several test cases. The first line of the file contains a single number M, which is the number of test cases (1 <= M <= 10). Each of the following M lines, is exactly one correct expression. There may be arbitrarily space characters in each line. The length of each line (including spaces) is at most 255 characters.

Output

The output for each test case is the same expression without redundant parentheses. Notice that the order of operands in an input expression and its corresponding output should be the same. Each output expression must be on a separate line. Space characters should be omitted in the output expressions.

Sample Input

3
(A-B + C) - (A+(B - C)) - (C-(D- E) )
  ((A)-( (B)))
A-(B+C)

Sample Output

A-B+C-(A+B-C)-(C-(D-E))
A-B
A-(B+C)

【题意】把给出表达式改为规范的表达式

【思路】三种情况不用加括号

1.整个表达式不用用括号括起来

2.括号内没有运算不用括起来

3。括号前是加号不用括起来

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=260;
int vis[N],kk[N];
char a[N],str[N];
int main()
{
    int t;
    scanf("%d",&t);
    getchar();//吸收回车键
    while(t--)
    {
        gets(a);//读入整行,以
或EOF为结束
        //scanf("%s",a);
        memset(vis,0,sizeof(vis));
        memset(kk,-1,sizeof(kk));
        int i,j;
        for(i=0,j=0;a[i];i++)
            if(a[i]!=' ') str[j++]=a[i];
        str[j]=0;
        for(i=0;str[i];i++)
        {
            if(str[i]==')'&kk[i]==-1)
            {
                for(j=i-1;j>=0;j--)
                {
                    if(str[j]=='('&&vis[j]==0)
                    {
                        kk[i]=j;
                        vis[j]=1;break;
                    }
                }
               // vis[i]=1;
            }

        }
        int flag,del[N];
        memset(del,0,sizeof(del));

        for(i=0;str[i];i++)
        {
            if(!del[i]&&str[i]==')')
            {
                flag=0;
                for(j=i-1;j>kk[i];j--)

                    if(str[j]=='+'||str[j]=='-')
                    {
                        flag=1;break;
                    }
                    if(kk[i]==0||str[kk[i]-1]=='-'&&flag==0||str[kk[i]-1]!='-')
                        del[kk[i]]=1,del[i]=1;

            }
        }
        for( i=0;str[i];i++)
        {
            if(del[i]) continue;
            printf("%c",str[i]);
        }
        printf("
");

    }
    return 0;
}
原文地址:https://www.cnblogs.com/iwantstrong/p/5868430.html