home *** CD-ROM | disk | FTP | other *** search
- /* fpdivide.c - floating point math program
- *
- * The program's action depends on its name:
- *
- * fpdivide divide two floating point numbers
- * fpmultiply multiply two floating point numbers
- * fpadd add two floating point numbers
- * fpsubtract subtract two floating point numbers
- *
- * In any case, the program takes two arguments and prints its result
- * to the standard output.
- *
- * The program was written to add floating point arithmetic to shell programs.
- *
- * Author: Wolf N. Paul, ihnp4!killer!dcs
- *
- * Released into the Public Domain, Jan 18, 1988
- */
-
- #include <stdio.h>
-
-
- char *basename(s)
- char *s;
- {
- char *base;
- char *strrchr();
-
- if ( ( base = strrchr(s, '/')) != NULL)
- return(&base[1]);
- return(s);
- }
-
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- int division, multiplication, addition, subtraction;
- char *progname;
- char *basename();
- float dividend, divisor, quotient;
- float factor1, factor2, product;
- float num1, num2, sum;
- float atof();
-
- progname = basename(argv[0]);
-
- if ( strcmp(progname, "fpdivide") == 0 )
- division = 1;
- else if ( strcmp(progname, "fpmultiply") == 0 )
- multiplication = 1;
- else if ( strcmp(progname, "fpadd") == 0 )
- addition = 1;
- else if ( strcmp(progname, "fpsubtract") == 0)
- subtraction = 1;
-
- if ( argc != 3 )
- {
- if ( division )
- fprintf(stderr,"Usage: %s dividend divisor\n\n", progname);
- else if ( multiplication )
- fprintf(stderr,"Usage: %s factor1 factor2\n\n", progname);
- else if ( addition || subtraction)
- fprintf(stderr,"Usage: %s num1 num1\n\n", progname);
- exit(-1);
- }
-
- if ( division )
- {
- dividend = atof(argv[1]);
- divisor = atof(argv[2]);
- if ( divisor <= 0.0 )
- {
- fprintf(stderr,"%s: divisor is zero or less.\n",
- argv[0]);
- exit(-2);
- }
- quotient = dividend / divisor;
- fprintf(stdout,"%.2f\n", quotient);
- }
- else if ( multiplication )
- {
- factor1 = atof(argv[1]);
- factor2 = atof(argv[2]);
- product = factor1 * factor2;
- fprintf(stdout,"%.2f\n", product);
- }
- else if ( addition || subtraction )
- {
- num1 = atof(argv[1]);
- num2 = atof(argv[2]);
- if ( addition ) sum = num1 + num2;
- else if ( subtraction ) sum = num1 - num2;
- fprintf(stdout,"%.2f\n", sum);
- }
- }
-