#include <fstream> using std::ifstream; #include <iomanip> using std::setprecisi
ID: 3541299 • Letter: #
Question
#include <fstream>
using std::ifstream;
#include <iomanip>
using std::setprecision;
using std::ios;
#include <cmath>
#include <cstring>
#include <cstdlib>
const int MAX_CHARS_PER_LINE = 200;
const int MAX_TOKENS_PER_LINE = 4;
const char* const DELIMITER = " ";
struct Square
{
double side;
};
struct Rectangle
{
double length;
double width;
};
struct Circle
{
double radius;
};
struct Cube
{
double side;
};
struct Prism
{
double length;
double width;
double height;
};
struct Cylinder
{
double radius;
double height;
};
void printSquare(Square*);
void printRectangle(Rectangle*);
void printCircle(Circle*);
void printCube(Cube*);
void printPrism(Prism*);
void printCylinder(Cylinder*);
int main()
{
void* shapes[100]={};
int shapeId[100]={};
int i;
ifstream fin;
fin.open("geo.txt");
if (!fin.good())
return 1;
cout<<"Programmer: Jaeyoung Paik ";
cout<<"Description: This program calculate shape ";
cout<<"(sqare,rectangle,circle,cube,prism,cylinder) ";
cout<<"using the input data ";
cout<<endl;
while (!fin.eof())
{
char buf[MAX_CHARS_PER_LINE];
fin.getline(buf, MAX_CHARS_PER_LINE);
int n=0;
const char* token[MAX_TOKENS_PER_LINE] = {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 (i = 0; i < n; i++)
{
if (!((strcmp(token[0],"SQUARE") == 0) || (strcmp(token[0],"RECTANGLE") == 0) || (strcmp(token[0],"CUBE") == 0) || (strcmp(token[0],"CIRCLE") == 0) || (strcmp(token[0],"PRISM") == 0) || (strcmp(token[0],"CYLINDER") == 0)))
{
cout << token[0] << " invalid object" << endl;
break;
}
else if(strcmp(token[0], "SQUARE")==0)
{
if (n == 1)
{
token[1] = "0";
}
Square* s = new Square;
s->side = token[1] == 0? 0.0 : atof(token[1]);
shapes[i] = s;
shapeId[i] = 1;
break;
}
else if(strcmp(token[0], "RECTANGLE") == 0)
{
if (n == 1)
{
token[1] = "0";
token[2] = "0";
}
if (n == 2)
{
token[2] = "0";
}
Rectangle* r = new Rectangle;
r->length = token[1] == 0? 0.0 : atof(token[1]);
r->width = token[2] == 0? 0.0 : atof(token[2]);
shapes[i] = r;
shapeId[i] = 2;
break;
}
else if(strcmp(token[0], "CIRCLE") == 0)
{
if (n == 1)
{
token[1] = "0";
}
Circle* c = new Circle;
c->radius = token[1] == 0? 0.0 : atof(token[1]);
shapes[i] = c;
shapeId[i] = 3;
break;
}
else if(strcmp(token[0], "CUBE") == 0)
{
if (n == 1)
{
token[1] = "0";
}
Cube* c = new Cube;
c->side = token[1] == 0? 0.0 : atof(token[1]);
shapes[i] = c;
shapeId[i] = 4;
break;
}
else if(strcmp(token[0], "PRISM") == 0)
{
if (n == 1)
{
token[1] = "0";
token[2] = "0";
token[3] = "0";
}
if (n == 2)
{
token[2] = "0";
token[3] = "0";
}
if (n == 3)
{
token[3] = "0";
}
Prism* p = new Prism;
p->length = token[1] == 0? 0.0 : atof(token[1]);
p->width = token[2] == 0? 0.0 : atof(token[2]);
p->height = token[3] == 0? 0.0 : atof(token[3]);
shapes[i] = p;
shapeId[i] = 5;
break;
}
else if(strcmp(token[0], "CYLINDER") == 0)
{
if (n == 1)
{
token[1] = "0";
token[2] = "0";
}
if (n == 2)
{
token[2] = "0";
}
Cylinder* c = new Cylinder;
c->radius = token[1] == 0? 0.0 : atof(token[1]);
c->height = token[2] == 0? 0.0 : atof(token[2]);
shapes[i] = c;
shapeId[i] = 6;
break;
}
}
}
fin.close();
int n=0;
for (i = 0; i < n; i++)
{
{
if (shapeId[i] == 1)
printSquare((Square*)shapes[i]);
else if (shapeId[i] == 2)
printRectangle((Rectangle*)shapes[i]);
else if (shapeId[i] == 3)
printCircle((Circle*)shapes[i]);
else if (shapeId[i] == 4)
printCube((Cube*)shapes[i]);
else if (shapeId[i] == 5)
printPrism((Prism*)shapes[i]);
else if (shapeId[i] == 6)
printCylinder((Cylinder*)shapes[i]);
}
}
}
void printSquare(Square* s)
{
double area, perimeter;
area=pow(s->side, 2);
perimeter=4*s->side;
cout.unsetf(ios::fixed|ios::showpoint);
cout << setprecision(6);
cout<<"SQUARE side="<<s->side;
cout.setf(ios::fixed|ios::showpoint);
cout<<setprecision(2)<<" area="<<area<<" perimeter="<<perimeter<<endl;
}
void printRectangle(Rectangle* r)
{
double area, perimeter;
area=(r->length)*(r->width);
perimeter=2*(r->length)+2*(r->width);
cout.unsetf(ios::fixed|ios::showpoint);
cout << setprecision(6);
cout<<"RECTANGLE length="<<(r->length)<<" width="<<(r->width);
cout.setf(ios::fixed|ios::showpoint);
cout<<setprecision(2)<<" area="<<area<<" perimeter="<<perimeter<<endl;
}
void printCircle (Circle* r)
{
double area, circumference;
area=4*atan(1.0)*pow((r->radius), 2);
circumference=2*4*atan(1.0)*(r->radius);
cout.unsetf(ios::fixed|ios::showpoint);
cout << setprecision(6);
cout<<"CIRCLE radius="<<(r->radius);
cout.setf(ios::fixed|ios::showpoint);
cout<<setprecision(2)<<" area="<<area<<" circumference="<<circumference<<endl;
}
void printCube(Cube* s)
{
double surface,volume;
volume=pow((s->side), 3);
surface=6*pow((s->side), 2);
cout.unsetf(ios::fixed|ios::showpoint);
cout << setprecision(6);
cout<<"CUBE side="<<(s->side);
cout.setf(ios::fixed|ios::showpoint);
cout<<setprecision(2)<<" surface area="<<surface<<" volume="<<volume<<endl;
}
void printPrism(Prism* p)
{
double surface,volume;
volume=(p->length)*(p->width)*(p->height);
surface=2*(p->length)*(p->width)+2*(p->width)*(p->height)+2*(p->height)*(p->length);
cout.unsetf(ios::fixed|ios::showpoint);
cout << setprecision(6);
cout<<"PRISM length="<<(p->length)<<" width="<<(p->width)<<" height="<<(p->height);
cout.setf(ios::fixed|ios::showpoint);
cout<<setprecision(2)<<" surface area="<<surface<<" volume="<<volume<<endl;
}
void printCylinder(Cylinder* c)
{
double surface,volume;
volume=4*atan(1.0)*pow((c->radius), 2)*(c->height);
surface=2*4*atan(1.0)*pow((c->radius), 2)+2*4*atan(1.0)*(c->radius)*(c->height);
cout.unsetf(ios::fixed|ios::showpoint);
cout << setprecision(6);
cout<<"CYLINDER radius="<<(c->radius)<<" height="<<(c->height);
cout.setf(ios::fixed|ios::showpoint);
cout<<setprecision(2)<<" surface area="<<surface<<" volume="<<volume<<endl;
}
input
SQUARE 14.5
RECTANGLE 14.5 4.65
CIRCLE 14.5
CUBE 13
PRISM 1 2 3
SPHERES 2.4
CYLINDER 1.23
CYLINDER 50 1.23
TRIANGLE 1.2 3.2
output
Programmer: Jaeyoung Paik
Description: This program calculate shape
(sqare,rectangle,circle,cube,prism,cylinder)
using the input data
SPHERES invalid object
TRIANGLE invalid object
i want to make output
SQUARE side=14.5 area=210.25 perimeter=58.00
RECTANGLE length=14.5 width=4.65 area=67.43 perimeter=38.30
CIRCLE radius=14.5 area=660.52 circumference=91.11
CUBE side=13 surface area=1014.00 volume=2197.00
PRISM length=1 width=2 height=3 surface area=22.00 volume=6.00
SPHERES invalid object
CYLINDER radius=1.23 height=0 surface area=9.51 volume=0.00
CYLINDER radius=50 height=1.23 surface area=16094.37 volume=9660.39
TRIANGLE invalid object
Explanation / Answer
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include <fstream>
using std::ifstream;
#include <iomanip>
using std::setprecision;
using std::ios;
#include <cmath>
#include <cstring>
#include <cstdlib>
const int MAX_CHARS_PER_LINE = 200;
const int MAX_TOKENS_PER_LINE = 4;
const char* const DELIMITER = " ";
struct Square
{
double side;
};
struct Rectangle
{
double length;
double width;
};
struct Circle
{
double radius;
};
struct Cube
{
double side;
};
struct Prism
{
double length;
double width;
double height;
};
struct Cylinder
{
double radius;
double height;
};
void printSquare(Square*);
void printRectangle(Rectangle*);
void printCircle(Circle*);
void printCube(Cube*);
void printPrism(Prism*);
void printCylinder(Cylinder*);
void printError(char* str)
{
cout << str << " invalid object" << endl;
}
int main()
{
void* shapes[100]={};
int shapeId[100]={};
int i;
ifstream fin;
fin.open("input.txt");
if (!fin.good())
return 1;
cout<<"Programmer: Jaeyoung Paik ";
cout<<"Description: This program calculate shape ";
cout<<"(square,rectangle,circle,cube,prism,cylinder) ";
cout<<"using the input data ";
cout<<endl;
int n=0;
int count = 0;
while (!fin.eof())
{
char buf[MAX_CHARS_PER_LINE];
fin.getline(buf, MAX_CHARS_PER_LINE);
if(isspace(buf[0]))
continue;
const char* token[MAX_TOKENS_PER_LINE] = {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;
}
}
if (!((strcmp(token[0],"SQUARE") == 0) || (strcmp(token[0],"RECTANGLE") == 0) || (strcmp(token[0],"CUBE") == 0) || (strcmp(token[0],"CIRCLE") == 0) || (strcmp(token[0],"PRISM") == 0) || (strcmp(token[0],"CYLINDER") == 0)))
{
//cout << token[0] << " invalid object" << endl;
char *str;
str = new char[20];
strcpy(str,token[0]);
shapes[count] = str;
shapeId[count] = 0;
//break;
}
else if(strcmp(token[0], "SQUARE")==0)
{
if (n == 1)
{
token[1] = "0";
}
Square* s = new Square;
s->side = token[1] == 0? 0.0 : atof(token[1]);
shapes[count] = s;
shapeId[count] = 1;
//printSquare((Square*)shapes[count]);
//break;
}
else if(strcmp(token[0], "RECTANGLE") == 0)
{
if (n == 1)
{
token[1] = "0";
token[2] = "0";
}
if (n == 2)
{
token[2] = "0";
}
Rectangle* r = new Rectangle;
r->length = token[1] == 0? 0.0 : atof(token[1]);
r->width = token[2] == 0? 0.0 : atof(token[2]);
shapes[count] = r;
shapeId[count] = 2;
//printRectangle((Rectangle*)shapes[count]);
//break;
}
else if(strcmp(token[0], "CIRCLE") == 0)
{
if (n == 1)
{
token[1] = "0";
}
Circle* c = new Circle;
c->radius = token[1] == 0? 0.0 : atof(token[1]);
shapes[count] = c;
shapeId[count] = 3;
//printCircle((Circle*)shapes[count]);
//break;
}
else if(strcmp(token[0], "CUBE") == 0)
{
if (n == 1)
{
token[1] = "0";
}
Cube* c = new Cube;
c->side = token[1] == 0? 0.0 : atof(token[1]);
shapes[count] = c;
shapeId[count] = 4;
//printCube((Cube*)shapes[count]);
//break;
}
else if(strcmp(token[0], "PRISM") == 0)
{
if (n == 1)
{
token[1] = "0";
token[2] = "0";
token[3] = "0";
}
if (n == 2)
{
token[2] = "0";
token[3] = "0";
}
if (n == 3)
{
token[3] = "0";
}
Prism* p = new Prism;
p->length = token[1] == 0? 0.0 : atof(token[1]);
p->width = token[2] == 0? 0.0 : atof(token[2]);
p->height = token[3] == 0? 0.0 : atof(token[3]);
shapes[count] = p;
shapeId[count] = 5;
//printPrism((Prism*)shapes[count]);
//break;
}
else if(strcmp(token[0], "CYLINDER") == 0)
{
if (n == 1)
{
token[1] = "0";
token[2] = "0";
}
if (n == 2)
{
token[2] = "0";
}
Cylinder* c = new Cylinder;
c->radius = token[1] == 0? 0.0 : atof(token[1]);
c->height = token[2] == 0? 0.0 : atof(token[2]);
shapes[count] = c;
shapeId[count] = 6;
//printCylinder((Cylinder*)shapes[count]);
//break;
}
count++;
}
fin.close();
for (i = 0; i < count; i++)
{
if (shapeId[i] == 1)
printSquare((Square*)shapes[i]);
else if (shapeId[i] == 2)
printRectangle((Rectangle*)shapes[i]);
else if (shapeId[i] == 3)
printCircle((Circle*)shapes[i]);
else if (shapeId[i] == 4)
printCube((Cube*)shapes[i]);
else if (shapeId[i] == 5)
printPrism((Prism*)shapes[i]);
else if (shapeId[i] == 6)
printCylinder((Cylinder*)shapes[i]);
else
printError((char*)shapes[i]);
}
system("pause");
return 0;
}
void printSquare(Square* s)
{
double area, perimeter;
area=pow(s->side, 2);
perimeter=4*s->side;
cout.unsetf(ios::fixed|ios::showpoint);
cout << setprecision(6);
cout<<"SQUARE side="<<s->side;
cout.setf(ios::fixed|ios::showpoint);
cout<<setprecision(2)<<" area="<<area<<" perimeter="<<perimeter<<endl;
}
void printRectangle(Rectangle* r)
{
double area, perimeter;
area=(r->length)*(r->width);
perimeter=2*(r->length)+2*(r->width);
cout.unsetf(ios::fixed|ios::showpoint);
cout << setprecision(6);
cout<<"RECTANGLE length="<<(r->length)<<" width="<<(r->width);
cout.setf(ios::fixed|ios::showpoint);
cout<<setprecision(2)<<" area="<<area<<" perimeter="<<perimeter<<endl;
}
void printCircle (Circle* r)
{
double area, circumference;
area=4*atan(1.0)*pow((r->radius), 2);
circumference=2*4*atan(1.0)*(r->radius);
cout.unsetf(ios::fixed|ios::showpoint);
cout << setprecision(6);
cout<<"CIRCLE radius="<<(r->radius);
cout.setf(ios::fixed|ios::showpoint);
cout<<setprecision(2)<<" area="<<area<<" circumference="<<circumference<<endl;
}
void printCube(Cube* s)
{
double surface,volume;
volume=pow((s->side), 3);
surface=6*pow((s->side), 2);
cout.unsetf(ios::fixed|ios::showpoint);
cout << setprecision(6);
cout<<"CUBE side="<<(s->side);
cout.setf(ios::fixed|ios::showpoint);
cout<<setprecision(2)<<" surface area="<<surface<<" volume="<<volume<<endl;
}
void printPrism(Prism* p)
{
double surface,volume;
volume=(p->length)*(p->width)*(p->height);
surface=2*(p->length)*(p->width)+2*(p->width)*(p->height)+2*(p->height)*(p->length);
cout.unsetf(ios::fixed|ios::showpoint);
cout << setprecision(6);
cout<<"PRISM length="<<(p->length)<<" width="<<(p->width)<<" height="<<(p->height);
cout.setf(ios::fixed|ios::showpoint);
cout<<setprecision(2)<<" surface area="<<surface<<" volume="<<volume<<endl;
}
void printCylinder(Cylinder* c)
{
double surface,volume;
volume=4*atan(1.0)*pow((c->radius), 2)*(c->height);
surface=2*4*atan(1.0)*pow((c->radius), 2)+2*4*atan(1.0)*(c->radius)*(c->height);
cout.unsetf(ios::fixed|ios::showpoint);
cout << setprecision(6);
cout<<"CYLINDER radius="<<(c->radius)<<" height="<<(c->height);
cout.setf(ios::fixed|ios::showpoint);
cout<<setprecision(2)<<" surface area="<<surface<<" volume="<<volume<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.