Question

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 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.

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:

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
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...
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...
In this lab, you complete a partially prewritten C++ program that uses an array. The program...
In this lab, you complete a partially prewritten C++ program that uses an array. The program prompts the user to interactively enter eight batting averages, which the program stores in an array. The program should then find the minimum and maximum batting average stored in the array as well as the average of the eight batting averages. The data file provided for this lab includes the input statement and some variable declarations. Comments are included in the file to help...
4) Write a Java program using Conditions: Write a program where it will ask user to...
4) Write a Java program using Conditions: Write a program where it will ask user to enter a number and after that it will give you answer how many digits that number has. Steps: 1) Create Scanner object and prompt user to enter the number and declare variable integer for input 2) Use if else condition with && condition where you will specify the digits of numbers by writing yourself the digit number that should display: Example(num<100 && num>=1), and...
In C++ Write a simple program to generate random integers between X and Y and store...
In C++ Write a simple program to generate random integers between X and Y and store them in a file, one number per line. The user should input the number of elements to generate and the boundary numbers X and Y (e.g. the inputs 100, 0, 999 mean your program should generate a list of 100 integers with values between 0 and 999, inclusive).
can you please do this lab? use lunix or C program its a continuation of a...
can you please do this lab? use lunix or C program its a continuation of a previous lab. the previous lab: Unix lab 4: compile and link multiple c or c++ files Please do the following tasks step by step: create a new directory named by inlab4 enter directory inlab4 create a new file named by reverse.c with the following contents and then close the file: /*reverse.c */ #include <stdio.h> reverse(char *before, char *after); main() {       char str[100];    /*Buffer...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
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 C++ write a program for a multiplication table It must Prompt the user for two...
In C++ write a program for a multiplication table It must Prompt the user for two integers between 1 and 20 (inclusive) • also must Calculates and displays the multiplication table in a well-formatted output The table must include a label for each row and column The program must follow the requirements: • Validates user input, displaying an error message and prompting to user to enter another integer if the input is invalid, and repeating it as many times as...
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,...