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

5

20 15 10

10 15 20

100 100 0

0 1 -1

0 0 0

Sample Output

case 1 : 20, 10

case 2 : 20, 10

case 3 : 100, 0

case 4 : 1, -1

case 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 2

1.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.54

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

9 -3

-12 4

3 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,

#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,

#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,

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):设置用字符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):设置用字符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):设置用字符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):设置用字符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):设置用字符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):设置用字符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):设置用字符c作为填充字符

Append Code

append.cc,

#include <iostream>
#include <iomanip>
using namespace std;
class Time{
private:
    int h,m,s;
public:
    Time():h(0),m(0),s(0){}
    Time(int hh, int mm, int ss):h(hh),m(mm),s(ss){ }
    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;  
    }  
    const Time &getTime()const  
    {  
        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() const{
        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()
{
    cout<<"Constant test output :"<<endl;
    const Time c;
    const Time cc(1, 2, 3);
    const Time ccc(23, 60, 60);
    cout<<setw(2)<<setfill('0')<<c.hour()<<":";
    cout<<setw(2)<<setfill('0')<<c.minute()<<":";
    cout<<setw(2)<<setfill('0')<<c.second()<<endl;
    cc.getTime().showTime();
    ccc.showTime();

    cout<<"\nTest data output :"<<endl;
    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();
        }
    }
}

30.时间类的12小时制输出

Description

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

\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”。

\9. Time::showTime12Hour()方法:输出12小时制的时间:上午输出“hh:mm:ss a.m.”、下午输出“hh:mm:ss p.m.”。如果对象不是合法的时间,则输出“Time error”。注意:该函数仅显示12小时制时间,并不修改对象的数据成员,对象依然存储24小时制时间。

12小时制以数字12、1、2、3、4、5、6、7、8、9、10、11依次序表示每个时段的。

24小时制的00:00~00:59,是12小时制的12:00 a.m.~12:59 a.m.;

24小时制的1:00~11:59是十二小时制的1:00 a.m.~11:59 a.m.。

24小时制的12:00~12:59,是12小时制的12:00 p.m.~12:59 p.m.;

24小时制的13:00~23:59是十二小时制的1:00 p.m.~11:59 p.m.。

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

函数调用格式见append.cc。

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

Input

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

Output

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

每组测试数据对应一组输出,奇数行的输入对应输出24小时制时间“hh:mm:ss”,偶数行的输入对应输出12小时制时间:上午输出“hh:mm:ss a.m.”、下午输出“hh:mm:ss p.m.”,不足两位的输出需要前面补0。如果输入的时间不合法,则输出“Time error”。格式见sample。

Sample Input

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

Sample Output

Constant test output :11:59:58 a.m.12:00:01 p.m.11:59:5812:00:01Test data output :00:00:0112:59:59 a.m.Time error11:00:00 p.m.23:59:59Time error

HINT

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

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

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

Append Code

append.cc,

#include <iostream>
#include <iomanip>
using namespace std;
class Time{
private:
    int h,m,s;
public:
    Time():h(0),m(0),s(0){}
    Time(int hh, int mm, int ss):h(hh),m(mm),s(ss){ }
    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;  
    }  
    const Time &getTime()const  
    {  
        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() const{
        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;
    }  
    void showTime12Hour()const{
        if(h >= 1 && h < 12 && m < 60 && s < 60)
            cout << setw(2) << setfill('0') << h << ":" << setw(2) << m << ":" << setw(2) << s << " a.m." << endl;            
        else if(h >= 0 && h < 1 && m < 60 && s < 60)
            cout << setw(2) << setfill('0') << "12" << ":" << setw(2) << m << ":" << setw(2) << s << " a.m." << endl;            
        else if(h >= 12 && h < 13 && m < 60 && s < 60)
            cout << setw(2) << setfill('0') << "12" << ":" << setw(2) << m << ":" << setw(2) << s << " p.m." << endl;            
        else if(h >= 13 && h < 24 && m < 60 && s < 60)
            cout << setw(2) << setfill('0') << h-12 << ":" << setw(2) << m << ":" << setw(2) << s << " p.m." << endl;            
        else
            cout << "Time error" << endl;            
    }
};
/////////////////////////////
int main()
{
    cout<<"Constant test output :"<<endl;
    const Time c(11, 59, 58);
    const Time cc(12, 0, 1);
    c.showTime12Hour();
    cc.showTime12Hour();
    c.showTime();
    cc.showTime();

    cout<<"\nTest data output :"<<endl;
    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.showTime12Hour();
        }
        if(i % 4 == 1)
        {
            int hour, minute, second;
            cin>>hour>>minute>>second;
            t.setTime(hour, minute, second).showTime();
        }
        if(i % 4 == 2)
            t.inputTime().showTime12Hour();
        if(i % 4 == 3)
        {
            int hour, minute, second;
            cin>>hour>>minute>>second;
            t.hour(hour);
            t.minute(minute);
            t.second(second);
            t.showTime();
        }
    }
}

31.时间类的静态成员计数

Description

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

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

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

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

\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 error”。

\6. 静态成员方法:

Time::getNumber()方法:返回程序中已创建的Time对象总数。

Time::displayNumber()方法:输出程序中已创建的Time对象总数。

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

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

函数调用格式见append.cc。

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

Input

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

Output

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

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

最后一行输出一个整数n,表示有n组测试数据输入。

Sample Input

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

Sample Output

Static member test output :Now, There is 0 object of Time.Now, There is 1 object of Time.There was a call to the copy constructor : 0,0,0Now, There is 2 object of Time.Now, There is 3 object of Time.There was a call to the copy constructor : 1,2,3Now, There is 4 object of Time.Test data output :00:00:0100:59:59Time error23:00:0023:59:59Time error6

HINT

Append Code

append.cc,

#include <iostream>
#include <iomanip>
using namespace std;
class Time{
private:
    int h,m,s;
    static int num;
public:
    Time():h(0),m(0),s(0){num++;}
    Time(int hh, int mm, int ss):h(hh),m(mm),s(ss){num++;}
    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;num++;}
    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;  
    }  
    const Time &getTime()const  
    {  
        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() const{
        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;
    }  
    static int getNumber(){return num;}
    static void displayNumber(){
        cout << "Now, There is " << num << " object of Time." << endl;
    }
};
int Time::num=0;
////////////////////
int main()
{
    cout<<"Static member test output :"<<endl;
    Time::displayNumber();
    Time t;
    t.displayNumber();
    Time tt(t);
    tt.displayNumber();
    Time ttt(1, 2, 3);
    ttt.displayNumber();
    Time tttt(ttt.getTime());
    tttt.displayNumber();
    int non_cases = Time::getNumber();

    cout<<"\nTest data output :"<<endl;
    int hour, minute, second;
    while(cin>>hour>>minute>>second)
    {
        Time t;
        t.setTime(hour, minute, second).showTime();
    }
    cout<<t.getNumber() - non_cases<<endl;
}

static–http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777441.html

C++的static有两种用法:面向过程程序设计的static和面向对象程序设计中的static。前者应用于普通变量和函数,不涉及类;后者主要说明static在类中的作用。

32.时间类的加、减法赋值运算

Description

封装一个时间类Time,在类上重载以下运算符,使得main()函数能够正确运行。

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

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

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

\4. 运算符

加法赋值运算符“+=”和减法赋值运算符“-=”:把一个整数m加到Time对象自身,并且仅对合法的时间操作,不会产生不合法的时间,比如:

若原时间对象为“00:00:00”,减去2后的对象为“23:59:58”;

若原时间对象为“23:59:59”,加上1后的对象为“00:00:00”;

若原时间对象为“24:60:60”,加减后的对象仍为“24:60:60”

函数调用格式见append.cc。

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

Input

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

Output

每个输入对应两行输出,分别为时间“hh,mm,ss”加上m秒和减去m秒后的值。错误的时间输出“Time error”

Sample Input

60 0 1 20 59 59 11 1 60 1023 0 0 6023 59 59 10024 1 0 3

Sample Output

00:00:0323:59:5901:00:0000:59:58Time errorTime error23:01:0022:59:0000:01:3923:58:19Time errorTime error

HINT

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

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

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

left :设置输出左对齐

right :设置输出优对齐

Append Code

append.cc,

#include <iostream>
#include <iomanip>
using namespace std;
class Time{
private:
    int h,m,s;
    int k;
public:
    Time():h(0),m(0),s(0){}
    Time(int hh, int mm, int ss):h(hh),m(mm),s(ss){ }
    Time(const Time& t){h = t.h;m = t.m;s = t.s;}
    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;  
    }  
    const Time &getTime()const  
    {  
        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() const{
        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;
    }  
    Time& operator +=(int t)  
    {  
        if(h*3600 + m*60 + s >= 24*3600|| m>59||s>59||h<0||s<0||m<0)  
            return *this;  
        else  
        {  
            k = (h*3600 + m*60 + s+t)%(24*3600);  
            h = k/3600;  
            m = (k -h*3600)/60;  
            s = k-h*3600-m*60;  
            return *this;  
        }  
    }  
    Time& operator -=(int t)  
    {  
           if(h*3600 + m*60 + s >= 24*3600|| m>59||s>59||h<0||s<0||m<0)  
            return *this;  
        else  
        {  
            k = (h*3600 + m*60 + s-t+24*3600)%(24*3600);  
            h = k/3600;  
            m = (k -h*3600)/60;  
            s = k-h*3600-m*60;  
            return *this;  
        }  
    }  
};
///////////////
int main()
{
    int cases;
    cin>>cases;
    for(int i = 1; i <= cases; ++i)
    {
        Time t;
        t.inputTime();
        Time tt(t);
        int num;
        cin>>num;
        t += num;
        t.showTime();
        tt -= num;
        tt.showTime();
    }
}

33.时间类的流插入、提取和递增、递减运算

Description

封装一个时间类Time,在类上重载以下运算符,使得main()函数能够正确运行。

流插入操作符“>>”,按照输入格式从标准输入读取三个整数:hh,mm,ss,分别表示时、分、秒,其值在int范围内。

流提取操作符“<<”;按照“hh:mm:ss”输出Time类的对象,不合法的时间输出“error!!!”。

前置自增运算符“++”:把时间对象的秒数加1并返回。

前置自减运算符“–”:把时间对象的秒数减1并返回。

后置自增运算符“++”:把时间对象的秒数加1,返回原值。

后置自减运算符“–”:把时间对象的秒数减1,返回原值。

以上4个自增、自减仅对合法的时间操作,并且不会产生不合法的时间。比如:

若原时间对象为“00:00:00”,自减运算后的对象为“23:59:59”;

若原时间对象为“23:59:59”,自增运算后的对象为“00:00:00”;

若原时间对象为“24:60:60”,自增或自减运算后对象仍为“24:60:60”。

函数调用格式见append.cc。

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

Input

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

Output

输出一张表:每列8个字符宽,两列之间有一个空格。

首先,输出一个表头:“++t –t t t– t++ t ”,

其次,对应每组测试数据在一行内依次以下内容:

前置++、前置–、原值、后置–、后置++、原值。

若输入的日期合法,输出格式为“hh:mm:ss”,不足两位的输出需要前面补0。如果输入的时间不合法,则输出“error!!!”。格式见sample。

Sample Input

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

Sample Output

++t –t t t– t++ t 00:00:02 00:00:01 00:00:01 00:00:01 00:00:00 00:00:0101:00:00 00:59:59 00:59:59 00:59:59 00:59:58 00:59:59error!!! error!!! error!!! error!!! error!!! error!!!23:00:01 23:00:00 23:00:00 23:00:00 22:59:59 23:00:0000:00:00 23:59:59 23:59:59 23:59:59 23:59:58 23:59:59error!!! error!!! error!!! error!!! error!!! error!!!

HINT

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

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

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

left :设置输出左对齐

right :设置输出优对齐

Append Code

append.cc,

#include <iostream>
#include <iomanip>
using namespace std;
class Time
{
private:
    int h,m,s;
    int flag;
public:
    friend istream & operator >> (istream &is,Time &t)
    {
        is>>t.h>>t.m>>t.s;
        if(t.h>23||t.h<0||t.m>59||t.m<0||t.s>59||t.s<0)
            t.flag = 1;
        else t.flag = 0;
        return is;
    }
    friend ostream & operator << (ostream &os,const Time &t)
    {
        if(t.h*3600+t.m*60+t.s>=24*3600||t.h<0||t.s<0||t.m<0||t.m>59||t.s>59) {os<<"error!!!";}
    else {os<<setw(2)<<setfill('0')<<t.h<<":"<<setw(2)<<t.m<<":"<<setw(2)<<t.s;}
    return os;
    }
    Time & operator ++ ()
    {
        if(h*3600+m*60+s >= 24*3600||h<0||s<0||m<0||m>59||s>59)
            return *this;
        else
        {
            flag=((h*3600+m*60+s)+1)%(24*3600);
            h=flag/3600;
            m=(flag-h*3600)/60;
            s=flag-h*3600-m*60;
            return *this;
        }
    }
    Time &operator --()
    {
         if(h*3600+m*60+s >= 24*3600||h<0||s<0||m<0||m>59||s>59)
            return *this;
        else
        {
            flag=((h*3600+m*60+s)-1+24*3600)%(24*3600);
            h=flag/3600;
            m=(flag-h*3600)/60;
            s=flag-h*3600-m*60;
            return *this;
        }
    }
    Time operator ++ (int )
    {
        Time t = (*this);
        if(h*3600+m*60+s >= 24*3600||h<0||s<0||m<0||m>59||s>59)
            return t;
        else
        {
            flag=((h*3600+m*60+s)+1)%(24*3600);
            h=flag/3600;
            m=(flag-h*3600)/60;
            s=flag-h*3600-m*60;
            return t;
        }
    }
     Time operator --(int)
    {
        Time t = (*this);
         if(h*3600+m*60+s >= 24*3600||h<0||s<0||m<0||m>59||s>59)
            return t;
        else
        {
            flag=((h*3600+m*60+s)-1+24*3600)%(24*3600);
            h=flag/3600;
            m=(flag-h*3600)/60;
            s=flag-h*3600-m*60;
            return t;
        }
    }
};
int main()
{
    Time t;
    int cases;
    cin>>cases;
    cout<<setw(8)<<left<<"++t"<<" ";
    cout<<setw(8)<<left<<"--t"<<" ";
    cout<<setw(8)<<left<<"t"<<" ";
    cout<<setw(8)<<left<<"t--"<<" ";
    cout<<setw(8)<<left<<"t++"<<" ";
    cout<<setw(8)<<left<<"t"<<right<<endl;
    for(int i = 1; i <= cases; ++i)
    {
        cin>>t;
        cout<<(++t)<<" ";
        cout<<(--t)<<" ";
        cout<<t<<" ";
        cout<<t--<<" ";
        cout<<t++<<" ";
        cout<<t<<endl;
    }
}

34.时间和日期类(I)

Description

设计一个时间类和一个日期类,用于读取输入的数据,按格式输出日期和时间。

设计日期类Date需支持以下操作:

Date::Date(int,int,int)构造方法:传入的参数依次为年月日,用参数将日期初始化。

Date::showDate()按格式输出Date对象。

设计时间类Time需支持以下操作:

Time::Time(int,int,int)构造方法:传入的参数依次为时分秒,用参数将时间初始化。

Time::showTime()按格式输出Time对象。

-—————————————————————————-

你设计Date类和Time类,使得main()函数能够正确运行。

函数调用格式见append.cc。

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

Input

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

后面的输入每行为一组测试数据。每组测试数据的前3个整数是日期的年月日,后3个整数是时间的时分秒。

Output

每组测试数据对应一行输出。日期的输出格式为“yyyy-mm-dd”,时间的输出格式为“hh:mm:ss”,中间用一个空格分开。

Sample Input

31982 10 1 0 0 02000 2 28 23 59 592014 7 2 13 30 01

Sample Output

1982-10-01 00:00:002000-02-28 23:59:592014-07-02 13:30:01

HINT

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

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

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

Append Code

append.cc,

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

};
/////////////////////////////
int main()
{
    int cases;
    cin >> cases;
    for(int ca = 0; ca < cases; ca++)
    {
        int year, month, day;
        cin >> year >> month >> day;
        Date date(year, month, day);
        date.showDate();
        cout << " ";
        int hour, minute, second;
        cin >> hour >> minute >> second;
        Time time(hour, minute, second);
        time.showTime();
        cout << endl;
    }
}

35.时间和日期类(II)

Description

设计一个日期时间类,用于读取输入的数据,按格式输出日期和时间。

设计日期时间类DateTime由2个成员组成,分别是一个Date类对象和一个Time类对象;

设计DateTime类需支持以下操作:

DateTime::DateTime()无参构造方法:初始化为1年1月1日、0时0分0秒;

DateTime::DateTime(const Date&,const Time&)构造方法:依照参数传入的日期和时间初始化对象;

DateTime::DateTime(int,int,int,int,int,int)构造方法:依照参数(顺序为年月日、时分秒)初始化对象;

DateTime::showDateTime()方法:按格式输出DateTime对象;

DateTime::setDateTime(int,int,int,int,int,int)方法:依照参数(顺序为年月日、时分秒)修改对象的属性值;

DateTime类包含了两个类:Date类和Time类

设计日期类Date需支持以下操作:

Date::Date()无参构造方法:初始化为1年1月1日

Date::Date(int,int,int)构造方法:传入的参数依次为年月日,用参数将日期初始化。

Date::showDate()方法:按格式输出Date对象。

Date::setDate(int,int,int)方法:传入的参数依次为年月日,用参数修改对象的属性值

设计时间类Time需支持以下操作:

Time::Time()无参构造方法:初始化为0时0分0秒

Time::Time(int,int,int)构造方法:传入的参数依次为时分秒,用参数将时间初始化。

Time::showTime()方法:按格式输出Time对象。

Time::setTime(int,int,int)方法:传入的参数依次为时分秒,用参数修改对象的属性值

-—————————————————————————-

你设计DateTime类、Date类和Time类,使得main()函数能够正确运行。

函数调用格式见append.cc。

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

Input

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

后面的输入每行为一组测试数据。每组测试数据的前3个整数是日期的年月日,后3个整数是时间的时分秒。

Output

每组测试数据对应一行输出。日期的输出格式为“yyyy-mm-dd”,时间的输出格式为“hh:mm:ss”,中间用一个空格分开。

Sample Input

31982 10 1 0 0 02000 2 28 23 59 592014 7 2 13 30 01

Sample Output

1000-10-10 01:01:011982-10-01 00:00:002000-02-28 23:59:592014-07-02 13:30:01

HINT

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

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

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

Append Code

append.cc,

#include <iostream>
#include <iomanip>
using namespace std;
class Date{
public:
    friend class DateTime;
    int y,m,d;
    Date():y(1),m(1),d(1){}
    Date(int yy,int mm,int dd):y(yy),m(mm),d(dd){}
    void showDate(){cout << y << "-" << setw(2) << setfill('0') << m << "-" << d << " ";}
    Date& setDate(int yy,int mm,int dd){y=yy;m=mm;d=dd;return *this;}
};
class Time{
public:
    friend class DateTime;
    int h,m,s;
    Time():h(0),m(0),s(0){}
    Time(int hh,int mm,int ss):h(hh),m(mm),s(ss){}
    void showTime(){cout << setw(2) << setfill('0') << h << ":" << m << ":" << s ;}
    Time& setTime(int hh,int mm,int ss){h=hh;m=mm;s=ss;return *this;}
};
class DateTime{
public:
    friend class Date;
    friend class Time;
    Date D;
    Time T;
    DateTime():D(0,0,0),T(0,0,0){}
    DateTime(const Date& d,const Time& t):D(d),T(t){}
    DateTime(int a,int b,int c,int d,int e,int f):D(a,b,c),T(d,e,f){}
    void showDateTime(){D.showDate();T.showTime();}
    DateTime& setDateTime(int a,int b,int c,int d,int e,int f){D.y=a;D.m=b;D.d=c;T.h=d;T.m=e;T.s=f;return *this;}
};
///////////////////
int main()
{
    Date date(1000, 10, 10);
    Time time(1, 1, 1);
    DateTime date_time(date, time);
    date_time.showDateTime();
    cout << endl;
    int cases, flag = 0;
    cin >> cases;
    for(int ca = 0; ca < cases; ca++)
    {
        int year, month, day;
        cin >> year >> month >> day;
        int hour, minute, second;
        cin >> hour >> minute >> second;
        if(flag == 0)
        {
            flag = 1;
            DateTime dt(year, month, day, hour, minute, second);
            dt.showDateTime();
        }
        else if(flag == 1)
        {
            flag == 0;
            date_time.setDateTime(year, month, day, hour, minute, second).showDateTime();
        }
        cout << endl;
    }
}

36.时间和日期类(III)

Description

设计一个日期时间类,用于读取输入的数据,按格式输出日期和时间。

设计日期时间类DateTime由2个成员组成,分别是一个Date类对象和一个Time类对象;

设计DateTime类需支持以下操作:

DateTime::DateTime()无参构造方法:初始化为1年1月1日、0时0分0秒;

DateTime::DateTime(int,int,int,int,int,int)构造方法:依照参数(顺序为年月日、时分秒)初始化对象;

在上述两个DateTime类的构造函数中输出:“CREATE DateTime : (y, m, d, hh, mm, ss)”,其中y、m、d为初始化对象时的年月日值,h、m、s为初始化对象时的时分秒值。参见输出。

DateTime::DateTime(const Date&,const Time&)构造方法:依照参数传入的日期和时间初始化对象;

在这个DateTime类的构造函数中输出:“CREATE DateTime : (y, m, d) (hh, mm, ss)”,其中y、m、d为初始化对象时的年月日值,h、m、s为初始化对象时的时分秒值。参见输出。

DateTime::showDateTime()方法:按格式输出DateTime对象;

DateTime::setDateTime(int,int,int,int,int,int)方法:依照参数(顺序为年月日、时分秒)修改对象的属性值;

DateTime类包含了两个类:Date类和Time类

设计日期类Date需支持以下操作:

Date::Date()无参构造方法:初始化为1年1月1日

Date::Date(int,int,int)构造方法:传入的参数依次为年月日,用参数将日期初始化。

在Date类的构造函数中输出:“CREATE Date : (y, m, d)”,其中y、m、d为初始化对象时的年月日值。参见输出。

Date::showDate()方法:按格式输出Date对象。

Date::setDate(int,int,int)方法:传入的参数依次为年月日,用参数修改对象的属性值

设计时间类Time需支持以下操作:

Time::Time()无参构造方法:初始化为0时0分0秒

Time::Time(int,int,int)构造方法:传入的参数依次为时分秒,用参数将时间初始化。

在Time类的构造函数中输出:“CREATE Time : (h, m, s)”,其中h、m、s为初始化对象时的时分秒值。参见输出

Time::showTime()方法:按格式输出Time对象。

Time::setTime(int,int,int)方法:传入的参数依次为时分秒,用参数修改对象的属性值

-—————————————————————————-

你设计DateTime类、Date类和Time类,使得main()函数能够正确运行。

函数调用格式见append.cc。

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

Input

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

后面的输入每行为一组测试数据。每组测试数据的前3个整数是日期的年月日,后3个整数是时间的时分秒。

Output

每组测试数据对应一行输出。日期的输出格式为“yyyy-mm-dd”,时间的输出格式为“hh:mm:ss”,中间用一个空格分开。

Sample Input

31982 10 1 0 0 02000 2 28 23 59 592014 7 2 13 30 01

Sample Output

CREATE Time : (0, 0, 0)CREATE Date : (1, 1, 1)CREATE DateTime : (1, 1, 1, 0, 0, 0)0001-01-01 00:00:001982-10-01 00:00:002000-02-28 23:59:592014-07-02 13:30:01

HINT

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

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

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

Append Code

append.cc,

#include <iostream>  
#include <algorithm>  
#include <iomanip>  
using namespace std;  


class Time{  
   private:  
      friend class DateTime;  
      int hh,mm,ss;  
   public:  
      Time():hh(0),mm(0),ss(0){  
          cout<<"CREATE Time : ("<<hh<<", "<<mm<<", "<<ss<<")"<<endl;  
      }  
      Time(int a,int b,int c):hh(a),mm(b),ss(c){}  
      Time &setTime(int a,int b,int c){hh=a;mm=b;ss=c;return *this;}  
      void showTime(){  
        cout<<setw(2)<<setfill('0')<<hh<<":"<<setw(2)<<setfill('0')<<mm<<":"<<setw(2)<<setfill('0')<<ss;  
      }  
      ~Time(){}  
};  

class Date{  
   private:  
       friend class DateTime;  
       int year,month,day;  
   public:  
       Date():year(1),month(1),day(1){  
           cout<<"CREATE Date : ("<<year<<", "<<month<<", "<<day<<")"<<endl;  
       }  
       Date(int a,int b,int c):year(a),month(b),day(c){}  
       Date &setDate(int a,int b,int c){year=a;month=b;day=c;return *this;}  
       void showDate() {  
           cout<<setfill('0')<<setw(4)<<year<<"-"<<setfill('0')<<setw(2)<<month<<"-"<<setfill('0')<<setw(2)<<day<<" ";  
       }  
       ~Date(){}  
};  



class DateTime{  
   private:  
       friend class Date;  
       friend class Time;  
       Time T;  
       Date D;  
       int year1,month1,day1,hh1,mm1,ss1;  
   public:  
       DateTime(){  
         cout<<"CREATE DateTime : ("<<D.year<<", "<<D.month<<", "<<D.day<<", "<<T.hh<<", "<<T.mm<<", "<<T.ss<<")"<<endl;  
       }  
       DateTime(const Date& d,const Time& t):D(d),T(t){}  
       DateTime(int a,int b,int c,int d,int e,int f):D(a,b,c),T(d,e,f){}  
       void showDateTime(){  
         D.showDate();  T.showTime();  
       }  
       DateTime &setDateTime(int a,int b,int c,int d,int e,int f){  
           D.setDate(a,b,c);T.setTime(d,e,f);return *this;  
       }  
       ~DateTime(){}  
};  

int main()  
{  
    DateTime date_time;  
    date_time.showDateTime();  
    cout << endl;  
    int cases;  
    cin >> cases;  
    for(int ca = 0; ca < cases; ca++)  
    {  
        int year, month, day;  
        cin >> year >> month >> day;  
        int hour, minute, second;  
        cin >> hour >> minute >> second;  
        date_time.setDateTime(year, month, day, hour, minute, second);  
        date_time.showDateTime();  
        cout << endl;  
    }  
}  

37.时间和日期类(IV)

Description

设计一个日期时间类,用于读取输入的数据,按格式输出日期和时间。

设计日期时间类DateTime由2个成员组成,分别是一个Date类对象和一个Time类对象;

设计DateTime类需支持以下操作:

DateTime::DateTime()无参构造方法:初始化为1年1月1日、0时0分0秒;

DateTime::DateTime(int,int,int,int,int,int)构造方法:依照参数(顺序为年月日、时分秒)初始化对象;

在上述两个DateTime类的构造函数中输出:“CREATE DateTime : (y, m, d, hh, mm, ss)”,其中y、m、d为初始化对象时的年月日值,h、m、s为初始化对象时的时分秒值。参见输出。

DateTime::DateTime(const Date&,const Time&)构造方法:依照参数传入的日期和时间初始化对象;

在这个DateTime类的构造函数中输出:“CREATE DateTime : (y, m, d) (hh, mm, ss)”,其中y、m、d为初始化对象时的年月日值,h、m、s为初始化对象时的时分秒值。参见输出。

DateTime:DateTime(const DateTime&)构造方法:拷贝构造函数,初始化对象。

在拷贝构造函数中输出:“COPY DateTime : (y, m, d) (hh, mm, ss)”,其中y、m、d为初始化对象时的年月日值,h、m、s为初始化对象时的时分秒值。参见输出。

DateTime::showDateTime()方法:按格式输出DateTime对象;

DateTime::setDateTime(int,int,int,int,int,int)方法:依照参数(顺序为年月日、时分秒)修改对象的属性值;

编写DateTime类的读写函数:year()、month()、day()、hour()、minute()、second()。读写函数的参数、返回值参见main()函数。

DateTime类包含了两个类:Date类和Time类

设计日期类Date需支持以下操作:

Date::Date()无参构造方法:初始化为1年1月1日

Date::Date(int,int,int)构造方法:传入的参数依次为年月日,用参数将日期初始化。

在Date类的构造函数中输出:“CREATE Date : (y, m, d)”,其中y、m、d为初始化对象时的年月日值。参见输出。

Date::Date(const Date&)构造方法:拷贝构造函数,初始化对象。

在拷贝构造函数中输出:“COPY Date : (y, m, d)”,其中y、m、d为初始化对象时的年月日值。参见输出。

Date::showDate()方法:按格式输出Date对象。

Date::setDate(int,int,int)方法:传入的参数依次为年月日,用参数修改对象的属性值

设计时间类Time需支持以下操作:

Time::Time()无参构造方法:初始化为0时0分0秒

Time::Time(int,int,int)构造方法:传入的参数依次为时分秒,用参数将时间初始化。

在Time类的构造函数中输出:“CREATE Time : (h, m, s)”,其中h、m、s为初始化对象时的时分秒值。参见输出

Time::Time(const Time&)构造方法:拷贝构造函数,初始化对象。

在拷贝构造函数中输出:“COPY Time : (h, m, s)”,其中h、m、s为初始化对象时的时分秒值。参见输出

Time::showTime()方法:按格式输出Time对象。

Time::setTime(int,int,int)方法:传入的参数依次为时分秒,用参数修改对象的属性值

-—————————————————————————-

你设计DateTime类、Date类和Time类,使得main()函数能够正确运行。

函数调用格式见append.cc。

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

Input

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

后面的输入每行为一组测试数据。每组测试数据的前3个整数是日期的年月日,后3个整数是时间的时分秒。

Output

每组测试数据对应一行输出。日期的输出格式为“yyyy-mm-dd”,时间的输出格式为“hh:mm:ss”,中间用一个空格分开。

Sample Input

31982 10 1 0 0 02000 2 28 23 59 592014 7 2 13 30 01

Sample Output

CREATE Date : (1000, 10, 10)COPY Date : (1000, 10, 10)CREATE Time : (1, 1, 1)COPY Time : (1, 1, 1)COPY Time : (1, 1, 1)COPY Date : (1000, 10, 10)CREATE DateTime : (1000, 10, 10) (1, 1, 1)COPY Time : (1, 1, 1)COPY Date : (1000, 10, 10)COPY DateTime : (1000, 10, 10) (1, 1, 1)DateTime : 1000 10 10 1 1 11982-10-01 00:00:002000-02-28 23:59:592014-07-02 13:30:01

HINT

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

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

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

Append Code

append.cc,

#include <iostream>  
#include <algorithm>  
#include <iomanip>  
using namespace std;  


class Time{  
   private:  
      friend class DateTime;  
      int hh,mm,ss;  
   public:  
      Time():hh(0),mm(0),ss(0){  
          cout<<"CREATE Time : ("<<hh<<", "<<mm<<", "<<ss<<")"<<endl;  
      }  
      Time(int a,int b,int c):hh(a),mm(b),ss(c){  
           cout<<"CREATE Time : ("<<hh<<", "<<mm<<", "<<ss<<")"<<endl;  
      }  
      Time(const Time &p):hh(p.hh),mm(p.mm),ss(p.ss){  
          cout<<"COPY   Time : ("<<hh<<", "<<mm<<", "<<ss<<")"<<endl;  
      }  
      Time &setTime(int a,int b,int c){hh=a;mm=b;ss=c;return *this;}  
      void showTime(){  
        cout<<setw(2)<<setfill('0')<<hh<<":"<<setw(2)<<setfill('0')<<mm<<":"<<setw(2)<<setfill('0')<<ss;  
      }  
      ~Time(){}  
};  

class Date{  
   private:  
       friend class DateTime;  
       int year,month,day;  
   public:  
       Date():year(1),month(1),day(1){  
           cout<<"CREATE Date : ("<<year<<", "<<month<<", "<<day<<")"<<endl;  
       }  
       Date(const Date &p):year(p.year),month(p.month),day(p.day){  

          cout<<"COPY   Date : ("<<year<<", "<<month<<", "<<day<<")"<<endl;  
      }  
       Date(int a,int b,int c):year(a),month(b),day(c){  
          cout<<"CREATE Date : ("<<year<<", "<<month<<", "<<day<<")"<<endl;  
       }  
       Date &setDate(int a,int b,int c){year=a;month=b;day=c;return *this;}  
       void showDate() {  
           cout<<setfill('0')<<setw(4)<<year<<"-"<<setfill('0')<<setw(2)<<month<<"-"<<setfill('0')<<setw(2)<<day<<" ";  
       }  
       ~Date(){}  
};  



class DateTime{  
   private:  
       friend class Date;  
       friend class Time;  
       Time T;  
       Date D;  
       int year1,month1,day1,hh1,mm1,ss1;  
   public:  
       DateTime(){  
         cout<<"CREATE DateTime : ("<<D.year<<", "<<D.month<<", "<<D.day<<", "<<T.hh<<", "<<T.mm<<", "<<T.ss<<")"<<endl;  
       }  
       DateTime(const Date& d,const Time& t):D(d),T(t){  
         cout<<"CREATE DateTime : ("<<D.year<<", "<<D.month<<", "<<D.day<<") ("<<T.hh<<", "<<T.mm<<", "<<T.ss<<")"<<endl;  
       }  
       DateTime(const DateTime& p):T(p.T),D(p.D){  
        cout<<"COPY   DateTime : ("<<D.year<<", "<<D.month<<", "<<D.day<<") ("<<T.hh<<", "<<T.mm<<", "<<T.ss<<")"<<endl;  
       }  
       DateTime(int a,int b,int c,int d,int e,int f):D(a,b,c),T(d,e,f){  
       }  
       int const year(int year1){D.year=year1;}  
       int const year() const {return D.year;}  
       int const month(int month1){D.month=month1;}  
       int const month() const{return D.month;}  
       int const day(int day1){D.day=day1;}  
       int const day() const{return D.day;}  
       int const hour(int hh1){T.hh=hh1;}  
       int const hour() const{return T.hh;}  
       int const minute(int mm1){T.mm=mm1;}  
       int const minute() const{return T.mm;}  
       int const second(int ss1){T.ss=ss1;}  
       int const second() const{return T.ss;}  
       void showDateTime(){  
         D.showDate();  T.showTime();  
       }  
       DateTime &setDateTime(int a,int b,int c,int d,int e,int f){  
           D.setDate(a,b,c);T.setTime(d,e,f);return *this;  
       }  
       ~DateTime(){}  
};  

int main()  
{  
    const Date date(1000, 10, 10), dt(date);  
    const Time time(1, 1, 1), tm(time);  
    DateTime date_time(dt, tm);  
    const DateTime cnt(date_time);  
    cout << "DateTime : " << cnt.year() << " " << cnt.month() << " " << cnt.day();  
    cout << " " << cnt.hour() << " " << cnt.minute() << " " << cnt.second();  
    cout << endl;  
    int cases;  
    cin >> cases;  
    for(int ca = 0; ca < cases; ca++)  
    {  
        int year, month, day;  
        cin >> year >> month >> day;  
        int hour, minute, second;  
        cin >> hour >> minute >> second;  
        date_time.year(year);  
        date_time.month(month);  
        date_time.day(day);  
        date_time.hour(hour);  
        date_time.minute(minute);  
        date_time.second(second);  
        date_time.showDateTime();  
        cout << endl;  
    }  
}  

38.STL——灵活的线性表

Description

数组和链表是我们熟知的两种线性结构,但是它们不够灵活(不能同时实现直接插入、删除和访问操作),给你若干种操作,你能通过一种灵活的容器,实现它们的功能吗?

操作1:Build a b (产生一个大小为a的线性表,其值全部赋为b,每组样例仅出现一次,在起始行)

操作2:Modify a b (将线性表的第a个元素的值设为b)

操作3:Insert a b c (在线性表的第a个位置插入第b到第c个位置的所有元素)

操作4:Erase a b(删除线性表第a到第b个位置的所有元素)

操作5:Print a b (输出线性表的第a到第b个元素)

程序在执行操作5的时候要输出结果,格式如“[1]:3 [2]:4 [3]:5”([]内为线性表的位置,“:”后面为元素的值,不带引号,每组输出占一行)

Input

输入有多行,对应5个操作,以EOF结束

Output

见Sample

Sample Input

Build 10 1Modify 2 2Insert 3 1 2Modify 6 4Erase 3 5Print 1 8

Sample Output

[1]:1 [2]:2 [3]:4 [4]:1 [5]:1 [6]:1 [7]:1 [8]:1

HINT

使用vector可以很容易解决

Append Code

#include <iostream>  
#include <vector>  
#include <string>  
using namespace std;  
int main()  
{  
   string l;  
   int a,b,c;  
   vector<int> s;  
   while(cin>>l){  
    if(l=="Build"){cin>>a>>b;s.assign(a,b);}  
    else if(l=="Modify"){cin>>a>>b;s[a-1]=b;}  
    else if(l=="Insert"){cin>>a>>b>>c;s.insert(s.begin()+a-1,s.begin()+b-1,s.begin()+c);}  
    else if(l=="Erase"){cin>>a>>b;s.erase(s.begin()+a-1,s.begin()+b);}  
    else if(l=="Print"){  
        cin>>a>>b;  
        int i;  
        for(i=a-1;i<b;i++)  
        {  
            if(i!=b-1)  
                cout<<"["<<i+1<<"]"<<":"<<s[i]<<" ";  
            else  
                cout<<"["<<i+1<<"]"<<":"<<s[i]<<endl;  
        }  

    }  
   }  
   return 0;  
}  

STL中vector容器用法

http://www.cnblogs.com/ziyi--caolu/archive/2013/07/04/3170928.html

http://www.cnblogs.com/zhonghuasong/p/5975979.html

http://blog.csdn.net/xlm289348/article/details/8166820

http://blog.csdn.net/liuweiyuxiang/article/details/52735561

3. vector基本操作

(1). 容量

  • 向量大小: vec.size();
  • 向量最大容量: vec.max_size();
  • 更改向量大小: vec.resize();
  • 向量真实大小: vec.capacity();
  • 向量判空: vec.empty();
  • 减少向量大小到满足元素所占存储空间的大小: vec.shrink_to_fit(); //shrink_to_fit

(2). 修改

  • 多个元素赋值: vec.assign(); //类似于初始化时用数组进行赋值
  • 末尾添加元素: vec.push_back();
  • 末尾删除元素: vec.pop_back();
  • 任意位置插入元素: vec.insert();
  • 任意位置删除元素: vec.erase();
  • 交换两个向量的元素: vec.swap();
  • 清空向量元素: vec.clear();

(3)迭代器

  • 开始指针:vec.begin();
  • 末尾指针:vec.end(); //指向最后一个元素的下一个位置
  • 指向常量的开始指针: vec.cbegin(); //意思就是不能通过这个指针来修改所指的内容,但还是可以通过其他方式修改的,而且指针也是可以移动的。
  • 指向常量的末尾指针: vec.cend();

(4)元素的访问

  • 下标访问: vec[1]; //并不会检查是否越界
  • at方法访问: vec.at(1); //以上两者的区别就是at会检查是否越界,是则抛出out of range异常
  • 访问第一个元素: vec.front();
  • 访问最后一个元素: vec.back();
  • 返回一个指针: int* p = vec.data(); //可行的原因在于vector在内存中就是一个连续存储的数组,所以可以返回一个指针指向这个数组。这是是C++11的特性。

39.立体空间中的点(I)

Description

设计一个平面上的点Point类和3维的点Point_3D类,满足Point_3D类继承自Point类,用于读取输入的数据,输出所构造的两种点的坐标。

设计Point类需支持一下操作:

Point::Point()无参构造。

Point::Point(double,double)两个坐标参数构造。

Point::showPoint()按格式输出Point对象

设计Point_3D类需支持一下操作:

Point_3D::Point_3D()无参构造。

Point_3D::Point_3D(double,double,double)三个坐标参数构造。

Point_3D::showPoint()按格式输出Point_3D对象。

-—————————————————————————-

你设计Point类和Point_3D类,使得main()函数能够正确运行。

函数调用格式见append.cc。

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

Input

输入的第一个整数n,表示有n组测试数据,后面的输入每行为一组测试数据。每组测试数据的第一行是一个整数m,m有两种取值:2、3;m为2时,后面有两个浮点数x、y,表示一个平面上的点的坐标(x,y);m为3时后面有3个浮点数x、y、z,表示一个3维的点的坐标(x,y,z)。

Output

每组测试数据对应一行输出。

若输入为平面上的点,则输出:“2D Point (x,y)”,x和y为输入的坐标值。

若输入为3维的点,则输出:“3D Point (x,y,y)”,x、y和z为输入的坐标值。

Sample Input

53 1 2 33 0 0 02 -1 13 -1 -1 -12 0 0

Sample Output

3D Point (1,2,3)3D Point (0,0,0)2D Point (-1,1)3D Point (-1,-1,-1)2D Point (0,0)

HINT

Append Code

append.cc,

#include <iostream>
#include <iomanip>
using namespace std;
class Point{
public:
    double a,b;
    Point(){}
    Point(double aa,double bb):a(aa),b(bb){}
    void showPoint(){cout << "2D Point (" << a << "," << b << ")" << endl;}
};
class Point_3D:public Point{
public:
    double c;
    Point_3D(){}
    Point_3D(double aa,double bb,double cc):Point(aa,bb),c(cc){}
    void showPoint(){cout << "2D Point (" << a << "," << b << "," << c << ")" << endl;}
};
////////////////////
int main()
{
    int cases;
    cin>>cases;
    for(int i = 1; i <= cases; i++)
    {
        double x, y, z;
        int point_type;
        cin>>point_type;
        if(point_type == 2)
        {
            cin>>x>>y;
            Point p(x, y);
            p.showPoint();
        }
        if(point_type == 3)
        {
            cin>>x>>y>>z;
            Point_3D p(x, y, z);
            p.showPoint();
        }
    }
}

C++继承的概念及语法

http://www.weixueyuan.net/view/6358.html

40.立体空间中的点(II)

Description

设计一个平面上的点Point类和3维的点Point_3D类,满足Point_3D类继承自Point类,用于读取输入的数据,输出所构造的两种点的坐标。并统计输入的两种点的个数。

设计Point类需支持一下操作:

Point::Point()无参构造。

Point::Point(double,double)两个坐标参数构造。

Point::x()返回x坐标

Point::y()返回y坐标

Point::x(int)修改x坐标并返回

Point::y(int)修改y坐标并返回

Point::showPoint()按格式输出Point对象

Point::showNumber()返回Point对象总数的静态函数

设计Point_3D类需支持一下操作:

Point_3D::Point_3D()无参构造。

Point_3D::Point_3D(double,double,double)三个坐标参数构造。

Point_3D::z()返回z坐标。

Point_3D::z(int)修改z坐标并返回。

Point_3D::showPoint()按格式输出Point_3D对象。

Point_3D::setPoint(double,double,double)根据三个坐标参数修改Point_3D对象的坐标。

Point_3D::showNumber()返回Point_3D对象总数的静态函数。

-—————————————————————————-

你设计Point类和Point_3D类,使得main()函数能够正确运行。

函数调用格式见append.cc。

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

Input

输入的第一个整数n,表示有n组测试数据,后面的输入每行为一组测试数据。每组测试数据的第一行是一个整数m,m有两种取值:2、3;m为2时,后面有两个浮点数x、y,表示一个平面上的点的坐标(x,y);m为3时后面有3个浮点数x、y、z,表示一个3维的点的坐标(x,y,z)。

Output

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

每组测试数据对应一行输出。

若输入为平面上的点,则输出:“2D Point (x,y)”,x和y为输入的坐标值。

若输入为3维的点,则输出:“3D Point (x,y,y)”,x、y和z为输入的坐标值。

最后,分别输出总共输入的平面上的点数和3维的点数。

Sample Input

53 1 2 33 0 0 02 -1 13 -1 -1 -12 0 0

Sample Output

Invariable test output :3D Point (-100,0,100)Point (0,100,100)Test data output :3D Point (1,2,3)3D Point (0,0,0)2D Point (-1,1)3D Point (-1,-1,-1)2D Point (0,0)Number of 2D Points : 2Number of 3D Points : 3

HINT

Append Code

append.cc,

#include <iostream>
#include <iomanip>
using namespace std;
class Point{
public:
    static int num;
    double a,b;
    Point(){}
    Point(double aa,double bb):a(aa),b(bb){num++;}
    void showPoint(){cout << "2D Point (" << a << "," << b << ")" << endl;}
    double x(){return a;}
    double y(){return b;}
    double x(double xx){a=xx;return a;}
    double y(double yy){b=yy;return b;}
    static int showNumber(){return num+1;}
};
class Point_3D:public Point{
public:
    static int numm;
    double c;
    Point_3D(){}
    Point_3D(double aa,double bb,double cc):Point(aa,bb),c(cc){numm++;}
    double z(){return c;}
    double z(double zz){c=zz;return c;}
    static int showNumber(){return numm+1;}
    void setPoint(double aa,double bb,double cc){a=aa;b=bb;c=cc;}
    void showPoint(){cout << "2D Point (" << a << "," << b << "," << c << ")" << endl;}
};
int Point::num = 0;
int Point_3D::numm = 0;
////////////////////
int main()
{
    cout<<"Invariable test output :"<<endl;
    Point_3D p3d;
    p3d.setPoint(-100, 0, 100);
    p3d.showPoint();
    p3d.x(0);
    p3d.y(100);
    cout<<"Point ("<<p3d.x()<<","<<p3d.y()<<","<<p3d.z()<<")"<<endl;
    cout<<"\nTest data output :"<<endl;
    int cases;
    cin>>cases;
    for(int i = 1; i <= cases; i++)
    {
        double x, y, z;
        int point_type;
        cin>>point_type;
        if(point_type == 2)
        {
            cin>>x>>y;
            Point p(x, y);
            p.showPoint();
        }
        if(point_type == 3)
        {
            cin>>x>>y>>z;
            Point_3D p(x, y, z);
            p.showPoint();
        }
    }
    cout<<"Number of 2D Points : "<<Point::showNumber() - Point_3D::showNumber()<<endl;
    cout<<"Number of 3D Points : "<<Point_3D::showNumber() - 1<<endl;
}

41.正方形、长方形、立方体

Description

给出正方形(Square)、长方形(Rectangle)、立方体(Cuboid)的边长,求周长、面积、体积。

Square类只需存一条边长,构造函数产生一条输出,有边长、周长、面积的函数。

Rectangle类需存长和宽,若从Square类派生而来,因此只需增加一条边,构造函数产生一条输出,有长、宽、周长、面积的函数。

Cuboid类需存长宽高,若从Rectangle类派生而来,因此也只增加一条边,构造函数产生一条输出,有长、宽、高、周长、面积、体积的函数。它的周长定义为所有棱长之和。

-—————————————————————————-

请仔细阅读append.cc代码,并设计好正方形、长方形、立方体派生关系,使main()函数能够运行并得到正确的输出。

Input

输入分为三部分,每一部分都已一个整数n开始,表示后面有n组测试数据。

在第一部分测试数据中,每组是一个整数,表示正方形的边长。

在第二部分测试数据中,每组是两个整数,表示长方形的长和宽。

在第三部分测试数据中,每组是三个整数, 表示立方体的长和宽。

Output

每组测试数据对应的输出为两部分,前面是构造函数的输出,最后是输出图形的信息,包括长宽高、周长、面积、体积等信息,格式见sample;

Sample Input

1413 413 4 6

Sample Output

Construct Square (4)A Square length 4, Perimeter 16, Area 16=========================Construct Square (3)Construct Rectangle (3, 4)A Rectangle length 3, width 4, Perimeter 14, Area 12=========================Construct Square (3)Construct Rectangle (3, 4)Construct Cuboid (3, 4, 6)A Cuboid length 3, width 4, height 6, Perimeter 52, Area 108, Volume 72

HINT

Append Code

append.c, append.cc,

#include <iostream>
using namespace std;
class Square{
public:
    int a;
    Square(){}
    Square(int aa):a(aa){cout << "Construct Square (" << a << ")" << endl;}
    int length(){return a;}
    int perimeter(){return 4*a;}
    int area(){return a*a;}
};
class Rectangle:public Square{
public:
    int b;
    Rectangle(){}
    Rectangle(int aa,int bb):Square(aa),b(bb){cout << "Construct Rectangle (" << a << "," << b << ")" << endl;}
    int length(){return a;}
    int width(){return b;}
    int perimeter(){return 2*(a+b);}
    int area(){return a*b;}
};
class Cuboid:public Rectangle{
public:
    int c;
    Cuboid(){}
    Cuboid(int aa,int bb,int cc):Rectangle(aa,bb),c(cc){cout << "Construct Cuboid (" << a << "," << b << "," << c << ")" << endl;}
    int length(){return a;}
    int width(){return b;}
    int height(){return c;}
    int perimeter(){return 4*(a+b+c);}
    int area(){return 2*(a*b+a*c+b*c);}
    int volume(){return a*b*c;}
};
////////////////////////
int main()
{
    int cases, l, w, h;
    cin >> cases;
    for(int i = 1; i <= cases; ++i)
    {
        cin >> l;
        Square squa(l);
        cout << "A Square length " << squa.length() << ", ";
        cout << "Perimeter " << squa.perimeter() << ", ";
        cout << "Area " << squa.area() << endl;
    }

    cout << "=========================" << endl;

    cin >> cases;
    for(int i = 1; i <= cases; ++i)
    {
        cin >> l >> w;
        Rectangle rect(l, w);
        cout << "A Rectangle length " << rect.length() << ", width " << rect.width() << ", ";
        cout << "Perimeter " << rect.perimeter() << ", ";
        cout << "Area " << rect.area() << endl;
    }

    cout << "=========================" << endl;

    cin >> cases;
    for(int i = 1; i <= cases; ++i)
    {
        cin >> l >> w >> h;
        Cuboid cubo(l, w, h);
        cout << "A Cuboid length " << cubo.length() << ", width " << cubo.width() << ", height " << cubo.height() << ", ";
        cout << "Perimeter " << cubo.perimeter() << ", ";
        cout << "Area " << cubo.area() << ", ";
        cout << "Volume " << cubo.volume() << endl;
    }

}

42.字符串类(I)*

Description

封装一个字符串类,用于存储字符串和处理的相关功能,支持以下操作:

\1. STR::STR()构造方法:创建一个空的字符串对象。

\2. STR::STR(const char *)构造方法:创建一个字符串对象,串的内容由参数给出。

\3. STR::length()方法:返回字符串的长度。

\4. STR::putline()方法:输出串的内容,并换行。

-—————————————————————————-

你设计一个字符串类STR,使得main()函数能够正确运行。

函数调用格式见append.cc。

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

-—————————————————————————-

Invalid Word(禁用单词)错误:“string”、“vector”等被禁用。

Input

输入有若干行,每行一个字符串。

Output

每组测试数据对应输出一行,包含两部分内容,首先是一个整数,表示输入串的长度,然后是输入的字符串,两者用一个空格分开。格式见sample。

Sample Input

A123456789

Sample Output

0 12 Hello World!1 A9 123456789

HINT

Append Code

append.cc,


#include <iostream>
#include <stdio.h>
using namespace std;
int len(char * s)
{
    int i = 0; int len_ = 0;
    while(s[i] != '\0' ) { len_++; i++; }
    return len_;
}
void strcpy_(char *s, char *t)
{
    int i  = 0;
    while(t[i] != '\0') { s[i] = t[i]; i++; }
    s[i] = '\0';
}
class STR
{
public:
    int length() const { return len(stl); }
    void putline() const {  cout << stl << endl; }
public:
    STR(char *s = NULL )
    {
        if(s == NULL)
        {
            stl = new char[1];
            stl[0] = '\0';
        }
        else
        {
            stl = new char[len(s) + 1];
            strcpy_(stl,s);
        }
    }
private:
    char *stl;
};
int main()
{
    STR e;
    STR h("Hello World!");
    char s[100001];
    cout << e.length() << " ";
    e.putline();
    cout << h.length() << " ";
    h.putline();
    while(gets(s) != NULL)
    {
        STR str(s);
        cout << str.length() << " ";
        str.putline();
    }
}

43.字符串类(II)

Description

封装一个字符串类,用于存储字符串和处理的相关功能,支持以下操作:

\1. STR::STR()构造方法:创建一个空的字符串对象。

\2. STR::STR(const char *)构造方法:创建一个字符串对象,串的内容由参数给出。

\3. STR::length()方法:返回字符串的长度。

\4. STR::putline()方法:输出串的内容,并换行。

\5. 运算符“+”和“+=”,表示两个字符串的连接运算,规则为:

c = a + b 表示串c中的字符是a和b的连接:“a+b”的结果是一个新的字符串,串a和串b的内容不变。

a += b 表示串a中的字符是a和b的连接:串b中的内容不变

-—————————————————————————-

你设计一个字符串类STR,使得main()函数能够正确运行。

函数调用格式见append.cc。

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

-—————————————————————————-

Invalid Word(禁用单词)错误:“string”、“vector”等被禁用。

Input

输入有若干行,每行一个字符串。

Output

每组测试数据对应输出一行,包含两部分内容,首先是一个整数,表示输入串的长度,然后是输入的字符串,两者用一个空格分开。格式见sample。

Sample Input

A123456789

Sample Output

12 Hello World!0 12 Hello World!12 Hello World!12 Hello World!10 A1234567891 A9 12345678910 123456789A1 A

HINT

Append Code

append.cc,

44.数组类(I)

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

42 10 201 003 1 2 3

Sample Output

10 2001 2 3

HINT

Append Code

append.cc,

#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Array{
public:
    vector<int> a;
    int l;
    Array():l(0){}
    int size(){return l;}
    void get(int n){
        l=n;a.resize(n);
        for(int i=0;i<n;i++){
            int x;
            cin >> x;
            a[i]=x;
        }
    }
    int operator[](int x){return a[x];}
};
//////////////////////////////////////////
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;
    }
}

45.数组类(II)

Description

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

\1. Array::Array(int l)构造方法:创建一个长度为l的组对象。

\2. Array::size()方法:返回Array对象中元素个数。

\3. Array::put(int n)方法:按从大到小的顺序输出前n大元素,若数组长度小于n则从大到小输出全部元素。

\4. 下标运算符:返回下标所指的元素。

-—————————————————————————-

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

函数调用格式见append.cc。

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

Input

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

后面的每行以一个整数k开头,表示后面有k个同类型的数组元素。

数组元素有以下三种类型:整数、浮点数和字符,并且按固定的次序间隔出现。

Output

把输入的数组,按值从大到小输出前10个元素,若输入不足10个则全部输出。每行数据对应一个输出。格式见sample。

Sample Input

310 1 2 3 4 5 6 7 8 9 05 1.1 2.2 3.3 4.4 5.520 ABCDEGHIJMNPRSTUVWXY

Sample Output

9 8 7 6 5 4 3 2 1 05.5 4.4 3.3 2.2 1.1Y X W V U T S R P N

HINT

Append Code

append.cc,

#include<iostream>  
using namespace std;  
template<class T>  
class Array  
{  
private:  
    T *sss;  
    int si;  
public:  
    Array(int l):si(l){sss=new T[si];}  
    ~Array(){if (sss!=NULL)delete []sss;}  
    int size(){return si;}  
    void put(int n)  
    {  
        int i,j;  
        for(i=0;i<si-1;i++)//挨着和每个比,最后一个不用了  
        {  
            for(j=i+1;j<si;j++)//从下一个开始,到最后  
            {  
                if(sss[i]<sss[j])  
                {  
                    T t=sss[i];  
                    sss[i]=sss[j];  
                    sss[j]=t;  
                }  
            }  
        }  
        if(n>si)  
        n=si;  
            cout<<sss[0];  
        for(int i=1;i<n;i++)  
            cout<<" "<<sss[i];  
        cout<<endl;  
    }  
    T& operator[](int n)//少个引用,我去  
    {  
        return sss[n];  
    }  
}; 
//////////////////////////////////////////
int main()
{
    int cases, len;
    cin >> cases;
    for(int ca = 1; ca <= cases; ca++)
    {
        cin >> len;
        if(ca % 3 == 0)
        {
            Array<char> chr_arr(len);
            for(int i = 0; i < chr_arr.size(); i++)
                cin >> chr_arr[i];
            chr_arr.put(10);
        }
        if(ca % 3 == 1)
        {
            Array<int> int_arr(len);
            for(int i = 0; i < int_arr.size(); i++)
                cin >> int_arr[i];
            int_arr.put(10);
        }
        if(ca % 3 == 2)
        {
            Array<double> dbl_arr(len);
            for(int i = 0; i < dbl_arr.size(); i++)
                cin >> dbl_arr[i];
            dbl_arr.put(10);
        }
    }
}

46.Problem E: 农夫果园

Description

秋天到了,果园里的水果成熟了,商贩们来收水果了,农夫们都希望自己的水果能卖个好价钱。

现在果园里有三种水果正在销售,苹果(Apple)、香蕉(Banana)、梨(Pear)。每次销售都会记录下水果的种类、单价和总量,input()函数可以读取每条销售记录的单价和总量,total()函数可以计算出这次销售的总价。

但是,销售员在记录时忙中出错,各中水果的单价和总量的单位没有统一。单价是每公斤的价格,而水果是按箱记录的。其中,苹果一箱按30公斤计算,香蕉一箱按25公斤计算,梨一箱按20公斤计算。每种水果每次销售的总价是“单价总量每箱公斤数”。

现在,你来设计一个程序帮忙计算果园卖出水果的总价。由于total()函数对每种水果的计算方式都不一样,因此使用多态来实现。

-—————————————————————————-

你设计并实现这个水果类的派生体系,使得main()函数能够运行并得到正确的输出。调用格式见append.cc

Input

输入的第一个整数n,表示后面有n条水果收购的记录。每条记录分为3部分,水果种类、单价和总量。

Output

输出为一行,表示整个果园卖出水果的总价。

Sample Input

5Apple 4.2 100Banana 8.8 50Apple 4.5 200Banana 7.8 100Pear 3.7 100

Sample Output

Total Price : 77500

HINT

Append Code

append.c, append.cc,

#include<iostream>
using namespace std;
class Fruit{
public:
    string name;
    double per;
    int n;
    Fruit(){}
    Fruit(string n):name(n){}
    void input(){cin>>per>>n;}
    virtual double total()=0;
};
class Apple:public Fruit{
public:
    string name;
    ///double per;
    ///int n;
    Apple():name("Apple"){}
    double total(){return per*n*30;}
};
class Banana:public Fruit{
public:
    string name;
    ///double per;
    ///int n;
    Banana():name("Banana"){}
    double total(){return per*n*25;}
};
class Pear:public Fruit{
public:
    string name;
    ///double per;
    ///int n;
    Pear():name("Pear"){}
    double total(){return per*n*20;}
};
int main()
{
    Fruit* fruit;
    string fruit_name;
    double sum = 0.0;
    int cases;
    cin >> cases;
    for(int i = 1; i <= cases; i++)
    {
        cin >> fruit_name;
        if(fruit_name == "Apple")
            fruit = new Apple();
        if(fruit_name == "Banana")
            fruit = new Banana();
        if(fruit_name == "Pear")
            fruit = new Pear();
        fruit->input();
        sum += fruit->total();
        delete fruit;
    }
    cout << "Total Price : " << sum << endl;

     return 0;
}

47.一元二次方程类

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](./Problem B_ 一元二次方程类_files/Source Code.html),


#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
class Equation
{
private :
    double a,b,c,x1,x2;
   public:
     Equation(double x,double y,double z)
     {
         a = x;b = y; c = z;
     }
     void solve ()
     {

         x1 = (-b+sqrt(b*b-4*a*c))/(2*a);
         x2 = (-b-sqrt(b*b-4*a*c))/(2*a);
     }
    void printRoot()
    {
       //cout.precision(3);
       cout<<setiosflags(ios::fixed)<<setprecision(2)<<x1<<" "<<x2<<endl;
    }
};
setiosflags(ios::fixed)是用定点方式表示实数。 
使用setprecision(n)可控制输出流显示浮点数的数字个数。C++默认的流输出数值有效位是6。
如果setprecision(n)与setiosflags(ios::fixed)合用,可以控制小数点右边的数字个数。

48.字符类的封装

Description

先来个简单习题,练练手吧!现在需要你来编写一个Character类,将char这一基本数据类型进行封装。该类中需要有如下成员函数:

\1. 无参构造函数。

\2. 构造函数Character(char):用参数初始化数据成员。

\3. void setCharacter(char):重新设置字符值。

\4. int getAsciiCode():返回字符的ASII码。

\5. char getCharacter():返回字符值。

\6. 析构函数。

Input

输入只有1行,包含一个合法的、可打印的字符。

Output

输出有好多行,请参考样例来编写相应的函数。

Sample Input

c

Sample Output

Default constructor is called!Character a is created!ch1 is c and its ASCII code is 99.ch2 is a and its ASCII code is 97.Character a is erased!Character c is erased!

HINT

Append Code

[append.cc](./Problem D_ 字符类的封装_files/Source Code.html),

#include <bits/stdc++.h>
using namespace std;
class Character{
public:
    char c;
    Character(){cout << "Default constructor is called!" << endl;}
    Character(char cc):c(cc){cout << "Character " << c << " is created!" << endl;}
    void setCharacter(char cc){c=cc;}
    int getAsciiCode(){return (int)c;}
    char getCharacter(){return c;}
    ~Character(){cout << "Character " << c << " is erased!" << endl;}
};
///////////////////
int main()
{
    char ch;
    Character ch1, ch2('a');
    cin>>ch;
    ch1.setCharacter(ch);
    cout<<"ch1 is "<<ch1.getCharacter()<<" and its ASCII code is "<<ch1.getAsciiCode()<<"."<<endl;
    cout<<"ch2 is "<<ch2.getCharacter()<<" and its ASCII code is "<<ch2.getAsciiCode()<<"."<<endl;
    return 0;
}

33/42


文章作者: LANVNAL
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 LANVNAL !
  目录