118 lines
6.1 KiB
Markdown
118 lines
6.1 KiB
Markdown
---
|
||
title: '『[新版C言語プログラミングレッスン 入門編』 第四章 if'
|
||
author: kazu634
|
||
date: 2008-03-23
|
||
url: /2008/03/23/_922/
|
||
wordtwit_post_info:
|
||
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:3867;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
|
||
categories:
|
||
- C
|
||
- Programming
|
||
|
||
---
|
||
<div class="section">
|
||
<p>
|
||
つづきだよー
|
||
</p>
|
||
|
||
<p>
|
||
<a name="seemore"></a>
|
||
</p>
|
||
|
||
<h4>
|
||
論理和「||」
|
||
</h4>
|
||
|
||
<pre class="syntax-highlight">
|
||
<span class="synPreProc">#include </span><span class="synConstant"><stdio.h></span>
|
||
<span class="synPreProc">#include </span><span class="synConstant"><stdlib.h></span>
|
||
<span class="synPreProc">#define MAX_LINE </span><span class="synConstant">128</span><span class="synPreProc"> </span><span class="synComment">/* 入力行の最大長 */</span>
|
||
<span class="synType">int</span> main(<span class="synType">void</span>);
|
||
<span class="synType">int</span> main(<span class="synType">void</span>)
|
||
{
|
||
<span class="synType">char</span> buf[MAX_LINE];
|
||
<span class="synType">int</span> n;
|
||
printf (<span class="synConstant">"降水確率を入力してください。</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
||
gets(buf);
|
||
n = atoi(buf);
|
||
printf (<span class="synConstant">"降水確率は </span><span class="synSpecial">%d</span><span class="synConstant"> </span><span class="synSpecial">%%</span><span class="synConstant">です。</span><span class="synSpecial">\n</span><span class="synConstant">"</span>, n);
|
||
<span class="synStatement">if</span> (n < <span class="synConstant"></span> || <span class="synConstant">100</span> < n) {
|
||
printf (<span class="synConstant">"降水確率は0 - 100の間ですよ。</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
||
}
|
||
<span class="synStatement">else</span> <span class="synStatement">if</span> (n >= <span class="synConstant">50</span>) {
|
||
printf (<span class="synConstant">"傘を忘れずにね。</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
||
}
|
||
<span class="synStatement">else</span> {
|
||
printf (<span class="synConstant">"傘はいりません。</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
||
}
|
||
printf (<span class="synConstant">"いってらっしゃい</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
||
<span class="synStatement">return</span>(<span class="synConstant"></span>);
|
||
}
|
||
</pre>
|
||
|
||
<p>
|
||
or は「||」で表現する。
|
||
</p>
|
||
|
||
<h4>
|
||
現在の時間を入力してもらって、それに応じたメッセージを表示させる
|
||
</h4>
|
||
|
||
<pre class="syntax-highlight">
|
||
<span class="synPreProc">#include </span><span class="synConstant"><stdio.h></span>
|
||
<span class="synPreProc">#include </span><span class="synConstant"><stdlib.h></span>
|
||
<span class="synPreProc">#define MAX_LINE </span><span class="synConstant">128</span><span class="synPreProc"> </span><span class="synComment">/* 入力行の最大長 */</span>
|
||
<span class="synType">int</span> main(<span class="synType">void</span>);
|
||
<span class="synType">int</span> main(<span class="synType">void</span>)
|
||
{
|
||
<span class="synType">char</span> buf[MAX_LINE];
|
||
<span class="synType">int</span> n;
|
||
printf (<span class="synConstant">"現在の時刻(「時」だけでよい)を教えてください。</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
||
gets(buf);
|
||
n = atoi(buf);
|
||
<span class="synStatement">if</span> (n < <span class="synConstant"></span> || <span class="synConstant">24</span> < n) {
|
||
printf (<span class="synConstant">"Hour must be:</span><span class="synSpecial">\n</span><span class="synConstant"> 0 <= hour <= 24</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
||
}
|
||
<span class="synStatement">else</span> <span class="synStatement">if</span> (<span class="synConstant"></span> <= n && n <= <span class="synConstant">11</span>) {
|
||
printf (<span class="synConstant">"おはようございます。</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
||
}
|
||
<span class="synStatement">else</span> <span class="synStatement">if</span> (n == <span class="synConstant">12</span>) {
|
||
printf (<span class="synConstant">"お昼です。</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
||
}
|
||
<span class="synStatement">else</span> <span class="synStatement">if</span> (<span class="synConstant">13</span> <= n && n <= <span class="synConstant">18</span>) {
|
||
printf (<span class="synConstant">"こんにちわ。</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
||
}
|
||
<span class="synStatement">else</span> <span class="synStatement">if</span> (<span class="synConstant">19</span> <= n && n <= <span class="synConstant">23</span>) {
|
||
printf (<span class="synConstant">"こんばんわ。</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
||
}
|
||
<span class="synStatement">return</span>(<span class="synConstant"></span>);
|
||
}
|
||
</pre>
|
||
|
||
<p>
|
||
境界にあたる場合の条件に注意。自分の場合、「=」をつけ忘れていた。
|
||
</p>
|
||
|
||
<h4>
|
||
ここまでで Perl と違うと思った部分
|
||
</h4>
|
||
|
||
<ul>
|
||
<li>
|
||
必ず main から始まる
|
||
</li>
|
||
<li>
|
||
変数の方を最初に宣言しなければならない
|
||
</li>
|
||
<li>
|
||
Perl だとコンテクストに応じて様々な姿を変数はとるけれど、 C だと自分で型を変換しなければならない(→ atoi )
|
||
</li>
|
||
<li>
|
||
C の char 型は一文字しか格納できない。だから配列として宣言して文字列を格納している…ようだ
|
||
</li>
|
||
</ul>
|
||
|
||
<p>
|
||
自分は Perl のコンテクストみたいな考えの方が好きかもしれない。
|
||
</p>
|
||
</div> |