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
|
#include <iostream>
#include <iomanip>
#include <cmath>
#include "Shape.h"
using namespace std;
//Declare Functions
char inputShape();
void inputDimension (double radius);
void inputDimension (double radius, double height);
char inputCompType();
void performComputation (char charType, double radius);
void performComputation (char shape, char charType, double radius, double height);
double sphere_volume (double radius);
double sphere_surface_area (double radius);
double cylinder_volume (double radius, double height);
double cylinder_surface_area (double radius, double height);
double cone_volume (double radius, double height);
double cone_surface_area (double radius, double height);
int main()
{
char shape = '0';
char compType = '0';
double radius = 0.0;
double height = 0.0;
// Call inputShape function to request the first shape
// type or ‘q’ and return result to the shape variable
inputShape();
shape = inputShape();
// Loop until the user specifies QUIT for the shape type
while (shape != 'q')
{
// For Spheres:
if (shape == SPHERE)
{
// Call the appropriate inputDimension function
// to request the radius
inputDimension(radius);
// Call the inputCompType function to request
// the computation type and return result to
// the compType variable
inputCompType();
compType = inputCompType();
// Call the appropriate performComputation
// function to compute and display the result
performComputation (compType, radius);
}
// For Cylinders and Cones:
else if (shape == CYLINDER || shape == CONE)
{
// Call the appropriate inputDimension function
// to request the radius and height
inputDimension(radius, height);
// Call the inputCompType function to request
// the computation type and return result to
// the compType variable
inputCompType();
compType = inputCompType();
// Call the appropriate performComputation
// function to compute and display result
performComputation (shape, compType, radius, height);
}
// Call inputShape function to request the next shape
// type or ‘q’ and return result to the shape variable
inputShape();
shape = inputShape();
}
// end loop
if (shape == QUIT)
{
cout << "Good bye!" << endl;
}
system("pause");
return 0;
}
//Input Functions
char inputShape()
{
char input = '0';
cout << "Select a Shape (1) sphere (2) cylinder (3) cone (q) quit:";
cin >> input;
return input;
}
void inputDimension (double)
{
double sphereRadius = 0.0;
cout << "Enter radius: ";
cin >> sphereRadius;
}
void inputDimension (double, double)
{
double r = 0.0;
double h = 0.0;
cout << "Enter radius: ";
cin >> r;
cout << "Enter height: ";
cin >> h;
}
char inputCompType()
{
char type = '0';
cout << "Select a Computation (1) volume (2) surface area: ";
cin >> type;
return type;
}
//Computation Functions
void performComputation (char compType, double radius)
{
if (compType = VOLUME)
{
sphere_volume(radius);
cout << "Volume of sphere is " << setprecision(3) << sphere_volume(radius) << endl;
}
else if (compType = SURFACE_AREA)
{
sphere_surface_area(radius);
cout << "Surface area of sphere is " << setprecision(3) << sphere_surface_area(radius) << endl;
}
}
void performComputation (char shape, char compType, double radius, double height)
{
if (shape = CYLINDER)
{
if (compType = VOLUME)
{
cylinder_volume(radius, height);
cout << "Volume of cylinder is " << setprecision(3) << cylinder_volume(radius, height) << endl;
}
else if (compType = SURFACE_AREA)
{
cylinder_surface_area(radius, height);
cout << "Surface area of cylinder is " << setprecision(3) << cylinder_surface_area(radius, height) << endl;
}
}
else if (shape = CONE)
{
if (compType = VOLUME)
{
cone_volume(radius, height);
cout << "Volume of cone is " << setprecision(3) << cone_volume(radius, height) << endl;
}
else if (compType = SURFACE_AREA)
{
cone_surface_area(radius, height);
cout << "Surface area of cone is " << setprecision(3) << cone_surface_area(radius, height) << endl;
}
}
}
//Sphere Functions
double sphere_volume (double radius)
{
double sphere_volume = 0.0;
sphere_volume = (4/3) * PI * pow(radius,3);
return sphere_volume;
}
double sphere_surface_area (double radius)
{
double sphere_surface_area = 0.0;
sphere_surface_area = 4 * PI * pow(radius,2);
return sphere_surface_area;
}
//Cylinder Functions
double cylinder_volume (double radius, double height)
{
double cylinder_volume = 0.0;
cylinder_volume = PI * pow(radius,2) * height;
return cylinder_volume;
}
double cylinder_surface_area (double radius, double height)
{
double cylinder_surface_area = 0.0;
cylinder_surface_area = 2 * PI * pow(radius,2) + 2 * PI * radius * height;
return cylinder_surface_area;
}
//Cone Functions
double cone_volume (double radius, double height)
{
double cone_volume = 0.0;
cone_volume = (1/3) * PI * pow(radius,2) * height;
return cone_volume;
}
double cone_surface_area (double radius, double height)
{
double cone_surface_area = 0.0;
cone_surface_area = PI * pow(radius,2) + PI * radius * sqrt(pow(radius,2) + pow(height,2));
return cone_surface_area;
}
|