第十三周:运算符重载 + 虚函数

1.三维坐标点(运算符重载)

题目:

设计一个三维点坐标的类Point3D,要求如下:

(1)包含三个int类型数据成员my_x, my_y, my_z;

(2)默认构造函数,默认三维点的坐标值为0, 0, 0;

(3)SetPoint函数,设置坐标值;

(4)用成员函数重载”+”运算符,实现两个Point3D类对象的加法运算;

(5)重载“+”运算符,实现整型常量和PointedD类对象的加法运算。规则是:常量值与x坐标相加;

(6)在main( ) 函数中测试类Point3D“+”和"3+pt1”的功能,并输出结果。

(7)重载“<<”运算符,实现点类对象的输出,坐标值xyz之间以逗号分隔。

ostream & operator<<(ostream &out, const Point3D &pt)

{

out<<pt.my_x<<","<<pt.my_y<<","<<pt.my_z<<endl;

扫描二维码关注公众号,回复: 13291711 查看本文章

return out;

}

主函数已经给出,请勿修改。

int main()

{

Point3D pt1, pt2, pt3, pt4;

pt1.SetPoint(10, 20, 3);

pt2.SetPoint(40, 50, 60);

pt3 = pt1+pt2;

cout<<“pt1:”<<pt1;

cout<<“pt2:”<<pt2;

cout<<“pt1+pt2:”<<pt3;

pt4 = 3+pt1;

cout<<“3+pt1:”<<pt4;

return 0;

}

【输入形式】

两个三维点的坐标值

【输出形式】

第一行为点pt1的坐标值;第二行为点pt2的坐标值;第三行为pt1+pt2的结果;第四行为m+pt1的结果。各坐标值之间以“,”分隔。

【样例输入】

【样例输出】

pt1:10,20,3

pt2:40,50,60

pt1+pt2:50,70,63

3+pt1:13,20,3

代码:

#include <iostream>
using namespace std;

class Point3D{
    
    
public:
    Point3D(int x = 0,int y = 0,int z = 0):my_x(x), my_y(y), my_z(z)
    {
    
    }
    void SetPoint(int x, int y, int z){
    
    
        my_x = x;
        my_y = y;
        my_z = z;
    }
    Point3D operator+(const Point3D &p)const{
    
    
        return Point3D(my_x + p.my_x, my_y + p.my_y, my_z + p.my_z);
    }
    friend Point3D operator+(Point3D p, int x);
    friend Point3D operator+(int x, Point3D p);
    friend ostream & operator<<(ostream &out, const Point3D &pt);
private:
    int my_x, my_y, my_z;
};

ostream & operator<<(ostream &out, const Point3D &pt){
    
    
     out<<pt.my_x<<","<<pt.my_y<<","<<pt.my_z<<endl;
     return out;
}

Point3D operator+(Point3D p, int x){
    
    
    p.my_x += x;
    return p;
}

Point3D operator+(int x, Point3D p){
    
    
    p.my_x += x;
    return p;
}

int main(){
    
    
    Point3D pt1, pt2, pt3, pt4;
    pt1.SetPoint(10, 20, 3);
    pt2.SetPoint(40, 50, 60);
    pt3 = pt1 + pt2;
    cout<<"pt1:"<<pt1;
    cout<<"pt2:"<<pt2;
    cout<<"pt1+pt2:"<<pt3;
    pt4 = 3 + pt1;
    cout<<"3+pt1:"<<pt4;
    return 0;
}

2.学生类的输入输出(运算符的重载)

题目:

定义一个学生类,包括私有数据成员学号、姓名、性别、成绩和其他成员函数。请编程实现以下主函数的功能。

int main()

{

student s;

cin>>s;

cout<<s;

return 0;

}

【样例输入】

201901001 Zhang f 80
【样例输出】

201901001 Zhang f 80

代码:

#include <iostream>
#include <cstring>
using namespace std;

class Student{
    
    
    char id[20];
    char name[10];
    char sex;
    int score;
public:
    Student(){
    
    
        strcpy(id, "000");
        strcpy(name, "000");
        sex = 'm';
        score = 0;
    }
    friend istream &operator>>(istream&in, Student &s);
    friend ostream &operator<<(ostream&out, const Student &s);
};

istream &operator>>(istream&in, Student &s){
    
    
    in>>s.id>>s.name>>s.sex>>s.score;
    return in;
}

ostream &operator<<(ostream&out, const Student &s){
    
    
    out<<s.id<<" "<<s.name<<" "<<s.sex<<" "<<s.score;
    return out;
}

int main()
{
    
    
    Student s;
    cin>>s;
    cout<<s;
    return 0;
}

3.二进制类(运算符的重载)

题目:

将一个16位二进制数表示成0和1的字符序列,即用一个字符数组来存放这个二进制数。在这个类中设置两个构造函数,一个是传递整数参数的,另一个是传递字符串参数的。因为用户在创建对象时传递的二进制数,可能是以整数形式给出,也可能是以数字串形式给出,系统应该都能接受。另外有一个类型转换函数int(),用来将类类型向整型转换,即将二进制形式的类对象转换为整形数。两个重载运算符“+”,“-”,用来完成两个二进制数之间的加减运算。

class binary { //定义二进制类

char bits[16]; //二进制字模数组

public:

binary(char *); //字符串参数构造函数

binary(int); //整型参数构造函数

friend binary operator +(binary,binary); //重载“+”,友元函数

friend binary operator -(binary,binary); //重载“-”,友元函数

operator int(); //类类型转换函数,成员函数

friend ostream & operator <<(ostream &out, binary &b);//重载“<<”,以二进制形式输出

void print();//以整型形式输出

};

主函数设计如下,请勿修改:

int main(){

binary n1=“1011”;

binary n2=int(n1)+15;

binary n3=n1-binary(7);

cout<<n1<<endl;

cout<<n2<<endl;

cout<<n3<<endl;

cout<<int(n2)+5<<endl;

n2=n2-binary(5);

n2.print();

n3=n3+binary(5);

n3.print();

cout<<int(n3)-5<<endl;

return 0;

}

【样例输出】

0000000000001011

0000000000011010

0000000000000100

31

21

9

4

代码:

#include <iostream>
#include <cstring>
using namespace std;

int fun(int n){
    
    
    int sum = 1;
    for(int i = 0; i < n; i ++){
    
    
        sum *= 2;
    }
    return sum;
}

class binary{
    
    
   char bits[16];  //二进制字模数组
public:
   binary(char *c);   //字符串参数构造函数
   binary(int n);      //整型参数构造函数
   friend binary operator + (binary &b1, binary &b2);   //重载“+”,友元函数
   friend binary operator - (binary &b1, binary &b2);   //重载“-”,友元函数
   operator int();  //类类型转换函数,成员函数
   friend ostream & operator <<(ostream &out, binary &b);//重载“<<”,以二进制形式输出
   void print();//以整型形式输出
};

binary::operator int(){
    
    
    int num = 0;
    for(int i = 15; i >= 0; i --){
    
    
        num += (bits[i] - '0') * fun(15 - i); //两个字符相减,得到的就是ascll差值;
    }
    return num;
}

binary::binary( char *c){
    
    
    for(int i = 0; i < 16; i ++)bits[i] = '0';
    int m = strlen(c);
    int i = 15;
        for(int j = m - 1; j >= 0; j --){
    
    
            bits[i] = c[j];
            i --;
        }
}

binary::binary(int n){
    
    
    for(int i = 15; i >= 0; i --){
    
    
        bits[i] = n % 2 + '0';
        n /= 2;
    }
}

binary operator +(binary &b1, binary &b2){
    
    
    int m = int(b1) + int(b2);
    return binary(m);
}

binary operator -(binary &b1, binary &b2){
    
    
    int m = int(b1) - int(b2);
    return binary(m);
}

ostream & operator <<(ostream &out, binary &b){
    
    
    for(int i = 0; i < 16; i ++){
    
    
    out<<b.bits[i];
    }
    out<<endl;
    return out;
}

void binary::print(){
    
    
   cout<<int(*this)<<endl;
}

int main(){
    
    
   binary n1 = "1011";
   binary n2 = int(n1)+15;
   binary n3 = n1 - binary(7);
   cout<<n1<<endl;
   cout<<n2<<endl;
   cout<<n3<<endl;
   cout<<int(n2) + 5<<endl;
   n2 = n2 - binary(5);
   n2.print();
   n3 = n3 + binary(5);
   n3.print();
   cout<<int(n3) - 5<<endl;
   return 0;
}

4.二进制类的完善

题目:

在上一题的基础上,将 + 、 - 运算符定义为binary类的成员函数。并且重载运算符 ~ 、 & 、 | ,分别表示将二进制数按位取反、按位与及按位或。主函数设计如下,请勿修改:

int main(){

binary n1=“1011”;

binary n2=int(n1)+15;

binary n3=n1-binary(7);

cout<<n1<<endl;

cout<<n2<<endl;

cout<<n3<<endl;

binary n4=n1&n2;

binary n5=n1|n2;

binary n6=~n1;

cout<<n4<<endl;

cout<<n5<<endl;

cout<<n6<<endl;

return 0;

}

【样例输出】

0000000000001011

0000000000011010

0000000000000100

0000000000001010

0000000000011011

1111111111110100

代码:

#include <iostream>
#include <cstring>
using namespace std;

int fun(int n){
    
    
    int sum = 1;
    for(int i = 0; i < n; i ++){
    
    
        sum *= 2;
    }
    return sum;
}

class binary {
    
    
   char bits[16];
public:
    binary(){
    
    for(int i = 0; i < 16; i ++)bits[i] = '0';}
    binary(char *c);
    binary(int n);
    binary operator +(binary &b1);
    binary operator -(binary &b1);
    binary operator &(binary &b1);
    binary operator |(binary &b1);
    binary operator ~();
    operator int();
    friend ostream & operator <<(ostream &out, binary &b);
};

binary::operator int(){
    
    
    int num = 0;
    for(int i = 15; i >= 0; i --){
    
    
        num += (bits[i] - '0') * fun(15 - i);
    }
    return num;
}

binary::binary( char *c){
    
    
    for(int i = 0; i < 16; i ++)bits[i] = '0';
    int m = strlen(c);
    int i = 15;
    for(int j = m - 1; j >= 0; j --){
    
    
        bits[i] = c[j];
        i --;
    }
}

binary::binary(int n){
    
    
    for(int i = 15; i >= 0; i --){
    
    
        bits[i] = n % 2 + '0';
        n /= 2;
    }
}

binary binary::operator +(binary &b1){
    
    
    int m = int(b1) + int(*this);
    return binary(m);
}

binary binary::operator -(binary &b1){
    
    
    int m = int(*this) - int(b1);
    return binary(m);
}

binary binary::operator &(binary &b1){
    
    
    binary a;
    for(int i = 0, j = 0; i < 16, j < 16; i ++, j ++){
    
    
        if(b1.bits[i] == '1' && bits[j] == '1')a.bits[i] = '1';
        else a.bits[i] = '0';
    }
    return a;
}

binary binary::operator |(binary &b1){
    
    
    binary a;
    for(int i = 0, j = 0; i < 16, j < 16; i ++, j ++){
    
    
        if(b1.bits[i] == '0' && bits[j] == '0')a.bits[i] = '0';
        else a.bits[i] = '1';
    }
    return a;
}

binary binary::operator ~(){
    
    
    binary a;
    for(int i = 0; i < 16; i ++){
    
    
        if(bits[i] == '0')a.bits[i] = '1';
        if(bits[i] == '1')a.bits[i] = '0';
    }
    return a;
}

ostream & operator <<(ostream &out, binary &b){
    
    
    for(int i = 0; i < 16; i ++){
    
    
		out<<b.bits[i];
    }
    out<<endl;
    return out;
}

int main(){
    
    
   binary n1 = "1011";
   binary n2 = int(n1) + 15;
   binary n3 = n1 - binary(7);
   cout<<n1<<endl;
   cout<<n2<<endl;
   cout<<n3<<endl;
   binary n4 = n1 & n2;
   binary n5 = n1 | n2;
   binary n6 = ~n1;
   cout<<n4<<endl;
   cout<<n5<<endl;
   cout<<n6<<endl;
   return 0;
}

5.图形类(继承与派生,纯虚函数,动态联编的动态性)

题目:

(1)首先设计一个抽象类Shape,包含三个公有的纯虚函数,分别实现显示数据成员的值、计算面积、计算体积的功能,函数名为ShowData()、GoArea()、GoVolume()

(2)设计两个Shape的公有派生类平面图形类、立体图形类,包含公有的数据成员面积area和体积volume,并实现基类Shape的三个纯虚函数的函数体。

(3)分别设计圆形类、椭圆类、矩形类、等腰三角形类,作为平面图形类的公有派生类。并在各个类里依次添加数据成员圆形半径、椭圆的横轴和纵轴长度、矩形的长和宽、等腰三角形的底边长和高。并实现数据的显示输出函数、实现计算面积的函数。

(4)分别设计球类、圆柱体类、长方体类,作为立体图形类的公有派生类。并在各个类里依次添加数据成员球的半径,圆柱体的底面的半径和高,长方体的长宽高。并实现数据的显示输出函数、实现计算体积的函数。

(5)计算面积的函数和计算体积的函数均需要先cin相应的数据,再才能计算,计算结果为double型,且保留2位小数,将计算结果给相应的数据成员进行赋值。函数无返回值。

(6)测试数据时,定义各种类型的对象,然后定义Shape类型的指针p,用户依次输入1、2、3…6、7,分别代表圆形、椭圆、矩形、三角形、球、圆柱体、长方体。通过使用switch语句,将对象地址赋值给p指针。然后调用该对象的计算面积或计算体积的函数,输出计算结果。为方便测试,switch仅执行一次,程序则结束。

举例:

Shape *p;

Circle c;

p=&c;

p->GoArea();

p->ShowData();

【样例输入】

5

2

【样例输出】

33.51

【样例说明】5代表球,2代表球的半径,根据体积公式4/3pirrr得到33.51

代码:

#include <iostream>
#include <iomanip>
#define PI 3.1415926 //位数要写多一点,然后控制输出位数

using namespace std;

class Shape{
    
    
public:
    virtual void ShowData() = 0;
    virtual void GoArea() = 0;
    virtual void GoVolume() = 0;
};

class Planefigure:public Shape{
    
     //平面图形
public:
    double area;
    void ShowData() {
    
    } //不定义的话也要写一个空的;
    void GoArea() {
    
    }
    void GoVolume() {
    
    }

};

class Stereogram:public Shape{
    
     //立体图形
public:
    double v;
    void ShowData() {
    
    } //不定义也要写一个空的,不然会报错;
    void GoArea() {
    
    }
    void GoVolume() {
    
    }

};

class Circular:public Planefigure{
    
     //圆
public:
    Circular(){
    
    
        int n;
        cin>>n;
        r = n;
    }
    void GoArea(){
    
     area = PI * r * r; }
    void ShowData(){
    
     cout<< fixed << setprecision(2) << area << endl; } //setprecision(n)控制总输出n位数(包括整数部分);用到<iomanip>;
                                                                     //加上fixed是使小数位数为n;不包括整数部分;
private:
    int r;
};

class Ellipse:public Planefigure{
    
     //椭圆
public:
    Ellipse(){
    
    
        int m, n;
        cin>>m>>n;
        a = m;
        b = n;
    }
    void GoArea(){
    
      area = PI * a * b; }
    void ShowData(){
    
     cout<< fixed << setprecision(2) << area <<endl; }

private:
    int a, b;
};

class Pyramid:public Planefigure{
    
     //矩形
public:
    Pyramid(){
    
    
        int m, n;
        cin>>m>>n;
        a = m;
        b = n;
    }
    void GoArea(){
    
      area = a * b; }
    void ShowData(){
    
     cout<< fixed << setprecision(2)<<area<<endl; }

private:
    int a, b;
};

class Triangle:public Planefigure{
    
     //等腰三角形
public:
    Triangle(){
    
    
        int m, n;
        cin>>m>>n;
        botten = m;
        high = n;
    }
    void GoArea(){
    
      area = 1.0 / 2 * botten * high; }
    void ShowData(){
    
     cout<< fixed << setprecision(2)<<area<<endl; }

private:
    int botten, high;
};

class Ball:public Stereogram{
    
     //球
public:
    Ball(){
    
    
        int n;
        cin>>n;
        R = n;
    }
    void GoVolume() {
    
     v = 4.0 / 3 * PI * R * R * R; }
    void ShowData() {
    
     cout<< fixed << setprecision(2)<<v<<endl; }

private:
    int R;
};

class Cylinder :public Stereogram{
    
     //圆柱体
public:
    Cylinder(){
    
    
        int m, n;
        cin>>m>>n;
        dr = m;
        h = n;
    }
    void GoVolume() {
    
     v = PI * dr * dr * h; }
    void ShowData() {
    
     cout<< fixed << setprecision(2)<<v<<endl; }

private:
    int dr, h;
};

class Rectangle:public Stereogram{
    
     // 长方体
public:
    Rectangle(){
    
    
        int i, j, k;
        cin>>i>>j>>k;
        aa = i;
        bb = j;
        cc = k;
    }
    void GoVolume() {
    
     v = aa * bb * cc; }
    void ShowData() {
    
     cout<< fixed << setprecision(2)<<v<<endl; }

private:
    int aa, bb, cc;
};

int main(){
    
    
    Shape *p; int n;
    cin>>n;
    switch(n){
    
    
    case 1: p = new Circular();p->GoArea();p->ShowData();break; //当场创建对象!!
    case 2: p = new Ellipse();p->GoArea();p->ShowData();break;
    case 3: p = new Pyramid();p->GoArea();p->ShowData();break;
    case 4: p = new Triangle();p->GoArea();p->ShowData();break;
    case 5: p = new Ball();p->GoVolume();p->ShowData();break;
    case 6: p = new Cylinder();p->GoVolume();p->ShowData();break;
    case 7: p = new Rectangle();p->GoVolume();p->ShowData();break;
    }
    return 0;
}

6.时间类的改进(运算符重载)

题目:

对前面实验写过的Time类进行修改,删去Add和Sub成员函数,通过重载“+”、“-”运算符直接进行时间的加减运算。

提示:

(1)可以用友元函数来实现“+”“-”运算符的重载。

(2)加法运算符可以是两个Time对象进行相加,也可以是一个表示秒数的int型数据加上一个Time对象,还可以是Time对象加上int型数据,得到的结果都是Time类型的对象。

(3)减法运算符可以是两个Time对象进行相减,也可以是Time对象减去一个表示秒数的int型数据,得到的结果都是Time类型的对象。

主函数设计如下,请勿修改:

int main(){

Time t1(2,34),t2,t3;

t2.SetTime(13,23,34);

cout<<“t1+t2:”;

t3=t1+t2;//两个Time类对象相加

t3.print_24();

cout<<"\nt1+65:";

t3=t1+65;//Time类对象加上65秒

t3.print_24();

cout<<"\n65+t1:";

t3=65+t1;//65秒加上Time类对象

t3.print_24();

cout<<"\nt2-t1:";

t3=t2-t1;//两个Time类对象相减

t3.print_24();

cout<<"\nt1-70:";

t3=t1-70;//Time类对象减去70秒

t3.print_24();

return 0;

}

【样例输出】

t1+t2:15:57:34

t1+65:02:35:05

65+t1:02:35:05

t2-t1:10:49:34

t1-70:02:32:50

代码:

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

class Time
{
    
    
    int hour, minute, second;
public:
    Time(int h, int m, int s = 0);
    Time(int s = 0);
    int SecCalc(){
    
    return(hour * 60 + minute) * 60 + second;}
    void SetTime(int h = 0, int m = 0, int s = 0);
    void print_12();
    void print_24();
    Time operator+(Time &t)const;
    Time operator-(Time &t)const;
    friend Time operator+(Time &t, int n);
    friend Time operator+(int n, Time &t);
    friend Time operator-(Time &t, int n);
};

Time::Time(int h, int m, int s){
    
    
    hour = h;
    minute = m;
    second = s;
}
Time::Time(int s){
    
    
    hour = s / 3600;
    minute = (s - (hour * 3600)) / 60;
    second = s - (hour * 3600 + minute * 60);
}

void Time::SetTime(int h, int m, int s){
    
    
    hour = h;
    minute = m;
    second = s;
}

void Time::print_12(){
    
    
    if(hour <= 12)
        {
    
    cout<<setw(2)<<setfill('0')<<hour<<":"<<minute<<":"<<second<<" AM";}
    if(hour > 12)
        {
    
    cout<<setw(2)<<setfill('0')<<hour-12<<":"<<minute<<":"<<second<<" PM";}
}

void Time::print_24(){
    
    
    cout<<setw(2)<<setfill('0')<<hour<<":"<<setw(2)<<setfill('0')<<minute<<":"<<setw(2)<<setfill('0')<<second;
}

Time Time::operator+(Time &t)const{
    
    
    Time c;
    c.second = second + t.second;
    if(c.second >= 60){
    
    
        c.second -= 60;
        c.minute += 1;
    }
    c.minute = minute + t.minute;
    if(c.minute >= 60){
    
    
        c.minute -= 60;
        c.hour += 1;
    }
    c.hour = hour + t.hour;
    if(c.hour >= 24){
    
    
        c.hour -= 24;
    }
    return c;
}

Time Time::operator-(Time &t)const{
    
    
    Time c;
    if((t.hour * 60 + t.minute) * 60 + t.second >= (hour * 60 + minute) * 60 + second){
    
    
        int a = ((t.hour * 60 + t.minute) * 60 + t.second) - ((hour * 60 + minute) * 60 + second);
        c.hour = a / 3600;                                                                                 //大哥长长记性!!复杂运算记得多加括号啊!!!
        c.minute = (a - (c.hour * 3600)) / 60;
        c.second = a - (c.hour * 3600 + c.minute * 60);
    }
    else{
    
    
        int d = ((hour * 60 + minute) * 60 + second) - ((t.hour * 60 + t.minute) * 60 + t.second );
        c.hour = d / 3600;
        c.minute = (d - (c.hour * 3600)) / 60;
        c.second = d - (c.hour * 3600 + c.minute * 60);
    }
    return c;
}

Time operator+(Time &t, int n){
    
    
    Time c;
    c.second = n + t.second;
    if(c.second >= 60){
    
    
        c.second -= 60;
        c.minute += 1;
    }
    c.minute += t.minute;
    if(c.minute >= 60){
    
    
        c.minute -= 60;
        c.hour += 1;
    }
    c.hour += t.hour;
    if(c.hour >= 24){
    
    
        c.hour -= 24;
    }
    return c;
}
Time operator+(int n, Time &t){
    
    
    Time c;
    c.second = n + t.second;
    if(c.second >= 60){
    
    
        c.second -= 60;
        c.minute += 1;
    }
    c.minute += t.minute;
    if(c.minute >= 60){
    
    
        c.minute -= 60;
        c.hour += 1;
    }
    c.hour += t.hour;
    if(c.hour >= 24){
    
    
        c.hour -= 24;
    }
    return c;
}
Time operator-(Time &t, int n){
    
    
    Time c;
    if(t.second >= n){
    
    
        c.second = t.second - n;
    }
    else{
    
    
        c.second = t.second + (120 - n);
        c.minute += (t.minute - 2);
        if(c.minute < 0){
    
    
            c.minute += 60;
            c.hour -= 1;
        }
        c.hour += t.hour;
        if(c.hour < 0){
    
    
            c.hour += 24;
        }
    }
    return c;
}
int main(){
    
    
    Time t1(2, 34), t2, t3;
    t2.SetTime(13, 23, 34);
    cout<<"t1+t2:";
    t3 = t1 + t2;//两个Time类对象相加
    t3.print_24();
    cout<<"\nt1+65:";
    t3 = t1 + 65;//Time类对象加上65秒
    t3.print_24();
    cout<<"\n65+t1:";
    t3 = 65 + t1;//65秒加上Time类对象
    t3.print_24();
    cout<<"\nt2-t1:";
    t3 = t2 - t1;//两个Time类对象相减
    t3.print_24();
    cout<<"\nt1-70:";
    t3 = t1 - 70;//Time类对象减去70秒
    t3.print_24();
    return 0;
}

7.运算符的重载

题目:

不允许修改主函数,请将以下代码补充完整。
class String {
char *ptr;
public:
String(char *s)
{
ptr=new char[strlen(s)+1];
strcpy(ptr, s);
}
~String() { delete []ptr; }
void print() { cout<<ptr<<endl; }
};

int main( )
{ String p1(“book”), p2(“pen”),p3(“good”),p4;
p4 = p4 = p1 ;
p3 = p1 = p2;
cout<<“p2:”;
p2.print();
cout<<“p1:”<<p1;
cout<<“p3:”<<p3;
p4+=p3;
cout<<“p4:”<<p4;
return 0;
}

【样例输入】

【样例输出】
p2:pen
p1:pen
p3:pen
p4:bookpen

代码:

#include <iostream>
#include <cstring>
using namespace std;

class  String{
    
    
    char *ptr;
public:
    String(){
    
    
        ptr = NULL;
    }
    String(const char *s)
    {
    
    
        ptr = new char[strlen(s) + 1];
        strcpy(ptr, s);
    }
    ~String(){
    
    delete  []ptr; }
    void operator +=(const String &s);
    String& operator =(const String &s);
    friend ostream & operator <<(ostream &out, String &s);
    void print() {
    
     cout<<ptr<<endl; }
};

void String::operator +=(const String &s){
    
    
    char a[100];
    strcpy(a, ptr);
    delete []ptr;
    ptr = new char[strlen(a) + strlen(s.ptr) + 1];
    strcpy(ptr, strcat(a, s.ptr));
}

String& String::operator =(const String &s){
    
    
    if(this == &s)    return *this;
    delete []ptr;
    ptr = new char[strlen(s.ptr) + 1];
    strcpy(ptr, s.ptr);
    return *this;
}

ostream & operator <<(ostream &out, String &s){
    
    
    out<<s.ptr<<endl;
    return out;
}
int main( ){
    
    
    String  p1("book"), p2("pen"), p3("good"), p4;
    p4 = p4 = p1;        
    p3 = p1 = p2;
    cout<<"p2:";
    p2.print();
    cout<<"p1:"<<p1;
    cout<<"p3:"<<p3;
    p4 += p3;
    cout<<"p4:"<<p4;
    return 0;
}

8.字符串排序

题目:

将若干字符串按字母顺序(由小到大)输出。主函数如下,请补充完成该程序。

int main( )
{ void sort(char *alpha[], int n );
void print(char *alpha[], int n );
char *alpha[]={“Follow me”, “Basic”, “Great Wall”, “FORTRAN”, “Computer design”};
int n=5;
//分别调用sort函数和print函数完成字符串的排序及输出。请自行练习选择排序和插入排序方法。
return 0;
}

【样例输入】

【样例输出】
Basic
Computer design
FORTRAN
Follow me
Great Wall

代码:

#include <iostream>
#include <cstring>
using namespace std;

int main( ){
    
    
    void  sort(char *alpha[], int n );
    void   print(char *alpha[], int n );
    char *alpha[] = {
    
    "Follow me", "Basic", "Great Wall", "FORTRAN", "Computer design"};
    int  n = 5;
    //分别调用sort函数和print函数完成字符串的排序及输出。请自行练习选择排序和插入排序方法。
    sort(alpha, 5);
    print(alpha, 5);
    return 0;
}

void  sort(char *alpha[], int n ){
    
               //char *alpha[]是一个装着很多“指针”的数组
    for(int i = 0; i < n; i ++){
    
    
        for(int j = i + 1; j < n; j ++){
    
    
            if(strcmp(alpha[i], alpha[j]) > 0){
    
    
                char *tem = alpha[i];     //所以直接交换指针,不能用strcpy!!
                alpha[i] = alpha[j];
                alpha[j] = tem;
            }
        }
    }
}

void  print(char *alpha[], int n ){
    
    
    for(int i = 0; i < n; i ++){
    
    
        cout<<alpha[i]<<endl;
    }
}

欢迎提问,学弟学妹们加油~

猜你喜欢

转载自blog.csdn.net/qq_51800570/article/details/120925821