Change the suffix of an infix

Source: Internet
Author: User
Change the suffix of an infix

Time Limit: 1000 MS | memory limit: 65535 KB

Difficulty: 3

Description

People's Daily habit is to write an arithmetic expression as an infix, but it is more "used" for machines, general Data Structure books have relevant content for reference. I will not go into details here. Now your task is to change the infix into a suffix.

Input

Enter an integer N in the first line. There are N groups of test data (n <10 ).
Each group of test data has only one row. It is a string of no more than 1000 characters, indicating the infix of this formula. Each formula ends with "=. This expression only contains the +-*/and parentheses. Parentheses can be nested. Data ensures that no negative numbers are displayed in the input operations.
Data guarantee that the divisor is not 0

Output

Each group outputs the suffix corresponding to the infix in this group. Adjacent operand operators are required to be separated by spaces.

Sample Input

2

1.000 + 2/4 =

(1 + 2) * 5 + 1)/4 =

Sample output

1.000 2 4/+ =

1 2 + 5*1 + 4/=

Question: Add spaces to the specified position in the array postexp.

Program code:

# Include <stdio. h>

# Include <stdlib. h>

 

# Define Max size 1000

# Define maxop 7

 

Struct // set the operator priority

{

Char ch; // Operator

Int PRI; // priority

} Lpri [] = {'=', 0}, {'(', 1}, {'*', 5}, {'/', 5 }, {'+', 3}, {'-', 3}, {')', 6 }},

Rpri [] = {'=', 0}, {'(', 6}, {'*', 4}, {'/', 4 }, {'+', 2}, {'-', 2}, {')', 1 }};

Int leftpri (char OP) // evaluate the op priority of the Left Operator

{

Int I;

For (I = 0; I <maxop; I ++)

If (lpri [I]. CH = OP)

Return lpri [I]. PRI;

}

 

Int rightpri (char OP) // evaluate the op priority of the Right Operator

{

Int I;

For (I = 0; I <maxop; I ++)

If (rpri [I]. CH = OP)

Return rpri [I]. PRI;

}

 

Int inop (char ch) // determines if ch is an operator

{

If (CH = '(' | CH = ') '| CH =' + '| CH ='-'| CH =' * '| CH = '/')

Return 1;

Else

Return 0;

}

 

Int precede (char OP1, char OP2) // comparison result of OP1 and OP2 operator priority

{

If (leftpri (OP1) = rightpri (OP2 ))

Return 0;

Else if (leftpri (OP1) <rightpri (OP2 ))

Return-1;

Else

Return 1;

}

 

Void trans (char * exp, char postexp [])

// Convert the arithmetic expression exp to the suffix expression postexp

{

Struct

{

Char data [maxsize]; // storage Operator

Int top; // Stack pointer

} Op; // defines the operator Stack

Int I = 0; // I is the subscript of postexp

Op. Top =-1;

Op. Top ++; // Add '=' to the stack

Op. Data [op. Top] = ';

While (* exp! = '\ 0') // loop when the EXP expression is not completely scanned

{

If (* exp = ')

Break;

If (! Inop (* exp) // number

{

While (* exp> = '0' & * exp <= '9' | * exp = '.') // determines it as a number.

{

Postexp [I ++] = * exp;

Exp ++;

}

// Postexp [I ++] = '#'; // use # to mark the end of a numeric string

Postexp [I ++] = '';

}

Else // Operator

Switch (precede (op. Data [op. Top], * exp ))

{

Case-1: // low priority of the stack top operator: Stack entry

Op. Top ++;

Op. Data [op. Top] = * exp;

Exp ++; // Continue scanning other characters

Break;

Case 0: // only parentheses meet this situation

Op. Top --; // move (back to stack

Exp ++; // Continue scanning other characters

Break;

Case 1: // return the stack and output it to postexp

Postexp [I ++] = op. Data [op. Top];

Postexp [I ++] = '';

Op. Top --;

Break;

}

} // While (* exp! = '\ 0 ')

While (op. Data [op. Top]! = ')

// At this time, the exp scan is complete and the stack is pushed back to '= '.

{

Postexp [I ++] = op. Data [op. Top];

Postexp [I ++] = '';

Op. Top --;

}

Postexp [I ++] = '; // Add an end ID to the postexp expression

Postexp [I ++] = '\ 0 ';

}

 

Int main ()

{

Int N;

Scanf ("% d", & N );

Getchar ();

While (n --)

{

Char exp [maxsize];

Scanf ("% s", exp );

Char postexp [maxsize];

Trans (exp, postexp );

Printf ("% s \ n", postexp );

}

System ("pause ");

Return 0;

}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.