C++语言训练_写一个集合类Set

<span style="font-size:32px;background-color: rgb(255, 255, 255);">这是一位老美的C++作业,写着练练手</span>

 

任务要求如下:

 

// Assignment 3 (25 points): Assignment 3 is similar
//  to example 8.  For this assignment, declare a class
//  named Set in C++ header file Set.h.  Class Set has
//  the following two private member variables.
//   1. set: a pointer to a double
//   2. number: an integer which contains the number of
//      of elements in the set.

// Class Set has the following two constructors.
//   1. A constructor that has no parameters that creates
//      an empty set by setting set to NULL and number to
//      zero.  It then uses cout to display the following
//      message.
//          An empty set will be created.
//   2. A constructor that has the following two parameters.
//      a. number: which is received as a constant parameter
//         and represents the number of elements in a set.
//      b. set: which is received as a constant pointer to
//         a double array which contains the elements of a set.
//      The names of the parameters MUST be number and set.
//      Since these names match the names of the private member
//      variables of class Set, you will have to use the this->
//      operator (notation) to refer to the private member variables.
//      If you have any questions, ask the instructor.
//      The constructor examines the value of parameter number and
//      does the following.
//      number > 0: If number is greater than zero, the constructor
//                  sets private member variable number to the parameter
//                  number.  Then it allocates a double array of size
//                  number using the new operator, making private member
//                  variable set, point to the newly allocated array.  It
//                  then copies the elements of array set, passed as the
//                  second parameter, to private member variable array
//                  set.  This initializes a Set object having number
//                  elements.
//      number == 0: If number is equal to zero, the constructor sets
//                  private member variable number to zero and sets
//                  private pointer set to NULL.  This creates the empty
//                  set.  It then uses cout to display the following
//                  message.
//                      An empty set will be created.
//      number < 0: If number is less than zero, the constructor uses cerr
//                  to display the following error messages.
//                      xxx must be greater than or equal to zero.
//                      An empty set will be created.
//                  where xxx is the value of parameter number.  After
//                  displaying the above messages, the constructor creates
//                  the empty set (as in the case number == 0).

// Class Set has a single public print function that prints the elements
// of the Set object, ALL on a SINGLE line.  The print function should have
// a prototype that uses const just as in example 8.

// Class Set has a public member function named getNumber that gets the
// number of elements in a Set object.  The prototype of getNumber must
// use const similar to function getLength in example 8.

// Class Set has a public member function named getSet that gets the
// pointer to a Set object.  The prototype of getSet must use const similar
// to function getString in example 8.

// Class Set has a public member function named setSet that updates a Set
// object.  Function setSet is similar to function setString in example 8.
// Function setSet has the following two parameters.
//      a. n: which is received as a constant parameter
//         and represents the number of elements in a set.
//      b. s: which is received as a constant pointer to
//         a double array which contains the elements of a set.

//      Function setSet examines the value of parameters n and s, and
//      does the following.
//      n > 0: If n is greater than zero, function setSet sets private
//             member variable number to the parameter n.  Then it deletes
//             set.  After deleting set, it allocates a double array of
//             size number using the new operator, making private member
//             variable set, point to the newly allocated array.  It then
//             copies the elements of array set, passed as the second
//             parameter, to private member variable array set.  This
//             reinitializes a Set object having number elements.
//      n == 0 or s == NULL: If n is equal to zero or s is NULL, function
//             setSet sets private member variable number to zero and sets
//             private pointer set to NULL.  This creates the empty set.
//             It then uses cout to display the following message.
//                  An empty set will be created.
//      n < 0: If n is less than zero, function setSet uses cerr to
//             display the following error messages.
//                  xxx must be greater than or equal to zero.
//                  An empty set will be created.
//             where xxx is the value of parameter n.  After displaying
//             the above messages, function setSet creates the empty set.

// Class Set has two friend functions that implement the operators + and *
// where + is set union and * is set intersection defined as follows.
// Consider two Set objects X and Y where
//  X = { -6.2, -3.1, 0.0, 1.9, 5.2, 8.3, 9.5, 10.7, 11.0, 12.5 }
//  Y = { -8.1, -3.1, 0.0, 2.5, 5.8, 8.3, 9.5, 11.0 }
// Then
//  X + Y = { -8.1, -6.2, -3.1, 0.0, 1.9, 2.5, 5.2, 5.8, 8.3, 9.5, 10.,7 11.0, 12.5 }
//  X * Y = { -3.1, 0.0, 8.3, 9.5, 11.0 }
// That is, X + Y is the union of set X with set Y and X * Y is the
// intersection of set X with set Y.
// For + and * you may assume that the sets X and Y are in ascending order.
// This makes the construction of X + Y and X * Y easier.  The sets X + Y
// and X * Y MUST also be in ascending order.  If you implement + and *
// correctly you will NOT have to do any sorting.  Ask the instructor if
// you have questions.

// To test class Set your main program should do the following.
//  1. Declare two double arrays a and b that contain the following values
//     (in the given order).
//      array a = -6.2, -3.1, 0.0, 1.9, 5.2, 8.3, 9.5, 10.7, 11.0, 12.5
//      array b = -8.1, -3.1, 0.0, 2.5, 5.8, 8.3, 9.5, 11.0
//  2. Calculate the size of a and b using the sizeof function.  DO NOT assume
//     that array a has 10 elements and DO NOT assume b has 8 elements.
//  3. If na is the size of a and nb the size of b then create a Set object
//     named X using the constructor that takes NO parameters (this makes X
//     the empty set).  Then use the setSet function for set X, passing it
//     na and a.  This sets X to a set that contains the numbers in array a.
//  4. Then print X.
//  5. Create a second Set object named Y with the constructor that takes two
//     parameters.  Pass nb abd b to the constructor.
//  6. Then print Y.
//  7. Create a Set object named Z using the constructor that takes NO parameters
//     (this makes Z the empty set).
//  8. Calculate the set union Z = X + Y and print Z.
//  9. Calculate the set intersection Z = X * Y and print Z.
//  10. Attempt to create Set object U as follows.
//          Set U(-na,a);
//  11. Reset set Z as follows
//          Z.setSet(nb,NULL);

 

      很显然,写一个Set类,两成员变量number与set,两个构造函数,一个打印函数print,一个取长度函数getNumber,一个设置函数setSet,重载两个友元运算符+和*表示集合的并与交,main函数里写测试。

 

上代码:

Set.h

 

#include<iostream>
#include<stdio.h>
#include<algorithm>
#ifndef SET_H_INCLUDED
#define SET_H_INCLUDED

using namespace std;
class Set
{
private:
    double* set;//the pointer to double array
    int number;//the number of elements in array
public:
    Set()//constructor with no args
    {
        set=NULL;
        number=0;
    }
    Set(const int number,const double* set)//constructor with tow args
    {
        if(number>0&&set!=NULL)
        {
            this->number=number;
            double tmp[number];
            this->set=new double[number];
            for(int i=0;i<number;i++)
            {
                this->set[i]=set[i];
            }
            sort(this->set,this->set+number);

        }else if(number==0||set==NULL)
        {
            this->set=NULL;
            this->number=0;
            printf("An empty set will be created.\n");
        }else if(number<0)
        {

            printf("%d must be greater than or equal to zero.\n An empty set will be created.\n",number);
            this->set=NULL;
            this->number=0;
        }



    }


        void print()const//the print function
        {
            for(int i=0;i<this->number;i++)
            {
                cout<<this->set[i]<<"  ";
            }
            cout<<endl;
        }




        int getNumber()const//the function return the number of elements in array
        {
            return this->number;
        }

        void setSet(const int n,const double* s)//the setSet function
        {
            if(n>0&&s!=NULL)
            {
                this->number=n;
                this->set=new double[n];
                for(int i=0;i<n;i++)
                {
                    this->set[i]=s[i];
                }
                sort(this->set,this->set+n);

            }else if(n==0||s==NULL)
            {
                this->number=0;
                this->set=NULL;
                printf("An empty set will be created.\n");
            }else if(n<=0)
            {
                printf("%d must be greater than or equal to zero.\n An empty set will be created.\n",n);
                this->set=NULL;
                this->number=0;
            }

        }



        //tow friend functions overriding operator
        friend Set operator+(const Set a,const Set b);
        friend Set operator*(const Set a,const Set b);

};

Set operator+(const Set a,const Set b)
    {
        int na=a.getNumber();
        int nb=b.getNumber();
        double tmp[a.getNumber()+b.getNumber()+10];
        int i;
        for(i=0;i<na;i++)
        {
            tmp[i]=a.set[i];
        }
        for(int j=0;j<nb;j++)
        {
            tmp[i+j]=b.set[j];
        }
        sort(tmp,tmp+na+nb);
        double tmp_2[a.getNumber()+b.getNumber()+10];
        i=0;
        for(int j=0;j<(na+nb);j++)
        {
            if(j==0)
            {
                tmp_2[i]=tmp[j];
                i++;
            }else if(j>0&&tmp[j]!=tmp[j-1])
            {
                tmp_2[i]=tmp[j];
                i++;
            }

        }

        Set result(i,tmp_2);
        return result;
    }

Set operator*(const Set a,const Set b)
{
    double tmp[a.getNumber()+b.getNumber()+10];
    int cnt=0;
    for(int i=0;i<a.getNumber();i++)
    {
        for(int j=0;j<b.getNumber();j++)
        {
            if(a.set[i]==b.set[j])
            {
                tmp[cnt]=a.set[i];
                cnt++;
                break;
            }
        }
    }

    Set result(cnt,tmp);
    return result;

}


#endif // SET_H_INCLUDED


 

main.cpp

 

#include <iostream>
#include<stdio.h>
#include"Set.h"
using namespace std;

int main()
{

//We test the class Set here.


//  1. Declare two double arrays a and b that contain the following values
//     (in the given order).
//      array a = -6.2, -3.1, 0.0, 1.9, 5.2, 8.3, 9.5, 10.7, 11.0, 12.5
//      array b = -8.1, -3.1, 0.0, 2.5, 5.8, 8.3, 9.5, 11.0

    double a[]={-6.2,-3.1,0.0,1.9,5.2,8.3,9.5,10.7,11.0,12.5};
    double b[]={-8.1,-3.1,0.0,2.5,5.8,8.3,9.5,11.0};

//  2. Calculate the size of a and b using the sizeof function.  DO NOT assume
//     that array a has 10 elements and DO NOT assume b has 8 elements.

    int na=sizeof(a)/8;
    int nb=sizeof(b)/8;


//  3. If na is the size of a and nb the size of b then create a Set object
//     named X using the constructor that takes NO parameters (this makes X
//     the empty set).  Then use the setSet function for set X, passing it
//     na and a.  This sets X to a set that contains the numbers in array a.
//  4. Then print X.
//  5. Create a second Set object named Y with the constructor that takes two
//     parameters.  Pass nb abd b to the constructor.
//  6. Then print Y.

    Set X(na,a);
    Set Y(nb,b);


    X.print();
    Y.print();

//  7. Create a Set object named Z using the constructor that takes NO parameters
//     (this makes Z the empty set).
//  8. Calculate the set union Z = X + Y and print Z.
//  9. Calculate the set intersection Z = X * Y and print Z.


    Set Z;
    Z=X+Y;
    Z.print();
    Z=X*Y;
    Z.print();


//  10. Attempt to create Set object U as follows.
//          Set U(-na,a);

    Set U(-na,a);

//  11. Reset set Z as follows
//          Z.setSet(nb,NULL);


    Z.setSet(nb,NULL);


    //cout << "Hello world!" << endl;
    return 0;
}

 


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值