home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
vol_100
/
154_01
/
calc.c
< prev
next >
Wrap
Text File
|
1979-12-31
|
1KB
|
75 lines
/*
calc.c: simple display accumulator for quick,
on-screen calculations.
It accepts simple commands of the form "x opr",
where "x" is a double, and "opr" is one of:
s ==> reset accumulator with number
+ ==> add number to accumulator
- ==> subtract number from accumulator
* ==> multiply accumulator by number
/ ==> divide accumulator by number (checks for 0)
q ==> end session (or ^Z)
Example: 10 s (10.0000)
5 + (15.0000)
3 / ( 3.0000)
0 e (exit)
-----
(c) Chuck Allison, 1985
-----
*/
#include <stdio.h>
main()
{
double accum = 0.0,
x;
char line[80],
opr;
int argc;
while (gets(line) != NULL)
{
argc = sscanf(line," %lf%1s",&x,&opr);
if (argc != 2)
{
/* ..invalid entry (probably backwards).. */
printf("? try again ..\n");
continue;
}
switch(opr)
{
case '+':
accum += x;
break;
case '-':
accum -= x;
break;
case '*':
accum *= x;
break;
case '/':
if (x != 0)
accum /= x;
else
printf("? division by 0: ");
break;
case 's':
case 'S':
accum = x;
break;
case 'q':
case 'Q':
exit(0);
default :
printf("? invalid operator: ");
}
printf("%11.4f %c\n =%11.4f\n",x,opr,accum);
}
}