回答編集履歴
5
変更
answer
CHANGED
@@ -2,30 +2,29 @@
|
|
2
2
|
10件毎に保存メモリを拡張します
|
3
3
|
|
4
4
|
```c
|
5
|
-
#include<stdio.h>
|
6
|
-
#include<string.h>
|
7
|
-
#include<stdlib.h>
|
8
|
-
|
9
5
|
void main(void)
|
10
6
|
{
|
11
7
|
int ret;
|
12
8
|
int cnt=0;
|
13
9
|
int *mbuf;
|
10
|
+
int ii;
|
14
|
-
mbuf = (int *)malloc(sizeof(int) *
|
11
|
+
mbuf = (int *)malloc(sizeof(int) * 11);
|
15
12
|
while( fscanf( stdin , "%d" , &ret) != EOF ) {
|
16
|
-
printf( "ret=%d\n" , ret );
|
17
|
-
|
13
|
+
if ( cnt !=0 && cnt%10 == 0){
|
18
|
-
|
14
|
+
mbuf = (int *)realloc( mbuf, sizeof(int) * (cnt * 2) );
|
19
|
-
|
15
|
+
if( mbuf == NULL ){
|
20
|
-
|
16
|
+
printf( "メモリ確保に失敗\n" );
|
21
|
-
|
17
|
+
free( mbuf );
|
22
|
-
|
18
|
+
return;
|
23
|
-
}
|
24
|
-
// printf( "メモリ確保 %d\n", cnt );
|
25
19
|
}
|
20
|
+
printf( "メモリ確保 %d\n", cnt );
|
21
|
+
}
|
26
|
-
|
22
|
+
mbuf[cnt] = ret;
|
27
|
-
|
23
|
+
cnt++;
|
28
24
|
}
|
25
|
+
for (ii=0; ii<cnt; ii++){
|
26
|
+
printf (" %d", mbuf[ii]);
|
27
|
+
}
|
29
28
|
free(mbuf);
|
30
29
|
}
|
31
30
|
```
|
4
改修
answer
CHANGED
@@ -2,28 +2,31 @@
|
|
2
2
|
10件毎に保存メモリを拡張します
|
3
3
|
|
4
4
|
```c
|
5
|
+
#include<stdio.h>
|
6
|
+
#include<string.h>
|
7
|
+
#include<stdlib.h>
|
8
|
+
|
5
9
|
void main(void)
|
6
10
|
{
|
7
11
|
int ret;
|
8
12
|
int cnt=0;
|
9
13
|
int *mbuf;
|
10
|
-
int *tbuf;
|
11
|
-
mbuf = (int *)malloc(
|
14
|
+
mbuf = (int *)malloc(sizeof(int) * 10 + 1);
|
12
15
|
while( fscanf( stdin , "%d" , &ret) != EOF ) {
|
13
16
|
printf( "ret=%d\n" , ret );
|
14
|
-
mbuf[cnt] = ret;
|
15
|
-
cnt++;
|
16
|
-
if ( cnt%10 == 0){
|
17
|
+
if ( cnt !=0 && cnt%10 == 0){
|
17
|
-
printf( "メモリ確保\n" );
|
18
|
-
|
18
|
+
mbuf = (int *)realloc( mbuf, sizeof(int) * cnt * 2);
|
19
|
-
if(
|
19
|
+
if( mbuf == NULL ){
|
20
20
|
printf( "メモリ確保に失敗\n" );
|
21
21
|
free( mbuf );
|
22
22
|
return;
|
23
23
|
}
|
24
|
-
|
24
|
+
// printf( "メモリ確保 %d\n", cnt );
|
25
25
|
}
|
26
|
+
mbuf[cnt] = ret;
|
27
|
+
cnt++;
|
26
28
|
}
|
29
|
+
free(mbuf);
|
27
30
|
}
|
28
31
|
```
|
29
32
|
|
3
hennkou
answer
CHANGED
@@ -1,14 +1,28 @@
|
|
1
1
|
簡単ですが、標準入力を受け付けて最後はctrl+dで終了します。
|
2
|
+
10件毎に保存メモリを拡張します
|
2
3
|
|
3
4
|
```c
|
4
|
-
#include<stdio.h>
|
5
|
-
#include<string.h>
|
6
|
-
|
7
5
|
void main(void)
|
8
6
|
{
|
9
7
|
int ret;
|
8
|
+
int cnt=0;
|
9
|
+
int *mbuf;
|
10
|
+
int *tbuf;
|
11
|
+
mbuf = (int *)malloc( sizeof(ret) * 10 );
|
10
|
-
while( fscanf( stdin , "%d" , &ret
|
12
|
+
while( fscanf( stdin , "%d" , &ret) != EOF ) {
|
11
13
|
printf( "ret=%d\n" , ret );
|
14
|
+
mbuf[cnt] = ret;
|
15
|
+
cnt++;
|
16
|
+
if ( cnt%10 == 0){
|
17
|
+
printf( "メモリ確保\n" );
|
18
|
+
tbuf = (int *)realloc( mbuf, sizeof(ret) * 10);
|
19
|
+
if( tbuf == NULL ){
|
20
|
+
printf( "メモリ確保に失敗\n" );
|
21
|
+
free( mbuf );
|
22
|
+
return;
|
23
|
+
}
|
24
|
+
mbuf = tbuf;
|
25
|
+
}
|
12
26
|
}
|
13
27
|
}
|
14
28
|
```
|
2
変更
answer
CHANGED
@@ -15,4 +15,4 @@
|
|
15
15
|
|
16
16
|
端末からの入力のできますが、パイプやリダイレクションでも良いです。
|
17
17
|
cat file1 | ./progA
|
18
|
-
progA < file1
|
18
|
+
./progA < file1
|
1
補足
answer
CHANGED
@@ -11,4 +11,8 @@
|
|
11
11
|
printf( "ret=%d\n" , ret );
|
12
12
|
}
|
13
13
|
}
|
14
|
-
```
|
14
|
+
```
|
15
|
+
|
16
|
+
端末からの入力のできますが、パイプやリダイレクションでも良いです。
|
17
|
+
cat file1 | ./progA
|
18
|
+
progA < file1
|