簡體   English   中英

優雅的 C++ 代碼:如何使用 while 循環和條件語句編寫更高效的代碼

[英]Elegant C++ Code: How to write more efficient code using while loops and conditional statements

你能給我一些關於如何簡化代碼的建議嗎?

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

using namespace std; 



int main() {

    string current_users[5];
    string new_users[5], new_user;
    ifstream read;


    read.open("current.txt");

    for (int index = 0; index < 5; index++) {
        read >> current_users[index];

    }

    read.close();


    cout << "Enter a username: ";
    cin >> new_user;


    char user_choice;
    int index = 0, new_index = 0;
    while (index <= 5) {


        if (new_user == current_users[index]) {
            cout << "That username already exists."
                << " Enter a different username: ";

            cin >> new_user;
            index = 0;
            continue;
        }


        if (index < 5)
            index++;


        else {
            new_users[new_index] = new_user;
            cout << "\nWelcome " << new_user << endl;
            new_index++;


             if (new_index < 5) {
               cout << "Would you like to register another user?:" 
                            <<"'Y' for yes or 'N' for no";
                cin >> user_choice;
            }


            if (user_choice == 'Y' || user_choice == 'y') {
                cout << "\nEnter a new username: ";
                cin >> new_user;
                index = 0;
            }

            else
              break;

        }

    }//end of while 

    system("pause");
    return 0;

}

該程序要求用戶輸入用戶名並檢查該用戶名是否已存在。 如果存在,它會提示用戶使用不同的用戶名,同時檢查該用戶名是否已經存在。 如果用戶名是唯一的,程序會歡迎新用戶並詢問用戶是否想注冊另一個新用戶(很奇怪,但我想嘗試一下)。 如果用戶想將另一個用戶添加到“網站”,那么程序會再次運行,檢查冗余。 為了便於測試,我將此程序限制為 5 個可能的用戶名以進行檢查和添加。 沒有錯誤。

代碼很粗。 我想出了這個問題。 我不在學校。 負擔不起,也沒有被我申請的任何學校錄取。 對提供計算機科學學位的在線學校有什么建議嗎?

以下是一些建議:

結構陣列不是平行陣列

使用結構的std::vector而不是並行數組:

struct Record
{
    std::string  new_user;
    std::string  current_user;
};
std::vector<Record> database;

使用數據緩存的處理器喜歡將它們的元素靠近在一起。 在這里, new_user[0]將在緩存中的current_user[0]旁邊。

使用並行數組, new_users[0]就在current_user[4]旁邊; 所以處理器必須經過 4 個元素才能到達第一個new_users元素。

循環展開

您可以消除用於讀取值的for循環:

read >> current_users[0];
read >> current_users[1];
read >> current_users[2];
read >> current_users[3];
read >> current_users[4];

這消除了與for循環相關的開銷。

在比較之前轉換為全部小寫或全部大寫

您可以通過在比較之前轉換為大寫或小寫來減少比較次數:

if (std::toupper(user_choice) == 'Y')

你所擁有的大部分都是好的。 我會將所有內容都包裝到一個函數中,並使用標准庫中的std::find來查找重復項。

template<std::size_t N, std::size_t M>
void GetUsers( std::string (&new_users)[N], std::string const (&current_users)[M] ) {
  int idx = 0;
  while (idx < 5) {
    std::cout << "Enter a username: ";
    std::string user; std::cin >> user;
    if (std::find(current_users.begin(), current_users.end(), user) != current_users.end()) {
      std::cout << "That username already exists.\n";
      continue;
    } else {
      new_users[idx++] = user;
      if (idx < 5) {
        std::cout << "Would you like to register another user? [Y/n]: ";
        if (std::tolower(std::cin.get()) == 'y') {
          continue;
        }
      }
      break;
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM