練習問題
#include <stdio.h>
#include <stdlib.h> /* srand(), rand() */ #include <time.h> /* time() */
int main(void) {
int i;
double a[5];
char filename[21] = "data.txt";
???? *fp; /* ファイルポインタの宣言 */ /* 乱数発生(0~1000までの乱数を100で割った値) */ srand((unsigned int)time(NULL)); for(i = 0; i < 5; i++){ a[i] = rand() % 1000 / 100.0; } /* ファイルを書き込みモードでオープンする */
if((fp = ?????(filename, ????)) == NULL){
printf("ファイルオープンに失敗しました.¥n"); return 0; }
練習問題1 ファイルへのデータ出力
•
配列 a[ ] の値をファイル data.txt に出力するプログラムを作成しなさい
1.23
2.45
3.56
4.78
5.91
data.txt
/* ファイルへデータを書き込む */ for(i = 0; i < 5; i++) ???????(fp, "%.2lf¥n", a[i]); fclose(fp); return 0; }data.txtファイルが
できるので,
メモ帳などで確認
してみる
#include <stdio.h> #define N 5
int main(void) {
int i, n;
double a[N], sum;
char filename[21] = "data.txt"; FILE *fp;
/* ファイルを読み込みモードでオープンし */ /* ファイルポインタfpに接続 */
if((?? = fopen(filename, ????)) == NULL){
printf("ファイルオープンに失敗しました.¥n"); return 0;
}
/* ファイルの最後まで(EOFまで)読み込む */
i = 0;
while(??????(??, "%lf", &a[i]) != EOF){ i++; } n = i;
練習問題2 ファイルからのデータ入力
•
練習問題1で作成されたファイル data.txt からデータを読み込んで配列
a[ ] に格納したあと,配列 a[ ] の合計を計算するプログラムを作成しなさい
sum = 0.0; for(i = 0; i < n; i++){ printf("a[%d] = %.2lf¥n", i, a[i]); sum += a[i]; } printf("合計:%.2lf¥n", sum); ??????(fp); /* ファイルをクローズ */ return 0; }1.23
2.45
3.56
4.78
5.91
data.txt
1.23
2.45
3.56
4.78
a[0]
a[1]
a[2]
a[3]
読み込む
#include <stdio.h> #define N 256 int main(void) { int i, counta; char buf[N];
char fnam[21] = "abstract.txt"; FILE *fp;
/* ファイルを読み込みモードでオープン */
if((fp = fopen(fnam, ????)) == NULL){
printf("[%s]:オープンに失敗¥n", fnam); return 0;
}
練習問題3 ファイルからの文字列入力
•
英文ファイル abstract.txt に含まれる 'a' および 'A' の文字数を数える
プログラムを作成しなさい
counta = 0;
/* ファイルから1行読み込むのをNULLまで繰り返す */
while(?????(buf, N, ??) != NULL){ i = 0; while(buf[i] != ????){ /* 文字列の最後まで */ /* 文字列の文字が'a'もしくは'A'であるかどうか */ if(buf[i] == ??? || buf[i] == ???){ ????????? /* そうであればcountaを+1*/ } ???? /* 次の文字を見る*/ } } fclose(fp); printf("‘a’ および ‘A’ は "); printf("%d 文字含まれる.¥n", counta); return 0; }
練習問題3 ファイルからの文字列入力(解説)
A new method is proposed for detection of the
temporal changes using 3-dimensional segmentation. The method is a kind of clustering methods for
temporal changes.[改行]
In the method, multi-temporal images form a image block in 3-dimensional space; x-y plane and time axis. [改行]
The image block is firstly divided into spatially uniform sub-blocks by applying binary division process. [改行]
The division rule is based on the statistical t-test using Mahalanobis distance between spatial
coefficient vectors of a local regression model fitted to neighboring sub-blocks to be divided. [改行]
The divided sub-blocks are, then, merged into clusters by using a clustering technique. [改行]
The block based processing such as the spatial segmentation technique is very effective in reduction of apparent changes due to noise. [改行]
Temporal change is detected as a boundary perpendicular to the time axis in the segmentation result. [改行]
The proposed method is successfully applied to actual multitemporal and multispectral LANDSAT/TM images. [EOF]
abstract.txt
ファイルから最大255文字
読み込み,buf [ ] に格納
char buf [ ] =
" A new method is ~ changes. "
A
n
e
w
e
s
.
¥0
m e
t
n
g
a
c
h
~
~
[0]
buf [ ]
[1] [2] [3] [4] [5] [6] [7] [8]
[90] [91] [92] [93] [94] [95] [96] [97] [98]
※ fgets によって[改行] まで読み込まれる
すなわち
実際には[改行]の
練習問題3 ファイルからの文字列入力(解説)
A
n
e
w
m e
t
~
c
h
a
n
g
e
s
.
¥0
[0]
buf [ ]
[1] [2] [3] [4] [5] [6] [7] [8]
[90] [91] [92] [93] [94] [95] [96] [97] [98]
文字列 buf [ ] に格納されている文字を1文字ずつ検索していく
'a' or 'A' ?
真
counta を +1 して次の文字を見る
A
n
e
w
m e
t
~
c
h
a
n
g
e
s
.
¥0
[0]
buf [ ]
[1] [2] [3] [4] [5] [6] [7] [8]
[90] [91] [92] [93] [94] [95] [96] [97] [98]
'a' or 'A' ?
偽
何もしないで次の文字を見る
A
n
e
w
m e
t
~
c
h
a
n
g
e
s
.
¥0
[0]
buf [ ]
[1] [2] [3] [4] [5] [6] [7] [8]
[90] [91] [92] [93] [94] [95] [96] [97] [98]
'¥0' であれば,終了
練習問題3 ファイルからの文字列入力(解説)
A new method is proposed for detection of the
temporal changes using 3-dimensional segmentation. The method is a kind of clustering methods for
temporal changes.[改行]
In the method, multi-temporal images form a image block in 3-dimensional space; x-y plane and time axis. [改行]
The image block is firstly divided into spatially uniform sub-blocks by applying binary division process. [改行]
The division rule is based on the statistical t-test using Mahalanobis distance between spatial
coefficient vectors of a local regression model fitted to neighboring sub-blocks to be divided. [改行]
The divided sub-blocks are, then, merged into clusters by using a clustering technique. [改行]
The block based processing such as the spatial segmentation technique is very effective in reduction of apparent changes due to noise. [改行]
Temporal change is detected as a boundary perpendicular to the time axis in the segmentation result. [改行]
The proposed method is successfully applied to actual multitemporal and multispectral LANDSAT/TM