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


Groups > comp.lang.c > #65429

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

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Setting accumulator with "do while" loop and inner switch statement
Date 2015-07-12 13:31 -0400
Organization Self
Message-ID <55A2A459.7020808@verizon.net> (permalink)
References <21fa90b1-084e-4fa9-85eb-33dbc9ec7a47@googlegroups.com>

Show all headers | View raw


On 07/12/2015 12:46 PM, Alla _ wrote:
> 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);

That's bad form: like all I/O functions, scanf() can fail. It will fail
if the user types something that doesn't look like a floating point
number followed by an arbitrary character, and also if there's any kind
of hardware problems with the input. You should check:

	if(scanf("%f %c", &number, &operator)!=2)
        {
	    // error handling
        }

>         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?

The result is stored in the variable named temp. During the next pass
through the loop, that value is still available to be used.


> I thought that when one of the cases has been performed, the switch 
> statement breaks; and starts again after the new number is entered.

Execution of the "break;" statement takes the program to the line after
the end of the switch() statement. However, that has no affect on the
values of any variables defined outside of the switch statement.

> 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?

Back to comp.lang.c | Previous | Next — Previous in thread | 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