home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume2 / mpg / fpdivide.c < prev    next >
C/C++ Source or Header  |  1991-08-07  |  2KB  |  98 lines

  1. /* fpdivide.c - floating point math program
  2.  *
  3.  * The program's action depends on its name:
  4.  *
  5.  * fpdivide        divide two floating point numbers
  6.  * fpmultiply    multiply two floating point numbers
  7.  * fpadd        add two floating point numbers
  8.  * fpsubtract    subtract two floating point numbers
  9.  *
  10.  * In any case, the program takes two arguments and prints its result
  11.  * to the standard output.
  12.  *
  13.  * The program was written to add floating point arithmetic to shell programs.
  14.  *
  15.  * Author: Wolf N. Paul, ihnp4!killer!dcs
  16.  * 
  17.  * Released into the Public Domain, Jan 18, 1988
  18.  */
  19.  
  20. #include <stdio.h>
  21.  
  22.  
  23. char *basename(s)
  24. char *s;
  25. {
  26.     char *base;
  27.     char *strrchr();
  28.  
  29.     if ( ( base = strrchr(s, '/')) != NULL)
  30.         return(&base[1]);
  31.     return(s);
  32. }
  33.  
  34.  
  35. main(argc, argv)
  36. int argc;
  37. char **argv;
  38. {
  39.     int division, multiplication, addition, subtraction;
  40.     char *progname;
  41.     char *basename();
  42.     float dividend, divisor, quotient;
  43.     float factor1, factor2, product;
  44.     float num1, num2, sum;
  45.     float atof();
  46.  
  47.     progname = basename(argv[0]);
  48.  
  49.     if ( strcmp(progname, "fpdivide") == 0 )
  50.         division = 1;
  51.     else if ( strcmp(progname, "fpmultiply") == 0 )
  52.         multiplication = 1;
  53.     else if ( strcmp(progname, "fpadd") == 0 )
  54.         addition = 1;
  55.     else if ( strcmp(progname, "fpsubtract") == 0)
  56.         subtraction = 1;
  57.  
  58.     if ( argc != 3 )
  59.     {
  60.         if ( division )
  61.             fprintf(stderr,"Usage: %s dividend divisor\n\n", progname);
  62.         else if ( multiplication )
  63.             fprintf(stderr,"Usage: %s factor1 factor2\n\n", progname);
  64.         else if ( addition || subtraction)
  65.             fprintf(stderr,"Usage: %s num1 num1\n\n", progname);
  66.         exit(-1);
  67.     }
  68.  
  69.     if ( division )
  70.     {
  71.         dividend = atof(argv[1]);
  72.         divisor  = atof(argv[2]);
  73.         if ( divisor <= 0.0 )
  74.         {
  75.             fprintf(stderr,"%s: divisor is zero or less.\n",
  76.                     argv[0]);
  77.             exit(-2);
  78.         }
  79.         quotient = dividend / divisor;
  80.         fprintf(stdout,"%.2f\n", quotient);
  81.     }
  82.     else if ( multiplication )
  83.     {
  84.         factor1 = atof(argv[1]);
  85.         factor2 = atof(argv[2]);
  86.         product = factor1 * factor2;
  87.         fprintf(stdout,"%.2f\n", product);
  88.     }
  89.     else if ( addition || subtraction )
  90.     {
  91.         num1 = atof(argv[1]);
  92.         num2 = atof(argv[2]);
  93.         if ( addition ) sum = num1 + num2;
  94.         else if ( subtraction ) sum = num1 - num2;
  95.         fprintf(stdout,"%.2f\n", sum);
  96.     }
  97. }
  98.