精美的C ++代码:如何使用while循环和条件语句编写更高效的代码

时间:2019-06-14 21:53:28

标签: c++ visual-c++ c++14

我不是完全有问题,但是希望编写复杂但行数尽可能少的代码。您能为我提供一些有关如何简化代码的建议吗?

我的程序可以运行,但是对我来说太笨拙了。我对此还很陌生,并且还有一条路要走。

#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种可能的用户名,以便检查和添加以方便测试。没有错误。

代码只是块状的。我想到了这个问题。我不在学校买不起,也没有被我申请的任何学校录取。对于提供计算机科学学位的在线学校有什么建议吗?

2 个答案:

答案 0 :(得分:1)

以下是一些建议:

结构数组不是并行数组

使用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')

答案 1 :(得分:0)

您拥有的大部分都是好东西。我将所有内容包装到一个函数中,并使用标准库中的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;
    }
  }
}