I have class City with following private data: name of city(type char), width(double),length(double) and height(double) of the city. I have to make dynamic array, which is inserted by constructor by default- City(),when the programs starts.Then the program uses method output() and prints inserted array of cities.mA is my object of cities.I should use bubble sort to sort the cities by their length.I have copy constructor, operator = and I have to convert double max(this is variable, used in bubble sort to store current max value) to type City and in this purpose I used contructor with one parameter: City(double max).The problem is that the sort doesn't work. I think that the problem is in my definition of constructor with one parameter(to convert type double to type city).
我有班级城市,其中包含以下私人数据:城市名称(类型字符),宽度(双人),长度(双人)和城市高度(双人)。我必须创建动态数组,默认情况下由构造函数插入 - City(),当程序启动时。然后程序使用方法输出()并打印插入的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 个解决方案
#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];
...
}
First you have to fix the constructor. Although it is not necessarily wrong, you should not ask for input in the constructor. Add a separate function for Input()
instead
首先,你必须修复构造函数。虽然它不一定是错的,但你不应该在构造函数中要求输入。为Input()添加单独的函数
City& operator=(const City that)
{
name = that.name;
...
return*this;
}
This assignment operator is wrong. name
is a pointer, you don't want to assign the pointers (not in this scenario). You should be using the same method as before to copy the name instead:
这个赋值运算符是错误的。 name是一个指针,您不想分配指针(不在此方案中)。您应该使用与以前相同的方法来复制名称:
name = new char[strlen(ime) + 1];
strcpy(name, ime);
bubble sort should be done like the method in below. Also, you have added #included <string>
. You should be using std::string
instead!
冒泡排序应该像下面的方法一样。此外,您已添加#included
#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;
}
#2
0
manually sorting requires comparing of resorted entries ------- comment --------- for (int j = 1; jmA[j].GetLength()) { ... exchange elements ... j-=2; //index of ... if(j < 0) j=0; } ------- end comment ---------
手动排序需要比较使用的条目-------注释--------- for(int j = 1; jmA [j] .GetLength()){... exchange elements ... j - = 2; //索引... if(j <0)j = 0; } -------结束评论---------
if there will be used std::sort conditions are reliable checked by STL. And there will only be checked compare conditions on callback function.
如果将使用std :: sort条件由STL可靠检查。并且只会在回调函数上检查比较条件。
regards, Hubert Hermanutz
亲爱的,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];
...
}
First you have to fix the constructor. Although it is not necessarily wrong, you should not ask for input in the constructor. Add a separate function for Input()
instead
首先,你必须修复构造函数。虽然它不一定是错的,但你不应该在构造函数中要求输入。为Input()添加单独的函数
City& operator=(const City that)
{
name = that.name;
...
return*this;
}
This assignment operator is wrong. name
is a pointer, you don't want to assign the pointers (not in this scenario). You should be using the same method as before to copy the name instead:
这个赋值运算符是错误的。 name是一个指针,您不想分配指针(不在此方案中)。您应该使用与以前相同的方法来复制名称:
name = new char[strlen(ime) + 1];
strcpy(name, ime);
bubble sort should be done like the method in below. Also, you have added #included <string>
. You should be using std::string
instead!
冒泡排序应该像下面的方法一样。此外,您已添加#included
#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;
}
#2
0
manually sorting requires comparing of resorted entries ------- comment --------- for (int j = 1; jmA[j].GetLength()) { ... exchange elements ... j-=2; //index of ... if(j < 0) j=0; } ------- end comment ---------
手动排序需要比较使用的条目-------注释--------- for(int j = 1; jmA [j] .GetLength()){... exchange elements ... j - = 2; //索引... if(j <0)j = 0; } -------结束评论---------
if there will be used std::sort conditions are reliable checked by STL. And there will only be checked compare conditions on callback function.
如果将使用std :: sort条件由STL可靠检查。并且只会在回调函数上检查比较条件。
regards, Hubert Hermanutz
亲爱的,Hubert Hermanutz