G-Ternary Calculation (string simulation)

Source: Internet
Author: User

G-Ternary Calculation (string simulation)

Complete the ternary calculation.

Input

There are multiple test cases. The first line of input contains an integerTIndicating the number of test cases. For each test case:

There is a string in the form"Number1OperatorANumber2OperatorBNumber3 ". each operator will be one of {'+', '-', '*', '/', '%'}, and each number will be an integer in [1, 1000].

Output

For each test case, output the answer.

Sample Input
51 + 2 * 31 - 8 / 31 + 2 - 37 * 8 / 55 - 8 % 3
Sample Output
7-10113
Note

The calculation"A%B"Means taking the remainderADividedB, And"A/B"Means taking the quotient.

The general meaning of the question is:

Give you three real integers, and then two operators, You need to simulate the process, and then get the final answer.

At first, I thought about saving these operators to the array, and then judging the first few and then performing the corresponding process. But then I found that there would be errors and the writing was very complicated, because you have not determined the priority of symbols, the final result may be wrong.

Later, I took a look at the problem and found that the brute force enumeration method could be used. Because the data volume is not large, it is better to directly classify it.

1. oper1 is a low-level operator (that is, + or-), and oper2 is an advanced operator (that is, *,/, and %). Then, the second operator is counted before the first operator;

2. oper1 is low-level, and oper2 is also low-level, so it is better to directly add the order.

3. oper1 is advanced, and oper2 is advanced or low-level, so it is easy to directly calculate the order.

# Include
 
  
# Include
  
   
Int main () {int a, B, c, T, I, j, k, sum; char ss1 [10], ss2 [10]; scanf ("% d ", & T); while (T --) {scanf ("% d % s % d", & a, ss1, & B, ss2, & c ); sum = 0; // note that ss2 [0] = '+' or '-' cannot be directly written here, because the calculation starts from the second one, then the first one is taken into consideration. // For example, in the example 1-3 + 2, an error occurs; if (ss1 [0] = '+' | ss1 [0] = '-') & (ss2 [0] = '*' | ss2 [0] = '/' | ss2 [0] = '% ')) {if (ss2 [0] = '*') sum = B * c; else if (ss2 [0] = '/') sum = B/c; else if (ss2 [0] = '%') sum = B % c; else if (ss2 [0] = '+') sum = B + c; else if (ss2 [0] = '-') sum = B-c; if (ss1 [0] = '+') sum + =; else if (ss1 [0] = '-') sum = a-sum ;} if (ss1 [0] = '+' | ss1 [0] = '-') & (ss2 [0] = '+' | ss2 [0] = '-') {if (ss1 [0] = '+ ') sum = a + B; else if (ss1 [0] = '-') sum = a-B; if (ss2 [0] = '+ ') sum + = c; else sum-= c ;} if (ss1 [0] = '*' | ss1 [0] = '/' | ss1 [0] = '% ') & (ss2 [0] = '*' | ss2 [0] = '/' | ss2 [0] = '%' | ss2 [0] = '+' | ss2 [0] = '-')) {if (ss1 [0] = '*') sum = a * B; else if (ss1 [0] = '/') sum = a/B; else if (ss1 [0] = '%') sum = a % B; if (ss2 [0] = '*') sum = sum * c; else if (ss2 [0] = '/') sum = sum/c; else if (ss2 [0] = '%') sum = sum % c; else if (ss2 [0] = '+') sum + = c; else if (ss2 [0] = '-') sum-= c ;} printf ("% d \ n", sum );}}
  
 


This kind of question may be a little longer for the first time, but it will become more powerful after accumulating ideas! Come on ~

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.