• 网易云课堂_C++程序设计入门(下)_第9单元:白公曾咏牡丹芳,一种鲜妍独“异常”_第9单元


    第9单元 - 作业5:OJ编程 - 使用异常进行图形类的错误处理

    �返回
     

    温馨提示:

    1.本次作业属于Online Judge题目,提交后由系统即时判分。

    2.学生可以在作业截止时间之前不限次数提交答案,系统将取其中的最高分作为最终成绩。

    基于第8单元的作业内容,为图形类添加异常处理代码

    依照学术诚信条款,我保证此作业是本人独立完成的。

    1
    基于第8单元作业2的代码,为图形类添加异常处理的代码。(5分)
    时间限制: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(), '
    ');
    	cin.get();
    #endif
    	return 0;
    }
    
  • 相关阅读:
    SQL exist
    Mac 加密软件
    appcon 图标打包
    启动页面
    获取第三方的授权
    实现一个简单但是好看的滑动栏
    实现聊天界面的代码
    简单的设置圆形按钮
    自己搭建服务端前透析
    A1056Mice and Rice (25分)
  • 原文地址:https://www.cnblogs.com/denggelin/p/7137349.html
Copyright © 2020-2023  润新知