1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Symbolic constant for the value of PI
const float PI = 3.14;
//Function Prototypes
int Menu();
int getValue(string, int, int );
float calcSphere(int );
float calcPrism(int, int, int );
float calcCone(int, int );
float calcPyramid(int, int );
int main()
{
int menuChoice, //The users choice from the menu
radius, //The radius for the sphere and cone
length, //The length of the side of the cone, prism, and pyramid
height, //The height of the side of the prism and pyramid
width; //The width of the side of the prism
float surfArea; //The surface area of all of the shapes
//Print the surface areas with 4 digits after the decimal points
cout << fixed << showpoint << setprecision(4);
//Display the menu to the user and get their choice
menuChoice = Menu();
//While the user does not want to quit
while( menuChoice != 5 )
{
//If the user wants to calculate the surface area of a sphere, get the
//radius of the sphere and then display the calculated surface area
if( menuChoice == 1 )
{
radius = getValue( "Enter the radius for the sphere", 1, 10 );
surfArea = calcSphere( radius );
cout << endl << "The surface area of a sphere with radius " << radius
<< " is " << surfArea;
}
//If the user wants to calculate the surface area of a cone, get the
//radius of the base and the length of the side of the cone, and then
//display the calculated surface area
else if ( menuChoice == 2 )
{
radius = getValue( "Enter the radius of the base of the cone", 1, 7 );
length = getValue( "Enter the length of the side of the cone", 1, 12 );
surfArea = calcCone( radius, length );
cout << endl << "The surface area of a cone with radius " << radius
<< " and side length " << length << " is " << surfArea;
}
//If the user wants to calculate the surface area of a prism, get the
//length, width, and height of the side of the prism, and then display
//the calculated surface area
else if ( menuChoice == 3 )
{
length = getValue( "Enter the length of the side of the prism", 1, 20 );
width = getValue( "Enter the width of the side of the prism", 1, 20 );
height = getValue( "Enter the height of the side of the prism", 1, 20 );
surfArea = calcPrism( length, width, height );
cout << endl << "The surface area of the prism with length " << length
<< " width " << width << " and height " << height << " is " << surfArea;
}
//Otherwise, the user wants to calculate the surface area of a pyramid.
//Get the length of the base and the slant height of the pyramid, and
//then display the calculated surface area
else if (menuChoice == 4 )
{
length = getValue( "Enter the length of the base of the pyramid", 1, 10 );
height = getValue( "Enter the slant height of the pyramid", 1, 15 );
surfArea = calcPyramid( length, height );
cout << endl << "The surface area of the pyramid with base " << length
<< " and slant height " << height << " is " << surfArea;
}
//Get another menu choice from the user
menuChoice = Menu();
}
return 0;
}
//********** Code the functions below this line **********
/*********************************************************
Function: Menu
Use: This function asks for the users choice for the menu
(choices 1-5 in this case).
Arguements: None
Returns: The number entered is the choice on the menu, and
opens up the calculator.
**********************************************************/
int Menu()
{
int menuChoice;
cout << "1. Surface Area of a Sphere\n"
<< "2. Surface Area of a Cone\n"
<< "3. Surface Area of a Prism\n"
<< "4. Surface Area of a Pyramid\n";
cin >> menuChoice;
return 0;
}
/***********************************************************
Function: Get Value
Use: This function asks the user to enter a value into the
calculator between certain guidelines.
Arguements: None
Returns: Whatever calculation is done after entering in the
proper number of values.
***********************************************************/
int getValue(string, int, int)
{
int value;
value = getValue();
return value;
}
/************************************************************
Function: Sphere
Use: Calculates the area of a sphere.
Arguements: Four * value of PI * the radius squared.
Returns: The surface area of a sphere.
************************************************************/
float calcSphere(int radius)
{
float sa;
cout << "Enter in your radius\n";
cin >> radius;
sa = 4 * PI * (radius*radius);
return sa;
}
/*************************************************************
Function: Prism
Use: Calculates the area of a prism.
Arguements: 2 * length * width added three times.
Returns: The surface area of a rectangular prism.
*************************************************************/
float calcPrism(int length, int width, int height)
{
float sa2;
cout << "Enter in your length\n";
cin >> length;
cout << "Enter in your width\n";
cin >> width;
cout << "Enter in your height\n";
cin >> height;
sa2 = (2 * length * width) + (2 * width * height) + (2 * length * height);
return sa2;
}
/**************************************************************
Function: Cone
Use: Calculates the surface area of a cone.
Arguements: PI * radius * length + PI * radius squared.
Returns: The surface area of a cone.
**************************************************************/
float calcCone(int radius, int length)
{
float sa3;
cout << "Enter in your radius\n";
cin >> radius;
cout << "Enter in your length\n";
cin >> length;
sa3 = (PI * radius * length) + (PI * (radius*radius));
return sa3;
}
/**************************************************************
Function: Pyramid
Use: Calculates the surface area of a pyramid.
Arguements: 2 * length * height + length squared.
Returns: The surface area of a pyramid.
**************************************************************/
float calcPyramid(int length, int height)
{
float sa4;
cout << "Enter in your length\n";
cin >> length;
cout << "Enter in your height\n";
cin >> height;
sa4 = (2 * length * height) + (length*length);
return sa4;
}
|