第十一周上机任务项目4

01./*      
02.* 程序的版权和版本声明部分      
03.* Copyright (c)2013, 烟台大学计算机学院学生      
04.* All rightsreserved.      
05.* 文件名称: point.cpp                                 
06.* 作    者:赵冠哲                                  
07* 完成日期:2013年5月9日      
08.* 版本号: v1.0            
09.* 输入描述:      
10.* 问题描述:    
11.*/
#include<iostream.h>
#include<Cmath>
#define PI 3.1415926//定义符号常量
class Point //定义坐标点类
{
private:
    double x;//横坐标
    double y;//纵坐标
public:
    Point(double x0,double y0) {x=x0; y=y0;}
    ~Point ()
    {cout<<"Destructor called"<<endl;}
    double get_x(){return x;} //公用基类的私有数据成员在派生类中不能访问,需要获取私有数据成员的公共接口
    double get_y(){return y;}
    friend ostream &operator << (ostream &, Point &);//声明运算符"<<"的重载
};

//利用坐标点类定义圆类, 其基类的数据成员表示圆的中心
class Circle: public Point
{
private:
    double d;
public:
    Circle(double xx,double yy,double dd): Point(xx,yy)
    {
        d=dd;
    }
    ~Circle()
    {
    }
    friend ostream &operator << (ostream &, Circle &);
    double get_d(){return d;}
};
class Cylinder: public Circle
{
private:
    double h;
public:
    Cylinder(double xx,double yy,double dd,double hh): Circle (xx,yy,dd)
    {
        h=hh;
    }
   ~Cylinder()
    {
    }
    friend ostream &operator << (ostream &,Cylinder &);
    double get_h(){return h;}
    double superficial_area();//表面积
    double volume();//圆柱体的体积
};
ostream &operator << (ostream & output, Point & c)
{
    output<<"点的横坐标为:"<<c.x<<endl;
    output<<"点的纵坐标为:"<<c.y<<endl;
    return output;
}
ostream &operator << (ostream & output, Circle & c)
{
    output<<"圆的半径为:"<<c.get_d()<<endl;
    output<<"圆的圆心为"<<"("<<c.get_x()<<","<<c.get_y()<<")"<<endl;
    return output;
}
ostream &operator << (ostream & output,Cylinder & c)
{
    output<<"圆柱的高为:"<<c.get_h()<<endl;
    output<<"圆柱底面圆的半径为:"<<c.get_d()<<endl;
    output<<"圆柱底面圆的圆心为"<<"("<<c.get_x()<<","<<c.get_y()<<")"<<endl;
    return output;
}

//圆柱体的表面积
double  Cylinder::superficial_area()
{
    double s=2*PI*get_d()*get_d()+2*PI*get_d()*get_h();
     return s;
}

//圆柱体的体积
double  Cylinder::volume()
{
    double v=PI*get_d()*get_d()*get_h();
    return v;
}
int main()
{
    Point p(1,1);
    cout<<p;
    Circle ci(1,2,3);
    cout<<ci;
    Cylinder cy(1,2,4,6);
    cout<<cy;
    cout<<"圆柱的体积为:"<<cy.volume ()<<endl;
    cout<<"圆柱的表面积为:"<<cy.superficial_area ()<<endl;
       return 0;
}


运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴为您提供 Python 第十一上机实践。本章的实践内容主要是关于文件处理、异常处理和模块的使用。 1. 文件处理 第一道题目是要求读取一个文件并统计其中单词的数量。您可以按照以下步骤完成任务: (1)使用 open() 函数打开文件。 (2)使用 read() 函数读取文件内容。 (3)使用 split() 函数将文件内容按照空格分割成单词。 (4)使用 len() 函数统计单词数量。 下面是一个示例代码: ```python with open('file.txt', 'r') as f: content = f.read() words = content.split(' ') count = len(words) print(f'The file has {count} words.') ``` 2. 异常处理 第二道题目是要求编写一个函数,将两个数相除并返回结果。但如果除数为 0,则应该抛出一个 ZeroDivisionError 异常。您可以按照以下步骤完成任务: (1)定义一个函数。 (2)使用 if 语句判断除数是否为 0。 (3)如果除数为 0,则使用 raise 语句抛出一个 ZeroDivisionError 异常。 (4)如果除数不为 0,则返回两个数的商。 下面是一个示例代码: ```python def divide(num1, num2): if num2 == 0: raise ZeroDivisionError('division by zero') else: return num1 / num2 try: result = divide(10, 0) except ZeroDivisionError as e: print(e) ``` 3. 模块的使用 第三道题目是要求使用 Python 标准库中的 random 模块生成一个随机数。您可以按照以下步骤完成任务: (1)使用 import 语句导入 random 模块。 (2)使用 random 模块中的 randint() 函数生成一个随机数。 下面是一个示例代码: ```python import random num = random.randint(1, 100) print(f'The random number is {num}.') ``` 希望这些实践题可以帮助您更好地理解 Python 文件处理、异常处理和模块的使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值