作业9
1300: c++2 第8章 上机题1 设计长方体类
时间限制:1.000s 内存限制:128MB
题目描述
1. 求3个长方柱的体积,请编写一个基于对象的程序。数据成员包括length,width,height。要求用成员函数实现以下功能:
(1) 由键盘分别输入3个长方柱的长、宽、高;
(2) 计算长方柱的体积;
(3) 输出3个长方柱的体积。
请编程序,上机调试并运行。
输入格式
三个长方柱的长、宽、高(直接输入数值,无需提示信息)
输出格式
三个长方柱的体积,输出格式为
volmue of box1 is XX
volmue of box2 is XX
volmue of box3 is XX
样例输入
3 4 5
4 5 6
5 6 7
样例输出
volmue of box1 is 60
volmue of box2 is 120
volmue of box3 is 210
#include<iostream>
using namespace std;
class Box
{
public:
void get_value();
float volume();
void display();
private:
float length;
float width;
float height;
};
void Box::get_value()
{
cin >> length >> width >> height;
}
float Box::volume()
{
return (length * width * height);
}
void Box::display()
{
cout << volume() << endl;
}
int main()
{
Box box1, box2, box3;
box1.get_value();
cout << "volmue of box1 is ";
box1.display();
box2.get_value();
cout << "volmue of box2 is ";
box2.display();
box3.get_value();
cout << "volmue of box3 is ";
box3.display();
return 0;
}
1304: 第8章上机题2 - 设计一个dog类
时间限制:1.000s 内存限制:128MB
题目描述
设计一个Dog类,包含name、age、sex和weight等属性以及对这些属性操作的方法。实现并测试这个类。
根据类的封装性要求,把name、age、sex和weight声明为私有的数据成员,编写公有成员函数setdata()对数据进行初始化,GetName()、GetAge()、GetSex()和GetWeight()获取相应属性。初始化数据由用户输入。
输入格式
Dog类对象的初始化数据
输出格式
根据Dog类对象的初始化数据输出一句话,请严格按照格式输出,句末有点号。
样例输入
ahuang 3 m 2.4
样例输出
It is my dog.
Its name is ahuang.
It is 3 years old.
It is male.
It is 2.4 kg.
#include <iostream>
using namespace std;
class Dog
{
private:
string name;
int age;
char sex;
double weight;
public:
void setdata(string dogName, int dogAge, char dogSex, double dogWeight)
{
name = dogName;
age = dogAge;
sex = dogSex;
weight = dogWeight;
}
string GetName()
{
return name;
}
int GetAge()
{
return age;
}
char GetSex()
{
return sex;
}
double GetWeight()
{
return weight;
}
};
int main()
{
Dog dog;
string cinName;
int cinAge;
char cinSex;
double cinWeight;
cin >> cinName >> cinAge >> cinSex >> cinWeight;
dog.setdata(cinName, cinAge, cinSex, cinWeight);
cout << "It is my dog." << endl;
cout << "Its name is " << dog.GetName() << "." << endl;
cout << "It is " << dog.GetAge() << " years old." << endl;
if (dog.GetSex() == 'm'){
cout << "It is male." << endl;
}
else{
cout << "It is female." << endl;
}
cout << "It is " << dog.GetWeight() << " kg." << endl;
return 0;
}
第8章上机题3 - 设计MyTime类
时间限制:1.000s 内存限制:128MB
题目描述
设计一个MyTime类,成员函数SetTime()设置时间,print_12()以12(0 - 11)小时制显示时间(AM上午,PM下午),print_24()以24(0 - 23)小时制显示时间。
输入格式
所需设置时间的时、分、秒(24小时制)
输出格式
按照12小时制和24小时制依次显示时间, 注意时间格式中的冒号是英文冒号, 时分秒都是两位,AM, PM前有一个空格,晚上12:00是00 : 00 : 00 AM,中午十二点是00 : 00 : 00 PM。
样例输入
13 23 34
样例输出
01 : 23 : 34 PM
13 : 23 : 34
#include<iostream>
using namespace std;
class MyTime
{
private:
int hour;
int minute;
int second;
public:
void SetTime(int, int, int);
void print_12();
void print_24();
};
void MyTime::SetTime(int h, int m, int s)
{
hour = h;
minute = m;
second = s;
}
void MyTime::print_12()
{
int hour_temp;
(hour - 12) >= 0 ? hour_temp = hour - 12 : hour_temp = hour;
if (hour - 24 >= 0) hour_temp = hour - 24;
if (hour_temp<10)
{
cout << '0';
}
cout << hour_temp << ':';
if (minute<10)
{
cout << '0';
}
cout << minute << ':';
if (second<10)
{
cout << '0';
}
cout << second << ' ';
if (hour >= 12 && hour<24)
{
cout << "PM" << endl;
}
else
{
cout << "AM" << endl;
}
}
void MyTime::print_24()
{
if (hour >= 24)
hour = hour - 24;
if (hour < 10)
{
cout << '0';
}
cout << hour << ':';
if (minute < 10)
{
cout << '0';
}
cout << minute << ':';
if (second < 10)
{
cout << '0';
}
cout << second;
cout << endl;
}
int main()
{
MyTime time;
int h, m, s;
cin >> h >> m >> s;
time.SetTime(h, m, s);
time.print_12();
time.print_24();
return 0;
}