A simple command line calculator

User Documentation | System Documentation | C code

User Documentation

Name:
mycalc

Purpose:
This program accepts integers and peforms on of four user selected math operations. All information is supplied on a single command line. The program accepts command line arguments and acts upon them. They user can choose the following math operations, add, sub (subtract), mult (multiply), div(divide). The user can specify an output file, if none is specified the output goes to the screen and to the file stdout.dat.

Usage:
The command line is entered as follows: mycalc -c operation [-o datafile.dat] peration can be add, sub, mult, div. -o is an option to create your own data file, substituting any name for datafile. some smple calls would be: mycalc -c add 4 5 6 7 8 12 54 415 -o out1.dat mycalc -c sub 100 12 45 1 5 0 -o out2.dat mycalc -c add 45 66 45 4

Format of Inputs:
Only command line inputs are allowed. All operators must be preceded by -c (e.g. -c add). Any user defined out put file must be proceeded by -o. Aside from the constraints above, order does not matter, mycalc -c add 4 5 6 7 8 12 54 415 -o out1.dat will produce the same result as: mycalc -o out1.dat 4 5 6 7 8 12 54 415 -c add

Sample Inputs:
These inputs are acceptable: mycalc -c add 4 5 6 7 8 12 54 415 -o out1.dat mycalc -c sub 100 12 45 1 5 0 -o out2.dat mycalc -c div 50 20 54 6 -o out3.dat mycalc -c mult 20 45 654 50 -o out4.dat mycalc 45 66 45 4 -c add The following will produce an error: mycalc add 45 66 22 -o out5.dat

How to Execute
All input is on a single command line, include program name: mycalc -c add 4 5 6 7 8 12 54 415 -o out1.dat The user then hits return.

Form of Outputs:
Output will be in a data file, or if the user has not specified a file on screen and in the file stdout.dat

Sample Outputs:

mycalc -c add 4 5 6 7 8 12 54 415 -o out1.dat

produced the file out1.dat:

4

5

6

7

8

12

54

415

add

511.000000

 

mycalc -c add 45 66 45 4

produced the file stdout.dat:

45

66

45

4

add

160.000000

mycalc add 45 66 22 -o out5.dat

produce the file out5.dat:

please use -c add, sub div, mult

unknown operator,

please use mult, div, add, sub

unknown operator,

please use mult, div, add, sub

45

66

22

-3103638501405769364983069581032085392046081879114122713977917592292023390220930 09667005059218909110261468235925317357361451794494523270037366226203597133125568 4466079126976425732024211952956679595493979391033580972353982439394115584.000000

mycalc 22 4 5 4 6 -c add -o

pruduced a core dump(no out put file specified after -0)

Author: John Lynch

Class: CS 342-60

Known Problems: Problems the user might expect are mostly from failing to put -c before an operator, and failing to put an output file after -o.

Back to top

System Documentation

Purpose:
The program serves as a command line calculator

Author:
John Lynch

Class:
CS 342-60

Known Problems:
Problems the user might expect are mostly from failing to put -c before an operator, and failing to put an output file after -o.

Development Environment:
The code was written in BB Edit on the MacOS. It was test compiled in softwindows using Borland C. It was final compiled using cc on the Sun workstation Enterprise.

Target Environment:
Same

Design Documentation:
Design method was stepwise refinement with some modularity (main function, operator function)

How to Build:
To compile on UNIX: mycalc -o (executable name) mycalc.c

Back to top
C Code
/****************************************************************************************/
/*                                                                                      */
/* Program Name: mycalc                                                                 */
/* Usage:                                                                               */
/* Purpose: reads integers and a specified operation (add,mult, sub, div) from the      */
/*          command line. Computes answer,  prints inputs, specified operator, and      */
/*          answer.                                                                     */
/* Author: John Lynch                                                                   */
/* comments: If output file has not specified been output file will be stdout.dat and   */
/* 	         to screen                                                                  */
/****************************************************************************************/



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

/* function prototypes */
/* function prototypes */
/* Preconditions: argc contains number of arguments to program.                         */
/*                argv s an array of size argc+1 pointers to arguments.                 */
/*                Last pointer is NULL.                                                 */
/*                max_filename contains maximum size of storage for                     */
/*                filename strings input_filename and output_filename.                  */
/* Postconditions: output_file contain C strings                                        */
/*                 containing appropriate filenames. If no filename given               */
/*                 as argument, defaults are used.                                      */
/*                 function "operation"   performs the user selected operation on       */
/*                 values given to it.                                                  */


int main(int argc, char *argv[]){

	int i=1, flag=1, first=0;
	char out[100];
	char operation[6];
	void operator(double *, int, char *);/*function to perform math operation*/
	double result=0;
	FILE *out_file;

	/*this while loop will walk through though array to find input, output files*/
	/*and which operation is to be used (add, mult, div, sub)*/

	out[0]='\0';
	operation[0]='\0';
	while(iargc){
		if(isdigit(argv[i][0]) && flag){/*checks for a digit, flag signals first pass*/
			result=atof(argv[i]);

			flag=0;
			i++;
		}/*if*/
		else{
			if(isdigit(argv[i][0])){/*checks if character is a digit */
				first=atoi(argv[i]);
				operator(&result, first, operation);/* operation performed on values*/
				i++;
			}/*if*/
			else
			i++;/*else*/

	       }/*else*/

	}/*while*/

	if(out[0]=='\0') out_file=fopen("stdout.dat","w");
	else out_file=fopen(out,"w");
		/*prints results, inputs, and type of operation*/
			i=1;
		while(i/pre> 
Back to top