Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program for a mini calculator using only the command line options. You

ID: 3786576 • Letter: W

Question

Write a C program for a mini calculator using only the command line options. You must use getopt to parse the command line.

Usage: minicalc [-a num] [-d num] [-m num] [-s num] [-e] value The variable value is the starting value.

• Value should be validated to be an integer between 1 and 99. Error message and usage shown if not.

• -a adds num to value.

• -d divides value by num.

• -m multiplies value by num.

• -s subtracts num from value.

• -e squares value. (Note: no num is needed.)

• Output should have exactly 2 decimal places no matter what the starting values are.

• If –e is included, it is executed first.

• Use standard order of operations for all operations.

Code should be nicely indented and commented.

Create a simple Makefile to compile your program into an executable called minicalc. You should submit the source code, your Makefile and a screenshot of the output file.

Explanation / Answer

++++++++++++++++++++++++++++C code: (Save it in file minicalc.c)++++++++++++++++

#include<stdio.h>
#include<getopt.h>
#include<stdlib.h>

void print_help();

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

// to store the main value on which operations need to be performed
int number;

// booleans for what all t=are the options which need to be performed
int multiply = 0,
divide = 0,
add = 0,
subtract = 0,
square = 0;

int multiplyBy,
divideBy,
addBy,
subtractBy;

char opt;

int order[4] = {-1,
-1,
-1,
-1
}; // to determine the order

float output;

/*
// no arguments given
*/
if (argc == 1) {
fprintf(stderr, "This program needs arguments.... ");
print_help();
}

int noOfOperatorsFound = 0;

// e is optional
while ((opt = getopt(argc, argv, "a:d:m:s:e")) != -1) {
int optionVal;

if (opt != 'e') {
if (sscanf(optarg, "%d", & optionVal) == 0) {
printf("Error: Incorrect value for option %c: %s(Not a number) ", opt, optarg);
exit(0);
}
}

switch (opt) {
case 'a':
add = 1;
addBy = optionVal;
order[noOfOperatorsFound] = 1;
break;
case 'd':
divide = 1;
divideBy = optionVal;
order[noOfOperatorsFound] = 2;
break;
case 'm':
multiply = 1;
multiplyBy = optionVal;
order[noOfOperatorsFound] = 3;
break;
case 's':
subtract = 1;
subtractBy = optionVal;
order[noOfOperatorsFound] = 4;
break;
case 'e':
square = 1;
break;

}
noOfOperatorsFound++;
}

if (optind < argc) {
int optionVal;

if (sscanf(argv[optind], "%d", & optionVal) == 0) {
printf("Error: Incorrect value: %s(Not a number) ", argv[optind]);
exit(0);
} else if ((optionVal < 1) || (optionVal > 99)) {
printf("Error: Incorrect value: %s (Not in range of 1-99) ", opt, optarg);
exit(0);
}

output = optionVal;
}

if (square == 1) {
// squaring the num
output = number * number;
}

int i;
for (i = 0; i < 4; i++) {
if (order[i] == -1) {
continue; // means no operation is remaining to be performed
} else if (order[i] == 1) {
// means addition to be performed
output += addBy;
} else if (order[i] == 2) {
// means divide to be performed
output /= divideBy;
} else if (order[i] == 3) {
// means multiplication to be performed
output *= multiplyBy;
} else if (order[i] == 4) {
// means subtraction to be performed
output -= subtractBy;
}
}

printf("%.2f ", output);

return 0;
}

void print_help() {
printf("[-a num] [-d num] [-m num] [-s num] [-e] value ");
}

+++++++++++ Now make file code ++++++++++ (save it in "makefile" in same directory)

all: minicalc.c ; gcc -o minicalc minicalc.c

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote