hahahia

1주차 본문

Language/C++

1주차

hahahia 2011. 6. 27. 09:33

#include <iostream>

using namespace std;

 

typedef unsigned short int USHORT;

typedef unsigned long int ULONG;

enum BOOL {FALSE, TRUE};

enum CHOICE {DrawRect = 1, GetArea, GetPerim, ChangeDimensions, Quit};

 

class Rectangle{

public:

        Rectangle(USHORT width, USHORT height);

        ~Rectangle();

 

        USHORT GetHeight() const

        {

               return itsHeight;

        }

        USHORT GetWidth() const

        {

               return itsWidth;

        }

        ULONG GetArea() const

        {

               return itsHeight * itsWidth;

        }

        ULONG GetPerim() const

        {

               return 2*itsHeight + 2*itsWidth;

        }

        void SetSize(USHORT newWidth, USHORT newHeight);

 

 

        void DrawShape() const;

 

private:

        USHORT itsWidth;

        USHORT itsHeight;

};

void Rectangle::SetSize(USHORT newWidth, USHORT newHeight)

{

        itsWidth = newWidth;

        itsHeight = newHeight;

}

Rectangle::Rectangle(USHORT width, USHORT height)

{

        itsWidth = width;

        itsHeight = height;

}

Rectangle::~Rectangle()

{

}

USHORT DoMenu();

void DoDrawRect(Rectangle);

void DoGetArea(Rectangle);

void DoGetPerim(Rectangle);

 

void main()

{

        Rectangle theRect(30, 5);

 

        USHORT choice = DrawRect;

        USHORT fQuit = FALSE;

 

        while(!fQuit)

        {

               choice = DoMenu();

               if(choice < DrawRect || choice > Quit)

               {

                       cout << endl << "Invalid Choice, please try again." << endl ;

                       continue;

               }

               switch(choice)

               {

               case DrawRect:

                       DoDrawRect(theRect);

                       break;

               case GetArea:

                       DoGetArea(theRect);

                       break;

               case GetPerim:

                       DoGetPerim(theRect);

                       break;

               case ChangeDimensions:

                       USHORT newLength, newWidth;

                       cout << endl << "New width: " ;

                       cin >> newWidth;

                       cout << "New height: " ;

                       cin >> newLength;

                       theRect.SetSize(newWidth, newLength);

                       DoDrawRect(theRect);

                       break;

               case Quit:

                       fQuit = TRUE;

                       cout << "Exiting... " << endl;

                       break;

               default:

                       cout << "Error in choice! " << endl;

                       fQuit = TRUE;

                       break;

               }

        }

}

 

USHORT DoMenu()

{

        USHORT choice;

        cout << endl << "***Menu*** " << endl;

        cout << "(1) Draw Rectangle " << endl;

        cout << "(2) Area " << endl;

        cout << "(3) Perimeter " << endl;

        cout << "(4) Resize " << endl;

        cout << "(5) Quit " << endl;

 

        cin >> choice;

        return choice;

}

 

void DoDrawRect(Rectangle theRect)

{

        USHORT height = theRect.GetHeight();

        USHORT width = theRect.GetWidth();

 

        for(USHORT i = 0; i<height; i++)

        {

               for(USHORT j=0; j<width; j++)

               {

                       cout << "*";

               }

               cout << endl;

        }

}

 

void DoGetArea(Rectangle theRect)

{

        cout << "Area: " << theRect.GetArea() << endl;

}

 

void DoGetPerim(Rectangle theRect)

{

        cout << "Perimeter : " << theRect.GetPerim() << endl;

}

 


Comments