12.57 #1

Merged
hnu202326010226 merged 0 commits from develop into main 5 months ago

@ -1,2 +0,0 @@
# louis_program

@ -0,0 +1,321 @@
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
// 定义用户结构
typedef struct {
char username[20];
char password[10];
char userType[10];
} User;
// 函数声明
void initializeUsers(User users[]);
int loginSystem(char *currentUserType);
void showMainMenu(const char *currentUserType);
int handleCommand(const char *command, char *currentUserType);
int switchUserType(const char *command, char *currentUserType);
void generatePaper(const char *userType);
int getQuestionCount();
void generateQuestions(const char *userType, int count);
void clearInputBuffer();
void trimString(char *str);
int isNumber(const char *str);
int main() {
// 设置控制台为UTF-8编码解决中文乱码
SetConsoleOutputCP(65001);
char currentUserType[10] = "";
// 主程序循环
while (1) {
// 调用登录系统函数
if (!loginSystem(currentUserType)) {
printf("登录失败,程序退出\n");
break;
}
// 登录成功后进入主菜单
showMainMenu(currentUserType);
// 命令处理循环
char command[50];
int shouldLogout = 0;
while (!shouldLogout) {
printf("> ");
// 读取整行输入
if (fgets(command, sizeof(command), stdin) == NULL) {
shouldLogout = 1;
break;
}
// 去除换行符和前后空格
trimString(command);
// 检查是否为空输入
if (strlen(command) == 0) {
continue;
}
// 处理命令
int result = handleCommand(command, currentUserType);
if (result == -1) {
shouldLogout = 1; // 退出当前用户,重新登录
} else if (result == 1) {
// 命令处理成功,继续循环
}
}
printf("退出当前用户,返回登录界面...\n\n");
}
printf("程序结束,按任意键退出...");
getchar();
return 0;
}
// 初始化用户数据
void initializeUsers(User users[]) {
User presetUsers[9] = {
{"张三1", "123", "小学"},
{"张三2", "123", "小学"},
{"张三3", "123", "小学"},
{"李四1", "123", "初中"},
{"李四2", "123", "初中"},
{"李四3", "123", "初中"},
{"王五1", "123", "高中"},
{"王五2", "123", "高中"},
{"王五3", "123", "高中"}
};
for (int i = 0; i < 9; i++) {
users[i] = presetUsers[i];
}
}
// 登录系统函数
int loginSystem(char *currentUserType) {
User users[9];
initializeUsers(users);
printf("=== 数学老师出题系统 ===\n");
printf("请输入用户名和密码(用空格隔开):\n");
char username[20], password[10];
int authenticated = 0;
int attempts = 0;
// 登录验证
while (!authenticated && attempts < 3) {
printf("> ");
scanf("%s %s", username, password);
clearInputBuffer();
// 验证用户
for (int i = 0; i < 9; i++) {
if (strcmp(users[i].username, username) == 0 &&
strcmp(users[i].password, password) == 0) {
authenticated = 1;
strcpy(currentUserType, users[i].userType);
break;
}
}
if (authenticated) {
printf("当前选择为 %s 出题\n", currentUserType);
return 1;
} else {
attempts++;
printf("请输入正确的用户名、密码\n");
if (attempts < 3) {
printf("请重新输入用户名和密码:\n");
} else {
printf("登录失败次数过多\n");
}
}
}
return 0;
}
// 显示主菜单
void showMainMenu(const char *currentUserType) {
printf("\n登录成功!当前选择为 %s 出题\n", currentUserType);
printf("系统提示:准备生成 %s 数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录\n", currentUserType);
printf("\n可用命令:\n");
printf("- 输入数字:生成指定数量的题目\n");
printf("- 切换为 小学/初中/高中:切换出题类型\n");
printf("- -1退出当前用户\n\n");
}
// 处理命令
int handleCommand(const char *command, char *currentUserType) {
// 检查退出命令
if (strcmp(command, "-1") == 0) {
return -1; // 退出当前用户
}
// 检查切换命令
if (strncmp(command, "切换为", 6) == 0) {
return switchUserType(command, currentUserType);
}
// 检查是否为数字(生成题目)
if (isNumber(command)) {
int count = atoi(command);
if (count > 0 && count <= 100) {
printf("正在生成 %d 道 %s 数学题目...\n", count, currentUserType);
generateQuestions(currentUserType, count);
printf("题目生成完成!\n\n");
printf("系统提示:准备生成 %s 数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录\n", currentUserType);
return 1;
} else if (count == 0) {
printf("题目数量必须大于0\n");
printf("系统提示:准备生成 %s 数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录\n", currentUserType);
return 1;
} else {
printf("题目数量必须在1-100之间\n");
printf("系统提示:准备生成 %s 数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录\n", currentUserType);
return 1;
}
}
printf("未知命令!\n");
printf("系统提示:准备生成 %s 数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录\n", currentUserType);
return 1;
}
// 切换用户类型
// 切换用户类型 - 从"切换为XX"中提取XX部分
int switchUserType(const char *command, char *currentUserType) {
// 命令格式是"切换为XX"我们需要提取XX部分
// "切换为"占6个字节所以从第6个字节开始就是XX部分
char newType[10] = "";
// 复制"切换为"后面的内容
if (strlen(command) >= 8) { // 确保命令长度足够
strcpy(newType, command +9); // 跳过"切换为"的6个字节
printf("%s",newType);
}
// 验证类型是否合法
if (strcmp(newType, "小学") == 0) {
strcpy(currentUserType, "小学");
printf("已切换到 小学 出题\n");
printf("系统提示:准备生成 小学 数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录\n");
return 1;
}
else if (strcmp(newType, "初中") == 0) {
strcpy(currentUserType, "初中");
printf("已切换到 初中 出题\n");
printf("系统提示:准备生成 初中 数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录\n");
return 1;
}
else if (strcmp(newType, "高中") == 0) {
strcpy(currentUserType, "高中");
printf("已切换到 高中 出题\n");
printf("系统提示:准备生成 高中 数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录\n");
return 1;
}
else {
printf("请输入小学、初中和高中三个选项中的一个\n");
printf("系统提示:准备生成 %s 数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录\n", currentUserType);
return 1;
}
}
// 生成数学题目
void generateQuestions(const char *userType, int count) {
printf("=== 生成的 %s 数学题目 ===\n", userType);
// 设置随机数种子
srand((unsigned int)time(NULL));
for (int i = 1; i <= count; i++) {
if (strcmp(userType, "小学") == 0) {
// 小学题目:简单的加减法
int a = rand() % 50 + 1;
int b = rand() % 50 + 1;
char op = (rand() % 2 == 0) ? '+' : '-';
printf("%d. %d %c %d = \n", i, a, op, b);
}
else if (strcmp(userType, "初中") == 0) {
// 初中题目:包含乘除法
int a = rand() % 20 + 1;
int b = rand() % 20 + 1;
char ops[] = {'+', '-', '*', '/'};
char op = ops[rand() % 4];
if (op == '/' && b == 0) b = 1;
printf("%d. %d %c %d = \n", i, a, op, b);
}
else if (strcmp(userType, "高中") == 0) {
// 高中题目:包含平方、开方等
int a = rand() % 10 + 1;
int type = rand() % 3;
if (type == 0) {
printf("%d. %d² = \n", i, a);
} else if (type == 1) {
printf("%d. √%d = \n", i, a*a);
} else {
printf("%d. log₁₀(%d) = \n", i, a*10);
}
}
}
printf("============================\n");
}
// 清除输入缓冲区
void clearInputBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
// 去除字符串前后空格和换行符
void trimString(char *str) {
if (str == NULL) return;
char *start = str;
char *end = str + strlen(str) - 1;
while (*start && isspace((unsigned char)*start)) {
start++;
}
while (end > start && isspace((unsigned char)*end)) {
end--;
}
memmove(str, start, end - start + 1);
str[end - start + 1] = '\0';
char *newline = strchr(str, '\n');
if (newline) *newline = '\0';
newline = strchr(str, '\r');
if (newline) *newline = '\0';
}
// 检查字符串是否为数字
int isNumber(const char *str) {
if (str == NULL || *str == '\0') {
return 0;
}
// 检查是否为-1
if (strcmp(str, "-1") == 0) {
return 1;
}
for (int i = 0; str[i] != '\0'; i++) {
if (!isdigit(str[i])) {
return 0;
}
}
return 1;
}
Loading…
Cancel
Save