Pascal竞赛编程中如何批量读取多行单词实现缩写加密
竞赛编程练习:长单词缩写加密题的实现与踩坑记录
任务说明
- 自身情况:编程初学者,以竞赛编程作为核心技能练习路径
- 题目要求:输入一组单词,按规则加密输出
- 加密规则:若单词长度大于10,保留首字母、末字母,将中间字符替换为中间段总长度;单词长度≤10则直接输出原单词
输入输出示例
输入格式:第一行标注单词总数量n,后续n行每行对应一个独立单词
示例输入:
4 word localization internationalization pneumonoultramicroscopicsilicovolcanoconiosis对应预期输出:
word l10n i18n p43s
代码迭代过程
初始版本
核心加密逻辑可正常运行,但存在输入环节缺陷:仅能逐个手动输入单词,无法一次性按行读取全部输入内容,易将连续多行输入识别为一整个连续长字符串,且采用无限循环等待输入的设计。
初始实现代码:
program A71; uses wincrt; var word: String; function correctname(word : String): Boolean; var i : Integer; begin correctname:= true; for i:=1 to length(word) do if not (upcase(word[i]) in ['A'..'Z']) then correctname:= false; end; function Cryptname(var word : String): String; var wordcrypt: String; begin wordcrypt:= ''; str(length(word) - 2, wordcrypt); Cryptname:= word[1] + wordcrypt + word[length(word)]; end; begin repeat repeat readln(word); until correctname(word); if correctname(word) and (length(word) > 10) then writeln(Cryptname(word)) else writeln(word); until false; end.
数组批量存储的崩溃版本
第一次尝试引入字符串数组批量存储待处理单词,修改后程序会在第二次输入环节直接崩溃。
问题版本代码:
program A71; uses wincrt; type tab = array[1..100] of String; var word: tab; correctwords,n, i : Integer; function correctname(word : tab; correctwords : Integer): Boolean; var i : Integer; begin correctname:= true; i:= 0; repeat i:=i+1; if not (upcase(word[correctwords][i]) in ['A'..'Z']) then correctname:= false; until not correctname or (i = length(word[correctwords])); end; procedure Cryptname(var word : tab; correctwords : Integer); var wordcrypt: String; begin str(length(word[correctwords]) - 2, wordcrypt); word[correctwords]:= word[correctwords][1] + wordcrypt + word[correctwords][length(word)]; end; begin correctwords:=0; readln(n); repeat readln(word[correctwords]); if correctname(word, correctwords) then correctwords:= correctwords+1; until correctwords = n; for i:= 1 to correctwords do begin if correctname(word, correctwords) and (length(word[correctwords]) > 10) then begin Cryptname(word, correctwords); writeln(word[correctwords]); end else writeln(word[correctwords]); end; readkey(); end.
最终可运行版本
调整函数参数传递逻辑后,程序实现了正常的批量输入读取与加密输出功能。
最终实现代码:
program A71; uses wincrt; type tab = array[1..100] of String; var word: tab; correctwords,n, i : Integer; tester : String; function correctname(tester : String): Boolean; var i : Integer; begin correctname:= true; i:= 0; repeat i:=i+1; if not (upcase(tester[i]) in ['A'..'Z']) then correctname:= false; until not correctname or (i = length(tester)); end; procedure Cryptname(var wordcrypt : String); var wordcryptaux: String; begin wordcryptaux:= wordcrypt; str(length(wordcrypt) - 2, wordcrypt); wordcrypt:= wordcryptaux[1] + wordcrypt + wordcryptaux[length(wordcryptaux)]; end; begin correctwords:=0; readln(n); repeat readln(tester); if correctname(tester) then correctwords:= correctwords+1; word[correctwords]:= tester; until correctwords = n; for i:= 1 to correctwords do begin if correctname(word[i]) and (length(word[i]) > 10) then begin Cryptname(word[i]); writeln(word[i]); end else writeln(word[i]); end; readkey(); end.
内容的提问来源于stack exchange,提问作者MEDX
相关产品推荐
相关产品推荐

