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语言怎么编程一个抽题系统
网站URL:http://www.hantingmc.com/qtweb/news2/436452.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联