新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
c語言怎么編程一個抽題系統(tǒng)
抽題系統(tǒng)設(shè)計

1. 需求分析
功能需求
1、用戶輸入問題數(shù)量。
2、用戶輸入問題內(nèi)容。
3、系統(tǒng)隨機(jī)抽取一個問題并顯示給用戶。
4、用戶可以查看所有已抽取的問題。
非功能需求
1、系統(tǒng)需要有良好的用戶體驗,界面簡潔明了。
2、系統(tǒng)需要有錯誤處理機(jī)制,如用戶輸入的問題數(shù)量為負(fù)數(shù)時,提示用戶重新輸入。
2. 系統(tǒng)設(shè)計
數(shù)據(jù)結(jié)構(gòu)設(shè)計
1、使用數(shù)組存儲問題內(nèi)容。
2、使用鏈表存儲已抽取的問題。
算法設(shè)計
1、使用隨機(jī)數(shù)生成器生成一個介于0和問題數(shù)量之間的隨機(jī)數(shù)。
2、根據(jù)隨機(jī)數(shù)從數(shù)組中抽取一個問題。
3、將抽取的問題添加到鏈表中。
3. 代碼實現(xiàn)
#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("請輸入問題內(nèi)容:"); 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; }
當(dāng)前名稱:c語言怎么編程一個抽題系統(tǒng)
當(dāng)前路徑:http://www.dlmjj.cn/article/cdcjjho.html


咨詢
建站咨詢
