作业10
1293: C++2第九章上机题1
时间限制:1.000s 内存限制:128MB
题目描述
建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针做函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号及最高成绩。
输入格式
5个学生的学号和成绩
输出格式
成绩最高者的学号和成绩
样例输入
101 78.5
102 85.5
103 98.5
104 100
105 95.5
样例输出
104 100
#include<iostream>
using namespace std;
class Student
{
public:
Student() {} //注意加入一个无参的构造函数,不然会报错
Student(string i, double s) :id(i), score(s) {}
string id;
double score;
};
void max(Student* p)
{
Student* t, *s;
double max;
for (t = p, s = t, max = t->score; t < (p + 5); t++)
{
if (t->score > max)
{
max = t->score;
s = t;
}
}
cout << s->id << " " << max << endl;
}
int main()
{
Student s[5];
for (int i = 0; i < 5; i++)
{
cin >> s[i].id >> s[i].score;
}
max(s);
return 0;
}
1294: C++2第九章上机题2
时间限制:1.000s 内存限制:128MB
题目描述
定义Box类,要求具有以下成员数据:长、宽和高分别为x,y,z;编写一个基于对象的程序,要求用带参的构造函数
实现成员数据的初始化,构造函数形参默认值都为0,并且使用成员函数实现求Box的表面积和体积。
程序可以输出长方体的长宽高以及表面积和体积。
输入格式
无
输出格式
输出长方体的长宽高以及表面积和体积
样例输入
无
样例输出
x = 0 y = 0 z = 0
s = 0
v = 0
x = 10 y = 0 z = 0
s = 0
v = 0
x = 10 y = 20 z = 0
s = 0
v = 400
x = 10 y = 20 z = 30
s = 6000
v = 2200
#include<iostream>
using namespace std;
class Box
{
private:
int x, y, z; int v, s;
public:
void intt(int x1 = 0, int y1 = 0, int z1 = 0) { x = x1; y = y1; z = z1; }
void volue() { s = x * y * z; }
void area() { v = 2 * (x * y + x * z + y * z); }
void show()
{
cout << "x=" << x << " y=" << y << " z=" << z << endl;
cout << "s=" << s << endl;
cout << "v=" << v << endl;
}
};
int main()
{
Box a;
a.intt(0, 0, 0);
a.volue();
a.area();
a.show();
Box b;
b.intt(10, 0, 0);
b.volue();
b.area();
b.show();
Box c;
c.intt(10, 20, 0);
c.volue();
c.area();
c.show();
Box d;
d.intt(10, 20, 30);
d.volue();
d.area();
d.show();
return 0;
}

浙公网安备 33010602011771号