宿題のために、このプログラムをテキストファイルから読み込んで、オブジェクト配列に入れ、バブルソートを使って配列をソートする必要があります。バブルソートでは、PhoneEntryのalphaGreat()関数を使用する必要があります。私はそれがうまくいった。この問題は、lastNameと渡されたオブジェクトの姓を比較する必要があるときに、私のalphaGreat関数で発生します。これをどうやって行うのですか?これはC++プログラム、オブジェクト変数
ドライバー
#include <iostream>
#include <fstream>
#include <string>
#include "phoneEntry.h"
using namespace std;
void mySort(double array[], int size)
{
double temp;
bool inOrder = false;
for (int i = size - 1; i > 0 && !inOrder; i--)
{
inOrder = true;
for (int j = 0; j < i; j++)
{
if (array[j] > array[j + 1])
{
inOrder = false;
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
};
int main()
{
const int MAXNUM = 500;
PhoneEntry entry[MAXNUM];
ifstream filezilla;
filezilla.open("phone.txt");
int count = 0;
if(filezilla)
{
while(count < MAXNUM && entry[count].readEntry(filezilla))
{
count++;
}
mySort(entry, count);
for(int i = 0; i < count; i++)
{
entry[i].writeEntry(cout) << endl;
}
}
else
{
cout << "Four Oh Four - File Not Found" << endl;
}
return 0;
}
電話エントリ
#include <iostream>
#include <fstream>
#include <string>
#include "phoneNumber.h"
using namespace std;
class PhoneEntry
{
private:
PhoneNumber phone;
string firstName,
lastName;
void _writeDots(ostream& fout, int n) const;
bool alphaGreater(const PhoneEntry&) const;
public:
istream& readEntry(istream&);
ostream& writeEntry(ostream&) const;
};
istream& PhoneEntry::readEntry(istream& Sin)
{
Sin >> firstName >> lastName;
phone.readNumber(Sin);
return Sin;
};
///////////////////////////////////////////
ostream& PhoneEntry::writeEntry(ostream& Sout) const
{
const int num = 28;
Sout << firstName << ", " << lastName;
int fill = num - (firstName.length() + lastName.length());
_writeDots(Sout, fill);
phone.writeNumber(Sout);
return Sout;
};
///////////////////////////////////////////
void PhoneEntry::_writeDots(ostream& fout, int n) const
{
if(n % 2)
fout << " .";
else
fout << ".";
for(int i = 0; i < n/2; i++)
{
fout << " .";
}
};
bool PhoneEntry::alphaGreater(const PhoneEntry& item) const
{
bool alpha = false;
// I have first and last names reversed in my entire program
if(item.compare(firstName) == 0)
{
if(item < lastName)
{
alpha = true;
}
else
{
alpha = false;
}
}
else if(item < firstName)
{
alpha = true;
}
else
{
alpha = false;
}
return alpha;
}
電話番号
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class PhoneNumber
{
private:
int areaCode,
prefix,
suffix;
public:
istream& readNumber(istream&);
ostream& writeNumber(ostream&) const;
};
istream& PhoneNumber::readNumber(istream& Sin)
{
Sin >> areaCode >> prefix >> suffix;
return Sin;
};
///////////////////////////////////////////
ostream& PhoneNumber::writeNumber(ostream& Sout) const
{
Sout << areaCode << "-" << prefix << "-" << suffix;
return Sout;
};
...今のところ私のためにハードプログラムされています10
テキストファイル
Fleischman Robert 336 746 5525
Rorie Tim 336 746 6915
Johnson Gary 336 746 6985
Hoyt James 336 781 2668
Krass Mike 606 393 5355
Rust James 606 393 5525
Sekutera Norman 606 746 3635
Kennedy Amy 606 746 3985
Behrends Leonard 606 746 4235
Rueter Clarence 606 746 4395
Rorie Lonnie 606 746 4525
Beck Keith 606 746 4655
Bornemeier Eugenia 606 746 4775
Fleischman Arlo 606 746 4915
Rorie Scott 606 746 4985
Elliott Alice 712 746 2166
Keller Thomas 712 746 2196
Halvorsen Lorenz 712 746 2211
Slominski Greg 712 872 2006
Blunt Ed 712 872 2041
Bailei David 712 872 2051
Krouse John 712 872 2063
Furgerson Glen 712 872 2064
Behii Michelle 712 872 2123
Harris Mark 712 872 2124
Ropers Lorens 712 872 2164
Krejdl Wayne 804 274 2695
Kitchen Pat 804 274 2805
Jones Terry 804 274 2865
Schulz Harvey 804 274 3145
Griffith Joe 804 274 3165
Sailors Loretta 804 274 3225
Johnston Jeffrey 804 274 3285
Jicha James 804 274 3485
Sailors Eugene 804 393 5525
Hanshaw Harold 804 393 5565
Fischer Francis 804 393 6065
Bogenreit Charles 804 393 6155
John Ted 804 393 6175
Fischer William 804 393 6225
Schweitzer Fred 804 746 2285
Clements Richard 804 746 2340
Kuehn Oral 804 746 2635
Huxbold Kevin 804 746 2665
Bornemeier Dan 804 746 2685
Root Howard 804 746 3535
Hanshaw Gary 804 746 5525
Sheard Donald 804 746 6355
Smith Brett 804 746 6595
Schreiner James 804 746 6645
右は、起こってたくさんあります。主なものは、alphaGreaterに渡されたオブジェクト変数firstName
にアクセスする方法を知る必要があることです。前もって感謝します!このような
大きな壁のテキスト? -1 – Puppy
これ以外の方法でコードブロックを表示するのですか? –