PAT 06-1 simple calculator, pat06-1 Calculator

Source: Internet
Author: User

PAT 06-1 simple calculator, pat06-1 Calculator

I'm sorry if you want to see what a simple calculator can do. This is not something you want, here, the question is set to "only allow addition, subtraction, multiplication, division", "All integers", "same priority", and "from left to right ". This question comes from PAT (http://www.patest.cn/contests/mooc-c/06-1). I think a garden friend wrote it with if... else. Well, I still have some value. The question requirements and code implementation are as follows:

1 / *
2 Name:
Copyright:
4 the Author:
5 the Date: 31/03/15 affliction
6 the Description:
Simulate the work of a simple arithmetic unit. Suppose the calculator can only add, subtract, multiply and divide, the operands and the results are integers, and the four operators have the same precedence, in order from left to right.
8
9 input format:
10
The input gives a four-way formula on one line with no Spaces and at least one operand. When the equal sign "=" indicates the end of input.
12
13 output format:
14
15 output the result of the calculation on one line, or an ERROR message "ERROR" if the division denominator is 0 or there is an illegal operator.
16
17 input sample:
1 + 2 * 10-10/2 = 18
19 output sample:
20 and 10
21 * /
22
23 # include < stdio, h >
24 # include < stdbool. H >
25
26 int main ()
{27
Int n, TMP;
29 char ch;
30 bool flag;
31
32 flag = true;
33 the scanf (" % d ", & n);
34 while((ch = getchar())! = '=')
{35
36 / / printf (" % c \ n ", ch); / / for the debug
37 the scanf (" % d ", & TMP);
38 / / printf (" % d \ n ", TMP); / / for the debug
39 if(TMP == 0 && ch == '/')
40 flag = false;
41 the else
{42
43 the switch (ch)
44 {
45 case '+' :
46 n + = TMP;
47 break;
48 case '-' :
49 n - = TMP;
50 break;
51 cases' * ':
52 n * = TMP;
53 break;
54 case '/' :
55 = n/TMP;
56 break;
57 the default:
58 flag = false;
59}
60}
61
62 the if (! Flag)
63 break;
64}
65
66 the if (flag)
67 printf (" % d \ n ", n);
68 the else
69 printf (" ERROR "\ n");
70
71 return 0;
72} 

 


Related Article

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.