Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #65420

Setting accumulator with "do while" loop and inner switch statement

Newsgroups comp.lang.c
Date 2015-07-12 09:46 -0700
Message-ID <21fa90b1-084e-4fa9-85eb-33dbc9ec7a47@googlegroups.com> (permalink)
Subject Setting accumulator with "do while" loop and inner switch statement
From Alla _ <modelling.data@gmail.com>

Show all headers | View raw


Hello!

One of the tasks in the text book is:

Write a program that acts as a simple "printing" calculator. 
The program should allow the user to type in expressions of the form
 number operator

The following operators should be recognized by the program:
+ - / * s e
The operator s tells the program to set the "accumulator" 
to the typed-in number. 
The e operator tells the program that execution is to end. 
The arithmetic operations are performed on the contents 
of the accumulator with the number that was keyed in acting 
as the second operand. 


Here is the program:

#include <stdio.h>

int main (void)
{
    float number, temp;
    char  operator;
    
    printf ("Please, enter a number and an operator:\n");
    
    do
    {
        scanf  ("%f   %c", &number, &operator);
        
        switch (operator)
        {
            case 's':
                temp = number;
                printf ("= %f\n", temp);
                break;
            
            case '+':
                temp = temp + number;
                printf ("= %.2f\n", temp);
                break;
                
            case '-':
                temp = temp - number;
                printf ("= %.2f\n", temp);
                break;
                
            case '*':
                temp = temp * number;
                printf ("= %.2f\n", temp);
                break;
                
            case '/':
                if (number == 0)
                    printf ("Division by zero\n");
                else
                    temp = temp / number;
                    printf ("= %.2f\n", temp);
                break;
                
            case 'e':
                
                printf ("= %.2f\nThe end of calculations\n", temp);
                break;
        }
    }
    while (operator != 'e');
    
    return 0;
}

It works fine. But I still can't understand how the switch 
statement works here.

For example, the user enters 9 -, the case with operator '-' 
is tested and the result if = -9.0000

Next the user types in (this happens until operator is not
'e') 8 +, and this is the case '+'; the result is = -1.0000

and so on. 

It is not clear to me how the switch statement gets the previous 
result from any of the previous case and increments it by
the newly typed-in operator and the number? 

I thought that when one of the cases has been performed, the switch 
statement breaks; and starts again after the new number is entered.
 
Is my question clear? I am not truly sure I managed to express it
correctly. As in my example few lines above, I don't see the mechanics
of calculator, how does it work?

Thank you!

Back to comp.lang.c | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Setting accumulator with "do while" loop and inner switch statement Alla _ <modelling.data@gmail.com> - 2015-07-12 09:46 -0700
  Re: Setting accumulator with "do while" loop and inner switch statement James Kuyper <jameskuyper@verizon.net> - 2015-07-12 13:31 -0400
    Re: Setting accumulator with "do while" loop and inner switch statement Alla _ <modelling.data@gmail.com> - 2015-07-13 06:35 -0700
      Re: Setting accumulator with "do while" loop and inner switch statement James Kuyper <jameskuyper@verizon.net> - 2015-07-13 11:54 -0400
      Re: Setting accumulator with "do while" loop and inner switch statement raltbos@xs4all.nl (Richard Bos) - 2015-07-13 21:50 +0000
  Re: Setting accumulator with "do while" loop and inner switch statement Barry Schwarz <schwarzb@dqel.com> - 2015-07-12 11:35 -0700
    Re: Setting accumulator with "do while" loop and inner switch statement Alla _ <modelling.data@gmail.com> - 2015-07-13 06:29 -0700

csiph-web