You are required to write a C program that will calculate th

You are required to write a C program that will calculate the following: Perimeter: Square, Rectangle, Triangle, Circle. Area: Square, Rectangle, Triangle, Circle. Volume: Cube, Rectangular Prism, Triangular Pyramid, Sphere.

DETAILS Your program must allow the user to choose whether they want to calculate the Perimeter, Area or Volume. Provide a menu. Once they’ve chosen their calculation from the Perimeter, Area or Volume menu your program must provide a menu with the available geometric shapes in the corresponding option. You must provide a menu again. Your program must ask the user for the appropriate measurements needed to perform the calculations. Don’t forget to validate the user’s input. Make sure that user knows the units in which the calculations are performed and display the units in the final result. You are free to use your own measuring units. Your program should print the result of the calculations to 2 decimal places. Make sure that your formulae use the correct data type to give the correct result. The user must be able to make as many calculations as he wishes. Your program must include a user define library and must have the following 4 user defined functions: float validateInput(float userInput); //returns the userInput corrected if invalid , otherwise makes no corrections float perimeterSquare(float length); //returns the perimeter of a square with a given length float areaSquare(float length); //returns the area of a square with a given length float volumeSquare(float length); //returns the volume of a square or cube with a given length You will add at least 3 more user defined functions. Your source code must be readable and maintainable as possible by including meaningful comments, proper indentation, meaningful variable identifiers, constants, etc. Your program must be user friendly, this includes readability of results and prompts. WHAT TO SUBMIT You must submit the source file (Calculator.c) and the files library source file (*.c) and the header file (*.h) through the class website. EXAMPLE OF OUTPUT This program calculates the Perimeter, Area, and Volume of geometrical figures. Menu 1) Perimeter 2) Area 3) Volume What would you like to calculate? User choice: 1 What geometrical figure would you like to use for Perimeter? 1) Square 2) Rectangle 3) Triangle 4) Circle User choice: 2 Please enter the following values in meters. Length: - 0.25 ERROR: The value of Length must be a positive value. Please try again. Length: 0.25 Width: 0.50 The perimeter of the rectangle is: 1.50 m Would you like to make another Perimeter calculation (1 for Yes, 2 for No)? 1 What geometrical figure would you like to use for Perimeter? 1) Square 2) Rectangle 3) Triangle 4) Circle User choice: 1 Please enter the following values in meters: Length: 0.3 The perimeter of the square is: 1.20 m Would you like to make another Perimeter calculation (1 for Yes, 2 for No)? 2 Would you like to go back to the Main PAV menu (1 for Yes, 2 for No)? 1 Menu 1) Perimeter 2) Area 3) Volume What would you like to calculate? User choice: 2 Which geometrical figure would you like to use for Area? 1) Square 2) Rectangle 3) Triangle 5) Circle User choice: 1 Please enter the following values in meters: Length: 2.5 The area for the square is 6.25 m^2 Would you like to make another Perimeter calculation (1 for Yes, 2 for No)? 2 Would you like to go back to the Main PAV menu (1 for Yes, 2 for No)? 2 ****** Thank you for using this calculator. Goodbye. ******

Solution

PROGRAM CODE:

/*
============================================================================
Name : sample.c
Author : Kasturi
Version :
Copyright : This is just an example code
Description : C, Ansi-style
============================================================================
*/

#include <stdio.h>
#include <stdlib.h>
#include \"Func.h\"

void area();
void perimeter();
void volume();

void mainMenu()
{
   int choice;
   printf(\"Choose your option: \ 1. Perimeter\ 2. Area\ 3. Volume\ Enter your choice: \");
   scanf(\"%d\",&choice);
   printf(\"Units are measured in meters\ \");
   switch(choice)
       {
       case 1: perimeter();
               break;
       case 2: area();
               break;
       case 3: volume();
               break;
       }
}

void area()
{
   int areaChoice, menuChoice;
   char isContinue = \'Y\';

   printf(\"Choose your option: \ 1. Square\ 2. Rectangle\ 3. Triangle\ 4. Circle\ \");
   scanf(\"%d\", &areaChoice);
   while(isContinue == \'Y\'){
       switch(areaChoice)
       {
           case 1: printf(\"Enter the length: \");
                   float length1;
                   scanf(\"%f\", &length1);
                   printf(\"\ Area of Square: %.2f\", areaSquare(length1));
                   break;
           case 2: printf(\"Enter length and width: \");
                   float length2, width;
                   scanf(\"%f%f\", &length2, &width);
                   printf(\"\ Area of Rectangle: %.2f\", areaRectangle(length2, width));
                   break;
           case 3: printf(\"Enter height and base: \");
                   float height, base;
                   scanf(\"%f%f\", &height, &base);
                   printf(\"\ Area of Triangle: %.2f\", areaTriangle(height, base));
                   break;
           case 4: printf(\"Enter the radius: \");
                   float radius;
                   scanf(\"%f\", &radius);
                   printf(\"\ Area of Circle: %.2f\", areaCircle(radius));
                   break;
       }
       printf(\"\ 1. Continue calculating area\ 2. Go to Main menu\ 3. Exit \ Enter your choice: \");
       scanf(\"%d\", &menuChoice);
       switch(menuChoice)
       {
       case 1: printf(\"Choose your option: \ 1. Square\ 2. Rectangle\ 3. Triangle\ 4. Circle\ \");
               scanf(\"%d\", &areaChoice);
               isContinue = \'Y\';
               break;
       case 2: isContinue = \'N\';
               mainMenu();
               break;
       case 3: exit(0);
       }
   }
}

void perimeter()
{
   int perimeterChoice, menuChoice;
   char isContinue = \'Y\';
   printf(\"Choose your option: \ 1. Square\ 2. Rectangle\ 3. Triangle\ 4. Circle\ \");
   scanf(\"%d\", &perimeterChoice);
   while(isContinue == \'Y\')
   {
       switch(perimeterChoice)
       {
           case 1: printf(\"Enter the length: \");
                   float length1;
                   scanf(\"%f\", &length1);
                   printf(\"\ Perimeter of Square: %.2f\", perimeterSquare(length1));
                   break;
           case 2: printf(\"Enter length and width: \");
                   float length2, width;
                   scanf(\"%f%f\", &length2, &width);
                   printf(\"\ Perimeter of Rectangle: %.2f\", perimeterRectangle(length2, width));
                   break;
           case 3: printf(\"Enter height and base: \");
                   float side1, side2, base;
                   scanf(\"%f%f%f\", &side1, &side2, &base);
                   printf(\"\ Perimeter of Triangle: %.2f\", perimeterTriangle(side1, base, side2));
                   break;
           case 4: printf(\"Enter the radius: \");
                   float radius;
                   scanf(\"%f\", &radius);
                   printf(\"\ Perimeter of Circle: %.2f\", perimeterCircle(radius));
                   break;
       }
       printf(\"\ 1. Continue calculating perimeter\ 2. Go to Main menu\ 3. Exit \ Enter your choice: \");
       scanf(\"%d\", &menuChoice);
       switch(menuChoice)
       {
       case 1: printf(\"Choose your option: \ 1. Square\ 2. Rectangle\ 3. Triangle\ 4. Circle\ \");
               scanf(\"%d\", &perimeterChoice);
               isContinue = \'Y\';
               break;
       case 2: isContinue = \'N\';
               mainMenu();
               break;
       case 3: exit(0);
       }
   }
}

void volume()
{
   int volumeChoice;
   int menuChoice ;
   char isContinue = \'Y\';
   printf(\"Choose your option: \ 1. Cube\ 2. Rectangular Prism\ 3. Triangular Pyramid\ 4. Sphere\ \");
   scanf(\"%d\", &volumeChoice);
   while(isContinue== \'Y\'){
       switch(volumeChoice)
       {
           case 1: printf(\"Enter the length: \");
                   float length1;
                   scanf(\"%f\", &length1);
                   printf(\"\ Volume of Cube: %.2f\", volumeCube(length1));
                   break;
           case 2: printf(\"Enter length and width and height: \");
                   float length2, width, height;
                   scanf(\"%f%f%f\", &length2, &width, &height);
                   printf(\"\ Volume of Rectangular Prism: %.2f\", volumeRectangularPrism(length2, width, height));
                   break;
           case 3: printf(\"Enter base height , base and pyramid height: \");
                   float baseheight, base, pyramidHeight;
                   scanf(\"%f%f%f\", &base, &baseheight, &pyramidHeight);
                   printf(\"\ Volume of Triangular Prism: %.2f\", volumeTriangularPyramid(base, baseheight, pyramidHeight));
                   break;
           case 4: printf(\"Enter the radius: \");
                   float radius;
                   scanf(\"%f\", &radius);
                   printf(\"\ Volume of Sphere: %.2f\", volumeSphere(radius));
                   break;
       }
       printf(\"\ 1. Continue calculating volume\ 2. Go to Main menu\ 3. Exit \ Enter your choice: \");
       scanf(\"%d\", &menuChoice);
       switch(menuChoice)
       {
       case 1: printf(\"Choose your option: \ 1. Cube\ 2. Rectangular Prism\ 3. Triangular Pyramid\ 4. Sphere\ \");
               scanf(\"%d\", &volumeChoice);
               isContinue = \'Y\';
               break;
       case 2: isContinue = \'N\';
               mainMenu();
               break;
       case 3: exit(0);
       }
   }
}


int main(void) {
   mainMenu();
   return EXIT_SUCCESS;
}

User defined library

Func.c

/*
* Func.h
*
* Created on: 23-Nov-2016
* Author: kasturi
*/

#ifndef SRC_FUNC_H_
#define SRC_FUNC_H_


//returns the userInput corrected if invalid , otherwise makes no corrections
float validateInput(float userInput)
{
   if(userInput<0)
       return 1;
   else return userInput;
}

//returns the perimeter of a square with a given length
float perimeterSquare(float length)
{
   return 4 * length;
}

float perimeterRectangle(float length, float width)
{
   return 2 * (length + width);
}

float perimeterTriangle(float side1, float base, float side2)
{
   return side1+base+side2;
}

float perimeterCircle(float radius)
{
   return 2 * 3.14 * radius;
}
//returns the area of a square with a given length
float areaSquare(float length)
{
   return length*length;
}

float areaRectangle(float length, float width)
{
   return length * width;
}

float areaTriangle(float height, float base)
{
   return (height * base)/2;
}

float areaCircle(float radius)
{
   return 3.14 * radius * radius;
}
//returns the volume of a square or cube with a given length
float volumeCube(float length)
{
   return length*length*length;
}

float volumeRectangularPrism(float length, float width, float height)
{
   return length * width * height;
}

float volumeTriangularPyramid(float base, float baseheight, float pyramidheight)
{
   return (areaTriangle(baseheight, base) * pyramidheight);
}

float volumeSphere(float radius)
{
   return (4 * 3.14 * radius * radius * radius)/3;
}


#endif /* SRC_FUNC_H_ */

OUTPUT:

Choose your option:

1. Perimeter

2. Area

3. Volume

Enter your choice: 1

Units are measured in meters

Choose your option:

1. Square

2. Rectangle

3. Triangle

4. Circle

2

Enter length and width: 2 4

Perimeter of Rectangle: 12.00

1. Continue calculating perimeter

2. Go to Main menu

3. Exit

Enter your choice: 1

Choose your option:

1. Square

2. Rectangle

3. Triangle

4. Circle

4

Enter the radius: 6

Perimeter of Circle: 37.68

1. Continue calculating perimeter

2. Go to Main menu

3. Exit

Enter your choice: 2

Choose your option:

1. Perimeter

2. Area

3. Volume

Enter your choice: 3

Units are measured in meters

Choose your option:

1. Cube

2. Rectangular Prism

3. Triangular Pyramid

4. Sphere

3

Enter base height , base and pyramid height: 4 3 6

Volume of Triangular Prism: 36.00

1. Continue calculating volume

2. Go to Main menu

3. Exit

Enter your choice: 3

You are required to write a C program that will calculate the following: Perimeter: Square, Rectangle, Triangle, Circle. Area: Square, Rectangle, Triangle, Circ
You are required to write a C program that will calculate the following: Perimeter: Square, Rectangle, Triangle, Circle. Area: Square, Rectangle, Triangle, Circ
You are required to write a C program that will calculate the following: Perimeter: Square, Rectangle, Triangle, Circle. Area: Square, Rectangle, Triangle, Circ
You are required to write a C program that will calculate the following: Perimeter: Square, Rectangle, Triangle, Circle. Area: Square, Rectangle, Triangle, Circ
You are required to write a C program that will calculate the following: Perimeter: Square, Rectangle, Triangle, Circle. Area: Square, Rectangle, Triangle, Circ
You are required to write a C program that will calculate the following: Perimeter: Square, Rectangle, Triangle, Circle. Area: Square, Rectangle, Triangle, Circ
You are required to write a C program that will calculate the following: Perimeter: Square, Rectangle, Triangle, Circle. Area: Square, Rectangle, Triangle, Circ

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site