Pascal programming

2007-08-22 11:31 pm
Write programs with the following output. (要三個program, 一個用FOR-TO-DO loop, 一個用 REPEAT UNTIL loop, 最後一個用WHILE DO loop, 都要同下面既output一模一樣)

Enter a word? Computer <----呢個字係個user打既, 唔係program set定既

Computer
Compute
Comput
Compu
Comp
Com
Co
C

回答 (2)

2007-08-23 12:50 am
✔ 最佳答案



Version 1: for - to - do loop







program WritePattern(input, output);

var
s : string[255];
i : integer;

begin
Write('Enter a word? ');
ReadLn(s);
WriteLn;

for i := 1 to Length(s) do
WriteLn(Copy(s, 1, Length(s) + 1 - i))
end.




— 或 —





program WritePattern(input, output);

var
s : string[255];
i : integer;

begin
Write('Enter a word? ');
ReadLn(s);
WriteLn;

for i := Length(s) downto 1 do
WriteLn(Copy(s, 1, i))
end.






Version 2: repeat - until loop







program WritePattern(input, output);

var
s : string[255];
i : integer;

begin
Write('Enter a word? ');
ReadLn(s);
WriteLn;

i := Length(s);

repeat
WriteLn(Copy(s, 1, i));
i := i - 1
until i < 1
end.






Version 3: while - do loop







program WritePattern(input, output);

var
s : string[255];
i : integer;

begin
Write('Enter a word? ');
ReadLn(s);

i := Length(s);

while i > 0 do
begin
WriteLn(Copy(s, 1, i));
i := i - 1
end
end.





參考: 個人經驗
2007-08-25 9:45 pm
多謝你給我這麼多的建議!!thank you!!


收錄日期: 2021-04-13 18:07:07
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070822000051KK03216

檢視 Wayback Machine 備份