可视化优雅C++代码:如何使用 while 循环和条件语句编写更高效的代码
visual 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 (¤t_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;
}
}
}
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 返回语句后的代码,没有警告
- 为什么在此C++代码中触发此 if 语句?
- 什么可以用来替换代码中的循环和 if 语句?
- 在大括号内添加语句会更改代码功能?
- 试图避免在 for 循环中出现 if-else 语句,但代码似乎有一些错误
- 如何在此代码中包含 While 语句
- 条件不变的 if 语句会减慢我的C++代码吗?
- 我如何在一个 if 语句中声明所有数字我尝试通过其他方式声明所有数字,如果一个接一个,但似乎代码有逻辑错误
- 有人能在我的代码中找到与C++中的IF语句相关的问题吗
- 代码样式:在 switch/if 语句的分支中重用控件表达式或控制变量
- 我的代码应该接受一个数字,并返回字母等级或"Grade is not valid"但 else 语句不起作用
- 为什么我的代码在第一个 if 语句处中断?
- 当您希望在Arduino Uno编程中同时执行不同函数时,使用什么代码/语句
- C - 如果语句问题.代码将不会运行其他,否则
- 可视化优雅C++代码:如何使用 while 循环和条件语句编写更高效的代码
- 当执行此代码中的第二个语句时,内部会发生什么
- 如果语句在我的代码中不起作用
- 我很难为 if/else 语句编写此赋值的代码.这是第一堂编程课
- MSVC编译器拆卸代码来自if/else语句