1. 程式人生 > >網易雲課堂_C++程序設計入門(下)_第9單元:白公曾詠牡丹芳,一種鮮妍獨“異常”_第9單元 - 作業5:OJ編程 - 使用異常進行圖形類的錯誤處理

網易雲課堂_C++程序設計入門(下)_第9單元:白公曾詠牡丹芳,一種鮮妍獨“異常”_第9單元 - 作業5:OJ編程 - 使用異常進行圖形類的錯誤處理

using define 幫助 發生 半角 etc mooc sub private

第9單元 - 作業5:OJ編程 - 使用異常進行圖形類的錯誤處理

返回

溫馨提示:

1.本次作業屬於Online Judge題目,提交後由系統即時判分。

2.學生可以在作業截止時間之前不限次數提交答案,系統將取其中的最高分作為最終成績。

基於第8單元的作業內容,為圖形類添加異常處理代碼

依照學術誠信條款,我保證此作業是本人獨立完成的。

1 基於第8單元作業2的代碼,為圖形類添加異常處理的代碼。(5分)

題目具體內容參見: 第9單元 - 作業5說明:【OJ - 使用異常進行圖形類的錯誤處理】

時間限制:500ms內存限制:32000kb

#include <iostream>
#include <string>
#include <sstream>
#include <stdexcept>

using namespace std;

class MyShape {
protected:
	int R_, G_, B_;

	string colorToString() {
		stringstream ss;
		ss << R_ << " " << G_ << " " << B_;
		return ss.str();
	}
public:
	void setColor(int R, int G, int B) {
		R_ = R; G_ = G, B_ = B;
	}
	int getR() {
		return R_;
	}
	int getG() {
		return G_;
	}
	int getB() {
		return B_;
	}
	virtual void Draw() = 0;
	MyShape() {
		R_ = 255; G_ = 255, B_ = 255;
	}
};

class MyCircle : public MyShape {
private:
	int x_, y_, radius_;
	int min;

public:
	MyCircle(int x, int y, int radius) {
		x_ = x;
		y_ = y;
		radius_ = radius;
	}

	MyCircle() {
		x_ = y_ = 200;
		radius_ = 100;
	}

	MyCircle(MyCircle& aCircle) {
		x_ = aCircle.x_;
		y_ = aCircle.y_;
		radius_ = aCircle.radius_;
		R_ = aCircle.getR();
		G_ = aCircle.getG();
		B_ = aCircle.getB();
	}
	void setCenter(int x, int y) {
		x_ = x;
		y_ = y;
	}

	void setRadius(int radius) {
		//3. 修改setRadius(int radius)函數,當radius的值小於等於0時,拋出underflow_error異常。
		//3.1 註意:僅僅在setRadius函數中檢查radius的值是否合法;在其他的函數中,包括MyShape的構造函數中,並不檢查radius的值是否合法)
		//3.2  在創建underflow_error異常對象時,給其構造函數傳遞一個字符串
		//3.2 字符串的內容是:"Radius underflow: RADIUS",(不包含雙引號)。其中的“RADIUS”是發生異常時setRadius()函數所接受的參數
		//2.3 字符串內容示例:"Radius underflow: -100" (實際輸出不包含雙引號;單詞間用一個空格分隔;冒號為半角字符,冒號前無空格;冒號與後面的數之間有一個空格。 - 100是傳遞給setRadius()函數的參數)

		if (radius > 0)
		{
			radius_ = radius;
		}
		else
		{
			stringstream ss;
			ss << "Radius underflow: " << radius;
			throw underflow_error(ss.str());
		}
	}

	void Draw() {
	}

	//----在此處添加關系運算符  >、<、>=、<=、==、!=  的重載原型聲明
	int& operator[](const int &index);
};

//----在此處添加關系運算符的重載定義
int& MyCircle::operator[](const int & index)
{
	//2. 修改數組下標運算符的重載代碼,當下標超出範圍時,拋出range_error異常 。(不再返回帶符號整型數中最小的值)
	//2.1 在創建range_error異常對象時,給其構造函數傳遞一個字符串
	//2.2 字符串的內容是:"Index exceeds scope: INDEX",(不包含雙引號)。其中的“INDEX”是發生異常時數組下標運算符所接受的參數
	//2.3 字符串內容示例:"Index exceeds scope: 3" (實際輸出不包含雙引號;單詞間用一個空格分隔;冒號為半角字符,冒號前無空格;冒號與後面的數字之間有一個空格。數字3是傳遞給數組下標運算符的參數)

	if (index == 0)
	{
		return x_;
	}
	else if (index == 1)
	{
		return y_;
	}
	else if (index == 2)
	{
		return radius_;
	}
	else
	{
		stringstream ss;
		ss << "Index exceeds scope: " << index;
		throw range_error(ss.str());
	}
}

int main() {
	int r1 = 0, r2 = 0;
	cin >> r1 >> r2;
	MyCircle c1, c2;
	try {
		c1.setRadius(r1);
		c2.setRadius(r2);
		for (int i = 0; i <= 3; i++) {
			cout << c1[i] << endl;
		}
	}
	catch (underflow_error& e) {
		cout << e.what() << endl;
	}
	catch (runtime_error& e) {
		cout << e.what() << endl;
	}

	try {
		c2[r1 / (r2 == 0 ? 1 : r2)] = 321;
		for (int i = 3; i >= 0; i--) {
			cout << c2[i - 1] << endl;
		}
	}
	catch (exception& e) {
		cout << e.what() << endl;
	}

	// GCC及VC編譯器在調試模式下會暫停,便於查看運行結果
#if ( defined(__DEBUG__) || defined(_DEBUG) )
	cin.ignore(numeric_limits<streamsize>::max(), ‘\n‘);
	cin.get();
#endif
	return 0;
}

網易雲課堂_C++程序設計入門(下)_第9單元:白公曾詠牡丹芳,一種鮮妍獨“異常”_第9單元 - 作業5:OJ編程 - 使用異常進行圖形類的錯誤處理