Question: can someone help me with the following code Write a C program that runs on ocelot for a mini calculator using only the command line
can someone help me with the following code
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. -x squares value. (Note: no num is needed.) Output should have exactly 2 decimal places no matter what the starting values are. If x 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 !!
heres me code
#include
int main(int argc, char **argv) { extern char *optarg; extern int optind; int c, err = 0; int xflag=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] [x] value "; // Set the flags and num values for each case while ((c = getopt(argc, argv, "em:d:a:s:")) != -1) switch (c) { case 'e': xflag = 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("xflag = %d ", xflag); 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(xflag == 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);
}
it seems to run with no errors the problem is that when i try to run it on ocelot and use the command options its telling me "no command found" for some reason, could someone help me fix the progrma so that it runs correctly with no issues and include a screenshot of you running it so I know exactly how to format the run on ocelot.
please help
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
