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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
******************************************************************
CSCI 240 Program 5 Part 2 Spring 2013
Programmer:
Section: 1
Date Due: 3/1/13
Purpose: This program uses functions to calculate the surface
area of various shapes.
It is an exercise in learning to write functions.
******************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
#define PI 3.14
//Symbolic constant for the value of PI
int Menu();
int getValue( string prompt, int lowerBound, int upperBound );
float calcSphere( int radius );
float calcPrism( int length, int width, int height );
float calcCone( int radius, int length );
float calcPyramid( int length, int slantHeight );
//Function Prototypes, not sure if right
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
{
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 **********
int Menu()
{
int menuChoice;
cout << "\n\n\nSurface Area Calulator\n\n";
cout << "1 Sphere\n";
cout << "2 Cone\n";
cout << "3 Rectangular Prism\n";
cout << "4 Pyramid\n";
cout << "5 Quit\n\n";
cout << "Enter your choice (1-5): ";
cin >> menuChoice;
while( menuChoice < 1 or menuChoice > 5 )
{
cout << "WRONG ANSWER! Try again: ";
cin >> menuChoice;
}
return menuChoice;
}
/***************************************************************
menu
Function: displays menu and gets choice
Use: check for validity and give user another chance if an invalid option is chosen
Arguments: takes no arguments
Returns: retuned to main()
Notes: sphere, pyramid, cone, rectangular prism, exit
***************************************************************/
int getValue( string prompt, int lowerBound, int upperBound );
{
int userNum;
cout << "Enter a number between" << lowerBound << "and " << upperBound << ": ";
cin >> userNum;
while( userNum < 0 or userNum > upperBound )
{
cout << "You messed again! Try again: ";
cin >> userNum;
}
return userNum;
}
/***************************************************************
Bounds
Function: get an integer value from user in a certain range
Use: makes sure value is in certain range
Arguments: a string and two integers
Returns: calling function
Notes:value = getValue( "Enter the radius for the sphere", 1, 10 );
***************************************************************/
float calcSphere( int radius );
{
return 4 * PI * (radius*radius)
}
/***************************************************************
sphere
Function: calculate surface area of a sphere
Use: does math based on formula
Arguments: an integer that holds the radius of the sphere
Returns: a floating point value which is the calculated surface area
Notes: Surface Area of Sphere = 4 * pi * radius2
***************************************************************/
float calcPrism( int length, int width, int height );
{
return ( 2 * length * width ) + ( 2 * width * height ) +
( 2 * length * height )
}
/***************************************************************
prism
Function: calculate surface area of a prism
Use:does math based on formula
Arguments: an integer that holds the length of the side of the prism, an integer that holds the width of the side of the prism, an integer that holds the height of the side of the prism
Returns: returns a floating point value which is the calculated surface area
Notes:Surface Area of Rectangular Prism = ( 2 * length * width ) + ( 2 * width * height ) +
( 2 * length * height )
***************************************************************/
float calcCone( int radius, int length );
{
return ( PI * radius * length ) + ( PI * (radius*radius) )
}
/***************************************************************
cone
Function: calculate surface area of a cone
Use: does math based on formula
Arguments: an integer that holds the radius of the base of the cone, and an integer that holds the length of the side of the cone
Returns: a floating point value which is the calculated surface area.
Notes: Surface Area of Cone = ( pi * radius * length ) + ( pi * radius2 )
***************************************************************/
float calcPyramid( int length, int slantHeight );
{
return ( 2 * length * height ) + (length*length)
}
/***************************************************************
pyramid
Function: calculate surface area of a pyramid
Use: does math based on formula
Arguments: an integer that holds the length of the base of the pyramid, and an integer that holds the slant height of the pyramid
Returns: floating point value which is the calculated surface area.
Notes: Surface Area of Pyramid = ( 2 * length * height ) + length2
***************************************************************/
|