我是一個初學者程序員,並有解決此問題的問題。該任務是要求使用此方法(greaterAlpha()),但我不完全確定目的是什麼。有人可以幫助我解決這個問題,並解釋這種方法的用途嗎?C++排序和二進制搜索與數組問題
我應該修復以前的作業,從文件中讀取,然後用點打印名稱和數字。 **加載項是排序函數,它應該使用greaterAlpha()方法按字母順序對姓氏進行排序。畢竟,它應該循環使用一個鍵來使用相同的greaterAlpha()方法進行二分搜索。 (二進制搜索中的排序爲它的比較中使用,如果發現必須返回元素的下標,最後測試元素的下標負,如果不。)
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
//*********************************************************************
class PhoneNumber
{
private:
int areaCode,
prefix,
lineNumber;
public: // the methods are public
void setNumber(int area, int pre, int num)
{
areaCode = area;
prefix = pre;
lineNumber = num;
}
int getAreaCode() const
{
return areaCode;
}
int getPrefix() const
{
return prefix;
}
int getLineNumber() const
{
return lineNumber;
}
istream& readPhone(istream&); // input/output functions
ostream& printPhone(ostream&) const;
};
//*********************************************************************
// Returns istream& so it can be used to overload the >> operator later
istream& PhoneNumber::readPhone(istream& fin)
{
return fin >> areaCode >> prefix >> lineNumber;
}
//*********************************************************************
// Returns ostream& so it can be used to overload the << opeator later
ostream& PhoneNumber::printPhone(ostream& fout) const
{
fout << setw(4) << areaCode << "-" << prefix << "-" << lineNumber;
return fout;
}
//*********************************************************************
class PhoneEntry
{
private:
string lastName,
firstName;
PhoneNumber Number;
public:
void setNames(string last, string first)
{
lastName = last;
firstName= first;
}
string getLastName()
{
return lastName;
}
string getFirstName()
{
return firstName;
}
istream& readEntry(istream&);
ostream& writeDots(ostream&, int);
ostream& printEntry(ostream&);
bool greaterAlpha(PhoneEntry&) const;
void selectionSort(string, int);
};
//*********************************************************************
istream& PhoneEntry::readEntry(istream& fin)
{
fin >> lastName >> firstName;
Number.readPhone(fin);
return fin;
}
//*********************************************************************
ostream& PhoneEntry::writeDots(ostream& fout, int length)
{
length = firstName.length();
length += lastName.length();
length += 2;
//This line is causing the issue but WHY?
if(length % 2 == 1)
{
fout << ".";
}
for(length; length <= 28; length++)
{
fout << " .";
length++;
}
return fout;
}
//*********************************************************************
ostream& PhoneEntry::printEntry(ostream& fout)
{
int length = 0;
fout << lastName << ", " << firstName;
writeDots(fout, length);
Number.printPhone(fout);
return fout;
}
//*********************************************************************
bool PhoneEntry::greaterAlpha (PhoneEntry& x) const
{
bool flag;
if (lastName > x.lastName)
flag = true;
else if ((lastName == x.lastName) && (firstName > x.firstName))
flag = true;
else
flag = false;
return flag;
}
//*********************************************************************
void selectionSort(string name[], int elems)
{
int startScan, minIndex;
string strName;
for (startScan = 0; startScan < (elems - 1); startScan++)
{
minIndex = startScan;
strName = name[startScan];
for(int index = startScan + 1; index < elems; index++)
{
if (name[index] < strName)
{
strName = name[index];
minIndex = index;
}
}
name[minIndex] = name[startScan];
name[startScan] = strName;
}
}
//*********************************************************************
int main()
{
int count;
const int MAX_CAPACITY = 500;
PhoneEntry Entry[MAX_CAPACITY],
EntryIn;
bool flag = false;
ifstream dataIn("phonenum.txt");
if(!dataIn)
{
perror("phonenum.txt");
exit(1);
}
count = 0;
while (count < MAX_CAPACITY && EntryIn.readEntry(dataIn))
{
Entry[count++] = EntryIn;
}
dataIn.close();
if (count == MAX_CAPACITY)
{
cerr << "Reached maximum capacity of " << MAX_CAPACITY << ", can't read anymore Phone Entries currently." << endl;
}
//EntryIn.greaterAlpha(Entry);
selectionSort(Entry, count);
if (flag)
{
for (int i = 0; i < count; i++)
{
Entry[i].printEntry(cout) << endl;
}
}
else
cerr << "Error with sort!" << endl;
return 0;
}
我知道這是編碼可怕的是,這是教練如何想寫這個。任何援助或建設性的批評將非常感激。
相當多的代碼的問題...你嘗試過將它縮小? – LihO
使用一個非常小的數據集(即只有幾個數據條目)並在調試器中遍歷代碼。它會幫助你縮小問題的可能範圍。 –
我不清楚問題是什麼。 – doctorlove