抽题系统设计

c语言怎么编程一个抽题系统c语言怎么编程一个抽题系统

(图片来源网络,侵删)

1. 需求分析

功能需求

1、用户输入问题数量。

2、用户输入问题内容。

3、系统随机抽取一个问题并显示给用户。

4、用户可以查看所有已抽取的问题。

非功能需求

1、系统需要有良好的用户体验,界面简洁明了。

2、系统需要有错误处理机制,如用户输入的问题数量为负数时,提示用户重新输入。

2. 系统设计

数据结构设计

1、使用数组存储问题内容。

2、使用链表存储已抽取的问题。

算法设计

1、使用随机数生成器生成一个介于0和问题数量之间的随机数。

2、根据随机数从数组中抽取一个问题。

3、将抽取的问题添加到链表中。

3. 代码实现

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Node {
    char question[100];
    struct Node *next;
} Node;
Node *head = NULL;
int question_count = 0;
void add_question(char question[]) {
    Node *new_node = (Node *)malloc(sizeof(Node));
    strcpy(new_node>question, question);
    new_node>next = head;
    head = new_node;
    question_count++;
}
void draw_question() {
    if (question_count == 0) {
        printf("暂无问题!n");
        return;
    }
    srand(time(NULL));
    int random_index = rand() % question_count;
    Node *current = head;
    for (int i = 0; i < random_index; i++) {
        current = current>next;
    }
    printf("抽取的问题:%sn", current>question);
}
void show_all_questions() {
    Node *current = head;
    while (current != NULL) {
        printf("%sn", current>question);
        current = current>next;
    }
}
int main() {
    int choice;
    do {
        printf("1. 添加问题n");
        printf("2. 抽取问题n");
        printf("3. 查看所有问题n");
        printf("4. 退出n");
        printf("请输入您的选择:");
        scanf("%d", &choice);
        switch (choice) {
            case 1: {
                char question[100];
                printf("请输入问题内容:");
                scanf("%s", question);
                add_question(question);
                break;
            }
            case 2: {
                draw_question();
                break;
            }
            case 3: {
                show_all_questions();
                break;
            }
            case 4: {
                printf("谢谢使用!n");
                break;
            }
            default: {
                printf("无效的选择,请重新输入!n");
                break;
            }
        }
    } while (choice != 4);
    return 0;
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。