Difference between revisions of "Basic Pascal Tutorial/Chapter 3/Solution/ja"

From Lazarus wiki
Jump to navigationJump to search
(Created page with "{{Solution 3}} 3Ea - 解答 (著者: Tao Yue, 状態: 原文のまま修正なし) '''フィボナッチ数列問題の解答''' <syntaxhighlight> (* 著者: Tao Yue ...")
 
m (bypass language bar/categorization template redirect [cf. discussion])
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Solution 3}}
+
{{Basic Pascal Tutorial/Chapter 3/Solution}}
  
 
3Ea - 解答 (著者: Tao Yue, 状態: 原文のまま修正なし)
 
3Ea - 解答 (著者: Tao Yue, 状態: 原文のまま修正なし)
  
 
'''フィボナッチ数列問題の解答'''
 
'''フィボナッチ数列問題の解答'''
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
(* 著者:    Tao Yue
 
(* 著者:    Tao Yue
 
   日付:      19 July 1997
 
   日付:      19 July 1997
Line 40: Line 40:
  
 
'''2の累乗値の解答'''
 
'''2の累乗値の解答'''
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
(* Author:    Tao Yue
+
(* 著者:    Tao Yue
   Date:      13 July 2000
+
   日付:      13 July 2000
   Description:
+
   記述:
       Display all powers of two up to 20000, five per line
+
       20000未満の2の累乗値をすべて求め、1行に5つ表示する。
 
   Version:
 
   Version:
 
       1.0 - original version
 
       1.0 - original version
Line 62: Line 62:
 
begin    (* Main *)
 
begin    (* Main *)
 
   writeln ('Powers of ', base, ', 1 <= x <= ', maxnum, ':');
 
   writeln ('Powers of ', base, ', 1 <= x <= ', maxnum, ':');
   (* Set up for loop *)
+
   (* ループの設定 *)
 
   number := 1;
 
   number := 1;
 
   linecount := 0;
 
   linecount := 0;
   (* Loop *)
+
   (* ループ *)
 
   while number <= maxnum do
 
   while number <= maxnum do
 
       begin
 
       begin
 
         linecount := linecount + 1;
 
         linecount := linecount + 1;
         (* Print a comma and space unless this is the first
+
         (* 行の最初の数字でなければカンマとスペースを表示させる *)
            number on the line *)
 
 
         if linecount > 1 then
 
         if linecount > 1 then
 
             write (', ');
 
             write (', ');
         (* Display the number *)
+
         (* 数字を表示させる *)
 
         write (number);
 
         write (number);
         (* Print a comma and go to the next line if this is
+
         (* 数列の最後の数字でなく、行の最後の数字ならコンマを表示し、次の行に行く。 *)
            the last number on the line UNLESS it is the
 
            last number of the series *)
 
 
         if (linecount = numperline) and not (number * 2 > maxnum) then
 
         if (linecount = numperline) and not (number * 2 > maxnum) then
 
             begin
 
             begin
Line 83: Line 80:
 
               linecount := 0
 
               linecount := 0
 
             end;
 
             end;
         (* Increment number *)
+
         (* 数字を増やす *)
 
         number := number * base;
 
         number := number * base;
 
       end;  (* while *)
 
       end;  (* while *)
 
   writeln;
 
   writeln;
  
   (* This program can also be written using a
+
   (* このプログラムも REPEAT..UNTIL ループを用いて書き換えることができる。 *)
      REPEAT..UNTIL loop. *)
 
  
 
end.    (* Main *)  
 
end.    (* Main *)  
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Note that I used three constants: the base, the number of powers to display on each line, and the maximum number. This ensures that the program can be easily adaptable in the future.
+
私が3つの定数、すなわち、基数、各行に表示するための累乗の数、最大数を使ったことに注意して欲しい。こうすることで簡単に、このプログラムが将来、改変できることを確実にしている。
  
Using constants rather than literals is a good programming habit to form. When you write really long programs, you may refer to certain numbers thousands of times. If you hardcoded them into your code, you'd have to search them out. Also, you might use the same value in a different context, so you can't simply do a global Search-and-Replace. Using a constant makes it simpler to expand the program.
+
直の値よりも定数を利用するというのは身につけておくべきよいプログラミング習慣である。非常に長いプログラムを書いたとき、ある数値を何千回も参照することになるかもしれない。もし、それらを決め打ちしていたなら、それらをすべて探し出さなくてはならなくなる。また、同じ値を異なる文脈で利用しているかもしれない。従い、単純に検索・置換するというわけにはいかない。定数を使うことでプログラムを拡張するのがずっと簡単になるのである。
  
Also note that I used the <tt>longint</tt> type for the number variable. This is because to fail the test <tt>number <= 20000</tt>, <tt>number</tt> would have to reach <tt>32768</tt>, the next power of two after <tt>16384</tt>. This exceeds the range of the integer type: <tt>-32768</tt> to <tt>32767</tt>. (try it without <tt>longint</tt> and see what happens)
+
また、数値変数に対して <tt>longint</tt> タイプを用いたことにも注意して欲しい。こうした訳は <tt>number <= 20000</tt> のチェックで失敗したときに、<tt>number</tt> <tt>16384</tt> の2つあとの累乗で <tt>32768</tt> に達してしまうからである。これは整数タイプの範囲(<tt>-32768</tt> から <tt>32767</tt>まで)を超えてしまう。(<tt>longint</tt> でない場合を試して、何が起こるか見てみなさい。)
  
 
{|style=color-backgroud="white" cellspacing="20"
 
{|style=color-backgroud="white" cellspacing="20"
|[[Programming_Assignment_3|previous]]   
+
|[[Basic Pascal Tutorial/Chapter 3/Programming Assignment/ja|previous]]   
|[[Contents|contents]]  
+
|[[Basic Pascal Tutorial/Contents/ja|contents]]  
|[[Procedures|next]]
+
|[[Basic Pascal Tutorial/Chapter 4/Procedures/ja|next]]
 
|}
 
|}

Latest revision as of 16:19, 20 August 2022

български (bg) English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

3Ea - 解答 (著者: Tao Yue, 状態: 原文のまま修正なし)

フィボナッチ数列問題の解答

(* 著者:    Tao Yue
   日付:      19 July 1997
   記述:
      最初の10個のフィボナッチ数列の数を求める。
   Version:
      1.0 - original version
*)

program Fibonacci;

var
   Fibonacci1, Fibonacci2 : integer;
   temp : integer;
   count : integer;

begin    (* Main *)
   writeln ('最初の10個のフィボナッチ数列は:');
   count := 0;
   Fibonacci1 := 0;
   Fibonacci2 := 1;
   repeat
      write (Fibonacci2:7);
      temp := Fibonacci2;
      Fibonacci2 := Fibonacci1 + Fibonacci2;
      Fibonacci1 := Temp;
      count := count + 1
   until count = 10;
   writeln;

   (* もちろん、この問題をとくために FOR ループや WHILE ループを使うこともできる。 *)

end.     (* Main *)

2の累乗値の解答

(* 著者:    Tao Yue
   日付:      13 July 2000
   記述:
      20000未満の2の累乗値をすべて求め、1行に5つ表示する。
   Version:
      1.0 - original version
*)

program PowersofTwo;

const
   numperline = 5;
   maxnum = 20000;
   base = 2;

var
   number : longint;
   linecount : integer;

begin    (* Main *)
   writeln ('Powers of ', base, ', 1 <= x <= ', maxnum, ':');
   (* ループの設定 *)
   number := 1;
   linecount := 0;
   (* ループ *)
   while number <= maxnum do
      begin
         linecount := linecount + 1;
         (* 行の最初の数字でなければカンマとスペースを表示させる *)
         if linecount > 1 then
            write (', ');
         (* 数字を表示させる *)
         write (number);
         (* 数列の最後の数字でなく、行の最後の数字ならコンマを表示し、次の行に行く。 *)
         if (linecount = numperline) and not (number * 2 > maxnum) then
            begin
               writeln (',');
               linecount := 0
            end;
         (* 数字を増やす *)
         number := number * base;
      end;  (* while *)
   writeln;

   (* このプログラムも REPEAT..UNTIL ループを用いて書き換えることができる。 *)

end.     (* Main *)

私が3つの定数、すなわち、基数、各行に表示するための累乗の数、最大数を使ったことに注意して欲しい。こうすることで簡単に、このプログラムが将来、改変できることを確実にしている。

直の値よりも定数を利用するというのは身につけておくべきよいプログラミング習慣である。非常に長いプログラムを書いたとき、ある数値を何千回も参照することになるかもしれない。もし、それらを決め打ちしていたなら、それらをすべて探し出さなくてはならなくなる。また、同じ値を異なる文脈で利用しているかもしれない。従い、単純に検索・置換するというわけにはいかない。定数を使うことでプログラムを拡張するのがずっと簡単になるのである。

また、数値変数に対して longint タイプを用いたことにも注意して欲しい。こうした訳は number <= 20000 のチェックで失敗したときに、number16384 の2つあとの累乗で 32768 に達してしまうからである。これは整数タイプの範囲(-32768 から 32767まで)を超えてしまう。(longint でない場合を試して、何が起こるか見てみなさい。)

previous contents next