Saturday, October 13, 2012

A simple calculator




Source is given without the parentheses arithmetic operations to calculate.
There is no limit to the length of the input entered without spaces, and must

# Include <stdio.h>
# Include <stdlib.h>
# Include <conio.h>
int main () {
   / / Result value sum, to hold protests to store a value term, temporary variable x.
   / / As anti-Iran multiply or divide bundled says.
   Will / / add or subtract a complete expression of these anti-
   int sum = 0, term, x;
   / / Op is an operator. As a sop, the addition or subtraction operator
   / / Operator is not yet handle and keep. Later, after the current term
   / / Sop subtract any current anti Whatever will get me.
   char op = 0, sop = 0;
   printf ("====== converter ====== \ n");
   printf ("Please enter an expression:");
   scanf ("% d", & x);
   / / Start if not sop there yet. And the current term is defined as the number first given.
   / / Continue to multiply or divide If that's the term multiplied or divided by the value of
   / / Values ​​will be assigned.
   term = x;
   scanf ("% c", & op);
   while (op == '*' | | op == '/') {
        scanf ("% d", & x);
        if (op == '*') term * = x;
        if (op == '/') term / = x;
        scanf ("% c", & op);
   }
   / / Now came the first time the + operator or operator.
   / / Operator sop to the archive.
   / / Obtained in the first term in the sum gives its value.
   / / Obtain a value that begins on the next term was
   if (op == '+' | | op == '-') {
        sop = op;
        sum = term;
        scanf ("% d", & x);
        term = x;
        scanf ("% c", & op);
   }
 
   / / Get all remaining anti-addition or subtracting them.
   / / = Operators continue processing until you are.
   while (op! = '=') {
        / / Port gives if its value multiplied or divided continues.
        while (op == '*' | | op == '/') {
             scanf ("% d", & x);
             if (op == '*') term * = x;
             if (op == '/') term / = x;
             scanf ("% c", & op);
        }
        / / + Operator or operators to meet current term, depending on the value of the sop
        / / Add or subtract the sum to give And now read the operator to give a sop.
        / / Reads the following number gives the value of the start of the term.
        / / And read the following operators op gives
        / / If op = is large while the door to escape.
        if (op == '+' | | op == '-') {
             if (sop == '+') sum + = term;
             if (sop == '-') sum-= term;
             sop = op;
             scanf ("% d", & x);
             term = x;
             scanf ("% c", & op);
        }
   }
   / / If you did not handle the last term
   / / Yitteut sop the + or - processing allows.
   / / If sop 0 means that term as one of the first read, the diet is over.
   if (sop == '+') sum + = term;
   if (sop == '-') sum-= term;
   if (sop == 0) sum = term;
   printf ("result:% d \ n", sum);

   return 0;
}

We will understand in the same way as above, if you

No comments:

Post a Comment