blog/content/post/2008-03-23-00000857.md

118 lines
6.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
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">&#60;stdio.h&#62;</span>
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdlib.h&#62;</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">&#34;降水確率を入力してください。</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
gets(buf);
n = atoi(buf);
printf (<span class="synConstant">&#34;降水確率は </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">&#34;</span>, n);
<span class="synStatement">if</span> (n &#60; <span class="synConstant"></span> || <span class="synConstant">100</span> &#60; n) {
printf (<span class="synConstant">&#34;降水確率は0 - 100の間ですよ。</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
}
<span class="synStatement">else</span> <span class="synStatement">if</span> (n &#62;= <span class="synConstant">50</span>) {
printf (<span class="synConstant">&#34;傘を忘れずにね。</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
}
<span class="synStatement">else</span> {
printf (<span class="synConstant">&#34;傘はいりません。</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
}
printf (<span class="synConstant">&#34;いってらっしゃい</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</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">&#60;stdio.h&#62;</span>
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdlib.h&#62;</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">&#34;現在の時刻(「時」だけでよい)を教えてください。</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
gets(buf);
n = atoi(buf);
<span class="synStatement">if</span> (n &#60; <span class="synConstant"></span> || <span class="synConstant">24</span> &#60; n) {
printf (<span class="synConstant">&#34;Hour must be:</span><span class="synSpecial">\n</span><span class="synConstant"> 0 &#60;= hour &#60;= 24</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
}
<span class="synStatement">else</span> <span class="synStatement">if</span> (<span class="synConstant"></span> &#60;= n &#38;&#38; n &#60;= <span class="synConstant">11</span>) {
printf (<span class="synConstant">&#34;おはようございます。</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
}
<span class="synStatement">else</span> <span class="synStatement">if</span> (n == <span class="synConstant">12</span>) {
printf (<span class="synConstant">&#34;お昼です。</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
}
<span class="synStatement">else</span> <span class="synStatement">if</span> (<span class="synConstant">13</span> &#60;= n &#38;&#38; n &#60;= <span class="synConstant">18</span>) {
printf (<span class="synConstant">&#34;こんにちわ。</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
}
<span class="synStatement">else</span> <span class="synStatement">if</span> (<span class="synConstant">19</span> &#60;= n &#38;&#38; n &#60;= <span class="synConstant">23</span>) {
printf (<span class="synConstant">&#34;こんばんわ。</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</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>