山东科技大学2020年4月13日题解

山东科技大学2020年4月13日题解

**题目一:**类的初体验

Description

定义一个类Data,只有一个double类型的属性和如下3个方法:

  1. void init(double d);——初始化属性值。

  2. double getValue()——获得属性值。

  3. void showValue()——显示属性值。

Input

一个double类型的数值。

Output

输出输入的值2次,每次占一行。

Sample Input

3.14

Sample Output

3.14
3.14

给定的主函数:

int main()
{
    Data data;
    double d;
    cin>>d;
    data.init(d);
    cout<<data.getValue()<<endl;
    data.showValue();
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip> 
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register 
#define LL long long 
#define pi 3.141
using namespace std;

class Data{
	public:
		void init(double d_number){
			number = d_number;
		}
		double getValue(){
			return number;
		}
		void showValue(){
			printf("%.2lf", number);
		}
	private:
		double number;
};
int main()
{
    Data data;
    double d;
    cin>>d;
    data.init(d);
    cout<<data.getValue()<<endl;
    data.showValue();
}

**题目二:**类的初体验(II)

Description

定义一个类Data,只有一个double类型的属性和如下3个方法:

  1. 带1个参数的构造函数——初始化属性值为参数值。

  2. double getValue()——获得属性值。

  3. void showValue()——显示属性值。

Input

一个double类型的数值。

Output

输出输入的值2次,每次占一行。

Sample Input

3.14

Sample Output

3.14
3.14

题目给定的主函数

int main()
{
    double d;
    cin>>d;
    Data data(d);
    cout<<data.getValue()<<endl;
    data.showValue();
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip> 
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register 
#define LL long long 
#define pi 3.141
using namespace std;

class Data{
	public:
		Data(double d_number){
			number = d_number;
		}
		double getValue(){
			return number;
		}
		void showValue(){
			cout << number << "\n";
		}
	private:
		double number;
};
int main()
{
    double d;
    cin>>d;
    Data data(d);
    cout<<data.getValue()<<endl;
    data.showValue();
}

题目三构造函数与析构函数练习

Description

有一个类,其属性由一个整数和一个实数确定,请定义这个类,使其能够在main函数中使用时,产生满足题目所要求的输出。

Input

一个整数,一个实数。

Output

见样例。

Sample Input

1 2.56

Sample Output

A new object of i=1 and d=2.56 is constructed.
A new object of i=0 and d=0 is constructed.
The object of i=0 and d=0 is deconstructed.
The object of i=1 and d=2.56 is deconstructed.

题目给定的主函数

int main()
{
    int a;
    double b;
    cin>>a>>b;
    myClass m1(a,b),m2;
    return 0;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip> 
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register 
#define LL long long 
#define pi 3.141
using namespace std;

class myClass{
	private:
		int i;
		double d;
	public:
		myClass(int ii = 0, double dd = 0){
			i = ii, d = dd;
			cout << "A new object of i=" << i << " and d=" << d << " is constructed." << "\n";
		}
		~myClass(){
			cout << "The object of i=" << i << " and d=" << d << " is deconstructed." << endl;
		}
};
int main()
{
    int a;
    double b;
    cin>>a>>b;
    myClass m1(a,b),m2;
    return 0;
}

**题目四:**平面上的点——Point类 (I)

Description

在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定。现在我们封装一个“Point类”来实现平面上的点的操作。

根据“append.cc”,完成Point类的构造方法和show()方法。

接口描述:
Point::show()方法:按输出格式输出Point对象。

Input

输入多行,每行为一组坐标“x,y”,表示点的x坐标和y坐标,x和y的值都在double数据范围内。

Output

输出为多行,每行为一个点,X坐标在前,Y坐标在后,Y坐标前面多输出一个空格。每个坐标的输出精度为最长16位。输出格式见sample。

C语言的输入输出被禁用。

Sample Input

1,2
3,3
2,1

Sample Output

Point : (1, 2)
Point : (3, 3)
Point : (2, 1)
Point : (0, 0)

HINT

注意精度控制,C语言的输入输出被禁用。

题目给定主函数

int main()
{
    char c;
    double a, b;
    Point q;
    while(std::cin>>a>>c>>b)
    {
        Point p(a, b);
        p.show();
    }
    q.show();
}

分析

由于题目禁用 C 语言是输入输出所以禁用 cstdio 头文件,同时为了保证输出位数不超过题目要求的 16 位,所以需要用到 setprecision() 函数。

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<iomanip> 
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register 
#define LL long long 
#define pi 3.141
using namespace std;

class Point{
	private:
		double x, y;
	public:
		Point(double x_x = 0, double y_y = 0){
			x = x_x, y = y_y;
		}
		void show(){
			cout << "Point : (" << setprecision(16) << x << ", " << setprecision(16) << y << ")" << "\n";
		}
};
int main()
{
    char c;
    double a, b;
    Point q;
    while(std::cin>>a>>c>>b)
    {
        Point p(a, b);
        p.show();
    }
    q.show();
}

题目五平面上的点——Point类 (II)

Description

在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定。现在我们封装一个“Point类”来实现平面上的点的操作。

根据“append.cc”,完成Point类的构造方法和show()方法,输出各Point对象的构造和析构次序。

接口描述:
Point::show()方法:按输出格式输出Point对象。

Input

输入多行,每行为一组坐标“x,y”,表示点的x坐标和y坐标,x和y的值都在double数据范围内。

Output

输出每个Point对象的构造和析构行为。对每个Point对象,调用show()方法输出其值:X坐标在前,Y坐标在后,Y坐标前面多输出一个空格。每个坐标的输出精度为最长16位。输出格式见sample。

C语言的输入输出被禁用。

Sample Input

1,2
3,3
2,1

Sample Output

Point : (0, 0) is created.
Point : (1, 2) is created.
Point : (1, 2)
Point : (1, 2) is erased.
Point : (3, 3) is created.
Point : (3, 3)
Point : (3, 3) is erased.
Point : (2, 1) is created.
Point : (2, 1)
Point : (2, 1) is erased.
Point : (0, 0) is copied.
Point : (1, 1) is created.
Point : (0, 0)
Point : (1, 1)
Point : (0, 0)
Point : (1, 1) is erased.
Point : (0, 0) is erased.
Point : (0, 0) is erased.

HINT

思考构造函数、拷贝构造函数、析构函数的调用时机。

题目给定主函数

int main()
{
    char c;
    double a, b;
    Point q;
    while(std::cin>>a>>c>>b)
    {
        Point p(a, b);
        p.show();
    }
    Point q1(q), q2(1);
    q1.show();
    q2.show();
    q.show();
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<iomanip> 
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register 
#define LL long long 
#define pi 3.141
using namespace std;

class Point{
	private:
	    double x,y;
	public:
	    Point(){
			x = 0, y = 0;
			cout << setprecision(16) << "Point : (0, 0) is created.\n";
		}
	    Point(double a,double b){
			x = a, y = b;
			cout << setprecision(16) << "Point : (" << x << ", " << y << ") is created.\n";
		}
	    void show(){
			cout << setprecision(16) << "Point : (" << x << ", " << y << ")\n";
		}
	    ~Point(){
			cout << setprecision(16) << "Point : (" << x << ", " << y << ") is erased.\n";
		}
	    Point(Point &a){
			x = 0, y = 0;
			cout << setprecision(16) << "Point : (0, 0) is copied.\n";
		}
	    Point(int a){
			x = a, y = a;
			cout << setprecision(16) << "Point : (" << x << ", " << y << ") is created.\n";
		}
};
int main()
{
    char c;
    double a, b;
    Point q;
    while(std::cin>>a>>c>>b)
    {
        Point p(a, b);
        p.show();
    }
    Point q1(q), q2(1);
    q1.show();
    q2.show();
    q.show();
}

**题目六:**时间类的构造和输出

Description

封装一个时间类Time,用于时间处理的相关功能,支持以下操作:

  1. Time::Time(int,int,int)构造方法:传递时分秒的三个参数构造对象。
  2. Time::showTime()方法:输出“hh:mm:ss”,不足两位的要前面补0。
    你设计一个时间类Time,使得main()函数能够正确运行。
    函数调用格式见append.cc。
    append.cc中已给出main()函数。

Input

输入的第一个整数n,表示有n组测试数据,每组3个整数:hh,mm,ss,分别表示时、分、秒,其值都在合法的时间范围内。

Output

每组测试数据对应一组输出“hh:mm:ss”,不足两位的输出需要前面补0,格式见sample。

Sample Input

5
0 0 1
0 59 59
1 1 1
23 0 0
23 59 59

Sample Output

00:00:01
00:59:59
01:01:01
23:00:00
23:59:59

HINT

输出格式用头文件中流操作算子:

setw(w) :设置数据的输出宽度为w个字符

setfill©:设置用字符c作为填充字符

题目给定主函数

int main()
{
    int cases;
    cin>>cases;
    for(int i = 1; i <= cases; ++i)
    {
        int hour, minute, second;
        cin>>hour>>minute>>second;
        Time t(hour, minute, second);
        t.showTime();
    }
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<iomanip> 
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register 
#define LL long long 
#define pi 3.141
using namespace std;

class Time{
	private:
		int hour, minute, second;
	public:
		Time(int hour_hour, int minute_minute, int second_second){
			hour = hour_hour, minute = minute_minute, second = second_second;
		}
		void showTime(){
			cout << setfill('0') << setw(2) << hour << ":" << setfill('0') << setw(2) << minute << ":" << setfill('0') << setw(2) << second << "\n";
		}
};

int main()
{
    int cases;
    cin>>cases;
    for(int i = 1; i <= cases; ++i)
    {
        int hour, minute, second;
        cin>>hour>>minute>>second;
        Time t(hour, minute, second);
        t.showTime();
    }
}

**题目七:**时间类的成员读写

Description

封装一个时间类Time,用于时间处理的相关功能,支持以下操作:

  1. Time::Time()无参构造方法。
  2. 成员读函数:
    Time::hour() :返回Time的小时数;
    Time::minute():返回Time的分钟数;
    Time::second():返回Time的秒数。
  3. 成员写函数:
    Time::hour(int) :传参修改Time的小时数;
    Time::minute(int):传参修改Time的分钟数;
    Time::second(int):传参修改Time的秒数。
    你设计一个时间类Time,使得main()函数能够正确运行。
    函数调用格式见append.cc。
    append.cc中已给出main()函数。

Input

输入的第一个整数n,表示有n组测试数据,每组3个整数:hh,mm,ss,分别表示时、分、秒,其值都在合法的时间范围内。

Output

每组测试数据对应一组输出“hh:mm:ss”,不足两位的输出需要前面补0,格式见sample。

Sample Input

5
0 0 1
0 59 59
1 1 1
23 0 0
23 59 59

Sample Output

00:00:01
00:59:59
01:01:01
23:00:00
23:59:59

HINT

输出格式用头文件中流操作算子:

setw(w) :设置数据的输出宽度为w个字符

setfill©:设置用字符c作为填充字符

题目给定主函数

int main()
{
    Time t;
    int cases;
    cin>>cases;
    for(int i = 1; i <= cases; ++i)
    {
        int hour, minute, second;
        cin>>hour>>minute>>second;
        t.hour(hour);
        t.minute(minute);
        t.second(second);
        cout<<setw(2)<<setfill('0')<<t.hour()<<":";
        cout<<setw(2)<<setfill('0')<<t.minute()<<":";
        cout<<setw(2)<<setfill('0')<<t.second()<<endl;
    }
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<iomanip> 
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register 
#define LL long long 
#define pi 3.141
using namespace std;

class Time{
	private:
		int hour_, minute_, second_;
	public:
		void hour(int hour_hour){
			hour_ = hour_hour;
		}
		void minute(int minute_minute){
			minute_ = minute_minute;
		}
		void second(int second_second){
			second_ = second_second;
		}
		int hour(){
			return hour_;
		}
		int minute(){
			return minute_;
		}
		int second(){
			return second_;
		}
};

int main()
{
    Time t;
    int cases;
    cin>>cases;
    for(int i = 1; i <= cases; ++i)
    {
        int hour, minute, second;
        cin>>hour>>minute>>second;
        t.hour(hour);
        t.minute(minute);
        t.second(second);
        cout<<setw(2)<<setfill('0')<<t.hour()<<":";
        cout<<setw(2)<<setfill('0')<<t.minute()<<":";
        cout<<setw(2)<<setfill('0')<<t.second()<<endl;
    }
}

**题目八:**Apple

Description

Jackie开了一家水果店,要购入一些苹果(Apple)销售。苹果每批进货有三个属性每公斤单价(price)、每箱重量(weight)、有多少箱(amount)。请你编写程序计算每批苹果的价格。

用C++编写Apple类来完成代码,调用格式见“Append Code”。
Apple::input()按格式输入每批苹果进货的属性。
Apple::value()计算每批苹果的价格。

Input

输入为多组。首先输入进货次数,然后每次输入进货的单价(price)、每箱重量(weight)、有多少箱(amount)。

Output

每行输出本次进货苹果的价格。

Sample Input

2
2.25 38.8 15
2.14 39.6 6

Sample Output*

1309.5
508.464

题目给定主函数

int main()
{
    int i, cases;
    Apple apple;
 
    cin >> cases;
    for(i = 1; i <= cases; i++)
    {
        apple.input();
        cout << apple.value() << endl;
    }
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<iomanip> 
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register 
#define LL long long 
#define pi 3.141
using namespace std;

class Apple{
	private:
		double weight, amount, price; 
	public:
		void input(){
			cin >> weight >> amount >> price;
		}
		double value(){
			return weight * amount * price;
		}
};

int main()
{
    int i, cases;
    Apple apple;

    cin >> cases;
    for(i = 1; i <= cases; i++)
    {
        apple.input();
        cout << apple.value() << endl;
    }
}

**题目九:**复数类的构造、析构和输出

Description

封装一个复数类CPLX,用来处理复数功能和运算,支持以下操作:

  1. CPLX::CPLX(double, double)构造:参数为实部、虚部,用传入的参数初始化对象,产生一行以“CREATE()”开始的输出,并输出复数的实部和虚部;
  2. CPLX::~CPLX()析构:产生一行以“RELEASE()”开始的输出,并输出复数的实部和虚部;
  3. CPLX::print():产生一行以“PRINT()”开始的输出,并以格式“(a, bi)”的形式输出复数;

你设计一个CPLX类,使得main()函数能够运行并得到正确的输出。调用格式见append.cc

Input

输入的第一个整数n,表示用n组测试样例。每组测试输入两个实数,分别为实部和虚部。

Output

每组测试数据对应一组输出,两组输出之间用若干“===”分割,详细格式见sample。

Sample Input

5
2 3
10 0
0 100
1 -1
-7 -7

题目给定主函数

int main()
{
    int cases;
    cin >> cases;
    for(int i = 1; i <= cases; ++i)
    {
        double a, b;
        cin >> a >> b;
        cout << "=========================" << endl;
 
        CPLX cplx(a, b);
        cplx.print();
    }
    cout << "=========================" << endl;
}

标程

#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<iomanip> 
#include<cstring>
#include<iterator>
#include<iostream>
#include<algorithm>
#define R register 
#define LL long long 
#define pi 3.141
using namespace std;

class CPLX{
	private:
		double x, y;
	public:
		CPLX(double x_x, double y_y){
			x = x_x, y = y_y;
			cout << "CREATE(): " << x << " " << y << "\n";
		}
		~CPLX(){
			cout << "RELEASE(): " << x << " " << y << "\n";
		}
		void print(){
			cout << "PRINT(): (" << x << ", " << y << "i)" << "\n";
		}
};


int main()
{
    int cases;
    cin >> cases;
    for(int i = 1; i <= cases; ++i)
    {
        double a, b;
        cin >> a >> b;
        cout << "=========================" << endl;

        CPLX cplx(a, b);
        cplx.print();
    }
    cout << "=========================" << endl;
}

猜你喜欢

转载自blog.csdn.net/a1351937368/article/details/105524359