• 検索結果がありません。

July prog11-02.c /*, */ /* $Id: prog11-02.c,v :48:07+09 naito Exp $ */ #include <stdio.h> #define N 10 int main(int argc, cha

N/A
N/A
Protected

Academic year: 2021

シェア "July prog11-02.c /*, */ /* $Id: prog11-02.c,v :48:07+09 naito Exp $ */ #include <stdio.h> #define N 10 int main(int argc, cha"

Copied!
14
0
0

読み込み中.... (全文を見る)

全文

(1)

/* 西 暦 Y 年 M 月 N 日 が, そ の 年 の 第 何 日 目 に な る か を 返 す 関 数 */ /* $Id: prog11-01.c,v 1.2 2002-07-01 21:39:41+09 naito Exp $ */

/* 西 暦 Y 年 M 月 N 日 が, そ の 年 の 第 何 日 目 に な る か を 返 す 関 数

* 戻 り 値 : M, N が 正 常 な 範 囲 に あ れ ば, 日 数 を 返 す. * そ う で な い 場 合, M が 範 囲 を 越 え て い れ ば -1, * そ う で な い 場 合, N が 範 囲 を 越 え て い れ ば -2 * を 返 す. */

int days(const unsigned int year, const unsigned int month, const unsigned int day) {

static unsigned int days_for_month_n[] = {31,28,31,30,31,30,31,31,30,31,30,31} ; /* 平 年 */

static unsigned int days_for_month_l[] = {31,29,31,30,31,30,31,31,30,31,30,31} ; /* 閏 年 */

unsigned int *days_for_month ; int i, n=0 ; days_for_month = days_for_month_n ; if (is_leap_year(year)) /* 閏 年 の 処 理 */ days_for_month = days_for_month_l ; if ((month==0)||(month > 12)) return -1 ; /* M が 範 囲 を 越 え て い る : エ ラ ー */ if ((!day)||(days_for_month[month-1] < day)) return -2 ; /* D が 範 囲 を 越 え て い る : エ ラ ー */ for(i=0;i<month-1;i++) n += *(days_for_month+i) ; n += day ; return n ; }

(2)

/* 配 列 と ポ イ ン タ の 等 価 性 と, ポ イ ン タ の イ ン ク リ メ ン ト */

/* $Id: prog11-02.c,v 1.1 2002-07-01 21:48:07+09 naito Exp $ */

#include <stdio.h> #define N 10

int main(int argc, char **argv) { int a[N] ; int i ; for(i=0;i<N;i++) *(a+i) = i ; for(i=0;i<N;i++) /* こ こ の a+i の 出 力 が い く つ 増 分 し て い る か に 注 意 */

printf("a[%d] = %d, address of a[%d] = %p\n", i, a[i], i, a+i) ; /* ************ */

printf("\"sizeof a\" = %lu\n", sizeof(a)) ; printf("\"sizeof a[0]\" = %lu\n", sizeof(a[0])) ; printf("\"sizeof *a\" = %lu\n", sizeof(*a)) ;

printf("\"sizeof(a)/sizeof(a[0])\" = %lu\n", sizeof(a)/sizeof(a[0])) ; /* こ れ ら の 返 す 値 に 注 意. */ /* ************ */ /* こ ん な 邪 悪 な 書 き 方 も 可 能 で あ る. */ i = 0 ; printf("i[a] = %d\n", 9[a]) ; printf("i[a] = %d\n", i[a]) ; printf("*(i+a) = %d\n", *(9+a)) ; printf("*(i+a) = %d\n", *(a+9)) ; return 0 ; }

(3)

/* 文 字 列 の 扱 い */

/* $Id: prog11-03.c,v 1.1 2002-07-01 21:52:15+09 naito Exp $ */

#include <stdio.h>

int _strcpy_0(char *, const char *) ; int _strcpy_1(char *, const char *) ;

int main(int argc, char **argv) {

char *src = "This is a test." ; char tgt[100] ; _strcpy_0(tgt, src) ; printf("%s\n", tgt) ; _strcpy_1(tgt, src) ; printf("%s\n", tgt) ; return 0 ; }

int _strcpy_0(char *t, const char *s) {

while(*t++ = *s++) ; return 0 ;

}

int _strcpy_1(char *t, const char *s) {

int i = 0 ;

while(t[i] = s[i]) i += 1 ; return 0 ;

(4)

/* argc, argv の 扱 い */

/* $Id: prog11-04.c,v 1.2 2002-07-01 21:54:16+09 naito Exp $ */

#include <stdio.h>

int main(int argc, char **argv) { int i ; for(i=0;i<argc;i++) printf("%d: %s\n", i, argv[i]) ; return 0 ; }

(5)

/* 標 準 入 力 か ら 文 字 列 を 読 み 取 る

* 標 準 入 力 か ら 読 み 取 っ た 文 字 列 は,

* 空 白 で 区 切 ら れ た 「 整 数 値 」 で あ る と 仮 定 し て, * そ の 「 整 数 値 」 を int 型 配 列 に 格 納 す る.

* 配 列 の 要 素 数 を 越 え た と き に は, エ ラ ー を 返 す */

/* $Id: prog11-05.c,v 1.1 2002-07-02 11:47:08+09 naito Exp $ */

#include <stdio.h> #include <stdlib.h> #include <ctype.h> #define MAXLINE 1024 #define N 10

int get_int_values(char *, int *, unsigned long) ; int is_separator(int) ;

int is_eol(int) ;

int _strncpy(char *,char *, size_t) ;

int main(int argc, char **argv) {

char buf[MAXLINE] ; int a[N], n, i ; while(!feof(stdin)) {

if (fgets(buf, MAXLINE, stdin) != NULL) { printf("buf = %s\n", buf) ; n = get_int_values(buf, a, N) ; if (n < 0) { fprintf(stderr, "error\n") ; continue ; } for(i=0;i<n;i++) {

fprintf(stdout, "a[%d] = %d\n", i, a[i]) ; } } } return 0 ; } /* buf に 与 え ら れ た 数 値 列 を 「 空 白 」 で 区 切 り, * 配 列 a に int 値 と し て 格 納 す る. * a の 最 大 数 は limit * 戻 り 値 : 格 納 し た 要 素 数 * 要 素 数 が limit を 越 え る 場 合 に は -1 を 返 す. */

int get_int_values(char *buf, int *a, unsigned long limit) { char *p, *q, temp[MAXLINE] ; unsigned long l=0L ; p = buf ; q = buf ; /* 先 頭 に 空 白 が 入 っ た と き の 処 理 */ while(isspace((int)*p)) p += 1 ; /* 行 末 ま で 読 み 取 る */ while(!is_eol((int)*p)) { /* 空 白 に 区 切 る */ while(!is_separator((int)*p)) p += 1 ; _strncpy(temp, q, p-q) ; *(a+l) = atoi(temp) ; l += 1 ; if (l > limit) return -1 ; p += 1 ; q = p ; } return l ;

(6)

} /* isspace, 改 行 文 字, 文 字 列 終 端 の 時 に 1 を 返 す */ int is_separator(int c) { if (isspace(c)) return 1 ; if (c == ’\n’) return 1 ; if (c == 0x00) return 1 ; return 0 ; } /* 改 行 文 字, 文 字 列 終 端 の 時 に 1 を 返 す */ int is_eol(int c) { if (c == ’\n’) return 1 ; if (c == 0x00) return 1 ; return 0 ; } /* 文 字 列 の コ ピ ー. 最 大 文 字 数 n */

int _strncpy(char *t, char *s, size_t n) { char *p ; p = s ; while((p-s < n)&&(*t++ = *p++)) ; *t = 0x00 ; return 0 ; }

(7)

/* 標 準 入 力 か ら 文 字 列 を 読 み 取 る

* 標 準 入 力 か ら 読 み 取 っ た 文 字 列 は,

* 空 白 で 区 切 ら れ た 「 整 数 値 」 で あ る と 仮 定 し て, * そ の 「 整 数 値 」 を int 型 配 列 に 格 納 す る.

* 配 列 の 要 素 数 を 越 え た と き に は, エ ラ ー を 返 す */

/* $Id: prog11-06-1.c,v 1.2 2002-07-02 12:04:14+09 naito Exp $ */

#include <stdio.h> #include "prog11-06.h"

int main(int argc, char **argv) {

char buf[MAXLINE] ; int a[N], n, i ; while(!feof(stdin)) {

if (fgets(buf, MAXLINE, stdin) != NULL) { printf("buf = %s\n", buf) ; n = get_int_values(buf, a, N) ; if ((n < 0)||(_atoi_error)) { fprintf(stderr, "error\n") ; continue ; } for(i=0;i<n;i++) {

fprintf(stdout, "a[%d] = %d\n", i, a[i]) ; }

} }

return 0 ; }

(8)

/* buf に 与 え ら れ た 数 値 列 を 「 空 白 」 で 区 切 り, * 配 列 a に int 値 と し て 格 納 す る. * a の 最 大 数 は limit * 戻 り 値 : 格 納 し た 要 素 数 * 要 素 数 が limit を 越 え る 場 合 に は -1 を 返 す. */

/* Library Source Code */

/* $Id: prog11-06-2.c,v 1.2 2002-07-02 12:04:10+09 naito Exp $ */ #include <stdio.h>

#include <stdlib.h> #include <ctype.h> #include "prog11-06.h"

static int _atoi_error ;

int get_int_values(char *buf, int *a, unsigned long limit) { char *p, *q, temp[MAXLINE] ; unsigned long l=0L ; _error = 0 ; p = buf ; q = buf ; /* 先 頭 に 空 白 が 入 っ た と き の 処 理 */ while(isspace((int)*p)) p += 1 ; /* 行 末 ま で 読 み 取 る */ while(!is_eol((int)*p)) { /* 空 白 に 区 切 る */ while(!is_separator((int)*p)) p += 1 ; _strncpy(temp, q, p-q) ; *(a+l) = _atoi(temp) ; /* _atoi 関 数 内 で エ ラ ー を 検 出 し た ら, _error を セ ッ ト */ if (_atoi_error) _error = 1 ; l += 1 ; if (l > limit) return -1 ; p += 1 ; q = p ; } return l ; } /* isspace, 改 行 文 字, 文 字 列 終 端 の 時 に 1 を 返 す */ int is_separator(int c) { if (isspace(c)) return 1 ; if (c == ’\n’) return 1 ; if (c == 0x00) return 1 ; return 0 ; } /* 改 行 文 字, 文 字 列 終 端 の 時 に 1 を 返 す */ int is_eol(int c) { if (c == ’\n’) return 1 ; if (c == 0x00) return 1 ; return 0 ; } /* 文 字 列 の コ ピ ー. 最 大 文 字 数 n */

int _strncpy(char *t, char *s, size_t n) { char *p ; p = s ; while((p-s < n)&&(*t++ = *p++)) ; *t = 0x00 ;

(9)

return 0 ; } /* atoi 関 数 * 桁 あ ふ れ 考 慮 な し * 非 整 数 値 の 場 合 に は, 0 を 返 し, _atoi_error に 1 を セ ッ ト す る * ASCII コ ー ド を 仮 定 * * 以 下 の フ ォ ー マ ッ ト の も の だ け を 数 値 と 判 定 * [空 白]*(+|-)[0-9]+ */ int _atoi(char *s) { char *p ; int r=0 ; int sign=0 ; _atoi_error = 0 ; p = s ; /* 空 白 を 読 み 飛 ば す */ while(isspace((int)*s)) s += 1 ; /* 符 号 を 確 認 */ if (*s == ’-’) { sign = 1 ; s += 1 ; } else if (*s == ’+’) s += 1 ; while(!is_eol((int)*s)) { r *= 10 ; if (isdigit((int)*s)) r += (*s) - ’0’ ; else { /* 非 数 値 な ら エ ラ ー */ _atoi_error = 1 ; return 0 ; } s += 1 ; } return sign ? -r : r ; }

(10)

/* Program 11-06 Header file */

/* $Id: prog11-06.h,v 1.3 2002-07-02 12:02:17+09 naito Exp $ */

#include <stdio.h> #define MAXLINE 1024 #define N 10

/* プ ロ ト タ イ プ 宣 言 */

int get_int_values(char *, int *, unsigned long) ; int is_separator(int) ;

int is_eol(int) ;

int _strncpy(char *,char *, size_t) ; int _atoi(char *) ;

/* 大 域 変 数 */ int _error ;

(11)

SRC = prog11-06-1.c prog11-06-2.c OBJ = $(SRC:.c=.o) PROG = prog11-06 CC = gcc all: $(PROG) $(PROG): $(OBJ) gcc $(OBJ) -o $(PROG) clean:

(12)

/*

* Copyright (c) 1988, 1993

* The Regents of the University of California. All rights reserved. *

* Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met:

* 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement:

* This product includes software developed by the University of * California, Berkeley and its contributors.

* 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission.

*

* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ‘‘AS IS’’ AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE.

*/

#if defined(LIBC_SCCS) && !defined(lint)

static char sccsid[] = "@(#)atoi.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */

#include <stdlib.h> #include <stddef.h> int atoi(str) const char *str; {

return((int)strtol(str, (char **)NULL, 10)); }

(13)

* Copyright (c) 1990, 1993

* The Regents of the University of California. All rights reserved. *

* Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met:

* 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement:

* This product includes software developed by the University of * California, Berkeley and its contributors.

* 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission.

*

* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ‘‘AS IS’’ AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE.

*/

#if defined(LIBC_SCCS) && !defined(lint)

static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */

#include <limits.h> #include <ctype.h> #include <errno.h> #include <stdlib.h>

/*

* Convert a string to a long integer. *

* Ignores ‘locale’ stuff. Assumes that the upper and lower case * alphabets and digits are each contiguous.

*/ long

strtol(nptr, endptr, base) const char *nptr; char **endptr; register int base; {

register const char *s = nptr; register unsigned long acc; register unsigned char c; register unsigned long cutoff; register int neg = 0, any, cutlim;

/*

* Skip white space and pick up leading +/- sign if any. * If base is 0, allow 0x for hex and 0 for octal, else * assume decimal; if base is already 16, allow 0x. */

(14)

do { c = *s++; } while (isspace(c)); if (c == ’-’) { neg = 1; c = *s++; } else if (c == ’+’) c = *s++;

if ((base == 0 || base == 16) &&

c == ’0’ && (*s == ’x’ || *s == ’X’)) { c = s[1]; s += 2; base = 16; } if (base == 0) base = c == ’0’ ? 8 : 10; /*

* Compute the cutoff value between legal numbers and illegal * numbers. That is the largest legal value, divided by the * base. An input number that is greater than this value, if * followed by a legal input character, is too big. One that * is equal to this value may be valid or not; the limit

* between valid and invalid numbers is then based on the last * digit. For instance, if the range for longs is

* [-2147483648..2147483647] and the input base is 10, * cutoff will be set to 214748364 and cutlim to either

* 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated * a value > 214748364, or equal but the next digit is > 7 (or 8), * the number is too big, and we will return a range error.

*

* Set any if any ‘digits’ consumed; make it negative to indicate * overflow.

*/

cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; cutlim = cutoff % (unsigned long)base;

cutoff /= (unsigned long)base; for (acc = 0, any = 0;; c = *s++) { if (!isascii(c))

break; if (isdigit(c)) c -= ’0’; else if (isalpha(c))

c -= isupper(c) ? ’A’ - 10 : ’a’ - 10; else

break; if (c >= base) break;

if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) any = -1; else { any = 1; acc *= base; acc += c; } } if (any < 0) {

acc = neg ? LONG_MIN : LONG_MAX; errno = ERANGE;

} else if (neg) acc = -acc; if (endptr != 0)

*endptr = (char *)(any ? s - 1 : nptr); return (acc);

参照

関連したドキュメント

H ernández , Positive and free boundary solutions to singular nonlinear elliptic problems with absorption; An overview and open problems, in: Proceedings of the Variational

Keywords: Convex order ; Fréchet distribution ; Median ; Mittag-Leffler distribution ; Mittag- Leffler function ; Stable distribution ; Stochastic order.. AMS MSC 2010: Primary 60E05

The periodic unfolding method for the classical homogenization was introduced in Cioranescu, Damlamian and Griso [4] for fixed domains (see [5] for detailed proofs) and extended

For example, a maximal embedded collection of tori in an irreducible manifold is complete as each of the component manifolds is indecomposable (any additional surface would have to

Inside this class, we identify a new subclass of Liouvillian integrable systems, under suitable conditions such Liouvillian integrable systems can have at most one limit cycle, and

Then it follows immediately from a suitable version of “Hensel’s Lemma” [cf., e.g., the argument of [4], Lemma 2.1] that S may be obtained, as the notation suggests, as the m A

To derive a weak formulation of (1.1)–(1.8), we first assume that the functions v, p, θ and c are a classical solution of our problem. 33]) and substitute the Neumann boundary

The proof uses a set up of Seiberg Witten theory that replaces generic metrics by the construction of a localised Euler class of an infinite dimensional bundle with a Fredholm