程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 《21天學通C++》第一周復習清單

《21天學通C++》第一周復習清單

編輯:C++入門知識

書籍看這裡:html">http://www.BkJia.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;

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved