質問編集履歴

7

修正

2016/07/01 06:16

投稿

soromon
soromon

スコア15

test CHANGED
File without changes
test CHANGED
@@ -56,17 +56,17 @@
56
56
 
57
57
 
58
58
 
59
+ int match_str_column(const char *a, const char *b, int column);
60
+
59
61
  int main (void)
60
62
 
61
63
  {
62
-
63
- int match_str_column(const char *a, const char *b, int column);
64
64
 
65
65
  FILE *fp;
66
66
 
67
67
  int chk;
68
68
 
69
- char a[100000],fname[256],sstr[1000];
69
+ char a[100000],fname[256],sstr[100];
70
70
 
71
71
  printf("検索されるファイル名 : ");
72
72
 
@@ -90,7 +90,7 @@
90
90
 
91
91
  while(fgets(a,1000,fp)!=NULL){
92
92
 
93
- chk = match_str_column(a, sstr);
93
+ chk = match_str_column(a, sstr,3);
94
94
 
95
95
  if (chk==1){
96
96
 
@@ -99,6 +99,8 @@
99
99
  }
100
100
 
101
101
  }
102
+
103
+ return 0;
102
104
 
103
105
  }
104
106
 
@@ -112,7 +114,7 @@
112
114
 
113
115
  char *tok;
114
116
 
115
- int count = 0;
117
+ int count = 3;
116
118
 
117
119
  strcpy(buf, a);
118
120
 
@@ -137,8 +139,6 @@
137
139
  }
138
140
 
139
141
  return (strcmp(str, b) == 0) ? 1 : 0;
140
-
141
- }
142
142
 
143
143
  ```
144
144
 

6

誤字

2016/07/01 06:16

投稿

soromon
soromon

スコア15

test CHANGED
File without changes
test CHANGED
@@ -38,11 +38,9 @@
38
38
 
39
39
  エラーメッセージ
40
40
 
41
- 上のようにHO2'もでてきてしまいます。
41
+ test6.c: In function ‘main’:
42
42
 
43
-
44
-
45
- 他にもここで一番右の列のOを検索すると全ての行のATOMが検索されてしまいます。
43
+ test6.c:21: error: too few arguments to function ‘match_str_column’
46
44
 
47
45
 
48
46
 
@@ -56,19 +54,19 @@
56
54
 
57
55
  #include<string.h>
58
56
 
59
- int match_str(const char *a, const char *b);
60
57
 
61
58
 
62
-
63
- void main()
59
+ int main (void)
64
60
 
65
61
  {
62
+
63
+ int match_str_column(const char *a, const char *b, int column);
66
64
 
67
65
  FILE *fp;
68
66
 
69
67
  int chk;
70
68
 
71
- char a[100000],fname[256],sstr[100];
69
+ char a[100000],fname[256],sstr[1000];
72
70
 
73
71
  printf("検索されるファイル名 : ");
74
72
 
@@ -82,7 +80,7 @@
82
80
 
83
81
  if( (fp=fopen(fname,"r"))==NULL){
84
82
 
85
- printf("ファイル %s が見つかりません\n",fname);return;
83
+ printf("ファイル %s が見つかりません\n",fname);return 0;
86
84
 
87
85
  }
88
86
 
@@ -92,11 +90,11 @@
92
90
 
93
91
  while(fgets(a,1000,fp)!=NULL){
94
92
 
95
- chk = match_str(a, sstr);
93
+ chk = match_str_column(a, sstr);
96
94
 
97
95
  if (chk==1){
98
96
 
99
- printf(" %s ",a);
97
+ printf("%s\n",a);
100
98
 
101
99
  }
102
100
 
@@ -104,45 +102,41 @@
104
102
 
105
103
  }
106
104
 
107
-
108
-
109
- int match_str(const char *a, const char *b)
105
+ int match_str_column(const char *a, const char *b, int column)
110
106
 
111
107
  {
112
108
 
113
- int i=0, j=0, max_a, max_b;
109
+ char buf[1000];
114
110
 
115
- while(1){
111
+ char *str;
116
112
 
117
- if(a[i]=='\0'){max_a = i; break;}
113
+ char *tok;
118
114
 
115
+ int count = 0;
116
+
117
+ strcpy(buf, a);
118
+
119
+ tok = strtok(buf, " \t\r\n");
120
+
121
+ if(tok == NULL)
122
+
123
+ return 0;
124
+
125
+ while(tok != NULL)
126
+
127
+ {
128
+
129
+ str = tok;
130
+
131
+ if(++count == column)
132
+
119
- i++;
133
+ break;
134
+
135
+ tok = strtok(NULL, " \t\r\n");
120
136
 
121
137
  }
122
138
 
123
- i=0;
124
-
125
- while(1){
126
-
127
- if(b[i]=='\0'){max_b = i; break;}
128
-
129
- i++;
130
-
131
- }
132
-
133
- for(i=0;i<max_a;i++){
134
-
135
- if(0==strncmp(&a[i],&b[j],1)){
139
+ return (strcmp(str, b) == 0) ? 1 : 0;
136
-
137
- if(j==max_b-1){return 1;}
138
-
139
- else{j++;}
140
-
141
- }
142
-
143
- }
144
-
145
- return 0;
146
140
 
147
141
  }
148
142
 

5

誤字

2016/07/01 03:23

投稿

soromon
soromon

スコア15

test CHANGED
File without changes
test CHANGED
@@ -167,3 +167,7 @@
167
167
  ```
168
168
 
169
169
  とそれのみが検索されるプログラムをお願いいたします。
170
+
171
+
172
+
173
+ 上のプログラムよりもよいものがある場合でもご教授頂けると幸いです。

4

誤字

2016/06/29 09:42

投稿

soromon
soromon

スコア15

test CHANGED
File without changes
test CHANGED
@@ -158,7 +158,9 @@
158
158
 
159
159
  Hを検索したら
160
160
 
161
+ ```
162
+
161
- ```ATOM 164 H2' G 5 -0.550 44.397 32.752 1.00 0.00 H
163
+ ATOM 164 H2' G 5 -0.550 44.397 32.752 1.00 0.00 H
162
164
 
163
165
  ATOM 166 HO2' G 5 1.286 42.680 32.880 1.00 0.00 H
164
166
 

3

誤字

2016/06/29 09:35

投稿

soromon
soromon

スコア15

test CHANGED
File without changes
test CHANGED
@@ -38,7 +38,7 @@
38
38
 
39
39
  エラーメッセージ
40
40
 
41
- ```上のようにHO2'もでてきてしまいます。
41
+ 上のようにHO2'もでてきてしまいます。
42
42
 
43
43
 
44
44
 
@@ -50,7 +50,9 @@
50
50
 
51
51
  ###C言語
52
52
 
53
+ ```
54
+
53
- ```#include<stdio.h>
55
+ #include<stdio.h>
54
56
 
55
57
  #include<string.h>
56
58
 
@@ -160,4 +162,6 @@
160
162
 
161
163
  ATOM 166 HO2' G 5 1.286 42.680 32.880 1.00 0.00 H
162
164
 
165
+ ```
166
+
163
- とそれのみが検索されるプログラムをお願いいたします。```
167
+ とそれのみが検索されるプログラムをお願いいたします。

2

文法

2016/06/29 09:34

投稿

soromon
soromon

スコア15

test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  ###発生している問題・エラーメッセージ
12
12
 
13
- 検索されるファイル名 : a.pdb
13
+ ```検索されるファイル名 : a.pdb
14
14
 
15
15
  検索文字列 : H2'
16
16
 
@@ -34,6 +34,8 @@
34
34
 
35
35
  ATOM 166 HO2' G 5 1.286 42.680 32.880 1.00 0.00 H
36
36
 
37
+ ```
38
+
37
39
  エラーメッセージ
38
40
 
39
41
  ```上のようにHO2'もでてきてしまいます。
@@ -48,7 +50,7 @@
48
50
 
49
51
  ###C言語
50
52
 
51
- #include<stdio.h>
53
+ ```#include<stdio.h>
52
54
 
53
55
  #include<string.h>
54
56
 
@@ -142,7 +144,7 @@
142
144
 
143
145
  }
144
146
 
145
-
147
+ ```
146
148
 
147
149
 
148
150
 
@@ -154,8 +156,8 @@
154
156
 
155
157
  Hを検索したら
156
158
 
157
- ATOM 164 H2' G 5 -0.550 44.397 32.752 1.00 0.00 H
159
+ ```ATOM 164 H2' G 5 -0.550 44.397 32.752 1.00 0.00 H
158
160
 
159
161
  ATOM 166 HO2' G 5 1.286 42.680 32.880 1.00 0.00 H
160
162
 
161
- とそれのみが検索されるプログラムをお願いいたします。
163
+ とそれのみが検索されるプログラムをお願いいたします。```

1

文法

2016/06/29 09:32

投稿

soromon
soromon

スコア15

test CHANGED
File without changes
test CHANGED
File without changes