成績
10開啟時間
2023年10月10日 星期二 18:00
折扣0.8
折扣時間
2023年11月10日 星期五 23:55
允許遲交
否關閉時間
2023年11月17日 星期五 23:55
假設現在要開發乙個字元介面的編輯器,對文字的編輯操作是通過輸入乙個乙個的「行編輯命令」完成的。
系統約定:行編輯命令由「命令關鍵字 命令引數」組成。其中,命令關鍵字都是單一的字母,命令與引數之間使用乙個空格進行分割,當命令引數為多個時採用「/」作為引數的分隔符,如果在命令的引數中間出現符號「/」,則用「\/」表示。我們將「\」定義為轉義符。
現在,行編輯器具有如下編輯命令:
行插入:i #/串
在指定行號(>0)插入乙個新行,新行的內容就是命令中給出的「串」。若插入的行超過了已有的行數,則在最後一行的後面插入乙個新行。
文字替換:c 被替換串/替換串
將整個文字中全部[被替換串]替換為[替換串]。
行刪除:d #
其中#為指定的行號
行合併:f #1/#2/#3...
將行號為 #2 和 #3...的行連線到行號為 #1 的行的後面,取消原來的 #2 行和#3等,合併後#2行和#3等原來的內容不存在了,被後面的行抵補替代。若指定行號超出正文的範圍,則放棄操作。
假設,我們已經得到了一段文字,這段文字用[text]......[/text]的形式描述,之後是一串操作命令。
[text]
1 222 33 44
2 333 45 d b g 3
3 444 56 333 54
[/text]
c 333/zhang
d 1i 1/this is a new line 1.
i 4/this is a new line 4.
f 3/4
i 100/new line.
以上命令的執行過程如下:
[result:c 333/zhang]
1 222 33 44
2 zhang 45 d b g 3
3 444 56 zhang 54
[result:d 1]
2 zhang 45 d b g 3
3 444 56 zhang 54
[result:i 1/this is a new line 1.]
this is a new line 1.
2 zhang 45 d b g 3
3 444 56 zhang 54
[result:i 4/this is a new line 4.]
this is a new line 1.
2 zhang 45 d b g 3
3 444 56 zhang 54
this is a new line 4.
[result:f 3/4]
this is a new line 1.
2 zhang 45 d b g 3
3 444 56 zhang 54this is a new line 4.
[result:i 100/new line.]
this is a new line 1.
2 zhang 45 d b g 3
3 444 56 zhang 54this is a new line 4.
100/new line.
完成各種操作之後的文字。
this is a new line 1.
2 zhang 45 d b g 3
3 444 56 zhang 54this is a new line 4.
new line.
鏈結 :
最近複習考研複試機試,做到這個行編輯,想起了大二的時候這道題wa了無數次,做了兩天兩夜。。貼上當時的**。
用單向鍊錶記錄每一行,每一行的結點指向該行的字串,字串採用雙向鍊錶。
現在想來其實沒必要乙個結點儲存乙個字元。
雖然**寫的不咋地,但最少不是直接字串處理那種偷懶的方式。
#include#include#includeconst int n = 100;
char cah[n];
int num_line = 0, len = 0;
struct str
;struct line
*lineh[n];
str *ps, *qs;
void create(int num)
lineh[num] = (line*)malloc(sizeof(line));
lineh[num]->shead = null;
lineh[num]->send = null;
for(i = 0; i < len; i++) }
}void dele(int num)
free(lineh[num]);
for(i = num + 1; i <= num_line; i++) }
void insert(str *bei, str *end, int len2, char s)
/* for(;oi != end;)
*/}void output(str *po)
}int main()
/ // execute commend
while(~scanf("%c", &com) && com != 'e')
else if (com == 'c')
tem[i] = '\0';
i = 0;
for(;;)
s[i] = '\0';
/* for(i = 0; cah[i] != 0; i++)
}for(i = 0; cah[i] != '\0'; i++)
tem[t] = '\0';
for(i = j; cah[i] != '\0'; i++)
s[r] = '\0';*/
int len1 = strlen(tem), len2 = strlen(s), flag = 0;
str *bps = ps;
for(r = 0; r < num_line; r++)
else
}if (j == len1)
flag = 1;
if (flag == 1)
else
break;
}insert(ps, qs, len2, s);
for(i = 0; i < len2; i++)
if (ps == null)
}else }}
}} else if (com == 'd')
else if (com == 'f')
*/scanf("%d", &fd[r++]);
while(~scanf("%c", &ss))
for(j = 1; j < r; j++)
for(j = 0; j < n; j++)
for(int k = 0; k < j; k++)
for(i = 0; i < n; i++)
}} for(i = 0; i < num_line; i++)
return 0;
}
C語言資料結構 棧 行編輯程式
順序棧標頭檔案 seqstack.h include include define stacksize 100 typedef char datatype typedef struct seqstack void initstack seqstack s 初始化棧 int stackempty se...
資料結構實驗之棧 行編輯器
time limit 1000ms memory limit 65536k 有疑問?點這裡 乙個簡單的行編輯程式的功能是 接受使用者從終端輸入的程式或資料,並存入使用者的資料區。由於使用者在終端上進行輸入時,不能保證不出差錯,因此,若在編輯程式中,每接受乙個字元即存入使用者資料區 的做法顯然不是最恰...
資料結構實驗之棧 行編輯器
乙個簡單的行編輯程式的功能是 接受使用者從終端輸入的程式或資料,並存入使用者的資料區。由於使用者在終端上進行輸入時,不能保證不出差錯,因此,若在編輯程式中,每接受乙個字元即存入使用者資料區 的做法顯然不是最恰當的。較好的做法是,設立乙個輸入緩衝區,用以接受使用者輸入的一行字元,然後逐行存入使用者資料...