拖更原因
CSP复赛(可能这是10月26日前最后一更了……)
小助手代码
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
// 显示带有动画效果的文本
void ccc(const string &s)
{
for (int i = 0; i < s.size(); i++)
{
cout << s[i];
Sleep(50);
}
cout << endl;
}
// 加密字符串(简单的移位加密)
string encrypt(string str, int shift = 4)
{
for (int i = 0; i < str.size(); i++)
{
str[i] += shift;
}
return str;
}
// 解密字符串
string decrypt(string str, int shift = 4)
{
for (int i = 0; i < str.size(); i++)
{
str[i] += shift;
if (str[i] > '9')
str[i] = 0;
}
return str;
}
// 保存用户名和密码到文件
void saveUser(const string &username, const string &password)
{
ofstream file("users.txt", ios::app);
if (file.is_open())
{
file << username << " " << encrypt(password) << endl;
file << "已经经过加密!";
file.close();
}
else
{
ccc("无法打开用户文件进行写入!请检查文件权限或磁盘空间。");
}
}
// 检查用户名和密码是否存在并正确
bool authenticateUser(const string &username, const string &password)
{
ifstream file("users.txt");
if (!file.is_open())
{
ccc("无法打开用户文件进行读取!请检查文件是否存在或权限是否正确。");
return false;
}
string savedUsername, savedPassword;
while (file >> savedUsername >> savedPassword)
{
if (savedUsername == username && savedPassword == encrypt(password))
{
return true;
}
}
return false;
}
// 检查用户名是否已存在
bool isUsernameExists(const string &username)
{
ifstream file("users.txt");
if (!file.is_open())
{
return false;
}
string savedUsername, savedPassword;
while (file >> savedUsername >> savedPassword)
{
if (savedUsername == username)
{
return true;
}
}
return false;
}
// 注册新用户
void registerUser()
{
string username, password;
ccc("请输入要注册的用户名:");
cin >> username;
while (isUsernameExists(username))
{
ccc("该用户名已存在,请重新输入。");
cin >> username;
}
while (true)
{
ccc("请输入要注册的密码:");
char ch;
password = "";
while ((ch = _getch()) != '\r')
{
if (ch == '\b')
{
if (!password.empty())
{
cout << "\b \b";
password.pop_back();
}
}
else
{
cout << '*';
password += ch;
}
}
cout << endl;
ccc("请再次输入密码确认:");
string confirmPassword = "";
while ((ch = _getch()) != '\r')
{
if (ch == '\b')
{
if (!confirmPassword.empty())
{
cout << "\b \b";
confirmPassword.pop_back();
}
}
else
{
cout << '*';
confirmPassword += ch;
}
}
cout << endl;
if (password == confirmPassword)
{
break;
}
else
{
ccc("两次输入的密码不一致,请重新输入。");
}
}
saveUser(username, password);
ccc("注册成功!");
Sleep(2000);
}
// 显示菜单
void showMenu()
{
ccc("欢迎使用小助手APP!");
ccc("1. 打开网站");
ccc("2. 显示北京时间");
ccc("3. 简单计算器");
ccc("4. 记事本");
ccc("5. 温度转换");
ccc("6. 打开驱动器");
ccc("7. 猜数游戏");
ccc("8. 更换字体颜色");
ccc("9. 退出");
}
// 处理打开网站功能
void openWebsite()
{
ccc("输入网站名:");
string s;
cin >> s;
map<string, string> websites =
{
{"YLCZ", "https://ylcz.top/"},
{"百度", "https://www.baidu.com/"},
{"CSDN", "https://www.csdn.net/"},
{"洛谷", "https://www.luogu.com/"},
{"上海月赛", "https://iai.sh.cn/"},
{"CHATGPT", "http://ai.qujiami.cn/"}
};
if (websites.count(s))
{
system(("start " + websites[s]).c_str());
}
else
{
ccc("啊哦,暂时没有这个网站呢TAT");
}
}
// 处理时间显示功能
void showTime()
{
SYSTEMTIME time;
while (true)
{
GetLocalTime(&time);
printf("%04d/%02d/%02d %02d:%02d:%02d\n", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond);
Sleep(1000);
system("cls");
}
}
// 处理简单计算器功能
void calculator()
{
ccc("简单计算器:");
double num1, num2;
char op;
ccc("请输入计算 (如: 1 + 1): ");
ccc("Tips:中间一定要加空格!");
ccc("输入1 e 1退出");
double result;
while (1)
{
cin >> num1 >> op >> num2;
if (num1 == 1 && op == 'e' && num2 == 1)
{
ccc("已退出!");
Sleep(2000);
break;
}
switch (op)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0)
{
result = num1 / num2;
}
else
{
ccc("除数不能为零!");
return;
}
break;
default:
ccc("无效的操作符!");
return;
}
cout << "结果是: " << result << endl;
Sleep(2000);
}
system("cls");
}
// 处理记事本功能
void notepad()
{
ccc("记事本功能:");
ccc("请输入内容,输入 'exit' 保存并退出:");
string line;
ofstream file("notepad.txt", ios::app);
if (!file.is_open())
{
ccc("无法打开记事本文件进行写入!请检查文件权限或磁盘空间。");
return;
}
cin.ignore(); // 清除之前的换行符
while (true)
{
getline(cin, line);
if (line == "exit")
{
break;
}
file << line << endl;
}
file.close();
ccc("内容已保存到 notepad.txt");
Sleep(2000);
}
// 打开驱动器功能
void openUplate()
{
ccc("请输入驱动器号(例如:E:):");
string driveLetter;
cin >> driveLetter;
if (driveLetter.length() == 2 && driveLetter[1] == ':')
{
string command = "start " + driveLetter + "\\";
system(command.c_str());
}
else
{
ccc("无效的驱动器号!");
}
}
// 处理温度转换功能
void temperatureConverter()
{
ccc("温度转换 (摄氏度<->华氏度):");
ccc("请输入温度值和单位(C/F),例如 32 C 或 100 F:");
double temp;
char unit;
cin >> temp >> unit;
if (unit == 'C' || unit == 'c')
{
double f = temp * 9 / 5 + 32;
cout << temp << " C = " << f << " F" << endl;
}
else if (unit == 'F' || unit == 'f')
{
double c = (temp - 32) * 5 / 9;
cout << temp << " F = " << c << " C" << endl;
}
else
{
ccc("无效的温度单位!");
}
Sleep(2000);
}
// 猜数游戏功能
void guessNumberGame()
{
srand(static_cast<unsigned int>(time(nullptr)));
int targetNumber = rand() % 100 + 1;
int guess;
int attempts = 0;
ccc("欢迎来到猜数游戏!我已经想好了一个 1 到 100 之间的数字,你来猜猜看。");
do
{
ccc("请输入你的猜测(1-100):");
cin >> guess;
attempts++;
if (guess < targetNumber)
{
ccc("你的猜测太小了!");
}
else if (guess > targetNumber)
{
ccc("你的猜测太大了!");
}
Sleep(2000);
system("cls");
}
while (guess != targetNumber);
ccc("恭喜你猜对了!你用了" + to_string(attempts) + "次。");
Sleep(2000);
}
void wordcolor()
{
int color;
ccc("请问您想用什么字体颜色?");
ccc("1.蓝色");
ccc("2.绿色");
ccc("3.浅绿色");
ccc("4.红色");
ccc("5.紫色");
ccc("6.黄色");
ccc("7.灰色");
ccc("8.淡蓝色");
ccc("9.淡绿色");
ccc("10.淡浅绿色");
ccc("11.淡红色");
ccc("12.淡紫色");
ccc("13.淡黄色");
ccc("14.亮白色");
cin >> color;
switch (color)
{
case 1:
system("color 01");
ccc("设置成功!");
Sleep(2000);
break;
case 2:
system("color 02");
ccc("设置成功!");
Sleep(2000);
break;
case 3:
system("color 03");
ccc("设置成功!");
Sleep(2000);
break;
case 4:
system("color 04");
ccc("设置成功!");
Sleep(2000);
break;
case 5:
system("color 05");
ccc("设置成功!");
Sleep(2000);
break;
case 6:
system("color 06");
ccc("设置成功!");
Sleep(2000);
break;
case 7:
system("color 08");
ccc("设置成功!");
Sleep(2000);
break;
case 8:
system("color 09");
ccc("设置成功!");
Sleep(2000);
break;
case 9:
system("color 0A");
ccc("设置成功!");
Sleep(2000);
break;
case 10:
system("color 0B");
ccc("设置成功!");
Sleep(2000);
break;
case 11:
system("color 0C");
ccc("设置成功!");
Sleep(2000);
break;
case 12:
system("color 0D");
ccc("设置成功!");
Sleep(2000);
break;
case 13:
system("color 0E");
ccc("设置成功!");
Sleep(2000);
break;
case 14:
system("color 0F");
ccc("设置成功!");
Sleep(2000);
break;
default:
ccc("没有这个字体颜色哦~");
break;
}
system("cls");
}
// 主函数
int main()
{
int a;
ccc("Welcome!");
ccc("By XuJunRui");
Sleep(3000);
string name, password, choice;
while (true)
{
system("cls");
cout << "_________________________" << endl;
cout << "| |" << endl;
cout << "| |" << endl;
cout << "| 登录/注册 |" << endl;
cout << "| |" << endl;
cout << "|_______________________|" << endl;
ccc("1. 登录");
ccc("2. 注册");
cin >> choice;
if (choice == "1")
{
ccc("请输入用户名:");
cin >> name;
ccc("请输入密码:");
char ch;
password = "";
while ((ch = _getch()) != '\r')
{
if (ch == '\b')
{
if (!password.empty())
{
cout << "\b \b";
password.pop_back();
}
}
else
{
cout << '*';
password += ch;
}
}
cout << endl;
if (authenticateUser(name, password))
{
ccc("登录成功!");
Sleep(1000);
system("cls");
while (true)
{
showMenu();
cin >> a;
system("cls");
switch (a)
{
case 1:
openWebsite();
break;
case 2:
showTime();
break;
case 3:
calculator();
break;
case 4:
notepad();
break;
case 5:
temperatureConverter();
break;
case 6:
openUplate();
break;
case 7:
guessNumberGame();
break;
case 8:
wordcolor();
break;
case 9:
ccc("再见!");
return 0;
default:
ccc("无效选项,请重新选择。");
break;
}
system("cls");
}
}
else
{
ccc("用户名或密码错误!");
Sleep(2000);
}
}
else if (choice == "2")
{
registerUser();
}
else
{
ccc("无效选项,请重新选择。");
Sleep(2000);
}
}
return 0;
}
更新日志:
1.0.0 小助手出现了
1.0.1 新增"打开U盘"功能
1.0.2 解决了"打开U盘"功能中"找不到此驱动"的现象
1.0.3 1、打开小助手页面更新了
2、增加了"猜数游戏"功能
3、在输入密码时变成"*",保证了隐蔽性
4、保证了不会有重复账户
5、在"users"(账户保存文件)中,将密码进行加密
6、修复了已知bug
1.0.4 在计算器功能中更改了输入与输出
添加了更改字体颜色功能