有没有大佬来指点一下
代码运行起来失败了
#include<iostream>
#include<ctime>
#include<fstream>
using namespace std;
char a[] = "abcdefghijklmnopqrstuvwxyz";
char b[] = "0123456789";
char c[] = "0123456789abcdefghijklmnopqrstuvwxyz";
char d[] = "0123456789abcdefghijklmnopqrstuvwxyz.,;':\"/<>?[]{}-_=+)(*&^%$#@!~`";
void generateRandomString1(char* str1, int length1)
{
for (int i = 0; i < length1 - 1; ++i)
{
int index1 = rand() % 26;
str1[i] = a[index1];
}
str1[length1 - 1] = '\0';
}
void generateRandomString2(char* str2, int length2)
{
for (int i = 0; i < length2 - 1; ++i)
{
int index2 = rand() % 10;
str2[i] = b[index2];
}
str2[length2 - 1] = '\0';
}
void generateRandomString3(char* str3, int length3)
{
for (int i = 0; i < length3 - 1; ++i)
{
int index3 = rand() % 36;
str3[i] = c[index3];
}
str3[length3 - 1] = '\0';
}
void generateRandomString4(char* str4, int length4)
{
for (int i = 0; i < length4 - 1; ++i)
{
int index4 = rand() % 66;
str4[i] = d[index4];
}
str4[length4 - 1] = '\0';
}
void delay_seconds(int duration)
{
int i;
for (i = 0; i < duration; i++)
{
time_t start_time = time(nullptr);
while (time(nullptr) - start_time < 1)
{
}
}
}
class User {
private:
char*name;
int score;
public:
User()
{
name = new char[1];
name[0] = '\0';
score = 0;
}
User(const char* str,int s)
{
int len = 0;
while (str[len] != '\0')
{
len++;
}
int length;
length = len;
name = new char[length + 1];
for (int i = 0; i < length; ++i)
{
name[i] = str[i];
}
name[length] = '\0';
score = s;
}
~User()
{
delete[]name;
}
const char* getName() const
{
return name;
}
int getScore() const
{
return score;
}
void writeToFile(ofstream& file) const
{
file << name << " " << score << endl;
}
void readFromFile(ifstream& file)
{
file >> name >> score;
}
};
int compareUsers(const User* p1, const User* p2)
{
return p1->getScore() > p2->getScore();
}
void sortUsers(User Users[], int size)
{
for (int i = 0; i < size - 1; ++i)
{
for (int j = 0; j < size - i - 1; ++j)
{
if (!compareUsers(&Users[j], &Users[j + 1]))
{
User temp = Users[j];
Users[j] = Users[j + 1];
Users[j + 1] = temp;
}
}
}
}
void saveUsersToFile(User Users[], int size, const char* filename)
{
ofstream file(filename);
if (file.is_open())
{
for (int i = 0; i < size; ++i)
{
Users[i].writeToFile(file);
}
file.close();
}
else {
cout << "无法打开文件进行写入。" << endl;
}
}
int loadUsersFromFile(User Users[], int maxSize, const char* filename)
{
ifstream file(filename);
int count = 0;
if (!file.is_open())
{
cout << "无法打开文件进行读取。" << endl;
return 0;
}
while (count < maxSize)
{
User tempUser;
tempUser.readFromFile(file);
if (file.fail())
{
break;
}
Users[count] = tempUser;
++count;
}
file.close();
return count;
}
void generateLeaderboard(User Users[], int size)
{
cout << "排行榜:" << endl;
for (int i = 0; i < size; ++i)
{
cout << i + 1 << ". " << Users[i].getName() << " - " << Users[i].getScore() << endl;
}
}
char*display_characters(void (*characters)(char* str, int length), int count, int duration)
{
cout << "\033[2J\033[H";
char*str5=new char[count+1];
characters(str5, count);
str5[count] = '\0';
cout <<"字符串为:"<< str5;
delay_seconds(duration);
cout << "\033[2J\033[H";
return str5;
}
char* get_user_input(int expected_length)
{
static char user_input[100];
cout << "请输入" << expected_length << "个字符:";
cin.getline(user_input, 100);
return user_input;
}
int compareStrings(const char* str1, const char* str2)
{
while (*str1 != '\0' && *str2 != '\0') {
if (*str1 != *str2) {
return 0;
}
str1++;
str2++;
}
return (*str1 == '\0' && *str2 == '\0');
}
int play_game(int difficulty_type, int duration)
{
void(*characters)(char* str, int length);
int score = 0;
int count = 2;
while (true)
{
if (difficulty_type == 1)
{
characters = generateRandomString1;
}
else if (difficulty_type == 2)
{
characters = generateRandomString2;
}
else if (difficulty_type == 3)
{
characters = generateRandomString3;
}
else if (difficulty_type == 4)
{
characters = generateRandomString4;
}
else
{
cout << "无效的难度类型" << std::endl;
return 0;
}
char*e=display_characters((*characters), count, duration);
char* user_input = get_user_input(count - 1);
if (compareStrings(user_input,e) != 0)
{
score = count-1;
++count;
}
else
{
break;
}
}
return score;
}
int main()
{
char name[100];
int score;
cout << "请输入用户名:";
cin.getline(name, 100);
srand(time(0));
int difficulty_type, duration;
cout << "1.纯字母" << endl << "2.纯数字" << endl << "3.字母数字混合" << endl << "4.全键盘字符混合" << endl;
cout << "请选择难度等级:";
cin >> difficulty_type;
cout << "请输入字符显示间隔时间:";
cin >> duration;
int scores = play_game(difficulty_type, duration);
User user(name, scores);
cout << endl << "你的成绩是:" << scores;
const int MAX_UserS = 1000;
User Users[MAX_UserS];
int UserCount = 0;
Users[UserCount++] =User(name, scores);
saveUsersToFile(Users,UserCount, "Users.txt");
int loadedCount = loadUsersFromFile(Users, MAX_UserS, "Users.txt");
sortUsers(Users, loadedCount);
cout << "是否生成排行榜(是请输入1;否请输入2):" ;
int p;
cin >> p;
if (p == 1)
{
generateLeaderboard(Users, loadedCount);
}
return 0;
}
代码运行起来失败了
#include<iostream>
#include<ctime>
#include<fstream>
using namespace std;
char a[] = "abcdefghijklmnopqrstuvwxyz";
char b[] = "0123456789";
char c[] = "0123456789abcdefghijklmnopqrstuvwxyz";
char d[] = "0123456789abcdefghijklmnopqrstuvwxyz.,;':\"/<>?[]{}-_=+)(*&^%$#@!~`";
void generateRandomString1(char* str1, int length1)
{
for (int i = 0; i < length1 - 1; ++i)
{
int index1 = rand() % 26;
str1[i] = a[index1];
}
str1[length1 - 1] = '\0';
}
void generateRandomString2(char* str2, int length2)
{
for (int i = 0; i < length2 - 1; ++i)
{
int index2 = rand() % 10;
str2[i] = b[index2];
}
str2[length2 - 1] = '\0';
}
void generateRandomString3(char* str3, int length3)
{
for (int i = 0; i < length3 - 1; ++i)
{
int index3 = rand() % 36;
str3[i] = c[index3];
}
str3[length3 - 1] = '\0';
}
void generateRandomString4(char* str4, int length4)
{
for (int i = 0; i < length4 - 1; ++i)
{
int index4 = rand() % 66;
str4[i] = d[index4];
}
str4[length4 - 1] = '\0';
}
void delay_seconds(int duration)
{
int i;
for (i = 0; i < duration; i++)
{
time_t start_time = time(nullptr);
while (time(nullptr) - start_time < 1)
{
}
}
}
class User {
private:
char*name;
int score;
public:
User()
{
name = new char[1];
name[0] = '\0';
score = 0;
}
User(const char* str,int s)
{
int len = 0;
while (str[len] != '\0')
{
len++;
}
int length;
length = len;
name = new char[length + 1];
for (int i = 0; i < length; ++i)
{
name[i] = str[i];
}
name[length] = '\0';
score = s;
}
~User()
{
delete[]name;
}
const char* getName() const
{
return name;
}
int getScore() const
{
return score;
}
void writeToFile(ofstream& file) const
{
file << name << " " << score << endl;
}
void readFromFile(ifstream& file)
{
file >> name >> score;
}
};
int compareUsers(const User* p1, const User* p2)
{
return p1->getScore() > p2->getScore();
}
void sortUsers(User Users[], int size)
{
for (int i = 0; i < size - 1; ++i)
{
for (int j = 0; j < size - i - 1; ++j)
{
if (!compareUsers(&Users[j], &Users[j + 1]))
{
User temp = Users[j];
Users[j] = Users[j + 1];
Users[j + 1] = temp;
}
}
}
}
void saveUsersToFile(User Users[], int size, const char* filename)
{
ofstream file(filename);
if (file.is_open())
{
for (int i = 0; i < size; ++i)
{
Users[i].writeToFile(file);
}
file.close();
}
else {
cout << "无法打开文件进行写入。" << endl;
}
}
int loadUsersFromFile(User Users[], int maxSize, const char* filename)
{
ifstream file(filename);
int count = 0;
if (!file.is_open())
{
cout << "无法打开文件进行读取。" << endl;
return 0;
}
while (count < maxSize)
{
User tempUser;
tempUser.readFromFile(file);
if (file.fail())
{
break;
}
Users[count] = tempUser;
++count;
}
file.close();
return count;
}
void generateLeaderboard(User Users[], int size)
{
cout << "排行榜:" << endl;
for (int i = 0; i < size; ++i)
{
cout << i + 1 << ". " << Users[i].getName() << " - " << Users[i].getScore() << endl;
}
}
char*display_characters(void (*characters)(char* str, int length), int count, int duration)
{
cout << "\033[2J\033[H";
char*str5=new char[count+1];
characters(str5, count);
str5[count] = '\0';
cout <<"字符串为:"<< str5;
delay_seconds(duration);
cout << "\033[2J\033[H";
return str5;
}
char* get_user_input(int expected_length)
{
static char user_input[100];
cout << "请输入" << expected_length << "个字符:";
cin.getline(user_input, 100);
return user_input;
}
int compareStrings(const char* str1, const char* str2)
{
while (*str1 != '\0' && *str2 != '\0') {
if (*str1 != *str2) {
return 0;
}
str1++;
str2++;
}
return (*str1 == '\0' && *str2 == '\0');
}
int play_game(int difficulty_type, int duration)
{
void(*characters)(char* str, int length);
int score = 0;
int count = 2;
while (true)
{
if (difficulty_type == 1)
{
characters = generateRandomString1;
}
else if (difficulty_type == 2)
{
characters = generateRandomString2;
}
else if (difficulty_type == 3)
{
characters = generateRandomString3;
}
else if (difficulty_type == 4)
{
characters = generateRandomString4;
}
else
{
cout << "无效的难度类型" << std::endl;
return 0;
}
char*e=display_characters((*characters), count, duration);
char* user_input = get_user_input(count - 1);
if (compareStrings(user_input,e) != 0)
{
score = count-1;
++count;
}
else
{
break;
}
}
return score;
}
int main()
{
char name[100];
int score;
cout << "请输入用户名:";
cin.getline(name, 100);
srand(time(0));
int difficulty_type, duration;
cout << "1.纯字母" << endl << "2.纯数字" << endl << "3.字母数字混合" << endl << "4.全键盘字符混合" << endl;
cout << "请选择难度等级:";
cin >> difficulty_type;
cout << "请输入字符显示间隔时间:";
cin >> duration;
int scores = play_game(difficulty_type, duration);
User user(name, scores);
cout << endl << "你的成绩是:" << scores;
const int MAX_UserS = 1000;
User Users[MAX_UserS];
int UserCount = 0;
Users[UserCount++] =User(name, scores);
saveUsersToFile(Users,UserCount, "Users.txt");
int loadedCount = loadUsersFromFile(Users, MAX_UserS, "Users.txt");
sortUsers(Users, loadedCount);
cout << "是否生成排行榜(是请输入1;否请输入2):" ;
int p;
cin >> p;
if (p == 1)
{
generateLeaderboard(Users, loadedCount);
}
return 0;
}