blog/content/post/2009/05/30/2009-05-30-00001164.md

104 lines
6.4 KiB
Markdown

---
title: 『プログラミング言語C 第2版 ANSI規格準拠』の勉強
author: kazu634
date: 2009-05-30
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:4623;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- C
---
<div class="section">
<p>
ようやく関数の項目まで入ってきました。とりあえず打ち込んだサンプルだけ、貼り付けておきますね。
</p>
<p>
今回は簡単な grep の実装だそうです:
</p>
<pre class="syntax-highlight">
<span class="synPreProc">#include </span><span class="synConstant">&#60;stdio.h&#62;</span>
<span class="synPreProc">#define MAXLINE </span><span class="synConstant">1000</span>
<span class="synComment">/* prototype declarations: */</span>
<span class="synType">int</span> getline(<span class="synType">char</span> line[], <span class="synType">int</span> max);
<span class="synType">int</span> strindex(<span class="synType">char</span> source[], <span class="synType">char</span> searchfor[]);
<span class="synComment">/* Declaration */</span>
<span class="synType">char</span> pattern[] = <span class="synConstant">&#34;ould&#34;</span>;
<span class="synComment">/* program begins here: */</span>
<span class="synType">int</span> main(<span class="synType">int</span> argc, <span class="synType">char</span> *argv[]) {
<span class="synType">char</span> line[MAXLINE];
<span class="synType">int</span> found = <span class="synConstant"></span>;
<span class="synStatement">while</span> (getline(line, MAXLINE) &#62; <span class="synConstant"></span>){
<span class="synStatement">if</span> (strindex(line, pattern) &#62;= <span class="synConstant"></span>){
printf(<span class="synConstant">&#34;</span><span class="synSpecial">%s</span><span class="synConstant">&#34;</span>, line);
found++;
}
}
printf(<span class="synConstant">&#34;</span><span class="synSpecial">%d</span><span class="synConstant">&#34;</span>, found);
<span class="synStatement">return</span> <span class="synConstant"></span>;
}
<span class="synComment">/* getline: sに行を入れて、長さを返す*/</span>
<span class="synType">int</span> getline(<span class="synType">char</span> s[], <span class="synType">int</span> lim)
{
<span class="synType">int</span> c, i;
i = <span class="synConstant"></span>;
<span class="synStatement">while</span> (--lim &#62; <span class="synConstant"></span> &#38;&#38; (c = getchar()) != <span class="synConstant">EOF</span> &#38;&#38; c != <span class="synSpecial">'\n'</span>) {
s[i++] = c;
}
<span class="synStatement">if</span> (c == <span class="synSpecial">'\n'</span>){
s[i++] = c;
}
s[i] = <span class="synSpecial">'\0'</span>;
<span class="synStatement">return</span> i;
}
<span class="synComment">/* strindex: sにおけるtのインデックスを返す。どこにもなければ-1を返す */</span>
<span class="synType">int</span> strindex(<span class="synType">char</span> s[], <span class="synType">char</span> t[])
{
<span class="synType">int</span> i, j, k;
<span class="synStatement">for</span> (i = <span class="synConstant"></span>; s[i] != <span class="synSpecial">'\0'</span>; i++) {
<span class="synStatement">for</span> (j = i, k = <span class="synConstant"></span>; t[k] != <span class="synSpecial">'\0'</span> &#38;&#38; s[j] == t[k]; j++, k++)
;
<span class="synStatement">if</span> (k &#62; <span class="synConstant"></span> &#38;&#38; t[k] == <span class="synSpecial">'\0'</span>) {
<span class="synStatement">return</span> i;
}
}
<span class="synStatement">return</span> -<span class="synConstant">1</span>;
}
</pre>
<div class="hatena-asin-detail">
<a href="http://www.amazon.co.jp/dp/4320026926/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4320026926/?tag=hatena_st1-22&ascsubtag=d-7ibv', '');"><img src="https://images-na.ssl-images-amazon.com/images/I/41W69WGATNL._SL160_.jpg" class="hatena-asin-detail-image" alt="プログラミング言語C 第2版 ANSI規格準拠" title="プログラミング言語C 第2版 ANSI規格準拠" /></a></p>
<div class="hatena-asin-detail-info">
<p class="hatena-asin-detail-title">
<a href="http://www.amazon.co.jp/dp/4320026926/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4320026926/?tag=hatena_st1-22&ascsubtag=d-7ibv', 'プログラミング言語C 第2版 ANSI規格準拠');">プログラミング言語C 第2版 ANSI規格準拠</a>
</p>
<ul>
<li>
<span class="hatena-asin-detail-label">作者:</span> <a href="http://d.hatena.ne.jp/keyword/B%2EW%2E%A5%AB%A1%BC%A5%CB%A5%CF%A5%F3" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/B%2EW%2E%A5%AB%A1%BC%A5%CB%A5%CF%A5%F3', 'B.W.カーニハン');" class="keyword">B.W.カーニハン</a>,<a href="http://d.hatena.ne.jp/keyword/D%2EM%2E%A5%EA%A5%C3%A5%C1%A1%BC" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/D%2EM%2E%A5%EA%A5%C3%A5%C1%A1%BC', 'D.M.リッチー');" class="keyword">D.M.リッチー</a>,<a href="http://d.hatena.ne.jp/keyword/%C0%D0%C5%C4%C0%B2%B5%D7" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%C0%D0%C5%C4%C0%B2%B5%D7', '石田晴久');" class="keyword">石田晴久</a>
</li>
<li>
<span class="hatena-asin-detail-label">出版社/メーカー:</span> <a href="http://d.hatena.ne.jp/keyword/%B6%A6%CE%A9%BD%D0%C8%C7" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%B6%A6%CE%A9%BD%D0%C8%C7', '共立出版');" class="keyword">共立出版</a>
</li>
<li>
<span class="hatena-asin-detail-label">発売日:</span> 1989/06/15
</li>
<li>
<span class="hatena-asin-detail-label">メディア:</span> 単行本
</li>
<li>
<span class="hatena-asin-detail-label">購入</span>: 28人 <span class="hatena-asin-detail-label">クリック</span>: 721回
</li>
<li>
<a href="http://d.hatena.ne.jp/asin/4320026926" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4320026926', 'この商品を含むブログ (206件) を見る');" target="_blank">この商品を含むブログ (206件) を見る</a>
</li>
</ul>
</div>
<div class="hatena-asin-detail-foot">
</div>
</div>
</div>