类与对象练习题(三)---Array/Person类

在刚开始学习c++的时候刷了很多基础题,这些基础题比较适合初学C++的码友,所以在学完就立即进行了整理,一是为了让初学C++的码友有所参考,二也是为了复习一下所学过知识。
但因为当时在整理时,时间有点紧促,可能会出现一些小错误,于是利用五一假期对之前的文章进行检查,修改了一些小错误,可能有些错误我还没有发现,欢迎码友们对其指正。

以下六道题要用到类与对象中的封装*等基础知识,适合初学类与对象这部分的码友进行练习。

Array类01

数组类Array用于存储一组双精度浮点数,其定义如下,给出各个成员函数实现。

const int MaxSize=100;  //数组中元素的最大个数
class Array
{
public:
       Array ();      //初始化length=0
       int Length();   //返回数组中元素实际个数
       void Insert(int i, double x);  //在下标i处插入x
       void Display();       //输出数组中实际元素        
private:
       double data[MaxSize];  //存储元素
       int length;              //数组中实际元素个数
};

main函数中输入n个(n<=100)双精度浮点数(以0结束),将这n个数按输入次序存储到同一个Array类对象的数据成员data中(利用成员函数Insert实现),并调用Display输出这些数。
Sample Input
35 3 64 7 6443 0
Sample Output
Length:5
Elements:35 3 64 7 6443

#include<iostream>
using namespace std;
const int MaxSize = 100;  //数组中元素的最大个数
class Array
{
public:
    Array();      //初始化length=0
    int Length();   //返回数组中元素实际个数
    void Insert(int i, double x);  //在下标i处插入x
    void Display();       //输出数组中实际元素        
private:
    double data[MaxSize];  //存储元素
    int length;              //数组中实际元素个数
};
Array::Array()
{
    length = 0;
}
int Array::Length()
{
    return length;
}
void Array::Insert(int i, double x)
{
    int j;
    for (j = length - 1; j >= i; j--)
        data[j + 1] =data[j];
    data[i] = x;
    length++;
}
void Array::Display()
{
    cout << "Length:" << length << endl;
    cout << "Elements:";
    for (int i = 0; i < length; i++)
        cout << data[i] << " ";
}
int main()
{
    double x;
    int i;
    cin >> x;
    Array A1;
    while (x)
    {
        i = A1.Length();
        A1.Insert(i, x);
        cin >> x;
    }
    A1.Display();
    return 0;
}

Array类02

数组类Array用于存储一组双精度浮点数,在现有类定义基础适当增加必要的成员函数,完成下列任务:
main函数中输入n个(n<100)双精度浮点数(以0结束),将这n个数存储到同一个Array类对象中,并调用Display输出这些数。再输入删除的元素x,从对象中删除所有的值为x的元素,再将剩余元素按照从小到大的顺序排序,并输出排序后结果。
已知数组类的定义(其它成员函数请根据需要添加)如下:

const int MaxSize=100;  //数组中元素的最大个数
class Array
{
public:
       Array ();      //初始化length=0
       int Length();   //返回数组中有效元素个数
       void Insert(int i, double x);  //在下标i处插入x
       void Display();       //输出数组中有效元素        
private:
       double data[MaxSize];  //存储元素
       int length;              //数组中有效元素个数
};
主函数如下(不允许改动主函数中代码):
int main()
{
       int i=0;
double num;
       Array a; //顺序表的初始化
       while(1)
       {
              cin>>num;
              if(!num) break;
              try{
                     a.Insert(i++,num);  //insert
              }
              catch(const char *ms)
              {
                     cout<<ms<<endl;
              }
       }
       a.Display ();
    double x;
       cin>>x;
       try
       {
              int n=a.Delete(x);   //delete all x
              cout<<"Count of deleted elements:"<<n<<endl;
       }
       catch(const char *ms)
       {
              cout<<ms<<endl;
       }
       a.Sort();   //sort
       a.Display();
    return 0;
}

Sample Input
23 2 2 344 3 32 2 5 0 2
Sample Output
Length:8
Elements:23 2 2 344 3 32 2 5
Count of deleted elements:3
Length:5
Elements:3 5 23 32 344

#include<iostream>
using namespace std;
const int MaxSize = 100;  //数组中元素的最大个数
class Array
{
public:
    Array();      //初始化length=0
    int Length();   //返回数组中有效元素个数
    void Insert(int i, double x);  //在下标i处插入x
    void Display();       //输出数组中有效元素  
    void Sort();
    int Delete(double x);
private:
    double data[MaxSize];  //存储元素
    int length;              //数组中有效元素个数
};
Array::Array()
{
    length = 0;
}
int Array::Length()
{
    return length;
}
void Array::Insert(int i, double x)
{
    int j = 0;
    for (j = length - 1; j >= i; j--)
        data[j + 1] = data[j];
    data[i] = x;
    length++;
}
void Array::Display()
{
    cout << "Length:" << length << endl;
    cout << "Elements:";
    for (int i = 0; i < length; i++)
        cout << data[i] << " ";
    cout << endl;
}
void Array::Sort()
{
    int i, j;
    for (i = 0; i < length - 1; i++)
    {
        for (j = 0; j < length - 1 - i; j++)
        {
            if (data[j] > data[j + 1])
            {
                double temp = data[j];
                data[j] = data[j + 1];
                data[j + 1] =temp;
            }
        }
    }
}
int Array::Delete(double x)
{
    int i,n,j=0;
    for (i = 0; i < length; i++)
    {
        if (data[i] != x)
        {
            data[j++] = data[i];
        }
    }
    n=length-j;
    length=j;
    return n;
}
//主函数如下(不允许改动主函数中代码):
int main()
{
    int i = 0;
    double num;
    Array a; //顺序表的初始化
    while (1)
    {
        cin >> num;
        if (!num) break;
        try {
            a.Insert(i++, num);  //insert
        }
        catch (const char* ms)
        {
            cout << ms << endl;
        }
    }
    a.Display();
    double x;
    cin >> x;
    try
    {
        int n = a.Delete(x);   //delete all x
        cout << "Count of deleted elements:" << n << endl;
    }
    catch (const char* ms)
    {
        cout << ms << endl;
    }
    a.Sort();   //sort
    a.Display();
    return 0;
}

Array类03

数组类Array用于存储一组双精度浮点数,其定义如下,给出各个成员函数实现。

class Array
{
public:
       Array (int size=100);  //初始化:MaxSize置为size,为data动态分配内存,length置为0
       Array (const Array &a);  //深拷贝构造函数
       ~Array ();            //析构函数
       void Insert(int i, double x);  //在下标i处插入x
       void Display();              //输出数组中的元素             
private:
       int MaxSize;              //数组中可存储的元素最大个数
       double *data;            //存储元素
       int length;                    //数组中有效元素个数
};

main函数中输入n个(n<100)双精度浮点数(以0结束),将这n个数存储到同一个Array类对象arr1中,并调用Display输出这些数,利用arr1构造Array类对象arr2,并输出arr2中所有元素的值。
Sample Input
23 42 13 42 4 25 34 0
Sample Output
Length:7
Elements:23 42 13 42 4 25 34
Length:7
Elements:23 42 13 42 4 25 34

#include<iostream>
using namespace std;
class Array
{
public:
    Array(int size = 100);  //初始化:MaxSize置为size,为data动态分配内存,length置为0
    Array(const Array& a);  //深拷贝构造函数
    ~Array();            //析构函数
    void Insert(int i, double x);  //在下标i处插入x
    void Display();              //输出数组中的元素             
private:
    int MaxSize;              //数组中可存储的元素最大个数
    double* data;            //存储元素
    int length;                    //数组中有效元素个数
};
Array::Array(int size)
{
    MaxSize = size;
    data = new double[MaxSize];
    length = 0;
}
Array::Array(const Array& a)
{
    MaxSize=a.MaxSize;
    data = new double[MaxSize];
    for (int i = 0; i < a.length; i++)
    {
        data[i] = a.data[i];
    }
    length = a.length;
}
Array::~Array()
{
    delete[]data;
}
void Array::Insert(int i, double x)
{
    int j;
    for (j = length - 1; j >= i; j--)
        data[j + 1] = data[j];
    data[i] = x;
    length++;
}
void Array::Display()
{
    cout << "Length:" << length << endl;
    cout << "Elements:";
    for (int i = 0; i < length; i++)
        cout << data[i] << " ";
    cout << endl;
}
int main()
{
    double m;
    int i = 0;
    Array arr1;
    cin >> m;
    while (m)
    {
        arr1.Insert(i, m);
        i++;
        cin >> m;
    }
    arr1.Display();
    Array arr2 = arr1;
    arr2.Display();
    return 0;
}

Array类04

数组类Array用于存储一组双精度浮点数,其定义如下,给出各个成员函数实现。

class Array{
public:
       Array (int size=100);  //初始化数据成员:MaxSize置为size,为data动态分配内存空间,length置为0
       Array (const Array&r);//深拷贝构造函数
       ~Array ();           //析构函数
       void Insert(int i, double x);  //在下标i处插入x
       void Display();              //输出数组中的元素             
private:
       int MaxSize;              //数组中可存储的元素最大个数
       double *data;            //存储元素
       int length;                    //数组中有效元素个数
};

为Array类增加下列成员函数:
(1)查找位置Locate
功能:在数组中查找x,若存在则返回x在数组中的下标,若不存在则返回-1。假设x至多存在一个。
(2)逆置函数Invert
功能:将data中的各元素逆置存放。

main函数中输入n个(n<100)双精度浮点数(以0结束),将这n个数存储到同一个Array类对象arr1中,并调用Display输出这些数,再验证新增成员函数功能。其中Locate函数查找失败时输出No found
Sample Input
12 42 535 66 64 2 4 0 535
Sample Output
Length:7
Elements:12 42 535 66 64 2 4
Position of 535:2
Length:7
Elements:4 2 64 66 535 42 12

#include<iostream>
using namespace std;
class Array 
{
public:
    Array(int size = 100);  //初始化数据成员:MaxSize置为size,为data动态分配内存空间,length置为0
    Array(const Array& r);//深拷贝构造函数
    ~Array();           //析构函数
    void Insert(int i, double x);  //在下标i处插入x
    void Display();              //输出数组中的元素   
    int Locate(double x);
    void Invert();
private:
    int MaxSize;              //数组中可存储的元素最大个数
    double* data;            //存储元素
    int length;                    //数组中有效元素个数
};
Array::Array(int size)
{
    MaxSize = size;
    data = new double[MaxSize];
    length = 0;
}
Array::Array(const Array& r)
{
    MaxSize = r.MaxSize;
    data = new double[MaxSize];
    for (int i = 0; i < r.length; i++)
    {
        data[i] = r.data[i];
    }
    length = r.length;
}
Array::~Array()
{
    delete []data;
}
void Array::Insert(int i, double x)
{
    int j;
    for (j = length - 1; j >= i; j--)
        data[j + 1] = data[j];
    data[i] = x;
    length++;
}
void Array::Display()
{
    cout << "Length:" << length << endl;
    cout << "Elements:";
    for (int i = 0; i < length; i++)
    {
        cout << data[i] << " ";
    }
    cout << endl;
}
int Array::Locate(double x)
{
    int i;
    for (i = 0; i < length; i++)
    {
        if (data[i] == x)
            break;
    }
    if (i < length)
        return i;
    else
        return -1;
}
void Array::Invert()
{
    int i;
    for (i = 0; i < length / 2; i++)
    {
        double temp = data[i];
        data[i] = data[length - 1 - i];
        data[length - 1 - i] = temp;
    }
}
int main()
{
    double m,x;
    int i=0;
    Array arr1;
    cin >> m;
    while (m)
    {
        arr1.Insert(i, m);
        i++;
        cin >> m;
    }
    arr1.Display();
    cin >> x;
    if (arr1.Locate(x))
    {
        cout << "Position of " << x << ":" << arr1.Locate(x) << endl;
    }
    else
        cout << "No found" << endl;
    Array arr2 = arr1;
    arr2.Invert();
    arr2.Display();
    return 0;
}

Person类01

设计一个Person类,其属性包括姓名name和身份证号id,其中name为指针类型,id为整型,编写构造函数Person,实现数据成员初始化;编写拷贝构造函数;编写Display函数显示数据成员信息;编写析构函数。
main函数中利用Person类构造函数建立类对象p1,再利用拷贝构造函数建立对象p2,打印每个person类对象的信息。
要求:两个对象的数据分行显示
Sample Input
liming 12
Sample Output
liming 12
liming 12

#include<iostream>
#include<cstring>
using namespace std;
class Person
{
public:
	Person(char* name, int id);
	Person(Person& p);
	void Display();
	~Person();
private:
	char* m_name;
	int m_id;
};
Person::Person(char* name, int id)
{
	int len = strlen(name);
	m_name = new char[len + 1];
	strcpy(m_name, name);
	m_id = id;
}
Person::Person(Person& p)
{
	m_id = p.m_id;
	int len = strlen(p.m_name);
	m_name = new char[len + 1];
	strcpy(m_name, p.m_name);
}
void Person::Display()
{
	cout << m_name << " " << m_id << endl;
}
Person::~Person()
{
	delete[]m_name;
}
int main()
{
	char name[20];
	int id;
	cin >> name >> id;
	Person P1(name, id);
	P1.Display();
	Person P2 = P1;
	P2.Display();
	return 0;
}

Person类02

设计一个Person类,其属性包括姓名name和年龄age,其中name为string类型,age为int型,编写构造函数Person,实现数据成员初始化;编写拷贝构造函数(其中新对象的姓名为原对象姓名尾部加feng,年龄加1);编写Display函数显示数据成员信息; main函数中利用Person类构造函数建立类对象p1,再利用拷贝构造函数建立对象p2,打印每个person类对象的信息。
Sample Input
zhangsan 18
Sample Output
zhangsan 18
zhangsanfeng 19

#include<iostream>
using namespace std;
class Person
{
public:
	Person(string name, int age);
	Person(Person& P1);
	void Display();
private:
	string m_name;
	int m_age;
};
Person::Person(string name, int age)
{
	m_name = name;
	m_age = age;
}
Person::Person(Person& P1)
{
	m_name = P1.m_name + "feng";
	m_age = P1.m_age + 1;
}
void Person::Display()
{
	cout << m_name << " " << m_age << endl;
}
int main()
{
	string name;
	int age;
	cin >> name >> age;
	Person P1(name, age);
	P1.Display();
	Person P2 = P1;
	P2.Display();
	return 0;
}

大家好,我是Lucky_追梦仔。一个正在学习编程的小白,希望我的博文,可以帮助到您学习,或者解决您遇到的问题。

  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值