c++練習1
/************
./header/class.hpp
************/
/************
./src/test1.hpp
************/
./header/class.hpp
************/
- #include
- using namespace std;
-
- /**
- * 1:
- */
- class Rectangle {
- public:
- // Rectangle
- Rectangle(int width, int height);
-
- // Rectangle
- ~Rectangle() {};
-
- // Rectangle
- int GetHeight() const {return itsHeight;}
-
- // Rectangle
- int GetWidth() const {return itsWidth;}
-
- // Rectangle
- int GetArea() const {return itsHeight * itsWidth;}
-
- // Rectangle
- int GetPerim() const {return 2 * itsHeight + 2 * itsWidth;}
-
- // Rectangle
- void SetSize(int newWidth, int newHeight);
-
- private:
- int itsWidth;
- int itsHeight;
- };
-
- // Rectangle
- void Rectangle::SetSize(int newWidth, int newHeight) {
- itsWidth = newWidth;
- itsHeight = newHeight;
- }
-
- // Rectangle
- Rectangle::Rectangle(int width, int height) {
- itsWidth = width;
- itsHeight = height;
- }
-
- //
- int DoMenu();
- //
- void DoDrawRect(Rectangle);
- //
- void DoGetArea(Rectangle);
- //
- void DoGetPerim(Rectangle);
/************
./src/test1.hpp
************/
- #include ../header/class.hpp
-
- // , 1
- enum CHOICE {
- DrawRect = 1,
- GetArea,
- GetPerim,
- ChangeDimensions,
- Quit
- };
-
- int main() {
- //
- Rectangle theRect(30, 5);
- //
- int choice = DrawRect;
- //
- bool fQuit = false;
-
- while (!fQuit) {
- //
- choice = DoMenu();
-
- // ,
- if (choice < DrawRect || choice > Quit) {
- cout <<
Invalid Choice, try again.;
- cout << endl << endl;
- continue;
- }
-
- //
- switch(choice) {
- case DrawRect:
- DoDrawRect(theRect);
- break;
- case GetArea:
- DoGetArea(theRect);
- break;
- case GetPerim:
- DoGetPerim(theRect);
- break;
- case ChangeDimensions:
- int newLength, newWidth;
- cout <<
New width:;
- cin >> newWidth;
- cout <<
New height:;
- cin >> newLength;
-
- theRect.SetSize(newWidth, newLength);
- DoDrawRect(theRect);
- break;
- case Quit:
- fQuit = true;
- cout <<
Exiting.. << endl << endl;
- break;
- default:
- cout <<
Error in choice << endl;
- fQuit = true;
- break;
- }
- }
- return 0;
- }
-
- //
- int DoMenu() {
- int choice;
-
- cout << endl << endl;
- cout << *** 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) {
- int height = theRect.GetHeight();
- int width = theRect.GetWidth();
- int i, j;
-
- for (i = 0; i < height; i++) {
- for (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;
- }