C++写一个刷题软件源码
时间: 2024-05-13 14:15:11 浏览: 90
基于C和C++的刷题代码设计源码
以下是一个简单的刷题软件的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PROBLEMS 1000
#define MAX_NAME_LENGTH 50
typedef struct {
char name[MAX_NAME_LENGTH];
int solved;
} Problem;
void printMenu() {
printf("1. 添加题目\n");
printf("2. 显示所有题目\n");
printf("3. 查找题目\n");
printf("4. 标记已解决的题目\n");
printf("5. 退出\n");
printf("请选择操作:");
}
int readInt() {
char buffer[256];
fgets(buffer, sizeof(buffer), stdin);
return atoi(buffer);
}
void clearInputBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
}
void addProblem(Problem problems[], int *numProblems) {
if (*numProblems == MAX_PROBLEMS) {
printf("题目数量已达到最大值!\n");
return;
}
printf("请输入题目名称:");
fgets(problems[*numProblems].name, sizeof(problems[*numProblems].name), stdin);
problems[*numProblems].name[strcspn(problems[*numProblems].name, "\n")] = '\0';
problems[*numProblems].solved = 0;
(*numProblems)++;
}
void printProblems(Problem problems[], int numProblems) {
if (numProblems == 0) {
printf("没有题目!\n");
return;
}
for (int i = 0; i < numProblems; i++) {
printf("%d. %s [%s]\n", i + 1, problems[i].name, problems[i].solved ? "已解决" : "未解决");
}
}
void findProblem(Problem problems[], int numProblems) {
char name[MAX_NAME_LENGTH];
printf("请输入要查找的题目名称:");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = '\0';
for (int i = 0; i < numProblems; i++) {
if (strcmp(problems[i].name, name) == 0) {
printf("%s [%s]\n", problems[i].name, problems[i].solved ? "已解决" : "未解决");
return;
}
}
printf("没有找到该题目!\n");
}
void markSolved(Problem problems[], int numProblems) {
int index;
printf("请输入题目编号:");
index = readInt();
if (index < 1 || index > numProblems) {
printf("无效的题目编号!\n");
return;
}
problems[index - 1].solved = 1;
printf("已标记题目“%s”为已解决。\n", problems[index - 1].name);
}
int main() {
Problem problems[MAX_PROBLEMS];
int numProblems = 0;
int choice;
while (1) {
printMenu();
choice = readInt();
switch (choice) {
case 1:
addProblem(problems, &numProblems);
break;
case 2:
printProblems(problems, numProblems);
break;
case 3:
findProblem(problems, numProblems);
break;
case 4:
markSolved(problems, numProblems);
break;
case 5:
printf("再见!\n");
return 0;
default:
printf("无效的操作!\n");
break;
}
clearInputBuffer();
}
}
```
该程序使用结构体来存储题目名称和是否已解决的信息,并提供了添加、显示、查找和标记已解决等功能。用户可以通过命令行菜单进行操作。注意,这只是一个示例代码,实际的刷题软件需要更复杂的功能和界面设计。
阅读全文