In: Computer Science
C Program: create a mini calculator with the following usage (using getopt to parse the command line arguments)
Usage: minicalc [-a num] [-m num] [-x] value
1. The variable value is the starting value.
2. Value should be validated to be an integer between 1 and 50
inclusive. Error message and usage shown if not.
3. For the -m option num should be a positive integer between 1 and
10 inclusive.
4. For the -a option num should be a positive integer between 1 and
500 inclusive.
5. -a adds num to value. -m multiplies value by num. -x squares
value. (Note: no num is needed.)
6. Output should have exactly 2 decimal places no matter what the
starting values are.
7. If -x is included, it is executed first. If -m is included it
would be next. The -a would be executed last.
8. 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.
9. There should be no user input while the program is running. It
runs in full from the command line.
Below is the code for the above problem.
#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;
}
OUTPUT:
Please up-vote the answer. If u have any doubts, you can ask in comments