Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.96 KB | None | 0 0
  1.  
  2. /*
  3.  * File: hw4.c
  4.  * SID: A15506859
  5.  * Name: Khanh Pham
  6.  * Date: October 23, 2019
  7.  * Sources of help: None
  8.  *
  9.  * 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.
  10.  */
  11.  
  12.  #include <stdio.h>
  13.  #include <math.h>
  14.  #define _USE_MATH_DEFINES
  15.  #define squareOption '1'
  16.  
  17.  //Function prototypes
  18.  void displayMenu();
  19.  char getCharInput();
  20.  double getDoubleInput();
  21.  
  22.  double areaSquare(double width);
  23.  double areaRectangle(double width, double height);
  24.  double areaCircle(double radius);
  25.  
  26.  double surfaceAreaCube(double width);
  27.  double surfaceAreaCylinder(double radius, double height);
  28.  
  29.  double volumeCube(double width);
  30.  
  31.  /*
  32.   * 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.
  33.   */
  34.  
  35.   int main()
  36.   {
  37.     char menuChoice;
  38.     double width;
  39.     double height;
  40.     double radius;
  41.     double result;
  42.    
  43.  
  44.     do
  45.     {
  46.         displayMenu();
  47.         menuChoice = getCharInput();
  48.         switch( menuChoice )
  49.         {
  50.             case squareOption:
  51.                 // printf("Menu Choice Is: %d \n", menuChoice);
  52.                 printf("Enter width: ");
  53.                 width = getDoubleInput();
  54.                 result = areaSquare(width);
  55.                 printf("Result: %.2lf meters\n\n", result);
  56.                 break;
  57.             case '2':
  58.                 printf("Enter width: ");
  59.                 width = getDoubleInput();
  60.                 printf("Enter height: ");
  61.                 height = getDoubleInput();
  62.                 result = areaRectangle(width, height);
  63.                 printf("Result: %.2lf meters\n\n", result);
  64.                 break;
  65.             case '3':
  66.                 printf("Enter radius: ");
  67.                 radius = getDoubleInput();
  68.                 result = areaCircle(radius);
  69.                 printf("Result: %.2lf meters\n\n", result);
  70.                 break;
  71.             case '4':
  72.                 printf("Enter width: ");
  73.                 width = getDoubleInput();
  74.                 result = surfaceAreaCube(width);
  75.                 printf("Result: %.2lf meters\n\n", result);
  76.                 break;
  77.             case '5':
  78.                 printf("Enter radius: ");
  79.                 radius = getDoubleInput();
  80.                 printf("Enter height: ");
  81.                 height = getDoubleInput();
  82.                 result = surfaceAreaCylinder(radius, height);
  83.                 printf("Result: %.2lf meters\n\n", result);
  84.                 break;
  85.             case '6':
  86.                 printf("Enter width: ");
  87.                 width = getDoubleInput();
  88.                 result = volumeCube(width);
  89.                 printf("Result: %.2lf meters\n\n", result);
  90.                 break;
  91.             case 'q':
  92.                 printf("Goodbye!");
  93.                 break;
  94.             case 'Q':
  95.                 printf("Goodbye!");
  96.                 break;
  97.             default:
  98.                 // printf("Menu Choice Is: %d \n", menuChoice);
  99.                 printf("Invalid choice");
  100.                 break;
  101.             // printf("Result: %.2lf meters\n\n", result);
  102.         }
  103.        
  104.     }while(menuChoice >= 49 && menuChoice <= 54);
  105.  
  106.     return 0;
  107.   }
  108.  
  109.   //Function defintions
  110.   void displayMenu()
  111.   {
  112.     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): ");
  113.   }
  114.  
  115.   char getCharInput()
  116.   {
  117.     char menuChoice;
  118.     scanf("%c", &menuChoice);
  119.     getchar();
  120.     return menuChoice;
  121.   }
  122.    
  123.   double getDoubleInput()
  124.   {
  125.     double dimension;
  126.     scanf("%lf", &dimension);
  127.     getchar();
  128.     return dimension;
  129.   }
  130.  
  131.   double areaSquare(double width)
  132.   {
  133.     return width*width;
  134.   }
  135.  
  136.   double areaRectangle(double width, double height)
  137.   {
  138.     return width*height;
  139.   }
  140.  
  141.   double areaCircle(double radius)
  142.   {
  143.     return M_PI*radius*radius;
  144.   }
  145.  
  146.   double surfaceAreaCube(double width)
  147.   {
  148.     return 6*(width*width);
  149.   }
  150.  
  151.   double surfaceAreaCylinder(double radius, double height)
  152.   {
  153.     return (2*(M_PI*radius*radius)) + ((2*M_PI*radius)*height);
  154.   }
  155.  
  156.   double volumeCube(double width)
  157.   {
  158.     return pow(width, 3);
  159.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement