这个计算元音的程序是如何工作的?

发布于 2024-10-10 22:53:45 字数 1436 浏览 4 评论 0原文

我想理解这段代码,尤其是 PROCEDURE

PROGRAM vowels; 

USES crt; 

{Program that counts the number of vowels in a sentence} 

CONST space=' '; 
      maxchar=80; 

TYPE vowel=(a,e,i,o,u); 

VAR buffer:ARRAY[1..maxchar] of char; 
    vowelcount:ARRAY[vowel] of integer; 

PROCEDURE initialize; 

VAR ch:vowel; 

BEGIN 
     FOR ch:=a TO u DO 
     BEGIN 
          vowelcount[ch]:=0; 
     END; 
END; 

PROCEDURE textinput; 

VAR index:integer; 

BEGIN 
     writeln('Input a sentence'); 
     FOR index:=1 TO maxchar DO 
         IF eoln THEN buffer[index]:=space 
         ELSE read(buffer[index]); 
         readln; 
END; 

PROCEDURE analysis; 

VAR index:integer; 
    ch:vowel; 

BEGIN 
     index:=1; 
     WHILE index<>maxchar+1 DO 
     BEGIN 
          IF buffer[index] IN ['a','e','i','o','u'] THEN 
          BEGIN 
               CASE buffer[index] OF 
               'a':ch:=a; 
               'e':ch:=e; 
               'i':ch:=i; 
               'o':ch:=o; 
               'u':ch:=u; 
               END; 
               vowelcount[ch]:=vowelcount[ch]+1; 
          END; 
          index:=index+1; 
     END; 
END; 

PROCEDURE vowelout; 

VAR ch:vowel; 

BEGIN 
     clrscr; 
     writeln; 
     writeln('   a   e   i   o   u'); 
     FOR ch:=a TO u DO 
     write(vowelcount[ch]:4); 
     writeln; 
END; 

BEGIN 
     initialize; 
     textinput; 
     analysis; 
     vowelout; 
END;

I want to understand this code, especially PROCEDURE

PROGRAM vowels; 

USES crt; 

{Program that counts the number of vowels in a sentence} 

CONST space=' '; 
      maxchar=80; 

TYPE vowel=(a,e,i,o,u); 

VAR buffer:ARRAY[1..maxchar] of char; 
    vowelcount:ARRAY[vowel] of integer; 

PROCEDURE initialize; 

VAR ch:vowel; 

BEGIN 
     FOR ch:=a TO u DO 
     BEGIN 
          vowelcount[ch]:=0; 
     END; 
END; 

PROCEDURE textinput; 

VAR index:integer; 

BEGIN 
     writeln('Input a sentence'); 
     FOR index:=1 TO maxchar DO 
         IF eoln THEN buffer[index]:=space 
         ELSE read(buffer[index]); 
         readln; 
END; 

PROCEDURE analysis; 

VAR index:integer; 
    ch:vowel; 

BEGIN 
     index:=1; 
     WHILE index<>maxchar+1 DO 
     BEGIN 
          IF buffer[index] IN ['a','e','i','o','u'] THEN 
          BEGIN 
               CASE buffer[index] OF 
               'a':ch:=a; 
               'e':ch:=e; 
               'i':ch:=i; 
               'o':ch:=o; 
               'u':ch:=u; 
               END; 
               vowelcount[ch]:=vowelcount[ch]+1; 
          END; 
          index:=index+1; 
     END; 
END; 

PROCEDURE vowelout; 

VAR ch:vowel; 

BEGIN 
     clrscr; 
     writeln; 
     writeln('   a   e   i   o   u'); 
     FOR ch:=a TO u DO 
     write(vowelcount[ch]:4); 
     writeln; 
END; 

BEGIN 
     initialize; 
     textinput; 
     analysis; 
     vowelout; 
END;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二货你真萌 2024-10-17 22:53:45

总体而言:好的,这段代码正在计算输入字符串中提供的元音数量。

让我们开始吧......

类型元音=(a,e,i,o,u); VAR
缓冲区:ARRAY[1..maxchar] of char;
元音计数:整数的ARRAY[元音];

此代码定义英语元音列表(a、e、i、o、u)。

程序初始化; VAR ch:元音;
开始 ch:=a TO 你开始
元音计数[ch]:=0;结尾;结束;

然后它定义一个变量来收集每个元音的数量,称为 Vowelcount。该变量是一个数组,看起来有点像这样:

vowelcount[a]=0;
vowelcount[e]=0;
vowelcount[i]=0; #... etc

然后定义过程“分析”。这从屏幕上获取输入(稍后将在程序中调用)并逐步执行输入中的每个字母。

WHILE索引<>maxchar+1 DO BEGIN IF
缓冲区[索引] IN ['a','e','i','o','u']
THEN BEGIN CASE 缓冲区[索引] OF
'a':ch:=a; 'e':ch:=e; 'i':ch:=i;
'o':ch:=o; 'u':ch:=u;结束;

如果这些字母中的任何一个恰好在字母列表中且与元音匹配,则它将在上面的元音计数数组中的数字上加一。 (vowelcount[ch]:=vowelcount[ch]+1) 其中 ch 是匹配的字母。正如您所看到的,只有当它是有效的元音时才会触发(IF buffer[index] IN ['a','e','i','o','u'] )

最后。程序的主要代码,或者说实际运行的内容:

开始 clrscr;写; writeln(' aei
哦你'); FOR ch:=a TO 你做
写(元音计数[ch]:4);写;结束;

开始初始化;文本输入;分析;
元音输出;结束。

这基本上将应用程序串在一起,首先清除屏幕(在 dos 提示符下),然后将元音输出到屏幕上。然后它添加一些格式并输出元音计数的当前计数(如上所述)。
然后它会请求您的输入,最后它将再次输出元音计数的内容,该内容已使用您输入的元音计数进行更新。

Overall: Okay this code is counting the number of vowels supplied in the input string.

Lets Begin....

TYPE vowel=(a,e,i,o,u); VAR
buffer:ARRAY[1..maxchar] of char;
vowelcount:ARRAY[vowel] of integer;

This code is defining a list of the vowels in english (a,e,i,o,u).

PROCEDURE initialize; VAR ch:vowel;
BEGIN FOR ch:=a TO u DO BEGIN
vowelcount[ch]:=0; END; END;

It then defines a variable to collect the number of each vowel, called vowelcount. That variable is an array, looks sort of like this:

vowelcount[a]=0;
vowelcount[e]=0;
vowelcount[i]=0; #... etc

Then the procedure "Analysis" is defined. This takes the input from the screen (which will be called later on in the program) and steps through each letter in the input.

WHILE index<>maxchar+1 DO BEGIN IF
buffer[index] IN ['a','e','i','o','u']
THEN BEGIN CASE buffer[index] OF
'a':ch:=a; 'e':ch:=e; 'i':ch:=i;
'o':ch:=o; 'u':ch:=u; END;

If any of those letters happens to be in the list of letters than matches a vowel, then it will add one to the number in the vowelcount array above. (vowelcount[ch]:=vowelcount[ch]+1) where ch is the matched letter. As you can see this is only triggered if it is a valid vowel (IF buffer[index] IN ['a','e','i','o','u'] )

Finally. The main code of the program, or what is actually run:

BEGIN clrscr; writeln; writeln(' a e i
o u'); FOR ch:=a TO u DO
write(vowelcount[ch]:4); writeln; END;

BEGIN initialize; textinput; analysis;
vowelout; END.

This basically strings the application together, starting by clearing the screen (in a dos prompt) and then outputting the vowels onto the screen. It then adds some formatting and outputs the current count of vowelcount (as above).
It will then request your input and finally it will output the contents of vowelcount again, which has been updated with the vowelcounts from the input you made.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文