안상형 님이 쓰신 글 :
: switch 문에서 case로 도형을 선택하여 해당 도형에 관한 부피와 넓이를 구하는 식을 구하려고 합니다.
: 여기서 각각의 case 에다가 해당 클래스를 입력해줘야하는데, 자꾸 막힌다고 해야할까요?
: 코드 수정좀 부탁드리겠습니다.
: 죄송합니다.
:
:
: #include
: using namespace std;
: class Rectangle
: {
: protected:
: float lengtha,lengthb, height;
: public:
: Rectangle() : lengtha(0.0),lengthb(0.0), height(0.0)
: {
: cout << "가로를 입력하시오: ";
: cin >> lengtha;
: cout << "세로를 입력하시오";
: cin >> lengthb;
: cout << "높이를 입력하시오";
: cin >> height;
: }
: };
: class Recvolume : public Rectangle
: {
: public:
: float volume()
: {
: return lengtha*lengthb*height;
: }
: };
: class RecArea : public Rectangle
: {
: public:
: float area()
: {
: return (lengtha*height) * 2 + (lengtha*lengthb) * 2 + (lengthb*height) * 2;
: }
: };
: class Square
: {
: protected:
: float lengtha, lengthb, height;
: public:
: Square() : lengtha(0.0), lengthb(0.0), height(0.0)
: {
: cout << "가로를 입력하시오: ";
: cin >> lengtha;
: cout << "세로를 입력하시오";
: cin >> lengthb;
: cout << "높이를 입력하시오";
: cin >> height;
: }
: };
: class Squavolume : public Square
: {
: public:
: float volume()
: {
: return lengtha*lengthb*height;
: }
: };
: class SquaArea : public Square
: {
: public:
: float area()
: {
: return (lengtha*height) * 2 + (lengtha*lengthb) * 2 + (lengthb*height) * 2;
: }
: };
: class Pitch
: {
: protected:
: float r ;
: public:
: Pitch() : r(0.0)
: {
: cout << "반지름을 입력하시오(π=3.14로 계산) ";
: cin >> r;
: }
: };
: class Pitvolume : public Pitch
:
: {
: public:
: double volume()
: {
: return 4 / 3 * 3.14*r*r;
: }
: };
:
: class PitArea : public Pitch
: {
: public:
: double area()
: {
: return 4 * 3.14 * r * r;
: }
: };
: class Cone
: {
: protected:
: float r ,height, l;
: public:
: Cone(): r(0.0), height(0.0), l(0.0)
: {
: cout << "반지름을 입력하시오(π=3.14로 계산) ";
: cin >> r;
: cout << "높이를 입력하시오: ";
: cin >> height;
: cout << "호의 길이를 구하시오: ";
: cin >> l;
: }
: };
: class ConeVolume : public Cone
: {
: public:
: double volume()
: {
: return 1 / 3 * 3.14*r*r*height;
: }
: };
: class ConeArea : public Cone
: {
: public:
: double area()
: {
: return 3.14*r*r + 3.14*r*l;
: }
: };
:
: int main()
: {
: char menu;
:
:
: while (true)
: {
: //메뉴출력
: cout << "1.직육면체 부피, 넓이 구하기\n";
: cout << "2.정육면체 부피, 넓이 구하기\n";
: cout << "3.구 부피, 넓이 구하기\n";
: cout << "4.원뿔 부피, 넓이 구하기\n";
: cout << "5.종료\n";
: cout << "메뉴 선택: ";
: cin >> menu;
:
: if (menu == '5')
: break;
:
: switch (menu)
: {
:
: case '1':{
: Rectangle b;
: cout << "Enter data for first rectangle to find area.\n";
: Recvolume a;
: cout << "직육면체의 부피는: "<< << "입니다";
:
: cout << "직육면체의 넓이는: ";
: }
: break;
: case '2':{
:
: cout << "정육면체의 부피는: ";
:
: cout << "정육면체의 넓이는: ";
: }
: break;
: case'3':{
:
: cout << "구의 부피는: ";
:
: cout << "구의 넓이는: ";
: }
: break;
: case '4':{
:
: cout << "원뿔의 부피는: ";
:
: cout << "원뿔의 넓이는: ";
: }
: break;
: }
: }
:
: return 0;
: }
:
#include < iostream >
using namespace std;
class Shape
{
public:
Shape() : lenA(0), lenB(0), height(0) {}
virtual char *type() = 0;
virtual float volume() = 0;
virtual float area() = 0;
virtual void input() = 0;
protected:
float lenA, lenB, height;
};
class Rectangle : public Shape
{
public:
char* type() { return "Rectangle "; }
void input()
{
cout << "lenA: "; cin >> lenA;
cout << "lenB: "; cin >> lenB;
cout << "height: "; cin >> height;
}
float volume() { return lenA * lenB * height; }
float area()
{
return ((lenA * height) + (lenA * lenB) + (lenB * height)) * 2;
}
};
class Square : public Rectangle
{
public:
char* type() { return "Square "; }
};
class Pitch : public Shape
{
public:
Pitch() : r(0) {}
char* type() { return "Pitch" ; }
void input() { cout << "r: "; cin >> r; }
float volume() { return 4 / 3 * 3.14 * r * r; }
float area() { return 4 * 3.14 * r * r; }
protected:
float r;
};
class Cone : public Pitch
{
public:
Cone() : l(0) {}
char* type() { return "Cone "; }
void input()
{
cout << "r: "; cin >> r;
cout << "height: "; cin >> height;
cout << "ho: "; cin >> l;
}
float volume() { return 1 / 3 * 3.14 * r * r * height; }
float area() { return 3.14 * r * r + 3.14 * r * l; }
protected:
float l;
};
Shape* CreateObject(int index)
{
Shape *obj;
switch(index)
{
case 1: obj = new Rectangle(); break;
case 2: obj = new Square(); break;
case 3: obj = new Pitch(); break;
case 4: obj = new Cone(); break;
}
return obj;
}
int main(int argc, char* argv[])
{
int menu;
while (true)
{
// menu
cout << endl;
cout << "1. Rectangle" << endl;
cout << "2. Square" << endl;
cout << "3. Pitch" << endl;
cout << "4. Cone" << endl;
cout << "5. Quit" << endl;
cout << "select menu: " << endl;
cin >> menu;
if (menu == 5)
break;
Shape *shape = CreateObject(menu);
shape->input();
cout << "type: "<< shape->type() << endl;
cout << "volume: " << shape->volume() << endl;
cout << "area: " << shape->area() << endl;
delete shape;
}
return 0;
}
|