综合实验8

InputOutput

描述

设计一个保存你个人信息的类,包含姓名和年龄。并使用以下代码测试

int main()
{
    string    name;
    int    year;
    cin >> name >> year;
    PersonInfo info(name, year);
    cout << "I am " << info.Name() << ", " << info.Age() << " years old.\n";
    return 0;
}

输入

姓名年龄

输出

见样例输出

输入样例 1 

master
999

输出样例 1

I am master, 999 years old.

提示

  1. 已有main函数,无需提交
  2. 不使用类,没有分数
  3. ","后面有一个空格,"."结束,没有其它格式输出
 #include<iostream> 
#include<string> 
using namespace std;
class PersonInfo {
public:
    string name;
    int year;
    PersonInfo(string& n, int y)
    {
        name = n;
        year = y;
    }
    string Name() { return name; }
    int Age() { return year; }
};
int main()
{
    string name;
    int  year;
    cin >> name >> year;
    PersonInfo info(name, year);
    cout << "I am " << info.Name() << ", " << info.Age() << " years old.\n";
    return 0;
}

​

Composite

描述

计算机包含CPU和硬盘。请设计Computer、CPU、Disk类。并满足以下测试

int main()
{
    string cpuType, diskType;
    double frequency, capacity;
    cin >> cpuType >> frequency >> diskType >> capacity;
    CPU cpu(cpuType, frequency);
    Disk disk(diskType, capacity);
    Computer computer(cpu, disk);

    computer.Print();
    return 0;
}

输入

cpu类型 cpu主频 disk类型 disk容量

输出

见样例

输入样例 1 

i7 2.9 ST 2

输出样例 1

The computer has a cpu and a disk.
CPU type: i7, CPU frequency: 2.9 GHz
disk type: ST, disk capacity: 2 T
#include<iostream> 
#include<string> 
using namespace std;
class CPU {
protected:
	string types;
	double count;
public:
	CPU(string& t, double c)
	{
		types = t;
		count = c;
	}
	string typ()
	{
		return types;
	}
	double fre()
	{
		return count;
	}
};
class Disk {
protected:
	string types;
	double count;
public:
	Disk(string& t, double c)
	{
		types = t;
		count = c;
	}
	string typ()
	{
		return types;
	}
	double fre()
	{
		return count;
	}
};
class Computer
{
protected:
	string t1, t2;
	double c1, c2;
public:
	Computer(CPU c, Disk d)
	{
		t1 = c.typ();
		t2 = d.typ();
		c1 = c.fre();
		c2 = d.fre();
	}
	void Print()
	{
		cout << "The computer has a cpu and a disk.\n" << "CPU type: " << t1 << ", CPU frequency: " << c1 <<
			" GHz\n" << "disk type: " << t2 << ", disk capacity: " << c2 << " T";
	}
};
int main()
{
	string cpuType, diskType;
	double frequency, capacity;
	cin >> cpuType >> frequency >> diskType >> capacity;
	CPU cpu(cpuType, frequency);
	Disk disk(diskType, capacity);
	Computer computer(cpu, disk);

	computer.Print();
	return 0;
}

静态数据成员

描述

定义一个学生类Student如下:

class Student
{
private:
    int age;   //年龄
    string name;  //姓名
public:
    static int count; //静态成员,表示学生人数
    Student(int a, string n);
    Student();
    ~Student();
    void Print();
};

主函数的定义及程序的运行结果如下,请完成类的定义及类中各函数的实现代码,补充成一个完整的程序。

int main()
{
    cout<<"count="<<Student::count<<endl;
    Student s1, *p=new Student(23,"ZhangHong");
    s1.Print();
    p->Print();
    delete p;
    s1.Print();
    Student Stu[4];
    cout<<"count="<<Student::count<<endl;
    return 0;
}

输入

输出

见样例,注意输出时,逗号后有一空格。

输入样例 1 

输出样例 1

count=0
Name=NoName, age=0
Name=ZhangHong, age=23
Name=NoName, age=0
count=5
提示

注意:已有main函数和 Student类定义,仅需提交类实现代码。

#include<iostream>
using namespace std;
class Student
{
private:
    int age;   //年龄
    string name;  //姓名
public:
    static int count; //静态成员,表示学生人数
    Student(int a, string n);
    Student();
    ~Student();
    void Print();
};

Student::Student(int a, string n)
{
    age = a;
    name = n;
    count++;
}

Student::~Student() { count--; }
void  Student::Print()
{
    cout << "Name=" << name << "," << " age=" << age << endl;
}
Student::Student() {
    age = 0;
    name = "NoName";
    count++;
}
int Student::count = 0;
int main()
{
   
    cout << "count=" << Student::count << endl;
    Student s1, * p = new Student(23, "ZhangHong");
    s1.Print();
    p->Print();
    delete p;
    s1.Print();
    Student Stu[4];
    cout << "count=" << Student::count << endl;
    return 0;
};

Ellipse

描述

已有一个点类Point,使用x和y两个参数确定:

class Point
{
private:
    double x, y;
public:
    Point(double a=0, double b=0):x(a),y(b) {}
    ~Point(){}
    double getX() const { return x;}
    double getY() const { return y;}
    void setX(double a) { x = a; }
    void setY(double b) { y = b; }
};

假定椭圆中心固定于原点 (0,0),则唯一由半长轴 x 和半短轴 y 确定,故可使用一个点对象作为椭圆类成员。由点类组合方式生成一个椭圆类:

class Ellipse
{
public:
    Point pAxis;
    Ellipse(double x = 0, double y = 0):pAxis(x,y) { }
    ~Ellipse() { }
    Ellipse(const Ellipse&);
    Ellipse transposition();    // 转置(交换长短半轴)
    double area();            // 面积
    double eccentricity();        // 离心率
    string position(const Point);    // 位置关系
};

测试多组数据,输入椭圆分别在x轴、y轴的截距的绝对值(半轴长)和点的坐标,要求:输出椭圆面积、椭圆离心率、点与椭圆的位置关系。

(点在椭圆内、上、外<==>输出“inside”“at”“outside”)

主函数提供如下:

int main()
{
    double a,b;
    while(cin>>a>>b)
    {
        Ellipse d1(a,b),d2(d1),d3=d2.transposition();
        cout<<d1.area()<<endl;
        cout<<d2.eccentricity()<<endl;
        cin>>a>>b;
        Point p(a,b);
        cout<<d3.position(p)<<endl;
    }
    return 0;
}

输入

见样例

输出

见样例

输入样例 1 

5 4
4 0

输出样例 1

62.8319
0.6
at

输入样例 2 

8 10
8 0

输出样例 2

251.327
0.6
inside

输入样例 3 

7 9
-6.6 5.9

输出样例 3

197.92
0.628539
outside

提示

const double PI = acos(-1.0);

#include<iostream>
#include<cmath>
const double PI = acos(-1.0);
using namespace std;

class Point
{
private:
    double x, y;
public:
    Point(double a = 0, double b = 0) :x(a), y(b) {}
    ~Point() {}
    double getX() const { return x; }
    double getY() const { return y; }
    void setX(double a) { x = a; }
    void setY(double b) { y = b; }
};


class Ellipse
{
public:
    Point pAxis;
    Ellipse(double x = 0, double y = 0) :pAxis(x, y) { }
    ~Ellipse() { }
    Ellipse(const Ellipse&);
    Ellipse transposition();    // 转置(交换长短半轴)
    double area();            // 面积
    double eccentricity();        // 离心率
    string position(const Point);    // 位置关系
};
Ellipse::Ellipse(const Ellipse& elly)
{
    pAxis.setX(elly.pAxis.getX());
    pAxis.setY(elly.pAxis.getY());
}
Ellipse Ellipse::transposition()
{
    Ellipse x(pAxis.getY(), pAxis.getX());
    return x;
}
double Ellipse::area()
{
    return PI * pAxis.getX() * pAxis.getY();
}
double Ellipse::eccentricity()
{
    if (pAxis.getY() > pAxis.getX())
        return sqrt(1 - pAxis.getX() * pAxis.getX() / pAxis.getY() / pAxis.getY());
    else return sqrt(1 - pAxis.getY() * pAxis.getY() / pAxis.getX() / pAxis.getX());
}
string Ellipse::position(const Point t)
{
    if (fabs(t.getX() * t.getX() / pAxis.getX() / pAxis.getX() + t.getY() * t.getY() / pAxis.getY() / pAxis.getY() / pAxis.getY()) == 1)
        return "at";
    else if (fabs(t.getX() * t.getX() / pAxis.getX() / pAxis.getX() + t.getY() * t.getY() / pAxis.getY() / pAxis.getY()) < 1)
        return "inside";
    else return "outside";
}
int main()
{
    double a, b;
    while (cin >> a >> b)
    {
        Ellipse d1(a, b), d2(d1), d3 = d2.transposition();
        cout << d1.area() << endl;
        cout << d2.eccentricity() << endl;
        cin >> a >> b;
        Point p(a, b);
        cout << d3.position(p) << endl;
    }
    return 0;
}

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值