冒泡排序无法对C ++中的动态对象数组进行排序

时间:2017-12-08 20:04:15

标签: c++ arrays object dynamic

我的班级城市有以下私人数据:城市名称(类型字符),宽度(双人),长度(双人)和城市的高度(双人)。我必须创建动态数组,默认情况下由构造函数插入 - City(),当程序启动时。然后程序使用方法output()并打印插入的cities数组.mA是我的城市对象。我应该使用bubble按照长度对城市进行排序。我有复制构造函数,operator =我必须将double max(这是变量,用于冒泡排序以存储当前最大值)转换为City类型,为此我使用了一个构造函数参数:City(double max)。问题是排序不起作用。我认为问题在于我使用一个参数定义构造函数(将类型double转换为类型city)。

#include "stdafx.h"
#include<iostream>
#include<math.h>
#include <algorithm> 
using namespace std;

class City{
private: char *name;
         double width;
         double length;
         double height;
public:

    void Output();
    City();
    ~City();
    City(double max){
        name = "";
        width = 0;
        length = max;
        height = 0;
    }
    double GetLength()
    {
        return length;
    }
    double GetWidth(){ return width; }
    double GetHeight(){ return height; }
    char GetName(){ return *name; }
    City(const City& that)
    {
        name = new char[strlen(that.name) + 1];
        for (int i = 0; i <= strlen(that.name); i++)
            name[i] = that.name[i];
        //strcpy(name, that.name);
        width = that.width;
        length = that.length;
        height = that.height;
    }


    City& operator=(const City that)
    {
        name = that.name;
        width = that.width;
        length = that.length;
        height = that.height;
        return*this;
    }
};
City::City()
{
    char ime[20];
    cout << "Name= ";
    cin >> ime;
    name = new char[strlen(ime) + 1];
    for (int i = 0; i <= strlen(ime); i++)
        name[i] = ime[i];

    cout << "Width= ";
    cin >> width;
    cout << "Length= ";
    cin >> length;
    cout << "Height= ";
    cin >> height;
}

void City::Output()
{
    cout << "Name is: " << name << endl;
    cout << " Width is: " << width << " deg" << endl;;
    cout << " Length is: " << length << " deg" << endl;
    cout << " Height is: " << height << " m" << endl;
    return;
}
City::~City()
{
    cout << " " << endl;
    cout << "Destructor of City!" << endl;
    delete[] name;

}

int main()
{

    int n;
    City *mA;
    cout << "Input number of cities: " << endl;
    cin >> n;
    mA = new City[n];
    for (int j = 0; j < n; j++)
    {
        mA[j].Output();
    }
    cout << "Cities from west to east, sorted by their length" << endl;


    double max = mA[0].GetLength();
    for (int j = 1; j<n; j++)
    {
        if (mA[j - 1].GetLength()>mA[j].GetLength())
        {
            max = mA[j - 1].GetLength();

            mA[j - 1] = mA[j];
            mA[j] = max;
        }
    }
    for (int j = 0; j < n; j++)
    {
        mA[j].Output();
    }

    delete[]mA;
    return 0;

}

2 个答案:

答案 0 :(得分:0)

手动排序需要比较使用的条目 -------评论--------- for(int j = 1; jmA [j] .GetLength()){             ......交换元素             ...... j- = 2; //指数             ... if(j <0)j = 0; } -------结束评论---------

如果将使用std :: sort条件由STL可靠检查。 并且只会在回调函数上检查比较条件。

问候,Hubert Hermanutz

答案 1 :(得分:0)

City::City()
{
    char ime[20];
    cout << "Name= ";
    cin >> ime;
    name = new char[strlen(ime) + 1];
    for (int i = 0; i <= strlen(ime); i++)
        name[i] = ime[i];
    ...
}

首先,您必须修复构造函数。虽然它不一定是错的,但您不应该在构造函数中请求输入。为Input()添加单独的函数

City& operator=(const City that)
{
    name = that.name;
    ...
    return*this;
}

此赋值运算符错误。 name是一个指针,您不想分配指针(不在此方案中)。您应该使用与以前相同的方法来复制名称:

name = new char[strlen(ime) + 1];
strcpy(name, ime);

冒泡排序应该像下面的方法一样。此外,您已添加#included <string>。您应该使用std::string代替!

#include <iostream>
#include <string>

using namespace std;

class City
{
private:
    char *name;
    double width;
    double length;
    double height;
public:
    City()
    {
        name = nullptr;
        width = 0;
        length = 0;
        height = 0;
    }

    City(const City& that)
    {
        name = new char[strlen(that.name) + 1];
        strcpy(name, that.name);
        width = that.width;
        length = that.length;
        height = that.height;
    }

    City& operator=(const City that)
    {
        name = new char[strlen(that.name) + 1];
        strcpy(name, that.name);
        width = that.width;
        length = that.length;
        height = that.height;
        return*this;
    }

    ~City()
    {
        delete[] name;
    }

    void Input()
    {
        char buffer[100];
        cout << "Name= ";
        cin >> buffer;
        name = new char[strlen(buffer) + 1];
        strcpy(name, buffer);
        cout << "Width= ";
        cin >> width;
        cout << "Length= ";
        cin >> length;
        cout << "Height= ";
        cin >> height;
    }

    void Output()
    {
        cout << "Name is: " << name << ", " << length << endl;
        cout << " Width is: " << width << " deg" << endl;
        cout << " Length is: " << length << " deg" << endl;
        cout << " Height is: " << height << " m" << endl << endl;
    }

    double GetLength() { return length; }
    double GetWidth() { return width; }
    double GetHeight() { return height; }
    char GetName() { return *name; }
};

int main()
{
    int n;
    City *mA;
    cout << "Input number of cities: " << endl;
    cin >> n;
    City *mA = new City[n];

    //read input
    for(int j = 0; j < n; j++)
        mA[j].Input();

    //bubble sort:
    for(int i = 0; i < n; i++)
    {
        for(int j = i + 1; j < n; j++)
        {
            if(mA[i].GetLength() > mA[j].GetLength())
            {
                //swap values:
                City temp = mA[i];
                mA[i] = mA[j];
                mA[j] = temp;
            }
        }
    }

    for(int j = 0; j < n; j++)
        mA[j].Output();

    delete[]mA;

    system("pause");
    return 0;
}