可视化优雅C++代码:如何使用 while 循环和条件语句编写更高效的代码

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

本文关键字:代码 语句 条件 高效 while C++ 可视化 何使用 循环      更新时间:2023-10-16

你能给我一些关于如何简化代码的建议吗?

#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;
    }
  }
}