Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: hw4.c
- * SID: A15506859
- * Name: Khanh Pham
- * Date: October 23, 2019
- * Sources of help: None
- *
- * This program will be a simple geometry calculator which will allow the user to calculate the area, surface area, and volume of different shapes. The program will have functions, choices, user input, and looping.
- */
- #include <stdio.h>
- #include <math.h>
- #define _USE_MATH_DEFINES
- #define squareOption '1'
- //Function prototypes
- void displayMenu();
- char getCharInput();
- double getDoubleInput();
- double areaSquare(double width);
- double areaRectangle(double width, double height);
- double areaCircle(double radius);
- double surfaceAreaCube(double width);
- double surfaceAreaCylinder(double radius, double height);
- double volumeCube(double width);
- /*
- * The main function has a do-while loop to display the menu and conditional switch-case statements to perform user's choice of calculation using pass by value.
- */
- int main()
- {
- char menuChoice;
- double width;
- double height;
- double radius;
- double result;
- do
- {
- displayMenu();
- menuChoice = getCharInput();
- switch( menuChoice )
- {
- case squareOption:
- // printf("Menu Choice Is: %d \n", menuChoice);
- printf("Enter width: ");
- width = getDoubleInput();
- result = areaSquare(width);
- printf("Result: %.2lf meters\n\n", result);
- break;
- case '2':
- printf("Enter width: ");
- width = getDoubleInput();
- printf("Enter height: ");
- height = getDoubleInput();
- result = areaRectangle(width, height);
- printf("Result: %.2lf meters\n\n", result);
- break;
- case '3':
- printf("Enter radius: ");
- radius = getDoubleInput();
- result = areaCircle(radius);
- printf("Result: %.2lf meters\n\n", result);
- break;
- case '4':
- printf("Enter width: ");
- width = getDoubleInput();
- result = surfaceAreaCube(width);
- printf("Result: %.2lf meters\n\n", result);
- break;
- case '5':
- printf("Enter radius: ");
- radius = getDoubleInput();
- printf("Enter height: ");
- height = getDoubleInput();
- result = surfaceAreaCylinder(radius, height);
- printf("Result: %.2lf meters\n\n", result);
- break;
- case '6':
- printf("Enter width: ");
- width = getDoubleInput();
- result = volumeCube(width);
- printf("Result: %.2lf meters\n\n", result);
- break;
- case 'q':
- printf("Goodbye!");
- break;
- case 'Q':
- printf("Goodbye!");
- break;
- default:
- // printf("Menu Choice Is: %d \n", menuChoice);
- printf("Invalid choice");
- break;
- // printf("Result: %.2lf meters\n\n", result);
- }
- }while(menuChoice >= 49 && menuChoice <= 54);
- return 0;
- }
- //Function defintions
- void displayMenu()
- {
- printf("Geometry Calculator:\n1) Area of a Square\n2) Area of a Rectangle\n3) Area of a Circle\n4) Surface Area of a Cube\n5) Surface Area of a Cylinder\n6) Volume of a Cube\n\nEnter choice (Q/q to quit): ");
- }
- char getCharInput()
- {
- char menuChoice;
- scanf("%c", &menuChoice);
- getchar();
- return menuChoice;
- }
- double getDoubleInput()
- {
- double dimension;
- scanf("%lf", &dimension);
- getchar();
- return dimension;
- }
- double areaSquare(double width)
- {
- return width*width;
- }
- double areaRectangle(double width, double height)
- {
- return width*height;
- }
- double areaCircle(double radius)
- {
- return M_PI*radius*radius;
- }
- double surfaceAreaCube(double width)
- {
- return 6*(width*width);
- }
- double surfaceAreaCylinder(double radius, double height)
- {
- return (2*(M_PI*radius*radius)) + ((2*M_PI*radius)*height);
- }
- double volumeCube(double width)
- {
- return pow(width, 3);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement