Write a C program that runs on ocelot for a mini calculator using only the comma
ID: 3787436 • Letter: W
Question
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.
Usage: minicalc [-a num] [-d num] [-m num] [-s num] [-e] value
• The variable value is the starting value.
• Value should be validated to be an integer between 1 and 99. Error message and usage shown if not.
• -a adds num to value.
• -d divides value by num.
• -m multiplies value by num.
• -s subtracts num from value.
• -e squares value. (Note: no num is needed.)
• Output should have exactly 2 decimal places no matter what the starting values are.
• If –e is included, it is executed first.
• Use standard order of operations for all operations.
Code should be nicely indented and commented. Create a simple Makefile to compile your program into an executable called minicalc.The Makefile should be called Makefile with no extension. I should be able to type make at the command line to compile your program.
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int add( int ,int)
int div( int ,int )
int mul( int ,int )
int sub(int ,int )
int main()
{
int num1, num2, choice ;
printf("1.Addition 2:Division 3:Multipliication 4:Substarction 0:Exit enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 0:
return 0;
break;
case 1:
printf("enter the first number: ");
scanf("%d",&num1);
printf("enter the second number: ");
scanf("%d",&num2);
printf("%d",add(num1,num2));
break;
case 2:
printf("enter first number: );
scanf('%d",&num1);
printf("enter second number: );
scanf("%d",&num2);
printf("%d",div(num1, num2));
break;
case 3;
printf("enter first number: );
scanf("%d",&num1);
printf("enter second number: );
scanf("%d",&num2);
printf("%d",mul(num1,num2));
break;
case 4:
printf("enter first number : ");
scanf("%d",&num1);
printf("enter second number: ");
scanf("%d",&num2);
printf("%d",sub(num1,num2));
break;
default:
printf("this is not a valid choice");
break;
}
}
// Addition
int add(int a, int b)
{
int c=a+b;
return c;
}
//multiplication
int mul(int x, int y)
{
int z=x * y;
return z;
}
//division
int div(int i ,int j)
{
int k=i / j;
return k;
}
// substraction
int sub(int m, int n)
{
int l= m - n;
return l;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.