Question

Write a C program that runs on ocelot for a mini calculator using only the command...

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.

Create a simple Makefile to compile your program into an executable called minicalc.

Homework Answers

Answer #1

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:

Please upvote the answer.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Lab 2 Write a C program that runs on ocelot for a mini calculator using only...
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...
In this project, you will write C code to solve the Collatz conjecture using ordinary pipes...
In this project, you will write C code to solve the Collatz conjecture using ordinary pipes to share data between parent and child processes Here are the rules for your program: The starting number will be entered on the command line. Be sure to check that it is positive. The parent will handle the command line and pass the integer to the child. The child will do the calculation and pass the sequence generated back to the parent. The parent...
C program question: Write a small C program connect.c that: 1. Initializes an array id of...
C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to...
Write a C++ program, using while statement, to convert meters to feet. The program should request...
Write a C++ program, using while statement, to convert meters to feet. The program should request the starting meter value, the ending meter value, and the increment between metric values. The display should have appropriate headings and list the meters and the corresponding feet value. If the number of iterations is greater than 20, have your program substitute a default increment of (ending value - starting value) / 19. Use the relationship that 1 meter = 3.28 feet.
Write a C program Design a program that uses an array to store 10 randomly generated...
Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for an integer between 0 and 9999, inclusive. The script should then calculate the digit in each of the 1000’s, 100’s, 10’s, and 1’s place of the number. Create a variable for each of the 4 extracted digits. For example, if your variables are named nThousands, nHundreds, nTens, and nOnes, then, in the case of 9471, they would be end up being set to 9,...
Coding in Python Add radio button options for filing status to the tax calculator program of...
Coding in Python Add radio button options for filing status to the tax calculator program of Project 1. The user selects one of these options to determine the tax rate. The Single option’s rate is 20%. The Married option is 15%. The Divorced option is 10%. The default option is Single. Be sure to use the field names provided in the comments in your starter code. ================== Project 1 code: # Initialize the constants TAX_RATE = 0.20 STANDARD_DEDUCTION = 10000.0...
C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program...
C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program that displays all the prime numbers in the given array with the following constraint. Constraint: Only those prime numbers should be displayed whose location is a composite number. Although you may have several prime numbers in the array, only those prime numbers should be displayed which are stored at non-prime locations. Remember that the first position in an array corresponds to the location/index 0....
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will produce the following report (as shown). The first item in the report is a number with starting value 1. Second column is the word “ X ” (representing the times symbol). Third column is the table-number (itself). Following is an equal sign “ = “ (representing a result). Last column is the result of the operation for that line or row which is the...