質問編集履歴
3
削除された内容の復元を行いました
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
c言語
|
1
|
+
昇順検査 c言語 コマンドライン
|
body
CHANGED
@@ -1,57 +1,113 @@
|
|
1
|
+
■やりたい事
|
2
|
+
コマンド行に整数値の列を収めたファイル名が与えられたとき、そのファイル名のファイルが昇順列を収めたものになっているかどうかを判定する。ここで、整数値の列が昇順列であるとは、並んでいる順にだんだんと値が大きくなる(その列の中で隣り合う2つの整数値 x, y が必ず x < y となっている)ことをいう。
|
1
|
-
|
3
|
+
与えられたファイル名のファイルの中身が昇順列であるかどうかを調べ、その結果を書き出すプログラムを作れ。書出しは、ファイル名に続けて「: 」を書き出し、その後に続けて、昇順列であれば「increasing」、そうでなければ「not increasing」と書き出して、最後に改行すること。
|
2
|
-
コード自体を書いてみましたが。所何処と間違っていると思われます。
|
3
|
-
|
4
|
+
なお、空の列は昇順列。
|
4
|
-
追加・削除箇所も教えて下さい。宜しくお願い致します。
|
5
|
-
|
6
|
-
|
7
5
|
```C
|
8
6
|
#include <stdio.h>
|
9
|
-
|
10
|
-
/* 数値を格納する配列 */
|
11
|
-
int number[100];
|
12
|
-
|
13
|
-
|
7
|
+
#include <stdlib.h>
|
14
|
-
int total;
|
15
|
-
|
8
|
+
int main(int argc, char *argv[]){
|
9
|
+
|
10
|
+
FILE *f;
|
16
|
-
|
11
|
+
f= fopen(argv[1],"r");
|
17
|
-
|
12
|
+
if( f==NULL ){
|
18
|
-
|
19
|
-
|
13
|
+
printf("%s: can't open.\n",argv[1]); exit(-1);
|
20
|
-
|
21
|
-
/* 数値を昇順にソート */
|
22
|
-
for (i=0; i<total; ++i) {
|
23
|
-
for (j=i+1; j<total; ++j) {
|
24
|
-
if (number[i] > number[j]) {
|
25
|
-
tmp = number[i];
|
26
|
-
number[i] = number[j];
|
27
|
-
number[j] = tmp;
|
28
|
-
|
14
|
+
}
|
29
|
-
|
15
|
+
printf("%s: ");
|
16
|
+
|
30
|
-
|
17
|
+
・・・ファイル変数fに対応するファイルの中身を調べて
|
31
|
-
|
18
|
+
・・・昇順列なら"increasing"、
|
32
|
-
|
19
|
+
・・・そうでないなら"not increasing"と書き出す
|
20
|
+
|
33
|
-
|
21
|
+
fclose(f);
|
34
|
-
|
22
|
+
|
35
|
-
|
23
|
+
return 0;
|
36
24
|
}
|
37
25
|
```
|
38
|
-
|
39
26
|
やりたい事に則した...の部分を教えて頂きたく。
|
27
|
+
■追記
|
28
|
+
```C
|
29
|
+
#include <stdio.h>
|
30
|
+
#include <stdlib.h>
|
31
|
+
int main(int argc, char *argv[]){
|
32
|
+
|
33
|
+
FILE *f;
|
34
|
+
f= fopen(argv[1],"r");
|
40
35
|
|
41
|
-
|
42
|
-
■追記
|
43
|
-
`;
|
44
|
-
|
36
|
+
if( f==NULL ){
|
45
|
-
|
37
|
+
printf("%s: can't open.\n",argv[1]); exit(-1);
|
46
|
-
|
38
|
+
}
|
47
|
-
|
39
|
+
printf("%s: ");
|
48
40
|
{
|
49
|
-
|
41
|
+
int increasing = 1;
|
42
|
+
int first = 1;
|
43
|
+
int max;
|
44
|
+
char buffer[30001];
|
45
|
+
char * t = NULL;
|
46
|
+
|
47
|
+
if(NULL == fgets(buffer, sizeof(buffer) - 1, f)){
|
48
|
+
printf("%s: can't reed.\n",argv[1]); exit(-1);
|
49
|
+
}
|
50
|
+
buffer[sizeof(buffer) -1] = '¥0';
|
51
|
+
|
52
|
+
t = strtok(buffer, " ");
|
53
|
+
while(t != NULL) {
|
54
|
+
int i = atoi(t);
|
55
|
+
if(first == 1){
|
56
|
+
max = i;
|
57
|
+
first = 0;
|
58
|
+
}else if(i < max){
|
59
|
+
increasing = 0;
|
60
|
+
break;
|
61
|
+
}
|
62
|
+
ptr = strtok(NULL, " ");
|
63
|
+
}
|
64
|
+
|
65
|
+
if(increasing == 1){
|
66
|
+
printf("increasing\n");
|
67
|
+
}else{
|
68
|
+
puts("not increasing\n");
|
69
|
+
}
|
70
|
+
}
|
50
71
|
|
51
|
-
|
72
|
+
fclose(f);
|
73
|
+
|
52
|
-
|
74
|
+
return 0;
|
53
75
|
}
|
54
76
|
```
|
55
|
-
|
56
|
-
|
57
|
-
上記のように追記しましたがコンパイルエラーがでてしまいました。↓
|
77
|
+
上記のように追記しましたがコンパイルエラーがでてしまいました。↓
|
78
|
+
```
|
79
|
+
p8-3.c: In function 'main':
|
80
|
+
p8-3.c:11:5: warning: format '%s' expects a matching 'char *' argument [-Wformat=]
|
81
|
+
printf("%s: ");
|
82
|
+
^
|
83
|
+
p8-3.c:23:37: warning: multi-character character constant [-Wmultichar]
|
84
|
+
buffer[sizeof(buffer) -1] = '¥0';
|
85
|
+
^
|
86
|
+
p8-3.c:23:9: warning: overflow in implicit constant conversion [-Woverflow]
|
87
|
+
buffer[sizeof(buffer) -1] = '¥0';
|
88
|
+
^
|
89
|
+
p8-3.c:25:5: warning: implicit declaration of function 'strtok' [-Wimplicit-function-declaration]
|
90
|
+
t = strtok(buffer, " ");
|
91
|
+
^
|
92
|
+
p8-3.c:25:7: warning: assignment makes pointer from integer without a cast [enabled by default]
|
93
|
+
t = strtok(buffer, " ");
|
94
|
+
^
|
95
|
+
p8-3.c:35:9: error: 'ptr' undeclared (first use in this function)
|
96
|
+
ptr = strtok(NULL, " ");
|
97
|
+
^
|
98
|
+
p8-3.c:35:9: note: each undeclared identifier is reported only once for each function it appears in
|
99
|
+
```
|
100
|
+
■成功例として
|
101
|
+
コマンドライン入力
|
102
|
+
data1.txt
|
103
|
+
標準出力
|
104
|
+
data1.txt: increasing
|
105
|
+
コマンドライン入力
|
106
|
+
data2.txt
|
107
|
+
標準出力
|
108
|
+
data2.txt: not increasing
|
109
|
+
コマンドライン入力
|
110
|
+
data3.txt
|
111
|
+
標準出力
|
112
|
+
data3.txt: increasing
|
113
|
+
再度教えて頂きたく。。。
|
2
一部変更
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
c言語 並べ替え ソート
|
body
CHANGED
@@ -1,27 +1,35 @@
|
|
1
|
-
■やりたい事
|
2
|
-
|
1
|
+
循環した整数を計算し出力に書き出すプログラムですが、いまいち内容がよく分かりません。
|
2
|
+
コード自体を書いてみましたが。所何処と間違っていると思われます。
|
3
|
+
詳しい方、細かく教えて下さい。宜しくお願いします。
|
4
|
+
追加・削除箇所も教えて下さい。宜しくお願い致します。
|
3
5
|
|
4
|
-
与えられたファイル名のファイルの中身が昇順列であるかどうかを調べ、その結果を書き出すプログラムを作れ。書出しは、ファイル名に続けて「: 」を書き出し、その後に続けて、昇順列であれば「increasing」、そうでなければ「not increasing」と書き出して、最後に改行すること。
|
5
|
-
なお、空の列は昇順列。
|
6
6
|
|
7
|
-
|
8
7
|
```C
|
9
8
|
#include <stdio.h>
|
10
|
-
#include <stdlib.h>
|
11
9
|
|
10
|
+
/* 数値を格納する配列 */
|
11
|
+
int number[100];
|
12
|
+
|
13
|
+
/* 数値の総数を入力 */
|
14
|
+
int total;
|
12
|
-
|
15
|
+
printf("入力する数値の総数 = ");
|
13
|
-
|
14
|
-
FILE *f;
|
15
|
-
|
16
|
+
scanf("%d", &total);
|
16
|
-
|
17
|
+
|
18
|
+
|
17
|
-
|
19
|
+
scanf("%d", &number[i]);
|
20
|
+
|
21
|
+
/* 数値を昇順にソート */
|
22
|
+
for (i=0; i<total; ++i) {
|
23
|
+
for (j=i+1; j<total; ++j) {
|
24
|
+
if (number[i] > number[j]) {
|
25
|
+
tmp = number[i];
|
26
|
+
number[i] = number[j];
|
27
|
+
number[j] = tmp;
|
28
|
+
}
|
18
29
|
}
|
19
|
-
|
30
|
+
}
|
20
|
-
|
21
|
-
|
31
|
+
|
22
|
-
|
32
|
+
|
23
|
-
・・・そうでないなら"not increasing"と書き出す
|
24
|
-
|
25
33
|
fclose(f);
|
26
34
|
|
27
35
|
return 0;
|
@@ -32,108 +40,18 @@
|
|
32
40
|
|
33
41
|
|
34
42
|
■追記
|
35
|
-
`
|
43
|
+
`;
|
36
|
-
#include <stdio.h>
|
37
|
-
#include <stdlib.h>
|
38
|
-
|
39
|
-
int main(int argc, char *argv[]){
|
40
|
-
|
41
|
-
FILE *f;
|
42
|
-
f= fopen(argv[1],"r");
|
43
44
|
if( f==NULL ){
|
44
45
|
printf("%s: can't open.\n",argv[1]); exit(-1);
|
45
46
|
}
|
46
47
|
printf("%s: ");
|
47
48
|
{
|
48
|
-
int increasing = 1;
|
49
|
-
int first = 1;
|
50
|
-
int max;
|
51
49
|
|
52
|
-
|
50
|
+
|
53
|
-
char * t = NULL;
|
54
51
|
|
55
|
-
if(NULL == fgets(buffer, sizeof(buffer) - 1, f)){
|
56
|
-
printf("%s: can't reed.\n",argv[1]); exit(-1);
|
57
|
-
}
|
58
|
-
buffer[sizeof(buffer) -1] = '¥0';
|
59
|
-
|
60
|
-
t = strtok(buffer, " ");
|
61
|
-
while(t != NULL) {
|
62
|
-
int i = atoi(t);
|
63
|
-
if(first == 1){
|
64
|
-
max = i;
|
65
|
-
first = 0;
|
66
|
-
}else if(i < max){
|
67
|
-
increasing = 0;
|
68
|
-
break;
|
69
|
-
}
|
70
|
-
ptr = strtok(NULL, " ");
|
71
|
-
}
|
72
|
-
|
73
|
-
if(increasing == 1){
|
74
|
-
printf("increasing\n");
|
75
|
-
}else{
|
76
|
-
puts("not increasing\n");
|
77
|
-
}
|
78
|
-
}
|
79
52
|
|
80
|
-
fclose(f);
|
81
|
-
|
82
|
-
return 0;
|
83
53
|
}
|
84
54
|
```
|
85
55
|
|
86
56
|
|
87
|
-
上記のように追記しましたがコンパイルエラーがでてしまいました。↓
|
57
|
+
上記のように追記しましたがコンパイルエラーがでてしまいました。↓
|
88
|
-
|
89
|
-
```
|
90
|
-
p8-3.c: In function 'main':
|
91
|
-
p8-3.c:11:5: warning: format '%s' expects a matching 'char *' argument [-Wformat=]
|
92
|
-
printf("%s: ");
|
93
|
-
^
|
94
|
-
p8-3.c:23:37: warning: multi-character character constant [-Wmultichar]
|
95
|
-
buffer[sizeof(buffer) -1] = '¥0';
|
96
|
-
^
|
97
|
-
p8-3.c:23:9: warning: overflow in implicit constant conversion [-Woverflow]
|
98
|
-
buffer[sizeof(buffer) -1] = '¥0';
|
99
|
-
^
|
100
|
-
p8-3.c:25:5: warning: implicit declaration of function 'strtok' [-Wimplicit-function-declaration]
|
101
|
-
t = strtok(buffer, " ");
|
102
|
-
^
|
103
|
-
p8-3.c:25:7: warning: assignment makes pointer from integer without a cast [enabled by default]
|
104
|
-
t = strtok(buffer, " ");
|
105
|
-
^
|
106
|
-
p8-3.c:35:9: error: 'ptr' undeclared (first use in this function)
|
107
|
-
ptr = strtok(NULL, " ");
|
108
|
-
^
|
109
|
-
p8-3.c:35:9: note: each undeclared identifier is reported only once for each function it appears in
|
110
|
-
```
|
111
|
-
|
112
|
-
|
113
|
-
■成功例として
|
114
|
-
コマンドライン入力
|
115
|
-
data1.txt
|
116
|
-
|
117
|
-
標準出力
|
118
|
-
data1.txt: increasing
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
コマンドライン入力
|
124
|
-
data2.txt
|
125
|
-
|
126
|
-
標準出力
|
127
|
-
data2.txt: not increasing
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
コマンドライン入力
|
133
|
-
data3.txt
|
134
|
-
|
135
|
-
標準出力
|
136
|
-
data3.txt: increasing
|
137
|
-
|
138
|
-
|
139
|
-
再度教えて頂きたく。。。
|
1
追記しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -28,4 +28,112 @@
|
|
28
28
|
}
|
29
29
|
```
|
30
30
|
|
31
|
-
やりたい事に則した...の部分を教えて頂きたく。
|
31
|
+
やりたい事に則した...の部分を教えて頂きたく。
|
32
|
+
|
33
|
+
|
34
|
+
■追記
|
35
|
+
```C
|
36
|
+
#include <stdio.h>
|
37
|
+
#include <stdlib.h>
|
38
|
+
|
39
|
+
int main(int argc, char *argv[]){
|
40
|
+
|
41
|
+
FILE *f;
|
42
|
+
f= fopen(argv[1],"r");
|
43
|
+
if( f==NULL ){
|
44
|
+
printf("%s: can't open.\n",argv[1]); exit(-1);
|
45
|
+
}
|
46
|
+
printf("%s: ");
|
47
|
+
{
|
48
|
+
int increasing = 1;
|
49
|
+
int first = 1;
|
50
|
+
int max;
|
51
|
+
|
52
|
+
char buffer[30001];
|
53
|
+
char * t = NULL;
|
54
|
+
|
55
|
+
if(NULL == fgets(buffer, sizeof(buffer) - 1, f)){
|
56
|
+
printf("%s: can't reed.\n",argv[1]); exit(-1);
|
57
|
+
}
|
58
|
+
buffer[sizeof(buffer) -1] = '¥0';
|
59
|
+
|
60
|
+
t = strtok(buffer, " ");
|
61
|
+
while(t != NULL) {
|
62
|
+
int i = atoi(t);
|
63
|
+
if(first == 1){
|
64
|
+
max = i;
|
65
|
+
first = 0;
|
66
|
+
}else if(i < max){
|
67
|
+
increasing = 0;
|
68
|
+
break;
|
69
|
+
}
|
70
|
+
ptr = strtok(NULL, " ");
|
71
|
+
}
|
72
|
+
|
73
|
+
if(increasing == 1){
|
74
|
+
printf("increasing\n");
|
75
|
+
}else{
|
76
|
+
puts("not increasing\n");
|
77
|
+
}
|
78
|
+
}
|
79
|
+
|
80
|
+
fclose(f);
|
81
|
+
|
82
|
+
return 0;
|
83
|
+
}
|
84
|
+
```
|
85
|
+
|
86
|
+
|
87
|
+
上記のように追記しましたがコンパイルエラーがでてしまいました。↓
|
88
|
+
|
89
|
+
```
|
90
|
+
p8-3.c: In function 'main':
|
91
|
+
p8-3.c:11:5: warning: format '%s' expects a matching 'char *' argument [-Wformat=]
|
92
|
+
printf("%s: ");
|
93
|
+
^
|
94
|
+
p8-3.c:23:37: warning: multi-character character constant [-Wmultichar]
|
95
|
+
buffer[sizeof(buffer) -1] = '¥0';
|
96
|
+
^
|
97
|
+
p8-3.c:23:9: warning: overflow in implicit constant conversion [-Woverflow]
|
98
|
+
buffer[sizeof(buffer) -1] = '¥0';
|
99
|
+
^
|
100
|
+
p8-3.c:25:5: warning: implicit declaration of function 'strtok' [-Wimplicit-function-declaration]
|
101
|
+
t = strtok(buffer, " ");
|
102
|
+
^
|
103
|
+
p8-3.c:25:7: warning: assignment makes pointer from integer without a cast [enabled by default]
|
104
|
+
t = strtok(buffer, " ");
|
105
|
+
^
|
106
|
+
p8-3.c:35:9: error: 'ptr' undeclared (first use in this function)
|
107
|
+
ptr = strtok(NULL, " ");
|
108
|
+
^
|
109
|
+
p8-3.c:35:9: note: each undeclared identifier is reported only once for each function it appears in
|
110
|
+
```
|
111
|
+
|
112
|
+
|
113
|
+
■成功例として
|
114
|
+
コマンドライン入力
|
115
|
+
data1.txt
|
116
|
+
|
117
|
+
標準出力
|
118
|
+
data1.txt: increasing
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
コマンドライン入力
|
124
|
+
data2.txt
|
125
|
+
|
126
|
+
標準出力
|
127
|
+
data2.txt: not increasing
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
コマンドライン入力
|
133
|
+
data3.txt
|
134
|
+
|
135
|
+
標準出力
|
136
|
+
data3.txt: increasing
|
137
|
+
|
138
|
+
|
139
|
+
再度教えて頂きたく。。。
|