【C++作业】第九章

2 篇文章 0 订阅

title: 【cpp】第九章 作业
date: 2020-01-13 11:27:05
categories:

  • cpp
  • 作业
    tags: 课程
    comments: true
    description:
    cover: https://i.loli.net/2020/01/17/trs2EOodlxgGJSc.jpg
    top_img: https://i.loli.net/2020/01/17/9Ty2tFiNYqQXKes.jpg

学生成绩统计

【问题描述】

先定义一个能描述平面上一条线段的类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>
using namespace std;
class beeline
{
private:
	int x1, y1, x2, y2;
public:
	beeline()
	{
		x1 = 0; x2 = 0; y1 = 0; y2 = 0;
	}
	beeline(int, int, int, int);
	float length();
	void show();
};
beeline::beeline(int a, int b, int c, int d)
{
	x1 = a;
	y1 = b;
	x2 = c;
	y2 = d;
}
float beeline::length()
{
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
void beeline::show()
{
	cout << "(" << x1 << ", " << y1 << ")" << "," << "(" << x2 << ", " << y2 << ")" << endl;
}
class Triangle
{
private:
	beeline line1, line2, line3;
public:
	Triangle(int, int, int, int, int, int);
	float Area();
	void print();
};
Triangle::Triangle(int a, int b, int c, int d, int e, int f) :line1(a, b, c, d), line2(c, d, e, f), line3(e, f, a, b)
{
}
float Triangle::Area()
{
	float x, y, z;
	x = line1.length();
	y = line2.length();
	z = line3.length();
	return ((1 / 4.00) * sqrt((x + y + z) * (x + y - z) * (x + z - y) * (y + z - x)));
}
void Triangle::print()
{
	cout << "Three edges' points are listed as follows:";
	cout << endl;
	line1.show();
	line2.show();
	line3.show();
	cout << "The area of this triangle is: ";
	cout << std::fixed << std::setprecision(2) << (*this).Area() << ".";
}
int main()
{
	int a, b, c, d, e, f;
	cin >> a >> b >> c >> d >> e >> f;
	Triangle x(a, b, c, d, e, f);
	x.print();
}

学生成绩类

【问题描述】

设计学生成绩类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,全部输入都为整型数)

【输出形式】

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-72Oas3eQ-1579607168308)(https://s2.ax1x.com/2020/01/13/l7nmJP.png)]

注意输入之间会有一些输出,但测试只看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;
		TotalScore += c + m + e;
		TotalStudent++;
	}
	int Sum()
	{
		return(Chinese + Math + English);
	}
	void Show()
	{
		cout << Chinese << " " << Math << " " << English;
	}
	double static getAve()
	{
		return (double)TotalScore / 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();
			 cout << endl;
		 }
		 else  if (op == 3)
		 {
			 cin >> i;
			 sco[i].Show();
			 cout << endl;
		 }
		 else
		 {
			 cout << std::fixed << std::setprecision(2) << Score::getAve();
			 cout << 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时,输出当前电视状态,具体格式见样例。

l7nfyD.png

【题解】

#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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值