2017-12-08 8 views
-1

都市の名前(char型)、幅(double)、長さ(double)、高さ(double)シティ。私は動的な配列を作成する必要があります。これは、プログラムが起動するときに、デフォルトでCity()によって挿入されます。プログラムはメソッドoutput()を使用して、cities.mAの配列を挿入します。ソートを長さでソートする。私はコピーコンストラクタ、operator =を持っていて、都市を入力するためにダブルマックス(これは変数、現在の最大値を格納するバブルソートで使用される)を変換しなければならず、この目的でコンストラクタをパラメータ:City(double max)。問題はソートが機能しないことです。私は問題が1つのパラメータを持つコンストラクタの定義にあると思う(タイプdoubleをタイプ都市に変換する)。 {;手動仕分けC++でオブジェクトの動的配列をソートするのにバブルソートが機能しない

#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; 

} 
+0

「動作しません」はエラーの説明ではありません。あなたは何をするのですか?代わりに何をしますか? – SoronelHaetir

+0

バブルソートには2つのネストされたループがあります。 – molbdnilo

+0

値を入れ替えるには 'std :: swap'を使うべきです。今度はあなたの 'mA [j] = max;'は毎回 'max'から新しいオブジェクトを作成します(あなたが疑うコンストラクタを使って)。暗黙の変換を望まない場合は、 '明示的な都市(double max)'を作ることができます。 –

答えて

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; 
} 
0

コメント--------- (気象庁[J] .GetLength()INT J = 1)のための-------頼っエントリ の比較が必要 ...交換要素 ... j- = 2; //インデックス ... if(j < 0)j = 0; } -------最終コメント---------

std :: sort条件が使用される場合は、STLによって確実にチェックされます。 そして、コールバック関数の比較条件のみがチェックされます。

について、ヒューバートHermanutz

関連する問題