使用三个相同的姓氏(C ++)按姓氏组织姓名?

时间:2018-06-17 02:07:48

标签: c++

我有一个分配按名称按字母顺序对名称列表进行排序的分配。但是,有三个名字具有相同的姓氏,我不能让名字用姓氏按字母顺序排列。必须编写自己的冒泡排序或其他排序算法。我之所以选择bubblesort,是因为它是迄今为止我们学到的唯一一个。任何帮助表示赞赏。一切正常,除了正确的分类。

这是我的代码:

// my name
// Program 6
// This program will show a list of names in a variety of orders.


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int size = 25;

void showContacts_FNLN(string fnameArray[], string lnameArray[], int size);
void showContacts_LNFN(string lnameArray[], string fnameArray[], int size);
void reverseContacts_FNLN(string fnameArray[], string lnameArray[], int size);
void reverseContacts_LNFN(string fnameArray[], string lnameArray[], int size);
void searchLasname(string lnameArray[], string fnameArray[], int size);
void searchFirname(string fnameArray[], string lnameArray[], int size);
void bubbleSort(string lnameArray[], string fnameArray[], int size);

int main(int argc, const char * argv[])
{
    int count = 0;
    int ans;
    ifstream nameData;
    string lnameArray[size];
    string fnameArray[size];
    string lname, fname;

    nameData.open("names.txt");

    while(nameData >> fname >> lname)
    {
        fnameArray[count] = fname;
        lnameArray[count] = lname;
        count ++;
    }

    bubbleSort(lnameArray, fnameArray, size);

    while(ans != 9)
    {
        cout << "What would you like to do?" << endl;
        cout << "1) display contacts by first name and last name" << endl;
        cout << "2) display contacts by last name and first name" << endl;
        cout << "3) display contacts by first name and last name in reverse order" << endl;
        cout << "4) display contacts by last name and first name in reverse order" << endl;
        cout << "5) search for contact by last name" << endl;
        cout << "6) search for contact by first name" << endl;
        cout << "9) exit" << endl;
        cout << "Enter: ";
        cin >> ans;
        cout << endl;

        switch(ans)
        {
            case 1: showContacts_FNLN(fnameArray, lnameArray, size); // shows contacts in FN-LN order
                break;
            case 2: showContacts_LNFN(lnameArray, fnameArray, size); // LN-FN order
                break;
            case 3: reverseContacts_FNLN(fnameArray, lnameArray, size); // reversed FN-LN order
                break;
            case 4: reverseContacts_LNFN (fnameArray, lnameArray, size); // reversed LN-FN order
                break;
            case 5: searchLasname(lnameArray, fnameArray, size); // searches based on LN
                break;
            case 6: searchFirname(fnameArray, lnameArray, size); // searches based on FN
                break;
            case 9: cout << "Goodbye!" << endl;
                break;
        }
    }

    nameData.close();

    return 0;
}

void showContacts_FNLN(string fnameArray[], string lnameArray[], int size)
{
    for(int i=0; i<size; i++)
    {
        cout << fnameArray[i] << " " << lnameArray[i] << endl;
    }
    cout << endl;
}

void showContacts_LNFN(string lnameArray[], string fnameArray[], int size)
{
    for(int i=0; i<size; i++)
    {
        cout << lnameArray[i] << ", " << fnameArray[i] << endl;
    }
    cout << endl;
}

void reverseContacts_FNLN(string fnameArray[], string lnameArray[], int size)
{
    for(int i=(size-1); i>=0; i--)
    {
        cout << fnameArray[i] << " " << lnameArray[i] << endl;
    }
    cout << endl;
}

void reverseContacts_LNFN(string fnameArray[], string lnameArray[], int size)
{
    for(int i=(size-1); i>=0; i--)
    {
        cout << lnameArray[i] << ", " << fnameArray[i] << endl;
    }
    cout << endl;
}

void searchLasname(string lnameArray[], string fnameArray[], int size)
{
    int c = 0;
    string slnam;
    cout << "Enter a last name: ";
    cin >> slnam;

    for(int i=0; i<size; i++)
    {
        if(slnam==lnameArray[i])
        {
            cout << lnameArray[i] << ", " << fnameArray[i] << endl;
            c++;
        }
    }
    if (c == 0)
    {
        cout << "There is no match.";
        cout << endl;
    }
    cout << endl;
}

void searchFirname(string fnameArray[], string lnameArray[], int size)
{
    int c = 0;
    string sfnam;
    cout << "Enter a first name: ";
    cin >> sfnam;

    for(int i=0; i<size; i++)
    {
        if(sfnam==fnameArray[i])
        {
            cout << fnameArray[i] << " " << lnameArray[i] << endl;
            c++;
        }
    }
    if (c==0)
    {
        cout << "There is no match." << endl;
    }
    cout << endl; 
}

void bubbleSort(string lnameArray[], string fnameArray[], int size)
{
    string tmp, tmp2;
    //int count=0;


    for( int i = 1; i <= size - 1; i++ )
    {
        for( int j = 0; j < size - i; j++ )
        {
            //count++;
            if( lnameArray[j] > lnameArray[j+1] )
            {
                tmp = lnameArray[j];
                lnameArray[j] = lnameArray[j+1];
                lnameArray[j+1] = tmp;
            }
            if(lnameArray[j] == lnameArray[j+1])
            {
                if(fnameArray[j] > fnameArray[j+1])
                {
                    tmp = lnameArray[j];
                    lnameArray[j] = lnameArray[j+1];
                    lnameArray[j+1] = tmp;

                    tmp2 = fnameArray[j];
                    fnameArray[j] = fnameArray[j+1];
                    fnameArray[j+1] = tmp2;
                }
            }
        }
    }
    //cout << "count = " << count << endl;
}

2 个答案:

答案 0 :(得分:0)

你走了:

void bubbleSort(string lnameArray[], string fnameArray[], int size)
{
    for( int i = 0; i < size - 1; i++ )
    {
        for( int j = 0; j < size - i - 1; j++ )
        {
            string name1 = lnameArray[j] + fnameArray[j];
            string name2 = lnameArray[j + 1] + fnameArray[j + 1];
            if(name1.compare(name2) > 0)
            {
                string tmp = lnameArray[j];
                lnameArray[j] = lnameArray[j + 1];
                lnameArray[j + 1] = tmp;

                tmp = fnameArray[j];
                fnameArray[j] = fnameArray[j + 1];
                fnameArray[j + 1] = tmp;
            }
        }
    }
}

而不是另一个&#39;如果&#39;对于那些姓氏相似的人,我将姓氏和名字结合起来,然后用它进行比较。

我还在for( int j = 0; j < size - i; j++ )的代码中注意到您忘记在- 1之后添加size - i。在j == size - i - 1(假设当前为i == 0)之后,您使用j + 1来访问索引,这将导致分段错误,因为您正在访问超出其范围的索引。

答案 1 :(得分:0)

您的排序不起作用,因为您只更改姓氏的位置(除了两个姓氏相同的情况。)

您应该使用struct将这些信息存储在一个数组中。然后,您可以大量简化代码:

struct Person {
    Person() = default;
    Person(string firstname, string lastname) : firstname(std::move(firstname)), lastname(std::move(lastname)) {}
    string firstname;
    string lastname;
};

void showContacts_FNLN(Person personArray[], int size) {
    for (int i = 0; i < size; i++) {
        cout << personArray[i].firstname << " " << personArray[i].lastname << endl;
    }
    cout << endl;
}

void bubbleSort(Person personArray[], int size) {
    for (int i = 1; i <= size - 1; i++) {
        // The condition must size - i - 1 because otherwise personArray[j+1] is faulty
        for (int j = 0; j < size - i - 1; j++) {
            if (personArray[j].lastname > personArray[j+1].lastname
                || (personArray[j].lastname == personArray[j+1].lastname &&
                    personArray[j].firstname > personArray[j+1].firstname)) {
                auto tmp = std::move(personArray[j]);
                personArray[j] = std::move(personArray[j+1]);
                personArray[j+1] = std::move(tmp);
            }
        }
    }
}

int main(int argc, const char * argv[]) {
    int count = 0;
    ifstream nameData;
    Person personArray[size];
    string lname, fname;

    nameData.open("names.txt");

    while(nameData >> fname >> lname) {
        personArray[count] = Person(fname, lname);
        count++;
    }
    nameData.close();

    bubbleSort(personArray, size);

    showContacts_FNLN(personArray, size);

    return 0;
}

我从这个例子中删除了一些非必要的部分,以保持它很短但你应该明白这个想法。 (请注意,我的代码使用了C ++ 11移动语义,如果这让您感到困惑,只需删除它们。)

更多建议:

  • 请勿使用using namespace std;。 (互联网会告诉你原因。)
  • 如果可以,请使用std::array,这样就无需始终传递数组的大小,并允许您使用C ++ 11范围进行循环。