# C++题目(SDUST-OJ)--(更新中)

0 篇文章 0 订阅

CSDN的更新不及时,而且CSDN的排版感觉有问题==,可以去这看。

C++题目(OJ)

1.数组类(1)

Description

封装一个整型数组类,用于存储整数和处理的相关功能,支持以下操作:

  1. Array::Array()无参构造方法:创建一个空数组对象。
  2. Array::size()方法:返回Array对象中元素个数。
  3. Array::get(int n)方法:按格式从输入读取n元素。
  4. 下标运算符:返回下标所指的元素。

-----------------------------------------------------------------------------

你设计一个数组类Array,使得main()函数能够正确运行。

函数调用格式见append.cc

append.cc中已给出main()函数

Input

输入的第一个整数n,表示有n组测试数据。

后面的每行以一个整数k开头,表示后面有k个整数。

Output

把输入的数组,输出出来。每行数据对应一个输出。格式见sample。

Sample Input

4

2 10 20

1 0

0

3 1 2 3

Sample Output

10 20

0

1 2 3

HINT

Append Code

append.cc,

#include <iostream>
#include <vector>
using namespace std;
class Array{
private:
	vector<int> arr;
	int l;
public:
	Array(){l=0;}
	int size(){return arr.size();}
	void get(int n){
		arr.resize(n);
		l=n;
		for(int i=0;i<l;i++){
			int m;
			cin >> m;
			arr[i]=m;
		}
	}
	int operator[](int n){return arr[n];}
};
/
int main()
{
    int cases;
    Array arr;
    cin >> cases;
    for(int ca = 1; ca <= cases; ca++)
    {
        int len;
        cin >> len;
        arr.get(len);
        for(int i = 0; i < arr.size(); i++)
            if(i + 1 == arr.size())
                cout << arr[i];
            else
                cout << arr[i] << " ";
        cout << endl;
    }
}

详细的函数实现功能:其中vector c.

​ c.clear() 移除容器中所有数据。

​ c.empty() 判断容器是否为空。

​ c.erase(pos) 删除pos位置的数据

​ c.erase(beg,end) 删除[beg,end)区间的数据

​ c.front() 传回第一个数据。

​ c.insert(pos,elem) 在pos位置插入一个elem拷贝

​ c.pop_back() 删除最后一个数据。

​ c.push_back(elem) 在尾部加入一个数据。

​ c.resize(num) 重新设置该容器的大小

​ c.size() 回容器中实际数据的个数。

​ c.begin() 返回指向容器第一个元素的迭代器

​ c.end() 返回指向容器最后一个元素的迭代器

vector用法

2. Base与Derived

Description

定义Base和Derived类,Derived类是Base类的子类,两个类都只有1个int类型的属性。定义它们的构造函数和析构函数,输出信息如样例所示。

Input

输入2个整数。

Output

见样例。

Sample Input

100

200

Sample Output

Base 100 is created.Base 100 is created.Derived 200 is created.Derived 200 is created.Base 100 is created.Base 100 is created.

HINT

Append Code

append.cc,

#include <bits/stdc++.h>
using namespace std;
class Base{
public:
	int a;
	Base(int aa):a(aa){cout << "Base " << a << " is created." << endl;}
	~Base(){cout << "Base " << a << " is created." << endl;}
};
class Derived: public Base{
public:
	int b;
	Derived(int aa,int bb):Base(aa),b(bb){cout << "Derived " << a << " is created." << endl;}
	~Derived(){cout << "Derived " << a << " is created." << endl;}
};
int main()
{
    int a, b;
    cin>>a>>b;
    Base base(a);
    Derived derived(a, b);
    return 0;
}

3. 编写函数:三个数的最大最小值 (Append Code)

Description

给出三个数a,b,c,最大值是?最小值是?

-----------------------------------------------------------------------------

编写以下两个函数:

get_num()的功能是读取输入的三个整数a,b,c;

max_min()的功能是求出a,b,c的最大值和最小值。

以上函数的调用格式见“Append Code”。这里不给出函数原型,请通过main()函数自行确定。

Input

输入的第一个整数n,表示有n组测试数据,每组3个整数:a,b,c。a,b,c都在int类型范围内。

Output

每组测试数据对应输出一行:为a,b,c的最大值和最小值,格式见sample。

Sample Input

520 15 1010 15 20100 100 00 1 -10 0 0

Sample Output

case 1 : 20, 10case 2 : 20, 10case 3 : 100, 0case 4 : 1, -1case 5 : 0, 0

HINT

Append Code

append.c, append.cc,

#include <bits/stdc++.h>
using namespace std;
void get_num(int &a,int &b,int &c){
	cin >> a >> b >> c;
}
void max_min(int &mmax,int &mmin,int &a,int &b,int &c){
	mmax = a;
	mmin = a;
	if(b > mmax)
		mmax = b;  
	if(b < mmin)
		mmin = b;
	if(c > mmax)
		mmax = c;
	if(c < mmin)
		mmin = c;
}

int main()
{
    int cases;
    int mmax, mmin, a, b, c;
 
    cin>>cases;
    for(int i = 1; i <= cases; ++i)
    {
        get_num(a, b, c);
        max_min(mmax, mmin, a, b, c);
        cout<<"case "<<i<<" : "<<mmax<<", "<<mmin<<endl;
    }
}

4. 重载函数:max

Description

编写两个名为max的函数,它们是重载函数 ,用于求两个整数或实数的最大值。它们的原型分别是:

int max(int a,int b);

double max(double a,double b);

返回值是a和b的最大值。

Input

输入4个数,前两个数是int类型的整数,后2个数是double类型的实数。

Output

输出2个数,每个数占一行。第一个数对应于输入的两个整数的最大值,第二个数对应于输入的两个实数的最大值。

Sample Input

1 21.4 1.3

Sample Output

21.4

HINT

Append Code

append.cc,

#include <iostream>
using namespace std;
int max(int a,int b){
	int max;
	if(a > b)
		max = a;
	else
		max = b;
	return max;
}
double max(double a,double b){
	double max;
	if(a > b)
		max = a;
	else
		max = b;
	return max;
}

int main()
{
    int a,b;
    double c,d;
    cin>>a>>b;
    cout<<max(a,b)<<endl;
    cin>>c>>d;
    cout<<max(c,d)<<endl;
    return 0;
}

5. 默认参数:求圆面积

Description

编写一个带默认值的函数,用于求圆面积。其原型为:

double area(double r=1.0);

当调用函数时指定参数r,则求半径为r的圆的面积;否则求半径为1的圆面积。

其中,PI取值3.14。

Input

一个实数,是圆的半径。

Output

输出有2行。第一行是以输入数值为半径的圆面积,第二行是半径为1的圆面积。

Sample Input

19

Sample Output

1133.543.14

HINT

Append Code

append.cc,

#include <iostream>
using namespace std;
#define PI 3.14
double area(double r = 1.0){
	return PI*r*r;
} 

/
int main()
{
    double r;
    cin>>r;
    cout<<area(r)<<endl;
    cout<<area()<<endl;
    return 0;
}

6. 求(x-y+z)*2

Description

编写一个程序,求解以下三个函数:

f(x,y,z)=2*(x-y+z)

f(x,y) =2*(x-y)

f(x) =2*(x-1)

函数调用格式见append.cc

append.cc中已给出main()函数。

Input

输入的测试数据为多组。每组测试数据的第一个数是n(1<=n<=3),表示后面有n个整数。

当n为3时,后跟3个输入为x,y,z;

当n为2时,后跟2个输入为x,y;

当n为1时,后跟1个输入为x;

当n为0时,表示输入结束

输入的n不会有其他取值。

所有运算都不会超出int类型范围。

Output

每组测试数据对应一个输出。输出x-y+z的值。

Sample Input

3 121 38 452 39 111 73

Sample Output

25656144

HINT

Append Code

append.cc,

#include <bits/stdc++.h>
using namespace std;
int f(int x,int y,int z){
	return 2*(x-y+z);
}
int f(int x,int y){
	return 2*(x-y);
}
int f(int x){
	return 2*(x-1);
}


int main()
{
    int n, x, y, z;
    while(cin>>n)
    {
        if(n == 3)
        {
            cin>>x>>y>>z;
            cout<<f(x, y, z)<<endl;
        }
        if(n == 2)
        {
            cin>>x>>y;
            cout<<f(x, y)<<endl;
        }
        if(n == 1)
        {
            cin>>x;
            cout<<f(x)<<endl;
        }
        if(n == 0)
            break;
    }
}

7. 编写函数:Swap (I) (Append Code)

Description

编写用来交换两个数的函数,使得“Append Code”中的main()函数能正确运行。

-----------------------------------------------------------------------------

用C实现三个函数int_swap()、dbl_swap()、SWAP(),其中SWAP()是个带参宏。

用C++实现两个函数,都以swap()命名。

以上函数的调用格式见“Append Code”。这里不给出函数原型,它们的参数请通过main()函数自行确定。

Input

输入为4行,每行2个数。

Output

输出为4行,每行2个数。每行输出的两数为每行输入的逆序。

Sample Input

12 579 -3-12 43 5

Sample Output

57 12-3 94 -125 3

HINT

“Append Code”中用到的头文件、全局变量或宏的定义应自行补充。

Append Code

append.c, append.cc,

#include <bits/stdc++.h>
using namespace std;
int swap(int *a,int *b){
	int c;
	c = *a;
	*a = *b;
	*b = c;
}
double swap(double *a,double *b){
	double c;
	c = *a;
	*a = *b;
	*b = c;

}

int main()
{
    int x1, y1;
     
    cin>>x1>>y1;
    swap(&x1, &y1);
    cout<<x1<<" "<<y1<<endl;
     
    cin>>x1>>y1;
    swap(x1, y1);
    cout<<x1<<" "<<y1<<endl;
 
    double x2, y2;
     
    cin>>x2>>y2;
    swap(&x2, &y2);
    cout<<x2<<" "<<y2<<endl;
     
    cin>>x2>>y2;
    swap(x2, y2);
    cout<<x2<<" "<<y2<<endl;
}

8. 你会定义类吗?

Description

定义一个类Demo,有构造函数、析构函数和成员函数show(),其中show()根据样例的格式输出具体属性值。该类只有一个int类型的成员。

Input

输入只有一个整数,int类型范围内。

Output

见样例。

Sample Input

-100

Sample Output

A data 10 is created!A data 0 is created!A data -100 is created!This is data 10This is data 0This is data -100A data -100 is erased!A data 0 is erased!A data 10 is erased!

HINT

Append Code

[append.cc](file:///D:/郝佳伟的文件/学习资料/OJ-C++/实验/1/Problem%20A_%20你会定义类吗?_files/Source%20Code.html),

#include <iostream>
using namespace std;
class Demo{
public:
	int a;
	Demo(int aa=0):a(aa){cout << "A data " << a << " is created!" << endl;}
	~Demo(){cout << "A data " << a << " is erased!" << endl;}
	void show(){cout << "This is data " << a << endl;}
};
/
int main()
{
    Demo tmp(10), tmp2;
    int d;
    cin>>d;
    Demo tmp3(d);
 
    tmp.show();
    tmp2.show();
    tmp3.show();
    return 0;
}

9. 一元二次方程类

Description

定义一个表示一元二次方程的类Equation,该类至少具有以下3个数据成员:a、b和c,用于表示方程“axx + b*x +c = 0”。同时,该类还至少具有以下两个成员函数:

\1. void solve():用于求方程的根。

\2. void printRoot():用于输出方程的根。

设定:

\1. 所有输入的a、b、c所生成的方程必定有个2个不同的实根。

\2. 输出的两个根按照从大到小的顺序输出,两个根之间用一个空格隔开,而且每个根必须且仅能保留2位小数,即使小数部分为0。

\3. 请根据样例和给出的main()函数定义相应的构造函数。

Input

输入有若干行,每行有3个实数,分别为方程“axx + b*x + c = 0”中的系数a、b、c。

Output

按照题目要求中的设定条件2输出方程的根。

Sample Input

1 3 2

Sample Output

-1.00 -2.00

HINT

可以使用fixed和setprecision()来实现输出固定小数位数的数值。

Append Code

[append.cc](file:///D:/郝佳伟的文件/学习资料/OJ-C++/实验/1/Problem%20B_%20一元二次方程类_files/Source%20Code.html),

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
class Equation{
public:
	double a,b,c,drt,x1,x2;
	Equation(double aa,double bb,double cc):a(aa),b(bb),c(cc){}
	void solve(){
		drt=sqrt(b*b-4*a*c);
	}
	void printRoot(){
		x1=(-b+drt)/(2*a);
		x2=(-b-drt)/(2*a);
		 cout << setiosflags(ios::fixed) << setprecision(2) << x1 << " " << x2 << endl; 
	}
};

int main()
{
    double a, b, c;
    while (cin>>a>>b>>c)
    {
        Equation equ(a,b,c);
        equ.solve();
        equ.printRoot();
    }
    return 0;
}

fixed 和setprecision()的用法

使用setprecision(n)可控制输出流显示浮点数的数字个数。C++默认的流输出数值有效位是6。
如果setprecision(n)与setiosflags(ios::fixed)合用,可以控制小数点右边的数字个数。setiosflags(ios::fixed)是用定点方式表示实数。
如果与setiosnags(ios::scientific)合用, 可以控制指数表示法的小数位数。setiosflags(ios::scientific)是用指数方式表示实数。

10. 整数的封装

Description

现在,请编写一个Integer类,将整数封装起来。目前,只需要你来实现最基本的功能:

\1. 具有2个构造函数:

(1)Integer::Integer(int):根据参数构建一个整数对象。

(2)Integer::Integer(char*, int):根据给定的字符串和进制来构建一个整数对象。

\2. 具有一个int Integer::getValue()方法,用于返回Integer类中所封装的整数的具体数值。

Input

输入分为多行。

第一行是一个正整数M,表示其后面的M行为M个整数,每行一个整数。

第M+2行是一个正整数N,表示其后有N行。每行由利用一个空格隔开的2部分组成:前半部分是一个字符串,后半部分是该字符串所使用的进制。

注意:

\1. 所有的输入,均在int类型的表示范围内,且所有的输入均为合法输入。

\2. 利用09和az可最大可以表示36进制的数值。

Output

输出为M+N行,每行为一个十进制整数,且输出顺序应与输入顺序相同。

Sample Input

2999-199940111 21a 16z 36a 16

Sample Output

999-19997263510

HINT

Append Code

[append.cc](file:///D:/郝佳伟的文件/学习资料/OJ-C++/实验/1/Problem%20C_%20整数的封装_files/Source%20Code.html),


11. 平面上的点——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,23,32,1

Sample Output

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

HINT

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

Append Code

append.cc,

#include <iostream>
using namespace std;
class Point{
public:
	double x,y;
	Point():x(0),y(0){}
	Point(double xx,double yy):x(xx),y(yy){}
	void show(){cout << "Point : (" << x << ", " << y << ")" << endl;}
};
//
int main()
{
    char c;
    double a, b;
    Point q;
    while(std::cin>>a>>c>>b)
    {
        Point p(a, b);
        p.show();
    }
    q.show();
}

12. 平面上的点——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

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

Append Code

append.cc,

#include <iostream>
using namespace std;
class Point{
public:
	double x,y;
	Point():x(0),y(0){cout << "Point : (" << x << ", " << y << ") is created." << endl;}
	Point(double xx,double yy):x(xx),y(yy){cout << "Point : (" << x << ", " << y << ") is created." << endl;}
	Point(const Point& p):x(p.x),y(p.y){cout << "Point : (0, 0) is copied." << endl;}
	Point(double xx):x(xx),y(xx){cout << "Point : (" << x << ", " << y << ") is created." << endl;}
	~Point(){cout << "Point : (" << x << ", " << y << ") is erased." << endl;}
	void show(){cout << "Point : (" << x << ", " << y << ")" << endl;}
};
//
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();
}

13. 平面上的点——Point类 (III)

Description

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

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

接口描述:
showPoint()函数按输出格式输出Point对象,调用Point::show()方法实现。
Point::show()方法:按输出格式输出Point对象。

Input

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

Output

输出每个Point对象的构造和析构行为。showPoint()函数用来输出(通过参数传入的)Point对象的值: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) is copied.

Point : (1, 2)

Point : (1, 2) is erased.

Point : (1, 2) is erased.

Point : (3, 3) is created.

Point : (3, 3) is copied.

Point : (3, 3)

Point : (3, 3) is erased.

Point : (3, 3) is erased.

Point : (2, 1) is created.

Point : (2, 1) is copied.

Point : (2, 1)

Point : (2, 1) is erased.

Point : (2, 1) is erased.

Point : (0, 0) is copied.

Point : (1, 1) is created.

Point : (0, 0) is copied.

Point : (1, 1) is copied.

Point : (0, 0) is copied.

Point : (0, 0)

Point : (1, 1)

Point : (0, 0)

Point : (0, 0) is erased.

Point : (1, 1) is erased.

Point : (0, 0) is erased.

Point : (1, 1) is erased.

Point : (0, 0) is erased.

Point : (0, 0) is erased.

HINT

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

Append Code

append.cc,

#include <iostream>
using namespace std;
class Point{
public:
	double x,y;
	Point():x(0),y(0){cout << "Point : (" << x << ", " << y << ") is created." << endl;}
	Point(double xx,double yy):x(xx),y(yy){cout << "Point : (" << x << ", " << y << ") is created." << endl;}
	Point(const Point& p):x(p.x),y(p.y){cout << "Point : (0, 0) is copied." << endl;}
	Point(double xx):x(xx),y(xx){cout << "Point : (" << x << ", " << y << ") is created." << endl;}
	~Point(){cout << "Point : (" << x << ", " << y << ") is erased." << endl;}
	void show(){cout << "Point : (" << x << ", " << y << ")" << endl;}
};
void showPoint(Point s){s.show();}
void showPoint(Point a,Point b,Point c){a.show();b.show();c.show();}

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

14. 平面上的点——Point类 (IV)

Description

在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定。现在我们封装一个“Point类”来实现平面上的点的操作。
根据“append.cc”,完成Point类的构造方法和show()、showCounter()、showSumOfPoint()方法;实现showPoint()函数。
接口描述:
showPoint()函数:按输出格式输出Point对象,调用Point::show()方法实现。
Point::show()方法:按输出格式输出Point对象。
Point::showCounter()方法:按格式输出当前程序中Point对象的计数。
Point::showSumOfPoint()方法:按格式输出程序运行至当前存在过的Point对象总数。

Input

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

Output

对每个Point对象,调用show()方法输出其值,或者用showPoint()函数来输出(通过参数传入的)Point对象的值:X坐标在前,Y坐标在后,Y坐标前面多输出一个空格。每个坐标的输出精度为最长16位。调用用showCounter()方法和showSumOfPoint()输出Point对象的计数统计,输出格式见sample。
C语言的输入输出被禁用。

Sample Input

1,2

3,3

2,1

Sample Output

Point : (1, 2)

Current : 2 points.

Point : (3, 3)

Current : 2 points.

Point : (2, 1)

Current : 2 points.

In total : 4 points.

Current : 3 points.

Point : (0, 0)

Point : (1, 1)

Point : (0, 0)

In total : 6 points.

HINT

对象计数通过静态成员来实现

Append Code

append.cc,

#include <iostream>
using namespace std;
class Point{  
private:  
    double x,y;  
    static int sum,num;  
public:  
    Point():x(0),y(0){num++;sum++;}  
    Point(double a):x(a),y(1){num++;sum++;}  
    Point(double a,double b):x(a),y(b){num++;sum++;}  
    Point(const Point&p){x=p.x;y=p.y;num++;sum++;}  
    ~Point(){num--;}  
    void show(){cout<<setprecision(16)<<"Point : ("<<x<<", "<<y<<")"<<endl;}  
    static void showCounter(){cout<<setprecision(16)<<"Current : "<<num<<" points."<<endl;}  
    static void showSumOfPoint(){cout<<setprecision(16)<<"In total : "<<sum<<" points."<<endl;}  
};  
void showPoint(Point &a,Point &b,Point &c){a.show();b.show();c.show();}  
int Point::sum=0;  
int Point::num=0;  

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

15. 平面上的点——Point类 (V)

Description

在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定。现在我们封装一个“Point类”来实现平面上的点的操作。
根据“append.cc”,完成Point类的构造方法和接口描述中的方法。
接口描述:
showPoint()函数:按输出格式输出Point对象。
Point::show()方法:按输出格式输出Point对象。
Point::showSumOfPoint()方法:按格式输出程序运行至当前存在过的Point对象总数。
Point::x()方法:取x坐标。
Point::y()方法:取y坐标。
Point::x(double)方法:传参数设置x坐标并返回。
Point::y(double)方法:传参数设置y坐标并返回。
Point::getX()方法:取x坐标。
Point::getY()方法:取y坐标。
Point::setX()方法:传参数设置x坐标并返回。
Point::setY()方法:传参数设置y坐标并返回。
Point::setPoint(double,double)方法:设置Point对象的x坐标(第一个参数)和y坐标(第二个参数)并返回本对象

Input

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

Output

用ShowPoint()函数来输出(通过参数传入的)Point对象的值或坐标值:X坐标在前,Y坐标在后,Y坐标前面多输出一个空格。每个坐标的输出精度为最长16位。
对每个Point对象,调用show()方法输出其值,输出格式与ShowPoint()函数略有不同:“Point[i] :”,i表示这是程序运行过程中第i个被创建的Point对象。
调用showSumOfPoint()输出Point对象的计数统计,输出格式见sample。
C语言的输入输出被禁用。

Sample Input

1,2

3,3

2,1

Sample Output

Point : (1, 2)

Point : (3, 3)

Point : (2, 1)

Point : (1, 1)

Point : (4, -3)

gorgeous separator

Point[1] : (1, 0)

Point[2] : (3, 3)

Point[3] : (0, 0)

Point[4] : (4, -3)

Point[64] : (1, 0)

Point[64] : (1, 0)

gorgeous separator

In total : 66 points.

HINT

传递和返回引用是不构造新对象的。给函数正确的返回值。

Append Code

append.cc,

#include <iostream>  
#include <iomanip>  
using namespace std;  
class Point  
{  
private :  
    double x_,y_;  
    static int total_num;  
    int id;  
public:  
    Point(){ x_ = 0;y_ = 0;total_num++; id = total_num; }  
    Point (double xx )  
    {  
        x_ = xx;  
        y_ = xx ;  
         total_num++;  
         id = total_num;  
    }  
    Point(double xx, double yy)  
    {  
        x_ = xx;  
        y_ = yy;  
        total_num++;  
        id = total_num;  
    }  
    Point(const Point & pt)  
    {  
        x_ = pt.x_;  
        y_ = pt.y_;  
        total_num++;  
        id = total_num;  
    }  
    double x() const  
    {  
        return x_;  
    }  
    double y() const  
    {  
        return y_;  
    }  
    double x( double xx)  
    {  
        return ( x_ = xx);  
    }  
    double y(double yy)  
    {  
        return ( y_ = yy);  
    }  
    double getX() const  
    {  
        return x_;  
    }  
    double getY() const  
    {  
        return y_;  
    }  
    double setX(double xx)  
    {  
        return ( x_ = xx);  
    }  
    double setY(double yy)  
    {  
        return (y_ = yy);  
    }  
    Point & setPoint(double xx,double yy)  
    {  
        x_ = xx;  
        y_ = yy;  
        return *this;  
    }  
    void show() const  
    {  
          cout<<setprecision(16)<<"Point["<<id<<"] : ("<< x_ << ", " << y_ << ")" <<endl;  
       //cout << "Current : " <<num << " points."  <<endl;  
    }  
   static void showSumOfPoint()  
    {  
        cout << setprecision(16) <<"In total : " <<  total_num << " points."<<endl;  
    }  
};  
int Point::total_num;  
void ShowPoint(Point p)  
{  
    cout<<std::setprecision(16)<<"Point : ("<<p.x()<<", "<<p.y()<<")"<<endl;  
}  
   
void ShowPoint(double x, double y)  
{  
    Point p(x, y);  
    cout<<std::setprecision(16)<<"Point : ("<<p.x()<<", "<<p.y()<<")"<<endl;  
}  
   
void ShowPoint(Point &p, double x, double y)  
{  
    cout<<std::setprecision(16)<<"Point : ("<<p.x(x)<<", "<<p.x(y)<<")"<<endl;  
}  
   
int main()  
{  
    int l(0);  
    char c;  
    double a, b;  
    Point pt[60];  
    while(std::cin>>a>>c>>b)  
    {  
        if(a == b)  
            ShowPoint(pt[l].setPoint(a, b));  
        if(a > b)  
            ShowPoint(a, b);  
        if(a < b)  
            ShowPoint(pt[l], a, b);  
        l++;  
    }  
    Point p(a), q(b);  
    ShowPoint(q);  
    double x(0), y(0);  
    for(int i = 0; i < l; i++)  
        x += pt[i].getX(), y -= pt[i].getY();  
    ShowPoint(pt[l].setX(x), pt[l].setY(y));  
    cout<<"==========gorgeous separator=========="<<endl;  
    for(int i = 0; i <= l; i++)  
        pt[i].show();  
    q.setPoint(q.x() - p.x() + a, q.y() - p.y() + b).show();  
    q.show();  
    cout<<"==========gorgeous separator=========="<<endl;  
    p.showSumOfPoint();  
}  

16. 平面上的点——Point类 (VI)

Description

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

根据“append.cc”,完成Point类的构造方法和接口描述中的方法和函数。

接口描述:
showPoint()函数:按输出格式输出Point对象。
Point::show()方法:按输出格式输出Point对象。
Point::showSumOfPoint()方法:按格式输出程序运行至当前存在过的Point对象总数。
Point::x()方法:取x坐标。
Point::y()方法:取y坐标。
Point::x(double)方法:传参数设置x坐标并返回。
Point::y(double)方法:传参数设置y坐标并返回。
Point::setPoint(double,double)方法:设置Point对象的x坐标(第一个参数)和y坐标(第二个参数)并返回本对象。
Point::isEqual()方法:判断传入的参数与对象的坐标是否相同,相同返回true。
Point::copy()方法:传参数复制给对象。
Point::inverse()方法,有两个版本:不传参数则将对象自身的x坐标和y坐标互换;若将Point对象做参数传入,则将传入对象的坐标交换复制给对象自身,不修改参数的值。

Input

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

Output

用ShowPoint()函数来输出(通过参数传入的)Point对象的值或坐标值:X坐标在前,Y坐标在后,Y坐标前面多输出一个空格。每个坐标的输出精度为最长16位。
对每个Point对象,调用show()方法输出其值,输出格式与ShowPoint()函数略有不同:“Point[i] :”,i表示这是程序运行过程中第i个被创建的Point对象。
调用showSumOfPoint()输出Point对象的计数统计,输出格式见sample。
C语言的输入输出被禁用。

Sample Input

1,2

3,3

2,1

Sample Output

Point[3] : (1, 2)

Point[1] : (2, 1)

Point[4] : (3, 3)

Point[1] : (3, 3)

Point[5] : (1, 2)

Point[1] : (1, 2)

Point[2] : (0, 0)

gorgeous separator

Point[2] : (-7, 5)

Point[3] : (1, 2)

Point[4] : (3, 3)

Point[5] : (1, 2)

Point[6] : (-7, 5)

gorgeous separator

Point[63] : (3, 3)

Point : (3, 3)

Point : (3, 3)

Point : (3, 3)

In total : 64 points.

HINT

给函数正确的返回值。注意常量对象调用的函数。

Append Code

append.cc,

#include <iostream>
#include <iomanip>
using namespace std;
class Point
{

    double m,n;
    int count;
    static int sum;
public :

    friend void ShowPoint(Point);
    friend void ShowPoint(double, double);

    void show()const
    {
        cout<<setprecision(16)<<"Point["<<count<<"] : ("<<m<<", "<<n<<")"<<endl;
    }
    double x(double a)
    {
        m = a;
        return a;

    }
    double y(double b)
    {
        n = b;
        return b;
    }
    double x()const
    {
        return m;
    }
    double y()const
    {

        return n;
    }
    Point  &setPoint(double a,double b)
    {
        m=a;
        n=b;
        return *this;
    }void showSumOfPoint()const
    {
        cout << "In total : " <<sum << " points." << endl;
    }
    Point(double a,double b)
    {

        m = a;
        n = b;
        sum++;
        count =sum;
    }
    Point():m(0),n(0)
    {
        sum++;
        count =sum;
    }
    Point (Point p)
    {
        setPoint(p.getx(),p.gety());
        return *this;
    }
    Point& inverse()
    {
        double temp=m;
        m=n;
        n=temp;
        return *this;
    }
    Point& inverse(Point p)
    {

        m=p.n;
        n=p.m;
        return *this;
    }
    double getx()
    {
     return this->m;
    }
    double gety()
    {
     return this->n;
    }

    bool isEqual(const Point &p)const
    {
        if(p.m == m && p.n == n)
            return true;
        return false;
    }

};
void ShowPoint(Point p)
{
    cout<<std::setprecision(16)<<"Point : ("<< p.x() <<", "<< p.y() <<")"<<endl;
}
void ShowPoint(double a,double b)
{
    cout<<std::setprecision(16)<<"Point : ("<<a<<", "<<b<<")"<<endl;
}
int Point::sum = 0;
int main()
{
    int l(0);
    char c;
    double a, b;
    Point p, q, pt[60];
    while(std::cin>>a>>c>>b)
    {
        if(a == b)
            p.copy(pt[l].setPoint(a, b));
        if(a > b)
            p.copy(pt[l].setPoint(a, b).inverse());
        if(a < b)
            p.inverse(pt[l].setPoint(a, b));
        if(a < 0)
            q.copy(p).inverse();
        if(b < 0)
            q.inverse(p).copy(pt[l]);
        pt[l++].show();
        p.show();
    }
    q.show();
    cout<<"==========gorgeous separator=========="<<endl;
    double x(0), y(0);
    for(int i = 0; i < l; i++)
        x += pt[i].x(), y -= pt[i].y();
    pt[l].x(y), pt[l].y(x);
    q.copy(pt[l]).show();
    for(int i = 0; i <= l; i++)
        pt[i].show();
    cout<<"==========gorgeous separator=========="<<endl;
    const Point const_point(3, 3);
    const_point.show();
    for(int i = 0; i <= l; i++)
    {
        if(const_point.isEqual(pt[i]))
        {
            ShowPoint(const_point);
            ShowPoint(const_point.x(), const_point.y());
            ShowPoint(Point(const_point.x(), const_point.y()));
        }
    }
    const_point.showSumOfPoint();
}

17. 平面上的点和线——Point类、Line类 (I)

Description

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

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

接口描述:

Point::show()方法:按格式输出Point对象。

Line::show()方法:按格式输出Line对象。

Input

输入的第一行为N,表示后面有N行测试样例。

每行为两组坐标“x,y”,分别表示线段起点和终点的x坐标和y坐标,两组坐标间用一个空格分开,x和y的值都在double数据范围内。

Output

输出为多行,每行为一条线段,起点坐标在前终点坐标在后,每个点的X坐标在前,Y坐标在后,Y坐标前面多输出一个空格,用括号包裹起来。输出格式见sample。

Sample Input

4

0,0 1,1

1,1 2,3

2,3 4,5

0,1 1,0

Sample Output

Point : (0, 0)

Line : (0, 0) to (1, 1)

Line : (1, 1) to (2, 3)

Line : (2, 3) to (4, 5)

Line : (0, 1) to (1, 0)

Line : (1, -2) to (2, -1)

Line : (1, -2) to (0, 0)

Line : (2, -1) to (0, 0)

Line : (0, 0) to (2, -1)

HINT

Append Code

append.cc,

#include <bits/stdc++.h>
using namespace std;
class Point{
	friend class Line;
public:
	double x,y;
	Point(){x=y=0;}
	Point(double xx,double yy):x(xx),y(yy){}
	void show(){cout << "Point : (0, 0)" << endl;}
};
class Line{
	friend class Point;
public:
	Point p1,p2;
	Line(Point p,Point q):p1(p),p2(q){}
	Line(double x1,double y1,double x2,double y2):p1(x1,y1),p2(x2,y2){}
	void show(){cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ")" << endl;}
};

/
int main()
{
    char c;
    int num, i;
    double x1, x2, y1, y2;
    Point p(1, -2), q(2, -1), t;
    t.show();
    std::cin>>num;
    for(i = 1; i <= num; i++)
    {
        std::cin>>x1>>c>>y1>>x2>>c>>y2;
        Line line(x1, y1, x2, y2);
        line.show();
    }
    Line l1(p, q), l2(p, t), l3(q, t), l4(t, q);
    l1.show();
    l2.show();
    l3.show();
    l4.show();
}

关于C++中的友元函数的总结

18. 平面上的点和线——Point类、Line类 (II)

Description

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

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

接口描述:

Point::show()方法:按格式输出Point对象。

Line::show()方法:按格式输出Line对象。

Input

输入的第一行为N,表示后面有N行测试样例。每行为两组坐标“x,y”,分别表示线段起点和终点的x坐标和y坐标,两组坐标间用一个空格分开,x和y的值都在double数据范围内。

Output

输出为多行,每行为一条线段,起点坐标在前终点坐标在后,每个点的X坐标在前,Y坐标在后,Y坐标前面多输出一个空格,用括号包裹起来。输出格式见sample。

Sample Input

4

0,0 1,1

1,1 2,3

2,3 4,5

0,1 1,0

Sample Output

Point : (0, 0)Line : (0, 0) to (1, 1) is created.

Line : (0, 0) to (1, 1)Line : (0, 0) to (1, 1) is erased.

Line : (1, 1) to (2, 3) is created.

Line : (1, 1) to (2, 3)

Line : (1, 1) to (2, 3) is erased.

Line : (2, 3) to (4, 5) is created.

Line : (2, 3) to (4, 5)Line : (2, 3) to (4, 5) is erased.

Line : (0, 1) to (1, 0) is created.

Line : (0, 1) to (1, 0)

Line : (0, 1) to (1, 0) is erased.

Line : (1, -2) to (2, -1) is created.

Line : (1, -2) to (0, 0) is created.

Line : (2, -1) to (0, 0) is created.

Line : (0, 0) to (2, -1) is created.

Line : (1, -2) to (2, -1)

Line : (1, -2) to (0, 0)

Line : (2, -1) to (0, 0)

Line : (0, 0) to (2, -1)

Line : (0, 0) to (2, -1) is erased.

Line : (2, -1) to (0, 0) is erased.

Line : (1, -2) to (0, 0) is erased.

Line : (1, -2) to (2, -1) is erased.

HINT

Append Code

append.cc,

#include <bits/stdc++.h>
using namespace std;
class Point{
	friend class Line;
public:
	double x,y;
	Point(){x=y=0;}
	Point(double xx,double yy):x(xx),y(yy){}
	void show(){cout << "Point : (0, 0)" << endl;}
};
class Line{
	friend class Point;
public:
	Point p1,p2;
	Line(Point p,Point q):p1(p),p2(q){cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ") is created." << endl;}
	Line(double x1,double y1,double x2,double y2):p1(x1,y1),p2(x2,y2){cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ") is created." << endl;}
	~Line(){cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ") is erased." << endl;}
    void show(){cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ")" << endl;}
};

/
int main()
{
    char c;
    int num, i;
    double x1, x2, y1, y2;
    Point p(1, -2), q(2, -1), t;
    t.show();
    std::cin>>num;
    for(i = 1; i <= num; i++)
    {
        std::cin>>x1>>c>>y1>>x2>>c>>y2;
        Line line(x1, y1, x2, y2);
        line.show();
    }
    Line l1(p, q), l2(p, t), l3(q, t), l4(t, q);
    l1.show();
    l2.show();
    l3.show();
    l4.show();
}

19. 平面上的点和线——Point类、Line类 (III)

Description

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

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

接口描述:

Point::show()方法:按格式输出Point对象。

Line::show()方法:按格式输出Line对象。

Input

输入的第一行为N,表示后面有N行测试样例。每行为两组坐标“x,y”,分别表示线段起点和终点的x坐标和y坐标,两组坐标间用一个空格分开,x和y的值都在double数据范围内。

Output

输出为多行,每行为一条线段,起点坐标在前终点坐标在后,每个点的X坐标在前,Y坐标在后,Y坐标前面多输出一个空格,用括号包裹起来。输出格式见sample。

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

Sample Input

4

0,0 1,1

1,1 2,3

2,3 4,5

0,1 1,0

Sample Output

Point : (1, -2) is created.

Point : (2, -1) is created.

Point : (0, 0) is created.

Point : (0, 0)

=========================

Point : (0, 0) is created.

Point : (1, 1) is created.

Line : (0, 0) to (1, 1) is created.

Line : (0, 0) to (1, 1)

Line : (0, 0) to (1, 1) is erased.

Point : (1, 1) is erased.

Point : (0, 0) is erased.

=========================

Point : (1, 1) is created.

Point : (2, 3) is created.

Line : (1, 1) to (2, 3) is created.

Line : (1, 1) to (2, 3)

Line : (1, 1) to (2, 3) is erased.

Point : (2, 3) is erased.

Point : (1, 1) is erased.

=========================

Point : (2, 3) is created.

Point : (4, 5) is created.

Line : (2, 3) to (4, 5) is created.

Line : (2, 3) to (4, 5)

Line : (2, 3) to (4, 5) is erased.

Point : (4, 5) is erased.

Point : (2, 3) is erased.

=========================

Point : (0, 1) is created.

Point : (1, 0) is created.

Line : (0, 1) to (1, 0) is created.

Line : (0, 1) to (1, 0)

Line : (0, 1) to (1, 0) is erased.

Point : (1, 0) is erased.

Point : (0, 1) is erased.

=========================

Point : (1, -2) is copied.

Point : (2, -1) is copied.

Line : (1, -2) to (2, -1) is created.

Point : (1, -2) is copied.

Point : (0, 0) is copied.

Line : (1, -2) to (0, 0) is created.

Point : (2, -1) is copied.

Point : (0, 0) is copied.

Line : (2, -1) to (0, 0) is created.

Point : (0, 0) is copied.

Point : (2, -1) is copied.

Line : (0, 0) to (2, -1) is created.

Line : (1, -2) to (2, -1)

Line : (1, -2) to (0, 0)

Line : (2, -1) to (0, 0)

Line : (0, 0) to (2, -1)

Line : (0, 0) to (2, -1) is erased.

Point : (2, -1) is erased.

Point : (0, 0) is erased.

Line : (2, -1) to (0, 0) is erased.

Point : (0, 0) is erased.

Point : (2, -1) is erased.

Line : (1, -2) to (0, 0) is erased.

Point : (0, 0) is erased.

Point : (1, -2) is erased.

Line : (1, -2) to (2, -1) is erased.

Point : (2, -1) is erased.

Point : (1, -2) is erased.

Point : (0, 0) is erased.

Point : (2, -1) is erased.

Point : (1, -2) is erased.

HINT

Append Code

append.cc,

#include <bits/stdc++.h>
using namespace std;
class Point{
	friend class Line;
public:
	double x,y;
	Point(){x=y=0;cout << "Point : (" << x << ", " << y <<") is created." << endl;}
	Point(double xx,double yy):x(xx),y(yy){ cout << "Point : (" << x << ", " << y <<") is created." << endl;}
	Point(const Point& p){x=p.x;y=p.y; cout << "Point : (" << x << ", " << y <<") is copied." << endl;  }
	void show(){cout << "Point : (0, 0)" << endl;}
	 ~Point ()  {cout << "Point : (" << x << ", " << y <<") is erased." << endl;}  
};
class Line{
	friend class Point;
public:
	Point p1,p2;
	Line(Point &p,Point &q):p1(p),p2(q){cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ") is created." << endl;}
	Line(double x1,double y1,double x2,double y2):p1(x1,y1),p2(x2,y2){cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ") is created." << endl;}
	~Line(){cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ") is erased." << endl;}
    void show(){cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ")" << endl;}
};

int main()
{
    char c;
    int num, i;
    double x1, x2, y1, y2;
    Point p(1, -2), q(2, -1), t;
    t.show();
    std::cin>>num;
    for(i = 1; i <= num; i++)
    {
        std::cout<<"=========================\n";
        std::cin>>x1>>c>>y1>>x2>>c>>y2;
        Line line(x1, y1, x2, y2);
        line.show();
    }
    std::cout<<"=========================\n";
    Line l1(p, q), l2(p, t), l3(q, t), l4(t, q);
    l1.show();
    l2.show();
    l3.show();
    l4.show();
}

20. 平面上的点和线——Point类、Line类 (IV)

Description

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

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

接口描述:

Point::show()方法:按格式输出Point对象。

Line::show()方法:按格式输出Line对象。

Input

输入的第一行为N,表示后面有N行测试样例。

每行为两组坐标“x,y”,分别表示线段起点和终点的x坐标和y坐标,两组坐标间用一个空格分开,x和y的值都在double数据范围内。

Output

输出为多行,每行为一条线段,起点坐标在前终点坐标在后,每个点的X坐标在前,Y坐标在后,Y坐标前面多输出一个空格,用括号包裹起来。输出格式见sample。

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

Sample Input

4

0,0 1,1

1,1 2,3

2,3 4,5

0,1 1,0

Sample Output

Point : (1, -2) is created.Point : (2, -1) is created.Point : (0, 0) is created.Point : (0, 0)Point : (0, 0) is created.Point : (0, 0) is created.Line : (0, 0) to (0, 0) is created.Point : (0, 0) is created.Point : (0, 0) is created.Line : (0, 0) to (0, 0) is created.Point : (0, 0) is created.Point : (0, 0) is created.Line : (0, 0) to (0, 0) is created.Point : (0, 0) is created.Point : (0, 0) is created.Line : (0, 0) to (0, 0) is created.=========================Line : (0, 0) to (1, 1)=========================Line : (1, 1) to (2, 3)=========================Line : (2, 3) to (4, 5)=========================Line : (0, 1) to (1, 0)=========================Point : (1, -2) is copied.Point : (2, -1) is copied.Line : (1, -2) to (2, -1) is created.Point : (1, -2) is copied.Point : (0, 0) is copied.Line : (1, -2) to (0, 0) is created.Point : (2, -1) is copied.Point : (0, 0) is copied.Line : (2, -1) to (0, 0) is created.Point : (0, 0) is copied.Point : (2, -1) is copied.Line : (0, 0) to (2, -1) is created.Line : (1, -2) to (2, -1)Line : (1, -2) to (0, 0)Line : (2, -1) to (0, 0)Line : (0, 0) to (2, -1)Line : (0, 0) to (2, -1) is erased.Point : (2, -1) is erased.Point : (0, 0) is erased.Line : (2, -1) to (0, 0) is erased.Point : (0, 0) is erased.Point : (2, -1) is erased.Line : (1, -2) to (0, 0) is erased.Point : (0, 0) is erased.Point : (1, -2) is erased.Line : (1, -2) to (2, -1) is erased.Point : (2, -1) is erased.Point : (1, -2) is erased.Line : (0, 1) to (1, 0) is erased.Point : (1, 0) is erased.Point : (0, 1) is erased.Line : (2, 3) to (4, 5) is erased.Point : (4, 5) is erased.Point : (2, 3) is erased.Line : (1, 1) to (2, 3) is erased.Point : (2, 3) is erased.Point : (1, 1) is erased.Line : (0, 0) to (1, 1) is erased.Point : (1, 1) is erased.Point : (0, 0) is erased.Point : (0, 0) is erased.Point : (2, -1) is erased.Point : (1, -2) is erased.

HINT

Append Code

append.cc,

#include <bits/stdc++.h>
using namespace std;
class Point{
	friend class Line;
public:
	double x,y;
	Point(){x=y=0;cout << "Point : (" << x << ", " << y <<") is created." << endl;}
	Point(double xx,double yy):x(xx),y(yy){ cout << "Point : (" << x << ", " << y <<") is created." << endl;}
	Point(const Point& p){x=p.x;y=p.y; cout << "Point : (" << x << ", " << y <<") is copied." << endl;  }
	void show(){cout << "Point : (0, 0)" << endl;}
	 ~Point ()  {cout << "Point : (" << x << ", " << y <<") is erased." << endl;}  
};
class Line{
	friend class Point;
public:
	Point p1,p2;
	Line():p1(0,0),p2(0,0){cout<<"Line : ("<<p1.x<<", "<<p1.y<<") to ("<<p2.x<<", "<<p2.y<<") is created."<<endl;}
	Line(Point &p,Point &q):p1(p),p2(q){cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ") is created." << endl;}
	Line(double x1,double y1,double x2,double y2):p1(x1,y1),p2(x2,y2){cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ") is created." << endl;}
	~Line(){cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ") is erased." << endl;}
    void show(){cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ")" << endl;}
	void SetLine(double a,double b,double c,double d)  
    {  
        p1.x = a;  
        p1.y = b;  
        p2.x = c;  
        p2.y = d;  
    }  
};
//
int main()
{
    char c;
    int num, i;
    double x1, x2, y1, y2;
    Point p(1, -2), q(2, -1), t;
    t.show();
    std::cin>>num;
    Line line[num];
    for(i = 0; i < num; i++)
    {
        std::cout<<"=========================\n";
        std::cin>>x1>>c>>y1>>x2>>c>>y2;
        line[i].SetLine(x1, y1, x2, y2);
        line[i].show();
    }
    std::cout<<"=========================\n";
    Line l1(p, q), l2(p, t), l3(q, t), l4(t, q);
    l1.show();
    l2.show();
    l3.show();
    l4.show();
}

21.平面上的点和线——Point类、Line类 (V)

Description

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

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

接口描述:

Point::show()方法:按格式输出Point对象。

Line::show()方法:按格式输出Line对象。

Line::SetLine(double, double, double, double)方法:设置Line对象起点的x,y坐标(第一个和第二参数)和终点的x,y坐标(第三个和第四个坐标),并返回本对象

Line::SetLine(const Point &, const Point &)方法:设置Line对象的起点(第一个参数)和终点(第二个坐标),并返回本对象

Line::SetLine(const Line&)方法:设置Line对象,复制参数的坐标,并返回本对象

Line::readLine()方法:从标准输入上读入坐标,格式见Sample

Input

输入的第一行为N,表示后面有N行测试样例。

每行为两组坐标“x,y”,分别表示线段起点和终点的x坐标和y坐标,两组坐标间用一个空格分开,x和y的值都在double数据范围内。

Output

输出为多行,每行为一条线段,起点坐标在前终点坐标在后,每个点的X坐标在前,Y坐标在后,Y坐标前面多输出一个空格,用括号包裹起来。输出格式见sample。

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

Sample Input

4

0,0 1,1

1,1 2,3

2,3 4,5

0,1 1,0

Sample Output

Point : (1, -2) is created.Point : (2, -1) is created.Point : (0, 0) is created.Point : (0, 0)Point : (0, 0) is created.Point : (0, 0) is created.Line : (0, 0) to (0, 0) is created.Point : (0, 0) is created.Point : (0, 0) is created.Line : (0, 0) to (0, 0) is created.Point : (0, 0) is created.Point : (0, 0) is created.Line : (0, 0) to (0, 0) is created.Point : (0, 0) is created.Point : (0, 0) is created.Line : (0, 0) to (0, 0) is created.Line : (0, 0) to (1, 1)Line : (1, 1) to (2, 3)Line : (2, 3) to (4, 5)Line : (0, 1) to (1, 0)Point : (1, -2) is copied.Point : (2, -1) is copied.Line : (1, -2) to (2, -1) is created.Point : (1, -2) is copied.Point : (0, 0) is copied.Line : (1, -2) to (0, 0) is created.Point : (2, -1) is copied.Point : (0, 0) is copied.Line : (2, -1) to (0, 0) is created.Point : (1, -2) is copied.Point : (2, -1) is copied.Line : (1, -2) to (2, -1) is copied.Line : (1, -2) to (2, -1)Line : (1, -2) to (2, -1)Line : (2, -1) to (0, 0)Line : (0, 0) to (2, -1)Line : (0, 0) to (2, -1) is erased.Point : (2, -1) is erased.Point : (0, 0) is erased.Line : (2, -1) to (0, 0) is erased.Point : (0, 0) is erased.Point : (2, -1) is erased.Line : (1, -2) to (2, -1) is erased.Point : (2, -1) is erased.Point : (1, -2) is erased.Line : (1, -2) to (2, -1) is erased.Point : (2, -1) is erased.Point : (1, -2) is erased.Line : (0, 1) to (1, 0) is erased.Point : (1, 0) is erased.Point : (0, 1) is erased.Line : (2, 3) to (4, 5) is erased.Point : (4, 5) is erased.Point : (2, 3) is erased.Line : (1, 1) to (2, 3) is erased.Point : (2, 3) is erased.Point : (1, 1) is erased.Line : (0, 0) to (1, 1) is erased.Point : (1, 1) is erased.Point : (0, 0) is erased.Point : (0, 0) is erased.Point : (2, -1) is erased.Point : (1, -2) is erased.

HINT

Append Code

append.cc,

#include <bits/stdc++.h>
using namespace std;
class Point{
	friend class Line;
public:
	double x,y;
	Point(){x=y=0;cout << "Point : (" << x << ", " << y <<") is created." << endl;}
	Point(double xx,double yy):x(xx),y(yy){ cout << "Point : (" << x << ", " << y <<") is created." << endl;}
	Point(const Point& p){x=p.x;y=p.y; cout << "Point : (" << x << ", " << y <<") is copied." << endl;  }
	void show(){cout << "Point : (0, 0)" << endl;}
	 ~Point ()  {cout << "Point : (" << x << ", " << y <<") is erased." << endl;}  
};
class Line{
	friend class Point;
public:
	Point p1,p2;
	Line():p1(0,0),p2(0,0){cout<<"Line : ("<<p1.x<<", "<<p1.y<<") to ("<<p2.x<<", "<<p2.y<<") is created."<<endl;}
	Line(Point &p,Point &q):p1(p),p2(q){cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ") is created." << endl;}
	Line(double x1,double y1,double x2,double y2):p1(x1,y1),p2(x2,y2){cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ") is created." << endl;}
	~Line(){cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ") is erased." << endl;}
    void show(){cout << "Line : (" << p1.x << ", " << p1.y << ") to (" << p2.x << ", " << p2.y << ")" << endl;}
	void readLine(){
        double x1, y1, x2, y2;  
        char c;  
        cin >> x1 >> c >> y1 >> x2 >> c >> y2;  
        SetLine(x1, y1, x2, y2);  

	}
	Line& SetLine(double a,double b,double c,double d)  
    {  
        p1.x = a;  
        p1.y = b;  
        p2.x = c;  
        p2.y = d;
        return *this;  
    }  
    Line& setLine(const Point& p, const Point& q){
        p1.x = p.x;  
        p1.y = p.y;  
        p2.x = q.x;  
        p2.y = q.y;
        return *this;     	
    }
    Line& setLine(const Line& l){
        p1.x = l.p1.x;  
        p1.y = l.p1.y;  
        p2.x = l.p2.x;  
        p2.y = l.p2.y;
        return *this;     	
    }
};
/
int main()
{
    int num, i;
    Point p(1, -2), q(2, -1), t;
    t.show();
    std::cin>>num;
    Line line[num];
    for(i = 0; i < num; i++)
    {
        line[i].readLine();
        line[i].show();
    }
    Line l1(p, q), l2(p,t), l3(q,t), l4(l1);
    l1.show();
    l2.setLine(l1).show();
    l3.show();
    l4.setLine(t,q).show();
}

22.平面上的点和线——Point类、Line类 (VI)

Description

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

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

接口描述:

Point::show()方法:按格式输出Point对象。

Point::x()方法:取x坐标。

Point::y()方法:取y坐标。

Line::show()方法:按格式输出Line对象。

Line::SetLine(double, double, double, double)方法:设置Line对象起点的x,y坐标(第一个和第二参数)和终点的x,y坐标(第三个和第四个坐标),并返回本对象

Line::SetLine(const Point &, const Point &)方法:设置Line对象的起点(第一个参数)和终点(第二个坐标),并返回本对象

Line::SetLine(const Line&)方法:设置Line对象,复制参数的坐标,并返回本对象

Line::readLine()方法:从标准输入上读入坐标,格式见Sample

Line::start()方法:取Line的起点

Line::end()方法:取Line的终点

Line::setStart()方法:设置Line的起点

Line::setEnd()方法:设置Line的终点

以下三个函数用于输出Line对象,格式同sample

showLineCoordinate(const Line&)

showLinePoint(const Line&)

showLine(const Line&)

Input

输入的第一行为N,表示后面有N行测试样例。

每行为两组坐标“x,y”,分别表示线段起点和终点的x坐标和y坐标,两组坐标间用一个空格分开,x和y的值都在double数据范围内。

Output

输出为多行,每行为一条线段,起点坐标在前终点坐标在后,每个点的X坐标在前,Y坐标在后,Y坐标前面多输出一个空格,用括号包裹起来。输出格式见sample。

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

Sample Input

40,0 1,11,1 2,32,3 4,50,1 1,0

Sample Output

Point : (1, -2) is created.Point : (2, -1) is created.Point : (0, 0) is created.Point : (0, 0)Point : (0, 0) is created.Point : (0, 0) is created.Line : (0, 0) to (0, 0) is created.Point : (0, 0) is created.Point : (0, 0) is created.Line : (0, 0) to (0, 0) is created.Point : (0, 0) is created.Point : (0, 0) is created.Line : (0, 0) to (0, 0) is created.Point : (0, 0) is created.Point : (0, 0) is created.Line : (0, 0) to (0, 0) is created.Point : (0, 0) is created.Point : (0, 0) is created.Line : (0, 0) to (0, 0) is created.Line : (0, 0) to (1, 1)Line : (1, 1) to (2, 3)Line : (2, 3) to (4, 5)Line : (0, 1) to (1, 0)Point : (1, -2) is copied.Point : (2, -1) is copied.Line : (1, -2) to (2, -1) is created.Point : (1, -2) is copied.Point : (0, 0) is copied.Line : (1, -2) to (0, 0) is created.Point : (2, -1) is copied.Point : (0, 0) is copied.Line : (2, -1) to (0, 0) is created.Point : (1, -2) is copied.Point : (2, -1) is copied.Line : (1, -2) to (2, -1) is copied.Line : (1, -2) to (2, -1)Line : Point : (1, -2) to Point : (0, 0)Line : Point : (1, -2) to Point : (2, -1)Line : (0, 0) to (2, -1)Line : (0, 0) to (2, -1) is erased.Point : (2, -1) is erased.Point : (0, 0) is erased.Line : (1, -2) to (2, -1) is erased.Point : (2, -1) is erased.Point : (1, -2) is erased.Line : (1, -2) to (0, 0) is erased.Point : (0, 0) is erased.Point : (1, -2) is erased.Line : (1, -2) to (2, -1) is erased.Point : (2, -1) is erased.Point : (1, -2) is erased.Line : (0, 1) to (1, 0) is erased.Point : (1, 0) is erased.Point : (0, 1) is erased.Line : (2, 3) to (4, 5) is erased.Point : (4, 5) is erased.Point : (2, 3) is erased.Line : (1, 1) to (2, 3) is erased.Point : (2, 3) is erased.Point : (1, 1) is erased.Line : (0, 0) to (1, 1) is erased.Point : (1, 1) is erased.Point : (0, 0) is erased.Line : (0, 0) to (2, -1) is erased.Point : (2, -1) is erased.Point : (0, 0) is erased.Point : (0, 0) is erased.Point : (2, -1) is erased.Point : (1, -2) is erased.

HINT

Append Code

append.cc,

#include <iostream>  
using namespace std;  
#include <iomanip>  
class Point{  
private:  
    double x_,y_;  
    friend class Line;  
public:  
    Point(double x,double y)  
    {  
        x_ = x;  
        y_ = y;  
        cout<<"Point : ("<<x_<<", "<<y_<<") is created."<<endl;  
    }  
    Point()  
    {  
        x_ = 0;  
        y_ = 0;  
        cout<<"Point : ("<<x_<<", "<<y_<<") is created."<<endl;  
    }  
    Point(double a):x_(a),y_(a) { cout<<"Point : ("<<x_<<", "<<y_<<") is created."<<endl;}  
    void setvalue(double xx,double yy)  
    {  
        x_ = xx;  
        y_ = yy;  
    }  
    //void setx(int xx) {x_ = xx;}  
    //void sety(int yy) {y_ = yy;}  
    void show()  
    {  
        cout<<"Point : ("<<x_<<", "<<y_<<")"<<endl;  
    }  
    double x() const { return x_;}  
    double y() const { return y_;}  
    Point(const Point &p)  
    {  
        x_ = p.x_;  
        y_ = p.y_;  
        cout<<"Point : ("<<x_<<", "<<y_<<") is copied."<<endl;  
    }  
    ~Point()  
    {  
        cout<<"Point : ("<<x_<<", "<<y_<<") is erased."<<endl;  
    }  
    void showNoEndOfLine()const{cout<<"Point : ("<<x_<<", "<<y_<<")";}  
};  
   
class Line{  
private:  
    Point x1_,y1_;  
    double x1,y1,x2,y2;  
    friend class Point;  
public:  
    Line(double xx1,double yy1,double xx2,double yy2):x1_(xx1,yy1),y1_(xx2,yy2)  
    {  
        cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<") is created."<<endl;  
    }  
    Line(Point &q1,Point &q2):x1_(q1),y1_(q2)  
    {  
        cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<") is created."<<endl;  
    }  
    Line(const Line&I):x1_(I.x1_),y1_(I.y1_) {cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<") is copied."<<endl;}  
    Line():x1_(),y1_(){cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<") is created."<<endl;}  
    Line &setLine(double xx3,double yy3,double xx4,double yy4)  
    {  
        x1_.x_ = xx3;  
        x1_.y_ = yy3;  
        y1_.x_ = xx4;  
        y1_.y_ = yy4;  
        return *this;  
    }  
    void show() const  
    {  
        cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<")"<<endl;  
    }  
    ~Line()  
    {  
        cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<") is erased."<<endl;  
    }  
    Line &setLine(const Point &p1,const Point &p2)  
    {  
        x1_ = p1;  
        y1_ = p2;  
        return *this;  
    }  
    Line &setLine(const Line& q)  
    {  
       *this = q;  
       return *this;  
    }  
    void readLine()  
    {  
       double x1,y1,x2,y2;  
       char c;  
       cin>>x1>>c>>y1>>x2>>c>>y2;  
       x1_.x_ = x1;  
       x1_.y_ = y1;  
       y1_.x_ = x2;  
       y1_.y_ = y2;  
    }  
    const Point &start() const  
    {  
        return x1_;  
    }  
    const Point &end() const  
    {  
        return y1_;  
    }  
    void setStart(Point &c) {x1_ = c;}  
    void setEnd(Point &e) {y1_ = e;}  
};  
void showLineCoordinate(const Line& line)  
{  
    std::cout<<"Line : ";  
    std::cout<<"("<<line.start().x()<<", "<<line.start().y()<<")";  
    std::cout<<" to ";  
    std::cout<<"("<<line.end().x()<<", "<<line.end().y()<<")";  
    std::cout<<std::endl;  
}  
   
void showLinePoint(const Line& line)  
{  
    std::cout<<"Line : ";  
    line.start().showNoEndOfLine();  
    std::cout<<" to ";  
    line.end().showNoEndOfLine();  
    std::cout<<std::endl;  
}  
   
void showLine(const Line& line)  
{  
    line.show();  
}  
   
int main()  
{  
    int num, i;  
    Point p(1, -2), q(2, -1), t;  
    t.show();  
    std::cin>>num;  
    Line line[num + 1];  
    for(i = 1; i <= num; i++)  
    {  
        line[i].readLine();  
        showLine(line[i]);  
    }  
    Line l1(p, q), l2(p,t), l3(q,t), l4(l1);  
    showLineCoordinate(l1);  
    showLinePoint(l2);  
    showLinePoint(l3.setLine(l1));  
    showLineCoordinate(l4.setLine(t,q));  
    line[0].setStart(t);  
    line[0].setEnd(q);  
}  1

23.平面上的点和线——Point类、Line类 (VII)

Description

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

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

接口描述:

Point::showCounter()方法:按格式输出当前程序中Point对象的计数。

Point::showSum()方法:按格式输出程序运行至当前存在过的Point对象总数。

Line::showCounter()方法:按格式输出当前程序中Line对象的计数。

Line::showSum()方法:按格式输出程序运行至当前存在过的Line对象总数。

Input

输入的第一行为N,表示后面有N行测试样例。

每行为两组坐标“x,y”,分别表示线段起点和终点的x坐标和y坐标,两组坐标间用一个空格分开,x和y的值都在double数据范围内。

Output

输出格式见sample。

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

Sample Input

40,0 1,11,1 2,32,3 4,50,1 1,0

Sample Output

Current : 3 points.In total : 3 points.Current : 6 lines.In total : 6 lines.Current : 17 points.In total : 17 points.Current : 6 lines.In total : 7 lines.Current : 15 points.In total : 17 points.Current : 6 lines.In total : 8 lines.Current : 17 points.In total : 21 points.Current : 6 lines.In total : 9 lines.Current : 15 points.In total : 21 points.Current : 6 lines.In total : 10 lines.Current : 17 points.In total : 25 points.Current : 6 lines.In total : 11 lines.Current : 15 points.In total : 25 points.Current : 6 lines.In total : 12 lines.Current : 17 points.In total : 29 points.Current : 6 lines.In total : 13 lines.Current : 15 points.In total : 29 points.Current : 9 lines.In total : 17 lines.Current : 21 points.In total : 37 points.Current : 13 lines.In total : 21 lines.Current : 21 points.In total : 45 points.

HINT

Append Code

append.cc,

#include <iostream>
using namespace std;
#include <iomanip>
class Point{
private:
    double x_,y_;
    friend class Line;
    static int sta1;
    static int sta2;
public:
    Point(double x,double y)
    {
        x_ = x;
        y_ = y;
        sta1++;
        sta2++;
        //cout<<"Point : ("<<x_<<", "<<y_<<") is created."<<endl;
    }
    Point(double d)
    {
        x_ = d;
        y_ = d;
        sta1++;
        sta2++;
    }
    Point()
    {
        x_ = 0;
        y_ = 0;
        sta1++;
        sta2++;
        //cout<<"Point : ("<<x_<<", "<<y_<<") is created."<<endl;
    }
    void setvalue(double xx,double yy)
    {
        x_ = xx;
        y_ = yy;
    }
    void setx(int xx) {x_ = xx;}
    void sety(int yy) {y_ = yy;}
    void show()
    {
        cout<<"Point : ("<<x_<<", "<<y_<<")"<<endl;
    }
    int x() { return x_;}
    int y() { return y_;}
    Point(const Point &p)
    {
        sta1++;
        sta2++;
        x_ = p.x_;
        y_ = p.y_;
        //cout<<"Point : ("<<x_<<", "<<y_<<") is copied."<<endl;
    }
    ~Point()
    {
       sta1 = sta1 - 1;
        //cout<<"Point : ("<<x_<<", "<<y_<<") is erased."<<endl;
    }
    static void showCounter()
    {
        cout<<"Current : "<<sta1<<" points."<<endl;
    }
    static void showSum()
    {
        cout<<"In total : "<<sta2<<" points."<<endl;
    }
};
 int Point::sta1(0);
 int Point::sta2(0);
class Line{friend class Point;
private:
    Point x1_,y1_;
    double x1,y1,x2,y2;
 
 
public:static int sta3;
    static int sta4;
    Line(double xx1,double yy1,double xx2,double yy2):x1_(xx1,yy1),y1_(xx2,yy2)
    {
        sta3++;
        sta4++;
        //cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<") is created."<<endl;
    }
    Line(Point &q1,Point &q2):x1_(q1),y1_(q2)
    {
        sta3++;
        sta4++;
        //cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<") is created."<<endl;
    }
    Line():x1_(),y1_(){sta3++; sta4++;/*cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<") is created."<<endl;*/}
    Line setLine(double xx3,double yy3,double xx4,double yy4)
    {
 
        sta3++;
        sta4++;
        x1_.x_ = xx3;
        x1_.y_ = yy3;
        y1_.x_ = xx4;
        y1_.y_ = yy4;
        return *this;
    }
    void show()
    {
        cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<")"<<endl;
    }
    ~Line()
    {
        sta3 = sta3 - 1;
        //cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<") is erased."<<endl;
    }
    Line &setLine(const Point &p1,const Point &p2)
    {
        x1_ = p1;
        y1_ = p2;
        return *this;
    }
    Line &setLine(const Line& q)
    {
       x1_ = q.x1_;
       y1_ = q.y1_;
       return *this;
    }
    void readLine()
    {
       double x1,y1,x2,y2;
       char c;
       cin>>x1>>c>>y1>>x2>>c>>y2;
       x1_.x_ = x1;
       x1_.y_ = y1;
       y1_.x_ = x2;
       y1_.y_ = y2;
    }
    Line(const Line &b):x1_(b.x1_),y1_(b.y1_)
    {
        sta3++;
        sta4++;
        //cout<<"Line : ("<<x1_.x_<<", "<<x1_.y_<<") to ("<<y1_.x_<<", "<<y1_.y_<<") is copied."<<endl;
    }
    static void showCounter()
    {
        cout<<"Current : "<<sta3<<" lines."<<endl;
    }
    static void showSum()
    {
        cout<<"In total : "<<sta4<<" lines."<<endl;
    }
};
int Line::sta3 = 0;
int Line::sta4(0);
 
int main()
{
    int num, i;
    Point p(1, -2), q(2, -1), t;
    t.showCounter();
    t.showSum();
    std::cin>>num;
    Line line[num + 1];
    for(i = 1; i <= num; i++)
    {
        Line *l1, l2;
        l1->showCounter();
        l1->showSum();
        l1 = new Line(p, q);
        line[i].readLine();
        p.showCounter();
        p.showSum();
        delete l1;
        l2.showCounter();
        l2.showSum();
        q.showCounter();
        q.showSum();
    }
    Line l1(p, q), l2(p,t), l3(q,t), l4(l1);
    Line::showCounter();
    Line::showSum();
    Point::showCounter();
    Point::showSum();
    Line *l = new Line[num];
    l4.showCounter();
    l4.showSum();
    delete[] l;
    t.showCounter();
    t.showSum();
}
 

24.时间类的构造和输出

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 12

3 0 02

3 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作为填充字符

Append Code

append.cc,

#include <iostream>
#include <iomanip>
using namespace std;
class Time{
public:
	int h,m,s;
	Time(int hh,int mm,int ss):h(hh),m(mm),s(ss){}
	void showTime(){
		cout << setw(2) << setfill('0') << h << ":" << setw(2) << m << ":" << setw(2) << s << endl;
	}
};

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();
    }
}

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

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

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

25. 时间类的成员读写

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 12

3 0 02

3 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作为填充字符

Append Code

append.cc,

#include <iostream>
#include <iomanip>
using namespace std;
class Time{
public:
	int h,m,s;
	Time(){}
	void hour(int hh){h=hh;}
	void minute(int mm){m=mm;}
	void second(int ss){s=ss;}
	int hour(){return h;}
	int minute(){return m;}
	int second(){return s;}
};

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;
    }
}

* 26.时间类的输入

Description

Description

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

\1. Time::Time()无参构造方法。

\2. Time::inputTime()方法:按格式从标准输入读取数据修改Time对象的时分秒数值。该方法返回修改后的对象。

\3. 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

50 0 10 59 591 1 123 0 023 59 59

Sample Output

00:00:0100:59:5901:01:0123:00:0023:59:59

HINT

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

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

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

Append Code

append.cc,

#include <iostream>
#include <iomanip>
using namespace std;
class Time{
public:
	int h,m,s;
	Time(){}
    Time(int hh, int mm, int ss) : s(ss), m(mm), h(hh) { }  
    Time& inputTime()  
    {  
        cin >> h >> m >> s;  
        return *this;  
    }  	
    void showTime(){
		cout << setw(2) << setfill('0') << h << ":" << setw(2) << m << ":" << setw(2) << s << endl;
	}
};
///
int main()
{
    Time t;
    int cases;
    cin>>cases;
    for(int i = 1; i <= cases; ++i)
        t.inputTime().showTime();
}

27.时间类的拷贝和整体读写

Description

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

\1. Time::Time()无参构造方法。

\2. Time::Time(int,int,int)构造方法:传递时分秒的三个参数构造对象。

\3. Time::Time(const T&)拷贝构造方法。拷贝构造函数调用时输出“There was a call to the copy constructor : h,m,s”,“h,m,s”为所构造对象的时分秒数值,无需补0。

\4. 对象整体读写方法:

Time::setTime(int,int,int)方法:传递时分秒三个参数修改Time对象的时分秒数。该方法返回修改后的对象。

Time::setTime(const T&)方法:传递一个参数修改Time对象的时分秒数。该方法返回修改后的对象。

Time::getTime()方法:返回对象自身的引用。其实,t.getTime()即t。

仅在Time类中的Time::getTime()方法实在是多余,在组合或者继承关系时才会有机会用到。

\5. Time::showTime()方法:输出“hh:mm:ss”,不足两位的要前面补0。

注意:在用Time对象传递参数时应传对象的引用而不是直接传对象,返回对象时同样返回引用,以免产生多余的对象拷贝。

你设计一个时间类Time,使得main()函数能够正确运行。

函数调用格式见append.cc

append.cc中已给出main()函数。main()函数内容稍微繁复,仅为测试对象的各种调用情况。

Input

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

Output

开始部分为由main()函数产生的固定输出,用于测试对象的某些方法的调用情况。输出“Test data output :”之后为测试数据对应的输出:

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

Sample Input

50 0 10 59 591 1 123 0 023 59 59

Sample Output

Copy constructor test output :There was a call to the copy constructor : 0,0,0There was a call to the copy constructor : 1,2,3Test data output :00:00:0100:59:5901:01:0123:00:0023:59:59

HINT

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

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

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

Append Code

append.cc,

#include <iostream>
#include <iomanip>
using namespace std;
class Time{
public:
	int h,m,s;
	Time():h(0),m(0),s(0){}
    Time(int hh, int mm, int ss) : s(ss), m(mm), h(hh) { }
    Time(const Time& t){h = t.h;m = t.m;s = t.s;cout << "There was a call to the copy constructor : " << h << "," << m << "," << s << endl;}
    Time &setTime(int hh,int mm,int ss)  
    {  
        h = hh;m = mm;s = ss;  
        return *this;  
    }
    Time &setTime(const Time & t)  
    {  
        h = t.hour();  
        m = t.minute();  
        s = t.second();  
        return *this;  
    }  
    Time &getTime()  
    {  
        return *this;  
    }  
    int hour()const{return h;}  
    int minute()const{return m;}  
    int second()const{return s;}  
    void showTime(){
		cout << setw(2) << setfill('0') << h << ":" << setw(2) << m << ":" << setw(2) << s << endl;
	}      
};
///
int main()
{
    cout<<"Copy constructor test output :"<<endl;
    Time t;
    Time tt(t);
    Time ttt(1, 2, 3);
    Time tttt(ttt.getTime());
    cout<<"\nTest data output :"<<endl;
 
    int cases;
    cin>>cases;
    for(int i = 1; i <= cases; ++i)
    {
        if(i % 2 == 0)
        {
            int hour, minute, second;
            cin>>hour>>minute>>second;
            t.setTime(hour, minute, second).showTime();
        }
        if(i % 2 == 1)
        {
            int hour, minute, second;
            cin>>hour>>minute>>second;
            Time tt(hour, minute, second);
            t.setTime(tt).showTime();
        }
    }
}

28.时间类的错误数据处理

Description

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

\1. Time::Time()无参构造方法。

\2. Time::Time(int,int,int)构造方法:传递时分秒的三个参数构造对象。

\3. Time::Time(const T&)拷贝构造方法。

\4. 成员读函数:

Time::hour() :返回Time的小时数;

Time::minute():返回Time的分钟数;

Time::second():返回Time的秒数。

\5. 成员写函数:

Time::hour(int) :传参修改Time的小时数;

Time::minute(int):传参修改Time的分钟数;

Time::second(int):传参修改Time的秒数。

\6. 对象整体读写方法:

Time::setTime(int,int,int)方法:传递时分秒三个参数修改Time对象的时分秒数。该方法返回修改后的对象。

Time::setTime(const T&)方法:传递一个参数修改Time对象的时分秒数。该方法返回修改后的对象。

Time::getTime()方法:返回对象自身的引用。其实,t.getTime()即t。

仅在Time类中的Time::getTime()方法实在是多余,在组合或者继承关系时才会有机会用到。

\7. Time::inputTime()方法:按格式从标准输入读取数据修改Time对象的时分秒数值。该方法返回修改后的对象。

\8. Time::showTime()方法:输出“hh:mm:ss”,不足两位的要前面补0。如果对象不是合法的时间,则输出“Time error”。

你设计一个时间类Time,使得main()函数能够正确运行。

函数调用格式见append.cc

append.cc中已给出main()函数。main()函数内容稍微繁复,仅为测试对象的各种调用情况。

Input

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

Output

每组测试数据对应一组输出“hh:mm:ss”,不足两位的输出需要前面补0。如果输入的时间不合法,则输出“Time error”。格式见sample。

Sample Input

60 0 10 59 591 1 6023 0 023 59 5924 1 0

Sample Output

00:00:0100:59:59Time error23:00:0023:59:59Time error

HINT

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

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

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

Append Code

append.cc,

#include <iostream>
#include <iomanip>
using namespace std;
class Time{
public:
	int h,m,s;
	Time():h(0),m(0),s(0){}
    Time(int hh, int mm, int ss) : s(ss), m(mm), h(hh) { }
    Time(const Time& t){h = t.h;m = t.m;s = t.s;cout << "There was a call to the copy constructor : " << h << "," << m << "," << s << endl;}
    Time &setTime(int hh,int mm,int ss)  
    {  
        h = hh;m = mm;s = ss;  
        return *this;  
    }
    Time &setTime(const Time & t)  
    {  
        h = t.hour();  
        m = t.minute();  
        s = t.second();  
        return *this;  
    }  
    Time &getTime()  
    {  
        return *this;  
    }  
	void hour(int hh){h=hh;}
	void minute(int mm){m=mm;}
	void second(int ss){s=ss;}    
    int hour()const{return h;}  
    int minute()const{return m;}  
    int second()const{return s;}  
    void showTime(){
    	if(h < 24 && m < 60 && s < 60)
			cout << setw(2) << setfill('0') << h << ":" << setw(2) << m << ":" << setw(2) << s << endl;
		else
			cout << "Time error" << endl;
	}    
	Time &inputTime(){
		cin >> h >> m >> s ;
		return *this;
	}  
};
/
int main()
{
    Time t;
    int cases;
    cin>>cases;
    for(int i = 1; i <= cases; ++i)
    {
        if(i % 4 == 0)
        {
            int hour, minute, second;
            cin>>hour>>minute>>second;
            Time tt(hour, minute, second);
            tt.showTime();
        }
        if(i % 4 == 1)
        {
            int hour, minute, second;
            cin>>hour>>minute>>second;
            t.setTime(hour, minute, second).showTime();
        }
        if(i % 4 == 2)
            t.inputTime().showTime();
        if(i % 4 == 3)
        {
            int hour, minute, second;
            cin>>hour>>minute>>second;
            t.hour(hour);
            t.minute(minute);
            t.second(second);
            t.showTime();
        }
    }
}

29.时间类的常量

Description

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

\1. Time::Time()无参构造方法。

\2. Time::Time(int,int,int)构造方法:传递时分秒的三个参数构造对象。

\3. Time::Time(const T&)拷贝构造方法。

\4. 成员读函数:

Time::hour() :返回Time的小时数;

Time::minute():返回Time的分钟数;

Time::second():返回Time的秒数。

\5. 成员写函数:

Time::hour(int) :传参修改Time的小时数;

Time::minute(int):传参修改Time的分钟数;

Time::second(int):传参修改Time的秒数。

\6. 对象整体读写方法:

Time::setTime(int,int,int)方法:传递时分秒三个参数修改Time对象的时分秒数。该方法返回修改后的对象。

Time::setTime(const T&)方法:传递一个参数修改Time对象的时分秒数。该方法返回修改后的对象。

Time::getTime()方法:返回对象自身的引用。其实,t.getTime()即t。

仅在Time类中的Time::getTime()方法实在是多余,在组合或者继承关系时才会有机会用到。

\7. Time::inputTime()方法:按格式从标准输入读取数据修改Time对象的时分秒数值。该方法返回修改后的对象。

\8. Time::showTime()方法:输出“hh:mm:ss”,不足两位的要前面补0。如果对象不是合法的时间,则输出“Time error”。

注意:为了保证Time类的常量对象能够正确的调用Time类的方法,那些不修改对象数据成员的函数都应该是常量成员函数,在返回对象自身的引用时也应返回常量引用。

你设计一个时间类Time,使得main()函数能够正确运行。

函数调用格式见append.cc

append.cc中已给出main()函数。main()函数内容稍微繁复,仅为测试对象的各种调用情况。

Input

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

Output

开始部分为由main()函数产生的固定输出,用于测试对象的某些方法的调用情况。输出“Test data output :”之后为测试数据对应的输出:

每组测试数据对应一组输出“hh:mm:ss”,不足两位的输出需要前面补0。如果输入的时间不合法,则输出“Time error”。格式见sample。

Sample Input

60 0 10 59 591 1 6023 0 023 59 5924 1 0

Sample Output

Constant test output :00:00:0001:02:03Time errorTest data output :00:00:0100:59:59Time error23:00:0023:59:59Time error

HINT

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

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

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

Append Code

append.cc,


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值