【C++】第9章作业


三角形类

【问题描述】

先定义一个能描述平面上一条线段的类Beeline,包含私有数据成员为线段两个端点的坐标(X1,Y1,X2,Y2),在类中定义形参默认值为0的构造函数,计算线段长度的公有成员函数Length(),显示线段两个端点坐标的公有成员函数show()。然后再定义一个能描述平面上三角形的类Triangle,其数据成员为用Beeline定义的对象line1,line2,line3。在类中定义的构造函数要能对对象成员进行初始化。再定义计算三角形面积的函数Area()及显示三条边端点坐标及面积的函数Print(),Print()函数中可调用show()函数显示三条边两端点坐标。

【输入形式】

输入三角形三个顶点的坐标(x1,y1)、(x2,y2)、(x3,y3)。

其中 -100 <= x1,x2,x3,y1,y2,y3 <= 100,且为整数。

在主函数中创建类对象tri(x1,y1,x2,y2,x3,y3),对应line1(x1, y1, x2, y2),line2(x2,y2,x3,y3),line3(x3,y3,x1,y1)。

【输出形式】

调用Print()函数,将三角形三条边的端点坐标及面积。面积保留两位小数。

具体格式见样例。

【样例输入】

0 0

0 4

3 0

【样例输出】

Three edges’ points are listed as follows:

(0, 0),(0, 4)

(0, 4),(3, 0)

(3, 0),(0, 0)

The area of this triangle is: 6.00.

【提示】

1.严格按照输出样例输出,建议复制。

2.计算面积建议用海伦公式。

3.严格控制保留2位小数。

4.如果没有严格使用类,得分为0。

#include<iostream>
#include<cmath>
#include<iomanip> 
class Beeline{
	private:
		int x1;
		int x2;
		int y1;
		int y2;
	public:
		Beeline(int xx1=0,int yy1=0,int xx2=0,int yy2=0):x1(xx1),y1(yy1),x2(xx2),y2(yy2){}; 
		float length();
		void show();
};
float Beeline::length()
{
	return (sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
}
void Beeline::show()
{
	std::cout << "(" << x1 << ", " << y1 << "),(" << x2 <<", " << y2 <<")" << std::endl;
}
class Triangle{
	private:
		Beeline line1,line2,line3;
	public:
		Triangle(int x1,int y1,int x2,int y2,int x3,int y3):line1(x1,y1,x2,y2),line2(x2,y2,x3,y3),line3(x3,y3,x1,y1){};
		float area();
		void print();
};
float Triangle::area()
{
	float a=line1.length();
	float b=line2.length();
	float c=line3.length();
	float p=(a+b+c)/2;
	return (sqrt(p*(p-a)*(p-b)*(p-c)));
}
void Triangle::print()
{
	line1.show();
	line2.show();
	line3.show();
}
int main()
{
	int x1,y1,x2,y2,x3,y3;
	std::cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
	Triangle t(x1,y1,x2,y2,x3,y3);
	std::cout << "Three edges' points are listed as follows:" << std::endl;
	t.print();
	std::cout << "The area of this triangle is: ";
	std::cout.setf(std::ios::fixed);
	std::cout << std::setprecision(2)<< t.area() << ".";
	return 0;
}


学生成绩类

【问题描述】设计学生成绩类Score。在主函数中定义学生成绩对象数组s[]。用Sum()计算每个学生的总成绩、用Show()显示每个学生的成绩。增加静态成员函数getAvg(),用于返回学生的总平均分。通过增加合适的成员、修改成员函数等完成这一功能。

【输入形式】

包含一组测试数据。第一行输入一个整数n(1<=n<=100)。

接下来n行。每行先输入一个整数op:

当op==1时,输入x, y, z。代表输入一位新同学i(i从1开始编号)的语文、数学、英语成绩,无需输出。

当op==2时,输入i,输出第i同学的总成绩。数据保证这位同学的成绩已经录入。

当op==3时,输入i,依次输出第i同学的语文数学英语成绩,成绩之间用空格隔开。

当op==4时,输出当前已经录入学生的总平均分,结果保留两位小数。

(1<=n<=100, 1<=id<=10, 1<=op<=3, 0<=x,y,z<=100,全部输入都为整型数)

【输出形式】
在这里插入图片描述

注意输入之间会有一些输出,但测试只看cout结果。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
using namespace std;
class Score {
private:
    int Chinese, Math, English;
    static int TotalScore;
    static int TotalStudent;
public:
    Score() {}
    void setScore (int c, int m, int e) {
        Chinese=c;
        Math = m;
        English = e;
        TotalStudent++;
        TotalScore = TotalScore + Chinese + Math + English;

    }
    int Sum() {
        return Chinese+Math+English;

    }
    void Show() {
       cout<<Chinese<<" "<<Math<<" "<<English<<endl;

    }
    double static getAve() {
       return double(TotalScore)/double(TotalStudent);

    }
};
int Score::TotalScore=0;
int Score::TotalStudent=0;

int main() {
    int n, op, i, c, m, e;
    cin >> n;
    int id = 1;
    Score sco[11];
    while(n--) {
        cin >> op;
        if(op == 1) {
            cin >> c >> m >> e;
            sco[id].setScore(c,m,e);id++;

        } else if(op == 2) {
            cin >> i;
            cout<<sco[i].Sum()<<endl;

        } else if(op == 3) {
            cin >> i;
            sco[i].Show();

        } else {
cout<<fixed<<setprecision(2)<<Score::getAve()<<endl;

        }
    }
    return 0;
}



电视类

【问题描述】

补全设计一个TV类和一个Remote类。Remote类的成员函数是TV类的友元, 电视类有状态、频道和音量基本属性,默认初始频道为5,默认初始音量为20。状态有开和关(-1表示关机状态,其他为开机状态)。

在主函数根据输入的op值进行不同操作。补全代码使程序满足如下要求。

【输入形式】

当op==1时,

输入电视操作命令如下:

OFF_ON(切换电视开关机状态)

VOL_UP(电视音量+1)

VOL_DOWN(电视音量-1)

CHA_NEXT(电视频道+1)

CHA_PRE(电视频道-1)

CHA_TO x(0<=x<=100,将电视频道切到x)

VOL_TO x(0<=x<=100,将电视音量切到x)

其中CHA_TO与VOL_TO通过调用友元类实现。

当op==2时,输出当前电视状态。

当op==3时,结束程序。

【输出形式】

当op==2时,输出当前电视状态,具体格式见样例。
在这里插入图片描述

#include <iostream>
using namespace std;
class TV;
class Remote
{
public:
    Remote() {};
    void volume_to(TV &tv, int x);
    void channel_to(TV &tv, int x);
};

class TV
{
private:
    int state;
    int channel;
    int volume;
public:
    friend void Remote::volume_to(TV &tv, int x);
    friend void Remote::channel_to(TV &tv, int x);
    TV() {};
    TV(int st) :state(st),volume(20),channel(5){}

    void onoff() {
                	state = -state;

    }
    void cha_next() {
        			channel++;

    }
    void cha_pre() {
        			channel--;

    }
    void vol_up() {
        			volume++;

    }
    void vol_down() {
        			volume--;

    }
    void print() {
        if(state == -1) {
            				cout << "The TV is OFF" << endl;

        } else {
            				cout << "The TV is ON" << endl;
            				cout << "The channel is " << channel << endl;
            				cout << "The volume is " << volume << endl;

        }
    }
};

void Remote::volume_to(TV &tv, int x) {
    tv.volume = x;
}
void Remote::channel_to(TV &tv, int x) {
    tv.channel = x;
}

int main()
{
    int x, op;
    string s;
    TV tv(-1);
    Remote rem;
    while(1) {
        cin >> op;
        if(op == 1) {
            cin >> s;
            if(s == "OFF_ON") tv.onoff();
            else if(s == "VOL_UP") tv.vol_up();
            else if(s == "VOL_DOWN") tv.vol_down();
            else if(s == "CHA_NEXT") tv.cha_next();
            else if(s == "CHA_PRE") tv.cha_pre();
            else if(s == "CHA_TO") {
                cin >> x;
                rem.channel_to(tv, x);
            } else if(s == "VOL_TO") {
                cin >> x;
                rem.volume_to(tv, x);
            }
        } else if(op == 2){
            tv.print();
        } else {
            break;

        }
    }
    return 0;
}


发布了38 篇原创文章 · 获赞 4 · 访问量 1654

猜你喜欢

转载自blog.csdn.net/qq_15989473/article/details/103229399