请将上述写成c语言代码
时间: 2023-07-31 08:11:14 浏览: 32
好的,以下是对应的 C 语言代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char password[4] = "abc"; // 登录密码
char input[4]; // 用户输入的密码
int count = 0; // 输入密码的次数
int choice; // 用户选择的项目
int guess; // 用户猜的数字
int number; // 随机产生的数字
int i; // 循环计数器
// 登录
while (count < 3) {
printf("请输入密码:");
scanf("%s", input);
if (strcmp(input, password) == 0) {
printf("登录成功!\n");
break;
} else {
count++;
printf("密码错误,请重新输入!\n");
}
}
if (count == 3) {
printf("密码输入错误次数超过三次,登录失败!\n");
return 0;
}
// 选择项目
while (1) {
printf("请选择项目:\n");
printf("1. 猜数字\n");
printf("2. 猜大小\n");
printf("3. 退出\n");
scanf("%d", &choice);
if (choice == 1) {
// 猜数字
srand((unsigned int)time(NULL)); // 随机数种子
number = rand() % 100; // 产生随机数
for (i = 0; i < 5; i++) {
printf("请输入你猜的数字:");
scanf("%d", &guess);
if (guess == number) {
printf("恭喜你猜对了!\n");
break;
} else if (guess > number) {
printf("猜大了!\n");
} else {
printf("猜小了!\n");
}
}
} else if (choice == 2) {
// 猜大小
srand((unsigned int)time(NULL)); // 随机数种子
number = rand() % 6 + 1; // 产生随机数
printf("请猜大小:1-3为小,4-6为大:");
scanf("%d", &guess);
if ((guess <= 3 && number <= 3) || (guess > 3 && number > 3)) {
printf("恭喜你猜对了!\n");
} else {
printf("很遗憾,猜错了!\n");
}
} else if (choice == 3) {
// 退出
break;
} else {
printf("选择错误,请重新选择!\n");
}
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)