home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 31 / CDASC_31_1996_juillet_aout.iso / internet / wsplug31.zip / CALC.C < prev    next >
Text File  |  1996-02-06  |  992b  |  43 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. main(argc,argv)
  6. int argc;
  7. char **argv;
  8. { double v1,v2,op ;
  9.   int oper ;
  10.  
  11.  
  12.   if (argc < 4)
  13.   {printf("calc operand1 (+,-,*,/) operand2\n");return(0);}
  14.  
  15.   if (sscanf(argv[1],"%lf",&v1) <= 0)
  16.   {printf("Error, first field must be a number\n");return(0);}
  17.  
  18.   if      (strcmp(argv[2],"+")==0) oper=0 ;
  19.   else if (strcmp(argv[2],"-")==0) oper=1 ;
  20.   else if (strcmp(argv[2],"*")==0) oper=2 ;
  21.   else if (strcmp(argv[2],"/")==0) oper=3 ;
  22.   else
  23.   {printf("Error, second field must be +,-,*,/\n");return(0);}
  24.  
  25.   if (sscanf(argv[3],"%lf",&v2) <= 0)
  26.   {printf("Error, third field must be a number\n");return(0);}
  27.  
  28.   if ((oper==3) && (v2==(double)(0)))
  29.   {printf("Error, division by zero\n");return(0);}
  30.  
  31.  
  32.   if       (oper==0) op=v1+v2 ;
  33.   else if  (oper==1) op=v1-v2 ;
  34.   else if  (oper==2) op=v1*v2 ;
  35.   else               op=v1/v2 ;
  36.  
  37.   printf("%lf %s %lf = %lf\n",v1,argv[2],v2,op);
  38.  
  39.   return(0);
  40.  
  41. }
  42.  
  43.