開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: C
- Clang (コンパイラ)
プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第5章(ポインタと配列)、5.1(ポインタとアドレス)、5.2(ポインタと関数引数)、演習5-2を解いてみる。
その他参考書籍
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
演習 5-2.
getfloatはint型のデータを関数値として返す。
コード
sample.c
#include <ctype.h>
int getch(void);
void ungetch(int);
int getfloat(float *pn)
{
int c, sign, d, pow;
while (isspace(c = getch()))
;
if (!isdigit(c) && c != EOF && c != '+' && c != '-' && c != '.') {
ungetch(c);
return 0;
}
sign = (c == '-') ? -1 : 1;
if (c == '+' || c == '-') {
d = c;
if (!isdigit(c = getch())) {
ungetch(d);
}
ungetch(c);
return d;
}
for (*pn = 0; isdigit(c); c = getch()) {
*pn = 10.0 * *pn + (c - '0');
}
if (c == '.') {
c = getch();
}
for (pow = 1; isdigit(c); c = getch()) {
*pn = 10.0 * *pn + (c - '0');
pow *= 10.0;
}
*pn *= sign / pow;
if (c != EOF) {
ungetch(c);
}
return c;
}
こんな感じでいいのかなぁ。
0 コメント:
コメントを投稿