質問編集履歴

1

コードを全文載せました

2021/06/10 08:04

投稿

zigzagsv
zigzagsv

スコア1

test CHANGED
File without changes
test CHANGED
@@ -16,9 +16,227 @@
16
16
 
17
17
 
18
18
 
19
+ #include <stdio.h>
20
+
21
+ #include <string.h>
22
+
23
+ #include <stdlib.h>
24
+
25
+
26
+
27
+ struct TelephoneBook{
28
+
29
+ char name[16];
30
+
31
+ char number[16];
32
+
33
+ struct TelephoneBook *next;
34
+
35
+ };
36
+
37
+
38
+
39
+ struct TelephoneBook *add_book(char *name, char *number,struct TelephoneBook *head);
40
+
41
+ void print_book(struct TelephoneBook *p);
42
+
43
+ void free_book(struct TelephoneBook *p);
44
+
45
+ int check_name(char *na);
46
+
47
+ int check_number(char *num);
48
+
49
+
50
+
51
+ int main(void){
52
+
53
+ struct TelephoneBook *head;
54
+
55
+ char name_number[33];
56
+
57
+ char name[16]; //入力した名前を格納する
58
+
59
+ char number[16]; //入力した電話番号を格納する
60
+
61
+ int check_na; //仕様通りに入力されてなければ1,\qが入力されていれば2が代入される
62
+
63
+ int check_nu; //仕様通りに入力されてなければ1が代入される
64
+
65
+ head = NULL;
66
+
67
+
68
+
69
+ while(1){
70
+
71
+ printf("input>");
72
+
73
+ fgets(name_number,33,stdin);
74
+
75
+ sscanf(name_number,"%s %s", name, number);
76
+
77
+ check_na = check_name(name);
78
+
79
+ if(check_na == 2){
80
+
81
+ print_book(head);
82
+
83
+ free_book(head);
84
+
85
+ return EXIT_SUCCESS;
86
+
87
+ }
88
+
89
+ if (check_na == 1){
90
+
91
+ printf("error");
92
+
93
+ return EXIT_FAILURE;
94
+
95
+ }
96
+
97
+ check_nu = check_number(number);
98
+
99
+ if (check_nu == 1){
100
+
101
+ printf("error");
102
+
103
+ return EXIT_FAILURE;
104
+
105
+ }
106
+
107
+ if(check_na == 0 && check_nu == 0){
108
+
109
+ printf("Ok\n");
110
+
111
+ }
112
+
113
+ head = add_book(name,number,head);
114
+
115
+ }
116
+
117
+ }
118
+
119
+
120
+
121
+ int check_name(char *na){
122
+
123
+ char name[16] = " ";
124
+
125
+ strcpy(name, na);
126
+
127
+ char *chr1 = "\q";
128
+
129
+ char *chr2 = "\Q";
130
+
131
+ if(strcmp(name,chr1) == 0 || strcmp(name,chr2) == 0){
132
+
133
+ return 2;
134
+
135
+ }
136
+
137
+ int len = strlen(name);
138
+
139
+ if(name[0] >= 'A' && name[0] <= 'Z'){
140
+
141
+
142
+
143
+ }else{
144
+
145
+ return 1;
146
+
147
+ }
148
+
149
+ for(int i = 1; i <= len; i++){
150
+
151
+ if(name[i] >= 'a' || name[i] <= 'z'){
152
+
153
+
154
+
155
+ }else{
156
+
157
+ return 1;
158
+
159
+ }
160
+
161
+ }
162
+
163
+ return 0;
164
+
165
+ }
166
+
167
+
168
+
169
+ int check_number(char *num){
170
+
171
+ char number[16];
172
+
173
+ strcpy(number, num);
174
+
175
+ for(int i = 0; i <= 2; i++){
176
+
177
+ if(number[i] >= '0' && number[i] <= '9'){
178
+
179
+
180
+
181
+ }else{
182
+
183
+ return 1;
184
+
185
+ }
186
+
187
+ }
188
+
189
+ for(int i = 4; i <= 7; i++){
190
+
191
+ if(number[i] >= '0' && number[i] <= '9'){
192
+
193
+
194
+
195
+ }else{
196
+
197
+ return 1;
198
+
199
+ }
200
+
201
+ }
202
+
203
+ for(int i = 9; i <= 12; i++){
204
+
205
+ if(number[i] >= '0' && number[i] <= '9'){
206
+
207
+
208
+
209
+ }else{
210
+
211
+ return 1;
212
+
213
+ }
214
+
215
+ }
216
+
217
+ if(number[3] != '-' || number[8] != '-'){
218
+
219
+ return 1;
220
+
221
+ }
222
+
223
+ return 0;
224
+
225
+
226
+
227
+ }
228
+
229
+
230
+
19
231
  struct TelephoneBook *add_book(char *name, char *number,struct TelephoneBook *head){
20
232
 
21
- struct TelephoneBook *p;
233
+ struct TelephoneBook *p = head;
234
+
235
+ while ( p->next != NULL ) {
236
+
237
+ p = p->next;
238
+
239
+ }
22
240
 
23
241
  p = (struct TelephoneBook *) malloc(sizeof(struct TelephoneBook));
24
242
 
@@ -30,14 +248,42 @@
30
248
 
31
249
  head = p;
32
250
 
33
-
34
-
35
251
  return head;
36
252
 
37
253
  }
38
254
 
39
255
 
40
256
 
257
+ void print_book(struct TelephoneBook *p){
258
+
259
+ while (p != NULL) {
260
+
261
+ printf("%s : %s\n", p->name, p->number);
262
+
263
+ p = p->next;
264
+
265
+ }
266
+
267
+ }
268
+
269
+
270
+
271
+ void free_book(struct TelephoneBook *p){
272
+
273
+ struct TelephoneBook *p2;
274
+
275
+ while (p != NULL) { /* 次ポインタがNULLまで処理 */
276
+
277
+ p2 = p->next;
278
+
279
+ free(p);
280
+
281
+ p = p2;
282
+
283
+ }
284
+
285
+ }
286
+
41
287
  ### 試したこと
42
288
 
43
289
  p->next = head;