logoISU  

CS256 - Principles of Structured Design

Fall 2021

Displaying ./code/cs256su21code/jul07/basicMath.c

/*

This program takes in two numbers and a string
representing what kind of operation we're doing


*/


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

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

	//this prints a usage statement if the user inputs too few arguments
	if(argc < 4){
		fprintf(stderr, "Usage: %s <num> <num> <operator>\n", argv[0]);
		exit(1);
	}

	int num1, num2;
	int ans;

	num1 = atoi(argv[1]); //using atoi to convert the argv string to an integer
	num2 = atoi(argv[2]);


	//the operation we're doing is stored in argv[3]
	if(strcmp(argv[3],"add") == 0){
		ans = num1+ num2;
	} else if(strcmp(argv[3], "subtract") == 0){
		ans = num1 - num2;
	} else if(strcmp(argv[3], "multiply") == 0){
		ans = num1 * num2;
	} else if(strcmp(argv[3], "divide") == 0){
		ans = num1 / num2;
	} else if(strcmp(argv[3], "mod") == 0) {
		ans = num1 % num2;
	} else {
		printf("Operation not supported\n");
		exit(1);
	}




	printf("Answer: %d\n", ans); //print out the answer here


	return 0;
}