Simple calculator (20)

Source: Internet
Author: User

Simulate the work of the simple scanner. Assume that the calculator can only perform addition, subtraction, multiplication, division operations. The calculation numbers and results are integers. The four operators have the same priority and are calculated from left to right.

Input Format:

The input provides a four-digit arithmetic formula in a row with no space and at least one operand. "=" Indicates that the input is complete.

Output Format:

Output the formula calculation result in a row, or if the Division denominator is 0 or there is an illegal operator, the output error message is "error ".

Input example:

1+2*10-10/2=

Output example:

10

# Include <stdio. h>

Int main ()
{
Int X, flag = 0;
Int result;
Char CH = '1 ';
Scanf ("% d", & result );

While (Ch! = '){
Scanf ("% C", & Ch );
If (CH = ')
Break;
Scanf ("% d", & X );
If (CH = '+ '){
Result + = X;
}
Else if (CH = '-'){
Result-= X;
}
Else if (CH = '*'){
Result * = X;
}
Else if (CH = '/'){
If (X! = 0 ){
Result/= X;
}
Else
Flag = 1;
}
Else
Flag = 1;
}
If (FLAG ){
Printf ("error ");
}
Else
Printf ("% d \ n", result );

Return 0;
}

 

Wood program writing http://www.codeforu.net

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.