c语言怎么编程一个抽题系统-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

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

抽题系统设计

1. 需求分析

功能需求

1、用户输入问题数量。

2、用户输入问题内容。

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

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

非功能需求

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

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

2. 系统设计

数据结构设计

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

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

算法设计

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

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

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

3. 代码实现

#include 
#include 
#include 
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;
}

本文标题:c语言怎么编程一个抽题系统
分享地址:http://kswsj.cn/article/cdcjjho.html

其他资讯