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
|
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;
#include <cmath>
#include <cstring>
using std::strtok;
using std::strcmp;
#include <cstdlib>
const int MAX_CHARS_PER_LINE = 50;
const int MAX_TOKENS_PER_LINE = 4;
const char* DELIMITER = " ";
void squareCalc(double);
void rectangleCalc(double, double);
void circleCalc(double);
void cubeCalc(double);
void prismCalc(double, double, double);
void sphereCalc(double);
void cylinderCalc(double);
void cylinderCalc(double, double);
void triangleCalc(double, double, double);
int main()
{
cout << endl;
cout << "Description: Calculates area, perimeter, surface area, and volume" << endl;
cout << "of 6 different geometric objects." << endl;
cout << endl;
ifstream fin;
fin.open("geo.txt");
if(!fin.good())
return 1;
char* token[MAX_TOKENS_PER_LINE] = {0};
double squareSide = 0;
double rectangleSide1 = 0;
double rectangleSide2 = 0;
double circle = 0;
double cubeSide = 0;
double prismSide1 = 0;
double prismSide2 = 0;
double prismSide3 = 0;
double sphere = 0;
double cylinder1 = 0;
double cylinder2 = 0;
double triangleSide1 = 0;
double triangleSide2 = 0;
double triangleSide3 = 0;
while(!fin.eof())
{
char buf[MAX_CHARS_PER_LINE];
fin.getline(buf, MAX_CHARS_PER_LINE);
int n = 0;
token[0] = strtok(buf, DELIMITER);
if(token[0])
{
for(n = 1; n < MAX_TOKENS_PER_LINE; n++)
{
token[n] = strtok(0, DELIMITER);
if(!token[n]) break;
} //for
} //if
if(token[0])
{
for(n=1; n < MAX_TOKENS_PER_LINE; n++)
{
if((strcmp(token[0], "SQUARE") == 0))
{
squareSide = atof(token[1]);;
} //if
if((strcmp(token[0], "RECTANGLE") == 0))
{
rectangleSide1 = atof(token[1]);
rectangleSide2 = atof(token[2]);
} //if
if((strcmp(token[0], "CIRCLE") == 0))
{
circle = atof(token[1]);
} //if
if((strcmp(token[0], "CUBE") == 0))
{
cubeSide = atof(token[1]);
} //if
if((strcmp(token[0], "PRISM") == 0))
{
prismSide1 = atof(token[1]);
prismSide2 = atof(token[2]);
prismSide3 = atof(token[3]);
} //if
if((strcmp(token[0], "SPHERES") == 0))
{
sphere = atof(token[1]);
} //if
if((strcmp(token[0], "CYLINDER") == 0))
{
cylinder1 = atof(token[1]);
cylinder2 = atof(token[2]);
} //if
if((strcmp(token[0], "TRIANGLE") == 0))
{
triangleSide1 = atof(token[1]);
triangleSide2 = atof(token[2]);
triangleSide3 = atof(token[3]);
} //if
if(!token[n]) break;
} //for
} //if
cout << endl;
} //while
squareCalc(squareSide);
rectangleCalc(rectangleSide1, rectangleSide2);
circleCalc(circle);
cubeCalc(cubeSide);
prismCalc(prismSide1, prismSide2, prismSide3);
sphereCalc(sphere);
cylinderCalc(cylinder1, cylinder2);
triangleCalc(triangleSide1, triangleSide2, triangleSide3);
cout << endl;
cout << "Press Enter to continue..." << endl;
cin.get();
return 0;
} //main
void squareCalc(double side)
{
double perimeter;
double area;
perimeter = side * 4;
area = side * side;
cout << "SQUARE side=" << side << " perimeter=" << perimeter;
cout << " area=" << area << endl;
} //squareCalc
void rectangleCalc(double side1, double side2)
{
double perimeter;
double area;
perimeter = (side1 * 2) + (side2 * 2);
area = side1 * side2;
cout << "RECTANGLE side1=" << side1 << " side2=" << side2;
cout << " perimeter=" << perimeter << " area=" << area << endl;
} //rectangleCalc
void circleCalc(double side1)
{
double circumference;
double area;
double pow(double, int);
double pie = 3.14;
circumference = 2 * pie * side1;
area = pie * pow(side1, 2);
cout << "CIRCLE radius=" << side1;
cout << " circumference=" << circumference << " area=" << area << endl;
} //rectangleCalc
void cubeCalc(double side1)
{
double volume;
double surfaceArea;
double pow(double, int);
volume = pow(side1, 3);
surfaceArea = 6 * pow(side1, 2);
cout << "CUBE side=" << side1;
cout << " volume=" << volume << " surface area=" << surfaceArea << endl;
} //rectangleCalc
void prismCalc(double side1, double side2, double side3)
{
double volume;
double surfaceArea;
volume = side1 * side2 * side3;
double lw = side1 * side2;
double lw2 = 2 * side1 + 2 * side2;
surfaceArea = 2 * lw + lw2 * side3;
cout << "PRISM side1=" << side1 << " side2=" << side2 << " side3=" << side3;
cout << " volume=" << volume << " surface area=" << surfaceArea << endl;
} //prismCalc
void sphereCalc(double side1)
{
double volume;
double surfaceArea;
double pie = 3.14;
volume = (4/3) * pie * pow(side1, 3);
surfaceArea = 4 * pie * pow(side1, 2);
cout << "SPHERE radius=" << side1;
cout << " volume=" << volume << " surfaceArea=" << surfaceArea << endl;
} //rectangleCalc
void cylinderCalc(double side1, double side2)
{
double volume;
double surfaceArea;
double pie = 3.14;
volume = 2 * pie * pow(side1, 2) * side2;
surfaceArea = 2 * pie * pow(side1, 2) + 2 * pie * side1 * side2;
cout << "CYLINDER side1=" << side1 << " side2=" << side2;
cout << " volume=" << volume << " surfaceArea=" << surfaceArea << endl;
} //rectangleCalc
void triangleCalc(double side1, double side2, double side3)
{
double perimeter;
double area;
perimeter = side1 + side2 + side3;
area = (1/2) * side1 * side2;
cout << "TRIANGLE side1=" << side1 << " side2=" << side2 << " side3=" << side3;
cout << " perimeter=" << perimeter << " area=" << area << endl;
} //triangleCalc
|