vim批量處理小記
引入
前段時間考馬原
舍友給了題庫的word文檔,
文檔里面是題目和答案一并給出的
想把答案遮住,點開才能看的形式,這樣復習會更加高效一點
怎么做呢,第一時間想到的是用vim
vim給我的首印象就是能做到更加靈活的全局搜索替換
解決
-
首先觀察到文件里面近乎所有的答案行都包含了“正確答案”這一關鍵字
在“正確答案”和 “:C”之間插入部分html代碼,得到
<details><summary>正確答案</summary>:C</details>
即可實現對答案隱藏 -
在“正確答案”前插入
<details><summary>,在“正確答案”后插入</summary>
:%s/^\(.*正確答案:\)/<details><summary>\1<\/summary>/g
解釋:
:%s:表示全局替換。
^(.*正確答案:):匹配每行開頭到“正確答案:”部分,將其捕獲到分組 \1 中。
<details><summary>\1<\/summary>:在匹配內容前增加<details><summary>,并在“正確答案:”后增加</summary>。
g:全局替換。
- 在每個含有“正確答案:”的行末尾追加
</details>:
:%s/\(正確答案:.*\)$/\1<\/details>/g
解釋:
(正確答案:.*)$:匹配“正確答案:”及其后所有內容,將其捕獲到分組 \1 中。
\1</details>:在匹配內容末尾追加</details>。
實現效果


追加
線上題庫測試本來說要在考試前兩天關閉,我那會刷的還不多,本來想配合這個能隱藏答案的文檔寫一個能隨機出題的程序來著,但是文件處理這塊貌似來不及學,在考前再浪費時間就有點得不償失,就簡單寫了個抽取隨機數對應這個文檔的題號的c。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TOTAL_SINGLE 40
#define TOTAL_MULTIPLE 20
#define TOTAL_TRUE_FALSE 20
#define MAX_CHAPTERS 8
// 各章節的題目數量
const int singleCounts[MAX_CHAPTERS] = {26, 188, 117, 117, 145, 32, 45, 22};
const int multipleCounts[MAX_CHAPTERS] = {26, 106, 119, 116, 87, 76, 59, 27};
const int trueFalseCounts[MAX_CHAPTERS] = {18, 114, 77, 54, 43, 50, 37, 49};
// 記錄得分
int score = 0;
// 隨機生成一個題目
void generateQuestion(int type, int chapter, int question, int currentIndex) {
const char *typeStr;
if (type == 1) typeStr = "dan"; // 單選
else if (type == 2) typeStr = "duo"; // 多選
else typeStr = "pan"; // 判斷
printf("試卷第%d題: %d.%s.%d\n", currentIndex, chapter, typeStr, question);
}
// 從某個題庫中隨機抽取題目
int getRandomQuestion(int totalQuestions) {
return rand() % totalQuestions + 1;
}
int main() {
srand(time(NULL)); // 初始化隨機種子
int usedSingle = 0, usedMultiple = 0, usedTrueFalse = 0;
int selectedChapters[MAX_CHAPTERS] = {0}; // 用于記錄抽取的章節
printf("隨機試卷生成開始\n\n");
int currentIndex = 1; // 當前是第幾題
// 隨機生成單選題
for (int i = 0; i < TOTAL_SINGLE; i++, currentIndex++) {
int chapter = rand() % MAX_CHAPTERS; // 隨機選擇章節
int question = getRandomQuestion(singleCounts[chapter]);
generateQuestion(1, chapter, question, currentIndex);
printf("請輸入答案是否正確(1錯誤/2正確,3提前交卷): ");
int isCorrect;
scanf("%d", &isCorrect);
if (isCorrect == 3) {
printf("提前交卷,未作答的題目將按錯誤處理。\n");
goto finish;
}
if (isCorrect == 2) score += 1; // 單選題每題1分
}
// 隨機生成多選題
for (int i = 0; i < TOTAL_MULTIPLE; i++, currentIndex++) {
int chapter = rand() % MAX_CHAPTERS; // 隨機選擇章節
int question = getRandomQuestion(multipleCounts[chapter]);
generateQuestion(2, chapter, question, currentIndex);
printf("請輸入答案是否正確(1錯誤/2正確,3提前交卷): ");
int isCorrect;
scanf("%d", &isCorrect);
if (isCorrect == 3) {
printf("提前交卷,未作答的題目將按錯誤處理。\n");
goto finish;
}
if (isCorrect == 2) score += 2; // 多選題每題2分
}
// 隨機生成判斷題
for (int i = 0; i < TOTAL_TRUE_FALSE; i++, currentIndex++) {
int chapter = rand() % MAX_CHAPTERS; // 隨機選擇章節
int question = getRandomQuestion(trueFalseCounts[chapter]);
generateQuestion(3, chapter, question, currentIndex);
printf("請輸入答案是否正確(1錯誤/2正確,3提前交卷): ");
int isCorrect;
scanf("%d", &isCorrect);
if (isCorrect == 3) {
printf("提前交卷,未作答的題目將按錯誤處理。\n");
goto finish;
}
if (isCorrect == 2) score += 1; // 判斷題每題1分
}
finish:
// 剩余未答題按錯誤處理
score += (TOTAL_SINGLE + TOTAL_MULTIPLE + TOTAL_TRUE_FALSE - currentIndex + 1) * 0;
printf("\n試卷完成,您的總得分是: %d 分\n", score);
return 0;
}
- 效果

結果被坑了,題庫最后一天前晚上一點多才關上的,折騰這些沒用上捏

浙公網安備 33010602011771號