Basic Pascal Tutorial/Chapter 2/Input/ja: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Basic Pascal Tutorial/Chapter 2/Input/ja
| Old | New | Line | |
|---|---|---|---|
| 1 | + | 2A - 入力 (著者: Tao Yue, 状態: 原文のまま修正なし) | |
| 2 | + | ||
| 3 | + | 入力とはプログラムに入ってくるもののことである。キーボードから入ることもあれば、マウス、ディスクのファイル、スキャナー、ジョイスティックなどなどから入ってくることもある。 | |
| 4 | + | ||
| 5 | + | マウス入力については詳しく取り上げない。その理由は、文法がマシン次第で異なるからである。加えて、今日のイベント・ドリブンのウインドウOSがたいていマウス入力を扱ってくれるからである。 | |
| 6 | + | ||
| 7 | + | データの読み込みの基本フォーマットは以下の通りである。 | |
| 8 | + | ||
| 9 | + | ``` pascal | |
| 10 | + | read (変数リスト); | |
| 11 | + | ``` | |
| 12 | + | ||
| 13 | + | `Variable_List` はカンマで区切られた変数識別子のリストである。 | |
| 14 | + | ||
| 15 | + | `read`は特殊な行末文字で行が区切られた一連の文字として入力を扱う。一方、`readln`は値を読み込んだ後、行末文字の後に自動的に移動することで次の行へと飛ぶ。 | |
| 16 | + | ||
| 17 | + | ``` pascal | |
| 18 | + | readln (変数リスト); | |
| 19 | + | ``` | |
| 20 | + | ||
| 21 | + | ユーザから次のような入力を受け取ったとしよう。`a, b, c,`そして `d` はみな整数である。 | |
| 22 | + | ||
| 23 | + | `45 97 3`\ | |
| 24 | + | `1 2 3` | |
| 25 | + | ||
| 26 | + | ここでは `read` と `readln` の命令文の例を、適当な変数に読み込まれた値と共に示す。 | |
| 27 | + | ||
| 28 | + | <table> | |
| 29 | + | <thead> | |
| 30 | + | <tr> | |
| 31 | + | <th>Statement(s)</th> | |
| 32 | + | <th>a</th> | |
| 33 | + | <th>b</th> | |
| 34 | + | <th>c</th> | |
| 35 | + | <th>d</th> | |
| 36 | + | </tr> | |
| 37 | + | </thead> | |
| 38 | + | <tbody> | |
| 39 | + | <tr> | |
| 40 | + | <td>read (a);</td> | |
| 41 | + | <td rowspan="2">45</td> | |
| 42 | + | <td rowspan="2">97</td> | |
| 43 | + | <td rowspan="2"></td> | |
| 44 | + | <td rowspan="2"></td> | |
| 45 | + | </tr> | |
| 46 | + | <tr> | |
| 47 | + | <td>read (b);</td> | |
| 48 | + | </tr> | |
| 49 | + | <tr> | |
| 50 | + | <td>readln (a);</td> | |
| 51 | + | <td rowspan="2">45</td> | |
| 52 | + | <td rowspan="2" style="text-align: right;">1</td> | |
| 53 | + | <td rowspan="2"></td> | |
| 54 | + | <td rowspan="2"></td> | |
| 55 | + | </tr> | |
| 56 | + | <tr> | |
| 57 | + | <td>read (b);</td> | |
| 58 | + | </tr> | |
| 59 | + | <tr> | |
| 60 | + | <td>read (a, b, c, d);</td> | |
| 61 | + | <td>45</td> | |
| 62 | + | <td>97</td> | |
| 63 | + | <td>3</td> | |
| 64 | + | <td>1</td> | |
| 65 | + | </tr> | |
| 66 | + | <tr> | |
| 67 | + | <td>readln (a, b);</td> | |
| 68 | + | <td rowspan="2">45</td> | |
| 69 | + | <td rowspan="2">97</td> | |
| 70 | + | <td rowspan="2">1</td> | |
| 71 | + | <td rowspan="2">2</td> | |
| 72 | + | </tr> | |
| 73 | + | <tr> | |
| 74 | + | <td>readln (c, d);</td> | |
| 75 | + | </tr> | |
| 76 | + | </tbody> | |
| 77 | + | </table> | |
| 78 | + | ||
| 79 | + | 整数として読む場合、数字が見つかるまでスペースはすべて読み飛ばされる。そうして、数字ではない文字(空白に限らず)が出てくるまで続くすべての数字が読まれる。 | |
| 80 | + | ||
| 81 | + | `8352.38` | |
| 82 | + | ||
| 83 | + | 上の入力から整数が読み込まれると、その値は `8352` となる。もし、その後に文字を読み込めば、その値は '`.`' になるだろう。なぜなら読み取りヘッドは最初の英数字で止まるからである。 | |
| 84 | + | ||
| 85 | + | 2つの整数を読み込もうとしたとしよう。それはうまくいかない。なぜなら、コンピュータが2番目の変数を満たすデータを探したとき、 '`.`' を見つけ、それが読み込むべきデータではないので止まってしまうからである。 | |
| 86 | + | ||
| 87 | + | 実数データでも、コンピュータは空白をスキップする。そして、読める限りの数値を読み込む。しかし、多くの Pascal コンパイラはひとつ追加の制限を設けている。それは省略された実数は許されず、 `0.` ではじまらなくてはならないということである。従って、 `.678` は不適切でコンピュータは実数として読めない。しかし、 `0.678` は問題がない。 | |
| 88 | + | ||
| 89 | + | 引数リストの識別子がすべて変数であることを確認しておこう。定数には数値を割り当てられないし、リテラル値にも数値は割り当てられない。 | |
| 90 | + | ||
| 91 | + | | | | | | |
| 92 | + | |----|----|----| | |
| 93 | + | | [[Basic Pascal Tutorial/Chapter 1/Solution/ja|previous]] | [[Basic Pascal Tutorial/Contents/ja|contents]] | [[Basic Pascal Tutorial/Chapter 2/Output/ja|next]] | | |
| 94 | + | ||
| 95 | + | --- | |
| 96 | + | *From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 2/Input/ja", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
