当前位置:编程学习 > C/C++ >>

《21天学通C++》第一周复习清单

书籍看这里:html">http://www.zzzyk.com/ebook/201007/19457.html

#include <iostream>

using namespace std;

enum CHOICE { DrawRect = 1, GetArea, GetPerim,

ChangeDimensions, Quit};

 

// Rectangle class declaration

class Rectangle

{

public:

// construcors

Rectangle(int width, int height);

~Rectangle();


// accessors

int GetHeight() const { return itsHeight; }

int GetWidth() const { return itsWidth; }

int GetArea() const { return itsHeight * itsWidth; }

int GetPerim() const { return 2*itsHeight + 2*itsWidth; }

void SetSize(int newWidth, int newHeight);


// Misc. methods


private:

int itsWidth;

int itsHeight;

};

 

// Class method implementations

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

{

itsWidth = newWidth;

itsHeight = newHeight;

}

 

Rectangle::Rectangle(int width, int height)

{

itsWidth = width;

itsHeight = height;

}

 

Rectangle::~Rectangle() {}

 

int DoMenu();

void DoDrawRect(Rectangle);

void DoGetArea(Rectangle);

void DoGetPerim(Rectangle);

 

int main()

{

// initialize a rectangle to 30,5

Rectangle theRect(30,5);


int choice = DrawRect;

int fQuit = false;


while(!fQuit)

{

choice = DoMenu();

if(choice < DrawRect || choice > Quit)

{

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

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... ";

break;

default:

cout << "Error in choice! ";

fQuit = true;

break;

}// end switch

}// end while

return 0;

}// end main

 

int DoMenu()

{

int choice;

cout << "     *** Munu ***  ";

cout << "(1) Draw Rectangle ";

cout << "(2) Area ";

cout << "(3) Perimeter ";

cout << "(4) Resize ";

cout << "(5) Quit ";


cin >> choice;

return choice;

}

 

void DoDrawRect(Rectangle theRect)

{

int height = theRect.GetHeight();

int width = theRect.GetWidth();


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

{

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

cout << "*";

cout << " ";

}

}

 

void DoGetArea(Rectangle theRect)

{

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

}

 

void DoGetPerim(Rectangle theRect)

{

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

}

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,