5-41 Simple Calculator (20 points)

Source: Internet
Author: User
Tags arithmetic printf
5-41 Simple Calculator (20 points)

Simulates the work of a simple arithmetic device. Assuming that the calculator can only perform subtraction operations, the operands and results are integers, and the four operators have the same precedence, calculated in left-to-right order. Input Format:

The input gives a arithmetic calculation in a row, with no spaces and at least one operand. An equal sign "=" indicates the end of the input. output Format:

Outputs the result of a calculation in a row, or if the division denominator is 0 or has an illegal operator, the error message "error" is output. Input Sample:

1+2*10-10/2=
Sample output:
10
//
/* Subtraction operation of the analog operator, however, here, the
arithmetic rule no longer applies, calculated in the order of number and operation symbols,
such as, 1+2*10-10/2=10
*/
#include "stdio.h"
int main ()
{
    int value1,value2;
    int sum=0;
    char ch;
    scanf ("%d", &value1);
    while ((Ch=getchar ()) = ' = ')
    {
        scanf ("%d", &value2);
        if (ch== '/' &&value2==0) {
                printf ("error\n");
                return 0;

        }
        Switch (CH)
        {case
            ' + ': sum=value1+value2;break;
            Case '-': sum=value1-value2;break;
            Case ' * ': sum=value1*value2;break;
            Case '/': Sum=value1/value2;break;
            default:printf ("error\n"); return 0;

        }
        value1=sum;


    }
    printf ("%d\n", value1);
    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.