回答編集履歴

5

変更

2016/08/05 03:04

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -5,14 +5,6 @@
5
5
 
6
6
 
7
7
  ```c
8
-
9
- #include<stdio.h>
10
-
11
- #include<string.h>
12
-
13
- #include<stdlib.h>
14
-
15
-
16
8
 
17
9
  void main(void)
18
10
 
@@ -24,33 +16,39 @@
24
16
 
25
17
  int *mbuf;
26
18
 
19
+ int ii;
20
+
27
- mbuf = (int *)malloc(sizeof(int) * 10 + 1);
21
+ mbuf = (int *)malloc(sizeof(int) * 11);
28
22
 
29
23
  while( fscanf( stdin , "%d" , &ret) != EOF ) {
30
24
 
31
- printf( "ret=%d\n" , ret );
25
+ if ( cnt !=0 && cnt%10 == 0){
32
26
 
33
- if ( cnt !=0 && cnt%10 == 0){
27
+ mbuf = (int *)realloc( mbuf, sizeof(int) * (cnt * 2) );
34
28
 
35
- mbuf = (int *)realloc( mbuf, sizeof(int) * cnt * 2);
29
+ if( mbuf == NULL ){
36
30
 
37
- if( mbuf == NULL ){
31
+ printf( "メモリ確保に失敗\n" );
38
32
 
39
- printf( "メモリ確保に失敗\n" );
33
+ free( mbuf );
40
34
 
41
- free( mbuf );
42
-
43
- return;
35
+ return;
44
-
45
- }
46
-
47
- // printf( "メモリ確保 %d\n", cnt );
48
36
 
49
37
  }
50
38
 
51
- mbuf[cnt] = ret;
39
+ printf( "メモリ確保 %d\n", cnt );
52
40
 
41
+ }
42
+
43
+ mbuf[cnt] = ret;
44
+
53
- cnt++;
45
+ cnt++;
46
+
47
+ }
48
+
49
+ for (ii=0; ii<cnt; ii++){
50
+
51
+ printf (" %d", mbuf[ii]);
54
52
 
55
53
  }
56
54
 

4

改修

2016/08/05 03:04

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -5,6 +5,14 @@
5
5
 
6
6
 
7
7
  ```c
8
+
9
+ #include<stdio.h>
10
+
11
+ #include<string.h>
12
+
13
+ #include<stdlib.h>
14
+
15
+
8
16
 
9
17
  void main(void)
10
18
 
@@ -16,25 +24,17 @@
16
24
 
17
25
  int *mbuf;
18
26
 
19
- int *tbuf;
20
-
21
- mbuf = (int *)malloc( sizeof(ret) * 10 );
27
+ mbuf = (int *)malloc(sizeof(int) * 10 + 1);
22
28
 
23
29
  while( fscanf( stdin , "%d" , &ret) != EOF ) {
24
30
 
25
31
  printf( "ret=%d\n" , ret );
26
32
 
27
- mbuf[cnt] = ret;
33
+ if ( cnt !=0 && cnt%10 == 0){
28
34
 
29
- cnt++;
35
+ mbuf = (int *)realloc( mbuf, sizeof(int) * cnt * 2);
30
36
 
31
- if ( cnt%10 == 0){
32
-
33
- printf( "メモリ確保\n" );
34
-
35
- tbuf = (int *)realloc( mbuf, sizeof(ret) * 10);
36
-
37
- if( tbuf == NULL ){
37
+ if( mbuf == NULL ){
38
38
 
39
39
  printf( "メモリ確保に失敗\n" );
40
40
 
@@ -44,11 +44,17 @@
44
44
 
45
45
  }
46
46
 
47
- mbuf = tbuf;
47
+ // printf( "メモリ確保 %d\n", cnt );
48
48
 
49
49
  }
50
50
 
51
+ mbuf[cnt] = ret;
52
+
53
+ cnt++;
54
+
51
55
  }
56
+
57
+ free(mbuf);
52
58
 
53
59
  }
54
60
 

3

hennkou

2016/08/05 02:52

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -1,14 +1,10 @@
1
1
  簡単ですが、標準入力を受け付けて最後はctrl+dで終了します。
2
+
3
+ 10件毎に保存メモリを拡張します
2
4
 
3
5
 
4
6
 
5
7
  ```c
6
-
7
- #include<stdio.h>
8
-
9
- #include<string.h>
10
-
11
-
12
8
 
13
9
  void main(void)
14
10
 
@@ -16,9 +12,41 @@
16
12
 
17
13
  int ret;
18
14
 
15
+ int cnt=0;
16
+
17
+ int *mbuf;
18
+
19
+ int *tbuf;
20
+
21
+ mbuf = (int *)malloc( sizeof(ret) * 10 );
22
+
19
- while( fscanf( stdin , "%d" , &ret ) != EOF ) {
23
+ while( fscanf( stdin , "%d" , &ret) != EOF ) {
20
24
 
21
25
  printf( "ret=%d\n" , ret );
26
+
27
+ mbuf[cnt] = ret;
28
+
29
+ cnt++;
30
+
31
+ if ( cnt%10 == 0){
32
+
33
+ printf( "メモリ確保\n" );
34
+
35
+ tbuf = (int *)realloc( mbuf, sizeof(ret) * 10);
36
+
37
+ if( tbuf == NULL ){
38
+
39
+ printf( "メモリ確保に失敗\n" );
40
+
41
+ free( mbuf );
42
+
43
+ return;
44
+
45
+ }
46
+
47
+ mbuf = tbuf;
48
+
49
+ }
22
50
 
23
51
  }
24
52
 

2

変更

2016/08/05 02:08

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -32,4 +32,4 @@
32
32
 
33
33
  cat file1 | ./progA
34
34
 
35
- progA < file1
35
+ ./progA < file1

1

補足

2016/08/05 01:27

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -25,3 +25,11 @@
25
25
  }
26
26
 
27
27
  ```
28
+
29
+
30
+
31
+ 端末からの入力のできますが、パイプやリダイレクションでも良いです。
32
+
33
+ cat file1 | ./progA
34
+
35
+ progA < file1