0

我是一名初学者程序员,在解决这个问题时遇到了问题。作业要求使用此方法(greaterAlpha()),但我不完全确定目的是什么。有人可以帮我完成这项工作并解释这种方法的用途吗?

我应该修复以前的分配以从文件中读取并用点打印名称和编号。**插件是排序函数,它应该使用更大的Alpha()方法按字母顺序对姓氏进行排序。毕竟,它应该使用相同的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;
}

我知道这是非常糟糕的编码,但这就是教练希望这样写的方式。任何帮助或建设性的批评将不胜感激。

4

1 回答 1

0

你只是问这个greaterAlpha方法是干什么用的?

如果是这样,您应该使用它来比较PhoneEntry您用于排序和搜索的两个实例<,即

void selectionSort(PhoneEntry[] entries, int elems) 
{
// ...
            PhoneEntry entry = entries[startScan];
// ... 
            if (entry.greaterAlpha(entries[index]))
//...  
} 

如果您已经使用std::strings 实现了二进制搜索,那么它也是一个非常简单的更改。

旁注:许多人更愿意将比较方法浓缩为

bool PhoneEntry::greaterAlpha (PhoneEntry& x) const 
{
    return  lastName > x.lastName
        || (lastName == x.lastName && firstName > x.firstName);
}
于 2013-10-03T14:12:58.230 回答