home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DP Tool Club 31
/
CDASC_31_1996_juillet_aout.iso
/
internet
/
wsplug31.zip
/
CALC.C
< prev
next >
Wrap
Text File
|
1996-02-06
|
992b
|
43 lines
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
main(argc,argv)
int argc;
char **argv;
{ double v1,v2,op ;
int oper ;
if (argc < 4)
{printf("calc operand1 (+,-,*,/) operand2\n");return(0);}
if (sscanf(argv[1],"%lf",&v1) <= 0)
{printf("Error, first field must be a number\n");return(0);}
if (strcmp(argv[2],"+")==0) oper=0 ;
else if (strcmp(argv[2],"-")==0) oper=1 ;
else if (strcmp(argv[2],"*")==0) oper=2 ;
else if (strcmp(argv[2],"/")==0) oper=3 ;
else
{printf("Error, second field must be +,-,*,/\n");return(0);}
if (sscanf(argv[3],"%lf",&v2) <= 0)
{printf("Error, third field must be a number\n");return(0);}
if ((oper==3) && (v2==(double)(0)))
{printf("Error, division by zero\n");return(0);}
if (oper==0) op=v1+v2 ;
else if (oper==1) op=v1-v2 ;
else if (oper==2) op=v1*v2 ;
else op=v1/v2 ;
printf("%lf %s %lf = %lf\n",v1,argv[2],v2,op);
return(0);
}