Question

In: Computer Science

C Program: create a mini calculator with the following usage (using getopt to parse the command...

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.

Solutions

Expert Solution

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


Related Solutions

Time Calculator Create a C++ program that lets the user enter a number of seconds and...
Time Calculator Create a C++ program that lets the user enter a number of seconds and produces output according to the following criteria: • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or...
Fat Percentage Calculator Create a C++ program that allows the user to enter the number of...
Fat Percentage Calculator Create a C++ program that allows the user to enter the number of calories and fat grams in a food. The application should display the percentage of the calories that come from fat. If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. One gram of fat has 9 calories, so: Calories from fat = fat grams *...
Create a program using python that provides a simple calculator: Requires a login with a prompt...
Create a program using python that provides a simple calculator: Requires a login with a prompt for a username and a password prior to using the calculator If username and password are incorrect, the program should re-prompt to re-enter correct username and password Once logged in, the user should have access to the calculator and should have the ability for the following mathematical operators: Addition Subtraction Multiplication Division Square Root PI Exponents
Create an ASP.Net Website using Visual Studio with C#: Create a simple calculator that has 3...
Create an ASP.Net Website using Visual Studio with C#: Create a simple calculator that has 3 text boxes: 2 of them to enter numbers, the 3rd one displays the results Create 4 buttons to add, subtract, multiply, and divide Prevent the user from entering text in the number fields Display a message indicating “cannot divide by” when the user click “/” and there is a zero the in the second box Create two additional buttons: - One to store data...
Using C or C++, write a program which implements the command pipeline ”ls -la | tr...
Using C or C++, write a program which implements the command pipeline ”ls -la | tr [a-zA-Z0-9] a”. Use any combination of fork, exec, wait, read, and write necessary to create this functionality. Note: the parent process must be the one to output the data to the console.
Create a python program that will: prompt a user for a command Command get_data Level 1:...
Create a python program that will: prompt a user for a command Command get_data Level 1: Take one of the commands my_max my_min my_range my_sum mean median mode fib factorize prime Requirements: Your commands should be case-insensitive You should use python lists to store data You should NOT use built-in python math functions, or math libraries to compute these values Tips: Write one function that will convert a string with comma-separated numbers into a python list with the numbers. You...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create 2 threads that will act as counters. One thread should count down from 5 to 0. Once that thread reaches 0, then a second thread should be used to count up to 20.
Introduction to Java Programing Using Loop Create a simple calculator program using loop Ask user to...
Introduction to Java Programing Using Loop Create a simple calculator program using loop Ask user to input two numbers using scanner class Print the instruction of the menu for the calculator program Ask user to press 0 to Quit Ask user to press 1 to Add Ask user to press 2 to Substract Ask user to press 3 to Multiply Ask user to press 4 to Divide Perform correct calcuation based on user inputs and print the result Print error...
Description: To create a Visual Basic program that will obtain a sentence from the user, parse...
Description: To create a Visual Basic program that will obtain a sentence from the user, parse the sentence, and then display a sorted list of the words in the sentence with duplicate words removed. Tasks: Design a method (algorithm) to solve the above problem using pseudocode or a flowchart. Translate your algorithm into an appropriate VB program and test it using the following input sentence. "We are the world We are the children" Submit: Pseudocode version of algorithm/Flowchart version of...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to experiment with pointers. Implement the following steps one by one in your program: YOU NEED TO ANSWER QUESTION Use printf to print your answers at the end(after 12). 1. Declare three integer variables a, b and c. Initialize them to 0, 100 and 225, respectively. 2. Print the value of each variable and its address. 3. Add the following declaration to your code: int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT