fork download
  1. //Cecilia Rangel CSC5 Chapter 4, P. 225, #21
  2. //
  3. /**************************************************************
  4.  *
  5.  * Computing geometry calculator
  6.  * ____________________________________________________________
  7.  * This program computes the area of specific shapes
  8.  *
  9.  *
  10.  * Computation is based on the following formulas:
  11.  * Area of Circle = PI * (Radius)^2
  12.  * Area of Rectangle = Length * Width
  13.  * Area of Triangle = Base * Height * .5
  14.  *___________________________________________________________
  15.  * CONSTANTS
  16.  * PI : Value of pi to 5 decimal places (3.14159)
  17.  * Circle choice : Menu option for a circle
  18.  * Rectangle choice : Menu option for a rectangle
  19.  * Triangle choice : Menu option for a triangle
  20.  * Quit choice : Menu option for exiting the program immediately
  21.  *
  22.  * INPUT
  23.  * choice : Integer expression for choosing menu option
  24.  * radius : Measurement of circle's radius
  25.  * length : Measurement of rectangle's length
  26.  * width : Measurement of rectangle's width
  27.  * base : Measurement of triangle's base
  28.  * height : Measurement of triangles height
  29.  *
  30.  * OUTPUT
  31.  * display : which number is greater
  32.  *
  33.  **************************************************************/
  34. #include <iostream>
  35. #include <iomanip>
  36. #include <cmath>
  37. using namespace std;
  38.  
  39. int main() {
  40. const double PI = 3.14159;
  41. const int CIRCLE_CHOICE = 1;
  42. const int RECTANGLE_CHOICE = 2;
  43. const int TRIANGLE_CHOICE = 3;
  44. const int QUIT_CHOICE = 4;
  45.  
  46.  
  47. int choice;
  48. double radius;
  49. double length;
  50. double width;
  51. double base;
  52. double height;
  53. double area;
  54.  
  55. cout << "Geometry Calculator" << endl << endl;
  56. cout << " 1. Calculate the Area of a Circle" << endl;
  57. cout << " 2. Calculate the Area of a Rectangle" << endl;
  58. cout << " 3. Calculate the Area of a Triangle" << endl;
  59. cout << " 4. Quit" << endl << endl;
  60. cout << " Enter your choice (1-4): ";
  61. cin >> choice;
  62. cout << endl << endl;
  63.  
  64. cout << left;
  65. switch (choice)
  66. {
  67. case CIRCLE_CHOICE:
  68. cout << "1. AREA OF A CIRCLE:" << endl;
  69. cout << setw(14) << "Enter radius: ";
  70. cin >> radius;
  71. if (radius > 0)
  72. {
  73. area = PI * pow(radius, 2);
  74. cout << endl;
  75. cout << setw(14) << "Area: " << area;
  76. }
  77. else
  78. cout << "Radius must be positive! Run program again.";
  79. break;
  80. case RECTANGLE_CHOICE:
  81. cout << "2. AREA OF A RECTANGLE:" << endl;
  82. cout << setw(14) << "Enter length: ";
  83. cin >> length;
  84. cout << setw(14) << "Enter width: ";
  85. cin >> width;
  86. if (length > 0 && width > 0)
  87. {
  88. area = length * width;
  89. cout << endl;
  90. cout << setw(14) << "Area: " << area;
  91. }
  92. else
  93. cout << "Length & width must be positive! Run program again.";
  94. break;
  95. case TRIANGLE_CHOICE:
  96. cout << "3. AREA OF A TRIANGLE:" << endl;
  97. cout << setw(19) << "Enter base length: ";
  98. cin >> base;
  99. cout << setw(19) << "Enter height: ";
  100. cin >> height;
  101. if (base > 0 && height > 0)
  102. {
  103. area = base * height * .5;
  104. cout << endl;
  105. cout << setw(19) << "Area: " << area;
  106. }
  107. else
  108. cout << "Base & height must be positive! Run program again.";
  109. break;
  110. case QUIT_CHOICE:
  111. break;
  112. default:
  113. cout << "Your choice must a number from 1-4! Run program again.";
  114. }
  115. cout << right;
  116. return 0;
  117. }
Success #stdin #stdout 0.01s 5288KB
stdin
1
3
stdout
Geometry Calculator

    1. Calculate the Area of a Circle
    2. Calculate the Area of a Rectangle
    3. Calculate the Area of a Triangle
    4. Quit

    Enter your choice (1-4): 

1. AREA OF A CIRCLE:
Enter radius: 
Area:         28.2743