Write a C program that runs on ocelot for a mini calculator using only the comma
ID: 3787582 • Letter: W
Question
Write a C program that runs on ocelot 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.The Makefile should be called Makefile with no extension. I should be able to type make at the command line to compile your program.
CAN NOT USE CONIO.H !!
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
extern char *optarg;
extern int optind;
int c, err = 0;
int eflag=0, mflag=0, dflag = 0, aflag=0, sflag=0;
int mnum = 0, dnum = 0, anum = 0, snum = 0;
float value;
static char usage[] = "usage: minicalc [-a num] [-d num] [-m num] [-s num] [e] value ";
// Set the flags and num values for each case
while ((c = getopt(argc, argv, "em:d:a:s:")) != -1)
switch (c) {
case 'e':
eflag = 1;
break;
case 'm':
mflag = 1;
mnum = atoi(optarg);
break;
case 'd':
dflag = 1;
dnum = atoi(optarg);
break;
case 'a':
aflag = 1;
anum = atoi(optarg);
break;
case 's':
sflag = 1;
snum = atoi(optarg);
break;
case '?':
err = 1;
break;
}
// Need at least one argument (change +1 to +2 for two, etc. as needeed)
if ((optind+1) > argc)
{
printf("optind = %d, argc=%d ", optind, argc);
fprintf(stderr, "%s: missing name ", argv[0]);
fprintf(stderr, usage, argv[0]);
exit(1);
}
else if (err)
{
fprintf(stderr, usage, argv[0]);
exit(1);
}
// Set value to the last argument from the command line
value = atof(argv[argc-1]);
// If value is greater than 99 or less than 1 then print an error and exit the program
if(value > 99 || value < 1)
{
printf("Sorry, starting value must be between 1 and 99. %s", usage);
exit(0);
}
/* Print which flags were set and what the arguments to the options are */
printf("eflag = %d ", eflag);
printf("mflag = %d, %d ", mflag, mnum);
printf("dflag = %d, %d ", dflag, dnum);
printf("aflag = %d, %d ", aflag, anum);
printf("sflag = %d, %d ", sflag, snum);
// If statements to execute the operations we want to do (addition, multiplication, etc.)
if(eflag == 1)
value *= value;
if(mflag == 1)
value *= mnum;
if(dflag == 1)
value /= dnum;
if(aflag == 1)
value += anum;
if(sflag == 1)
value -= snum;
// These are the arguments after the command line options
if (optind < argc)
for (; optind < argc; optind++)
printf("Starting Value: "%s" ", argv[optind]);
else {
printf("no arguments left to process ");
}
// Print the result
printf("Result : %.2f ", value);
exit(0);
}
makefile
minicalc : getopt.o
cc -o minicalc getopt.c
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.