【问题标题】:How to alphabetize a list on C++如何在 C++ 上按字母顺序排列列表
【发布时间】:2015-04-23 19:30:52
【问题描述】:

我的最终项目的代码遇到了一些问题。我到处都看过,我很难过,所以我想我会在这里问。我需要确保当所有姓名都列在此电话簿中时,它们会按字母顺序出现,但到目前为止我还不确定如何做到这一点。这是我目前拥有的程序!谢谢!

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

struct Contact {
   string name, number, notes;
};

Contact contactList[100];
int rec_num = 0;
int num_entries;

string toUpper (string S) {
   for (int i= 0; i < S.length(); i++)
      S[i] = toupper(S[i]);
   return S;
}

void ReadFile () {
   string S;
   fstream input("PhoneData.txt");
   while (!input.eof() && !input.fail()){
      input >> contactList[rec_num].name >> contactList[rec_num].number;
      getline(input, S);
      contactList[rec_num].notes = S;
      rec_num++;
   }
   cout << "Book read." << endl;
   num_entries = rec_num;
   input.close();
   return;
}

// stores phonebook for future runs of the program
void StoreFile () {
   fstream F ("PhoneData.txt");
   rec_num = 0;

   while (rec_num < num_entries){
      F << contactList[rec_num].name << " " << contactList[rec_num].number << " " << contactList[rec_num].notes  <<  " " << endl;
      rec_num++;
   }
   cout << "Phonebook stored." << endl;
   return;
}

// adds contact
void add_name(string name, string number, string notes){
   contactList[num_entries].name = name;
   contactList[num_entries].number = number;
   contactList[num_entries].notes = notes;
   num_entries++;
   return;
}

// finds contact
void retrieve_name(string name){
   for (int i = 0; i < num_entries; i++){
      if (toUpper(contactList[i].name) == toUpper(name)) {
         cout << "Phone Number: " << contactList[i].number << endl << "Notes: " << contactList[i].notes << endl;
         return;
      }
   }
   cout << "Name not found" << endl;
   return;
}

// updates contact info
void update_name(string name){
   string new_number;
   string new_notes;
   cout<<"New Phone Number"<<endl;
   cin>> new_number;
   cout<<"New Notes"<<endl;
   cin>> new_notes;
   for (int i = 0; i < num_entries; i++){
      if (toUpper(contactList[i].name) == toUpper(name)) {
         contactList[i].number = new_number;
         contactList[i].notes = new_notes;
         return;
      }
   }
}

// deletes contact
void delete_name(string name){
   int INDEX=0;
   for (int i = 0; i < num_entries; i++){
      if (toUpper(contactList[i].name) == toUpper(name)) {
         INDEX=i;
         for ( int j=INDEX; j < num_entries; j++ ){
            contactList[j].name = contactList[j+1].name;
            contactList[j].number = contactList[j+1].number;
            contactList[j].notes = contactList[j+1].notes;
         }
      }
   }
   return;
}

void listAllContacts() {
   int i = 0;
   while (i < num_entries) {
      cout << "-- " << contactList[i].name << " " << contactList[i].number << endl << "-- " << contactList[i].notes << endl << endl;
      i++;
   }
}

int main(){
   string name, number, notes;
   string FileName;
   char command;
   FileName = "PhoneData.txt";
   ReadFile ();
   cout << "Use \"e\" for enter, \"f\" for find, \"l\" for list, \"d\" for delete, \"u\" for update, \"s\" for send message, \"q\" to quit." << endl << "Command: ";
   cin >> command;
   while (command != 'q'){
      switch (command){
         case 'e': cin >> name; cout << "Enter Number: ";
                   cin >> number; cout << "Enter Notes: ";
                   cin.ignore(); getline(cin, notes);
                   add_name(name, number, notes); break;
         case 'f': cin >> name; retrieve_name(name); break;
         case 'l':
                   listAllContacts(); break;
         case 'u': cin>> name; update_name (name);break;
         case 'd' : cin>> name; delete_name (name); break;
      }
      cout << "\nCommand: "; cin >> command;
   }
   StoreFile();
   cout << "All set !";
   return 0;
}

【问题讨论】:

  • 我不认为你到处都看过。我搜索了 'alphabetize a list on C++' 并找到了一些解决方案。
  • 你能给我一个链接到那些将不胜感激的 trobbins 之一
  • 你说的另一个是C#而不是C++,但谢谢!兔兔大举菊
  • @tutuDajuju 我不认为她在使用列表。 (或 C#...)

标签: c++ alphabetical-sort alphabetized


【解决方案1】:

给定

Contact contactList[100];
int num_entries;

您可以使用std::sort 对联系人列表进行排序。 std::sort 有两种形式。在第一种形式中,您可以使用:

std::sort(contanctList, contactList+num_entries);

如果您为Contact 对象定义operator&lt;

在第二种形式中,你可以使用:

std::sort(contanctList, contactList+num_entries, myCompare);

如果您将myCompare 定义为可以比较两个Contact 对象的可调用对象。

要使用第一种形式,请将Contact 更改为:

struct Contact {
   string name, number, notes;
   bool operator<(Contact const& rhs) const
   {
      return (this->name < rhs.name);
   }
};

如果您希望名称的比较不区分大小写,请将两个名称都转换为大写或小写,然后进行比较。

【讨论】:

  • 我的问题实际上是重复的,这里是我的问题的更好答案的链接! stackoverflow.com/questions/5569109/…
  • @HannahStang,这个问题确实是重复的。但是,我没有看到更好的答案。如果其中一个答案让您感觉更好,那很好:)
猜你喜欢
  • 2020-03-29
  • 1970-01-01
  • 2018-09-15
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
相关资源
最近更新 更多