Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #65420 > unrolled thread
| Started by | Alla _ <modelling.data@gmail.com> |
|---|---|
| First post | 2015-07-12 09:46 -0700 |
| Last post | 2015-07-13 06:29 -0700 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.lang.c
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
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-07-12 09:46 -0700 |
| Subject | Setting accumulator with "do while" loop and inner switch statement |
| Message-ID | <21fa90b1-084e-4fa9-85eb-33dbc9ec7a47@googlegroups.com> |
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!
[toc] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-07-12 13:31 -0400 |
| Message-ID | <55A2A459.7020808@verizon.net> |
| In reply to | #65420 |
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?
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-07-13 06:35 -0700 |
| Message-ID | <f2dc3e82-42f7-46b1-a75e-644e51b12f22@googlegroups.com> |
| In reply to | #65429 |
On Sunday, July 12, 2015 at 9:31:14 PM UTC+4, James Kuyper wrote:
> 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.
>
> Sorry, it is still not clear.
for example: the case '+' is true, then the result is
temp += number;
break;
the case is closed, and given 'e' hasn't been entered, the program
gets back to the scanf statement, which is outside the switch statement.
do you say that in this case the temp value that has been computed
within case '+' is stored, and after the user enters next number and
operator (next scanf execution) the following switch statement remembers
the temp value from the previous switch round and works with it? How does
that happen technically, if the case is ended with 'break'? Sorry - just
building my understanding, it takes time (in my case :)).
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-07-13 11:54 -0400 |
| Message-ID | <55A3DF37.7080403@verizon.net> |
| In reply to | #65483 |
On 07/13/2015 09:35 AM, Alla _ wrote:
...
> Sorry, it is still not clear.
> for example: the case '+' is true, then the result is
> temp += number;
> break;
> the case is closed, and given 'e' hasn't been entered, the program
> gets back to the scanf statement, which is outside the switch statement.
>
> do you say that in this case the temp value that has been computed
> within case '+' is stored, and after the user enters next number and
> operator (next scanf execution) the following switch statement remembers
> the temp value from the previous switch round and works with it?
Correct.
> ... How does
> that happen technically, if the case is ended with 'break'?
Every object in a C program has a lifetime. During it's lifetime,
whenever the program reads the value stored in the object, the value
read will be the same as the last value stored in it (unless the
object's definition declares it to be 'volatile'). I can only make sense
of your question by assuming that you believe that the lifetime of
'temp' ends when the 'break;' statement is executed. That would only be
true if 'temp' were declared inside the switch block. You've defined
temp in the main() block. As a result, the lifetime of 'temp' ends only
when the main() block is exited. That won't happen until after the
'return 0;' statement at the end of main().
Here's a modified version of your program where your concerns would be
reasonable:
#include <stdio.h>
int main (void)
{
float number;
char operator;
printf ("Please, enter a number and an operator:\n");
do
{
scanf ("%f %c", &number, &operator);
switch (operator)
{
float temp;
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;
} // End of life time for 'temp'
}
while (operator != 'e');
return 0;
}
In this version of the program, the lifetime of 'temp' ends whenever a
'break;' is executed. Since 'temp' in uninitialized, that means that
whenever 'operator' is '+', '-', '*', '/', or 'e', the behavior of the
program is undefined.
Note: the behavior of your original program is also undefined, if the
first value of operator is one of those. You should change your code to
keep track of whether or not 'temp' has been set. If it hasn't been, any
operation that requires the previous value of 'temp' to be set should be
handled as an error.
[toc] | [prev] | [next] | [standalone]
| From | raltbos@xs4all.nl (Richard Bos) |
|---|---|
| Date | 2015-07-13 21:50 +0000 |
| Message-ID | <55a431d4.26833640@news.xs4all.nl> |
| In reply to | #65483 |
Alla _ <modelling.data@gmail.com> wrote:
> On Sunday, July 12, 2015 at 9:31:14 PM UTC+4, James Kuyper wrote:
> > 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.
> >
> Sorry, it is still not clear.
> for example: the case '+' is true, then the result is
> temp += number;
> break;
> the case is closed, and given 'e' hasn't been entered, the program
> gets back to the scanf statement, which is outside the switch statement.
>
> do you say that in this case the temp value that has been computed
> within case '+' is stored, and after the user enters next number and
> operator (next scanf execution) the following switch statement remembers
> the temp value from the previous switch round and works with it?
What makes you think that the change in value to any variable must be
forgotten after the end of a switch statement? Would you say the same
thing about a for loop? For example, in this case:
int i=0, t=0;
for (i=1; i<=10; i++) {
t+=i;
printf("Nr: %d; total: %d\n", i, t);
}
would you expect either i or t to revert to the value 0 after the loop?
If so, why? If not, why do you expect this of temp and your switch?
Richard
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-07-12 11:35 -0700 |
| Message-ID | <7dc5qad5fvfcvco15dukbjectrj8elu7af@4ax.com> |
| In reply to | #65420 |
On Sun, 12 Jul 2015 09:46:18 -0700 (PDT), Alla _
<modelling.data@gmail.com> wrote:
>int main (void)
>{
> float number, temp;
While the names of the variables do not affect the operation of the
program, it makes the program much easier to understand when you look
at it later if the names reflect the use of the variable. temp is not
a temporary value, it is the accumulator.
Consider what happens if the first input operator is not s. By
comparison, consider what happens when you first turn on any
electronic calculator.
> 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;
As part of your growing understanding of the language, you might want
to research the compound assignment operators. The equivalent
statement
temp += number;
is much easier on the eyes, especially when the target variable is a
long name or complex expression.
> printf ("= %.2f\n", temp);
> break;
>
--
Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-07-13 06:29 -0700 |
| Message-ID | <4846873e-0cfc-4e8e-8681-dfe858371df6@googlegroups.com> |
| In reply to | #65437 |
On Sunday, July 12, 2015 at 10:35:40 PM UTC+4, Barry Schwarz wrote:
> On Sun, 12 Jul 2015 09:46:18 -0700 (PDT), Alla _
> <modelling.data@gmail.com> wrote:
>
> >int main (void)
> >{
> > float number, temp;
>
> While the names of the variables do not affect the operation of the
> program, it makes the program much easier to understand when you look
> at it later if the names reflect the use of the variable. temp is not
> a temporary value, it is the accumulator.
>
> Consider what happens if the first input operator is not s. By
> comparison, consider what happens when you first turn on any
> electronic calculator.
>
> > 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;
>
> As part of your growing understanding of the language, you might want
> to research the compound assignment operators. The equivalent
> statement
> temp += number;
> is much easier on the eyes, especially when the target variable is a
> long name or complex expression.
>
> > printf ("= %.2f\n", temp);
> > break;
> >
>
> --
> Remove del for email
thank you. I agree about temp += number (and yes I already know it,
and usually use it :)). same with names: totally agree.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web