功能模块说明: 1、 登录:密码设置为“abc”,共三次输入密码机会,超过三次为登录不成功。 2、 选择项目:列出三个选项,1、猜数字。2、猜大小。3、退出。选择项目后,执行相应内容。 3、 猜数字:随机产生一个[0,99]间的整数,5次猜数字机会,没猜中会提示太大还是太小。 4、 猜大小:随机产生一个[1,6]间的整数,1-3为小,4-6为大,用户猜大小。 注:猜数字和猜大小模块后,回到选择项目模块。具体效果见执行文件。
时间: 2024-03-18 13:38:49 浏览: 23
好的,以下是对应的 C++ 语言代码:
```cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main() {
string password = "abc"; // 登录密码
string input; // 用户输入的密码
int count = 0; // 输入密码的次数
int choice; // 用户选择的项目
int guess; // 用户猜的数字
int number; // 随机产生的数字
int i; // 循环计数器
// 登录
while (count < 3) {
cout << "请输入密码:";
cin >> input;
if (input == password) {
cout << "登录成功!" << endl;
break;
} else {
count++;
cout << "密码错误,请重新输入!" << endl;
}
}
if (count == 3) {
cout << "密码输入错误次数超过三次,登录失败!" << endl;
return 0;
}
// 选择项目
while (1) {
cout << "请选择项目:" << endl;
cout << "1. 猜数字" << endl;
cout << "2. 猜大小" << endl;
cout << "3. 退出" << endl;
cin >> choice;
if (choice == 1) {
// 猜数字
srand((unsigned int)time(NULL)); // 随机数种子
number = rand() % 100; // 产生随机数
for (i = 0; i < 5; i++) {
cout << "请输入你猜的数字:";
cin >> guess;
if (guess == number) {
cout << "恭喜你猜对了!" << endl;
break;
} else if (guess > number) {
cout << "猜大了!" << endl;
} else {
cout << "猜小了!" << endl;
}
}
} else if (choice == 2) {
// 猜大小
srand((unsigned int)time(NULL)); // 随机数种子
number = rand() % 6 + 1; // 产生随机数
cout << "请猜大小:1-3为小,4-6为大:";
cin >> guess;
if ((guess <= 3 && number <= 3) || (guess > 3 && number > 3)) {
cout << "恭喜你猜对了!" << endl;
} else {
cout << "很遗憾,猜错了!" << endl;
}
} else if (choice == 3) {
// 退出
break;
} else {
cout << "选择错误,请重新选择!" << endl;
}
}
return 0;
}
```