Lab 2
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.
The calculator will only do addition, subtraction, multiplication, division, and a power of 2.
Usage: minicalc [-a num] [-d num] [-m num] [-s num] [-x] value
• The variable value is the starting value.
• Value should be validated to be an integer between 1 and 50 inclusive. Error message and usage shown if not.
• For the -m option num should be a positive integer between 1 and 5 inclusive.
• For the -d option num should be a positive integer between 1 and 5 inclusive.
• For the -a option num should be a positive integer between 1 and 500 inclusive.
• For the -s option num should be a positive integer between 1 and 500 inclusive.
• -a adds num to value.
• -d divides value by num.
• -s subtracts num from value.
• -m multiplies value by num.
• -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. If -m or -d is included it would be next. The -a and -s would be executed last.
• There will be at most one of each option, if there are more than one you can use either of the options in the calculation.
• There should be no user input while the program is running. It runs in full from the command line.
The source file should have your name & PantherID included in it as well as a program description and it should have the affirmation of originality from lab 1.
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 and your Makefile compressed into a zip file named FirstNameLastNameL2.zip. The source file name should be named as desired as long as the Makefile works. The Makefile should be called Makefile with no extension. I should be able to type make at the command line to compile your program. Do not include any other files or folders in the zipfile. This applies even if you are a Mac user. If the program does not compile and do something useful when it runs it will not earn any credit.
Below is the code for the above problem.
main.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
//main method to initalize the program
int main(int argc, char *argv[])
{
int option;
int numAdd,numSub,numMul,numDiv,value; //to store operands
int addFlag=0,subFlag=0,mulFlag=0,divFlag=0,squareFlag=0; //flags
for operations
/*using getopt function to readflags*/
while((option = getopt(argc, argv, ":a:d:m:s:x")) != -1)
{
switch(option)
{
//if there is flag -a in command line arguments
case 'a':
numAdd = atoi(optarg); //store oprerand of addition
if(numAdd > 500 || numAdd < 1) //check validity of
operand
{
printf("For the -a option num should be a positive integer between
1 and 500 inclusive");
}
else //if valid
{
addFlag = 1; //set addition flag
}
break;
//if there is flag -d in command line arguments
case 'd':
numDiv = atoi(optarg); //store oprerand of division
if(numDiv > 5 || numDiv < 1) //check validity of
operand
{
printf("For the -d option num should be a positive integer between
1 and 5 inclusive");
}
else //if valid
{
divFlag = 1; ////set division flag
}
break;
//if there is flag -m in command line arguments
case 'm':
numMul = atoi(optarg); //store oprerand of multiplication
if(numMul > 5 || numMul < 1) //check validity of
operand
{
printf("For the -m option num should be a positive integer between
1 and 5 inclusive");
}
else //if valid
{
mulFlag = 1; //set multiplication flag
}
break;
//if there is flag -s in command line arguments
case 's':
numSub = atoi(optarg); //store subtaction operand
if(numSub > 500 || numSub < 1) //check validity of
operand
{
printf("For the -s option num should be a positive integer between
1 and 500 inclusive");
}
else //if valid
{
subFlag = 1; //set subtaction flag
}
break;
//if there is flag -x in command line arguments
case 'x':
squareFlag = 1; //set squareFlag
break;
//if no operand is provided for listed flags
case ':':
printf("option needs a value\n");
break;
//if unknown flag is provided
case '?':
printf("unknown option: %c\n", optopt);
break;
}
}
//if no value is provided
if(argv[optind] == NULL)
{
printf("value is not provided!");
return -1;
}
//storing value operand
value = atoi(argv[optind]);
//check validity
if(value > 50 || value < 1)
{
printf("value should be between 1-50");
return -1;
}
//if squareFlag is set, do squaring of value
if(squareFlag)
{
printf("Squaring %d ....\n",value);
value = value *value;
printf("Now value becomes : %d\n\n",value);
}
//if division flag is set, do division
if(divFlag)
{
printf("dividing %d by %d ....\n",value,numDiv);
value = value / numDiv;
printf("Now value becomes : %d\n\n",value);
}
//if multiplication flag is set, do multiplication
if(mulFlag)
{
printf("Multipying %d by %d ....\n",value,numMul);
value = value * numMul;
printf("Now value becomes : %d\n\n",value);
}
//if addition flag is set, do addition
if(addFlag)
{
printf("adding %d with %d ....\n",value,numAdd);
value = value + numAdd;
printf("Now value becomes : %d\n\n",value);
}
//if subtaction flag is set, do subtaction
if(subFlag)
{
printf("subtracing %d with %d ....\n",value,numSub);
value = value - numSub;
printf("Now value becomes : %d\n\n",value);
}
//printing final answer
printf("final answer = %d\n",value);
return 0;
}
makefile
run: minicalc
./minicalc
minicalc: main.c
gcc main.c -o minicalc
OUTPUT:
Get Answers For Free
Most questions answered within 1 hours.