回答編集履歴
2
ソース追記
answer
CHANGED
@@ -8,4 +8,159 @@
|
|
8
8
|
で、フォーマットチェックは下記で出来るのでは?
|
9
9
|
if(scanf("%d:%d:%d",&h,&m,&s) != 3){
|
10
10
|
エラー処理
|
11
|
-
}
|
11
|
+
}
|
12
|
+
|
13
|
+
『追記』BAが出ていますが、取り敢えず^^
|
14
|
+
```text
|
15
|
+
usr ~/Project/test % ./a.out
|
16
|
+
N:? 3
|
17
|
+
12:34:56
|
18
|
+
23:59:00
|
19
|
+
20:00:60
|
20
|
+
12:34:56
|
21
|
+
|
22
|
+
correct time
|
23
|
+
|
24
|
+
23:59:00
|
25
|
+
|
26
|
+
correct time
|
27
|
+
|
28
|
+
20:00:60
|
29
|
+
|
30
|
+
incorrect time
|
31
|
+
```
|
32
|
+
```c
|
33
|
+
#include <stdio.h>
|
34
|
+
#include <string.h>
|
35
|
+
#include <ctype.h>
|
36
|
+
//
|
37
|
+
#define ARRY_SIZE (64)
|
38
|
+
|
39
|
+
int input(void);
|
40
|
+
void timecheck(int a, const char array[][ARRY_SIZE], int f[]);
|
41
|
+
int lettercheck(const char array[ARRY_SIZE]);
|
42
|
+
int formatcheck(const char array[ARRY_SIZE]);
|
43
|
+
int numbercheck(const char array[ARRY_SIZE]);
|
44
|
+
void output(int e, char array[][ARRY_SIZE], int k[]);
|
45
|
+
enum {
|
46
|
+
NML_END,
|
47
|
+
CorrectTime,
|
48
|
+
ValidFormat,
|
49
|
+
IncorrectTime,
|
50
|
+
InvalidFormat
|
51
|
+
};
|
52
|
+
|
53
|
+
int main(void)
|
54
|
+
{
|
55
|
+
int N, i;
|
56
|
+
N = input();
|
57
|
+
char timestamps[N][ARRY_SIZE];
|
58
|
+
int check[N];
|
59
|
+
for (i = 0; i < N; i++) {
|
60
|
+
check[i] = NML_END;
|
61
|
+
fgets(timestamps[i], ARRY_SIZE, stdin);
|
62
|
+
}
|
63
|
+
|
64
|
+
timecheck(N, timestamps, check);
|
65
|
+
output(N, timestamps, check);
|
66
|
+
return 0;
|
67
|
+
}
|
68
|
+
//
|
69
|
+
int input(void)
|
70
|
+
{
|
71
|
+
int x;
|
72
|
+
fputs("N:? ", stdout);
|
73
|
+
scanf("%d\n", &x);
|
74
|
+
return x;
|
75
|
+
}
|
76
|
+
//
|
77
|
+
void timecheck(int a, const char array[][ARRY_SIZE], int f[])
|
78
|
+
{
|
79
|
+
for (int i = 0; i < a; i++) {
|
80
|
+
// puts("formatcheck");
|
81
|
+
if ((f[i] = formatcheck(array[i])) == CorrectTime) {
|
82
|
+
// puts("lettercheck");
|
83
|
+
if ((f[i] = lettercheck(array[i])) == CorrectTime) {
|
84
|
+
// puts("numbercheck");
|
85
|
+
f[i] = numbercheck(array[i]);
|
86
|
+
}
|
87
|
+
}
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
int formatcheck(const char array[])
|
92
|
+
{
|
93
|
+
int h, m, s;
|
94
|
+
if (sscanf(array, "%2d:%2d:%2d", &h, &m, &s) != 3) {
|
95
|
+
// puts(array);
|
96
|
+
return InvalidFormat;
|
97
|
+
}
|
98
|
+
// printf("%2d-%2d-%2d\n",h,m,s);
|
99
|
+
return CorrectTime;
|
100
|
+
}
|
101
|
+
|
102
|
+
int lettercheck(const char array[])
|
103
|
+
{
|
104
|
+
const static int pos[] = {0, 1, 3, 4, 6, 7};
|
105
|
+
|
106
|
+
for (int i = 0; i < 5; i++) {
|
107
|
+
if (!isdigit(array[pos[i]])) {
|
108
|
+
return ValidFormat;
|
109
|
+
}
|
110
|
+
}
|
111
|
+
return CorrectTime;
|
112
|
+
}
|
113
|
+
|
114
|
+
int numbercheck(const char array[])
|
115
|
+
{
|
116
|
+
|
117
|
+
if (!isdigit(array[0]) || array[0] > '2') {
|
118
|
+
// puts("1");
|
119
|
+
return IncorrectTime;
|
120
|
+
}
|
121
|
+
if (!isdigit(array[1])) {
|
122
|
+
// puts("2");
|
123
|
+
return IncorrectTime;
|
124
|
+
}
|
125
|
+
if (!isdigit(array[3]) || array[3] > '5') {
|
126
|
+
// puts("3");
|
127
|
+
return IncorrectTime;
|
128
|
+
}
|
129
|
+
if (!isdigit(array[4])) {
|
130
|
+
// puts("4");
|
131
|
+
return IncorrectTime;
|
132
|
+
}
|
133
|
+
if (!isdigit(array[6]) || array[6] > '5') {
|
134
|
+
// puts("5");
|
135
|
+
return IncorrectTime;
|
136
|
+
}
|
137
|
+
if (!isdigit(array[7])) {
|
138
|
+
// puts("6");
|
139
|
+
return IncorrectTime;
|
140
|
+
}
|
141
|
+
|
142
|
+
return CorrectTime;
|
143
|
+
}
|
144
|
+
|
145
|
+
void output(int e, char array[][ARRY_SIZE], int k[])
|
146
|
+
{
|
147
|
+
for (int i = 0; i < e; i++) {
|
148
|
+
if (k[i] == CorrectTime) {
|
149
|
+
puts(array[i]);
|
150
|
+
puts(" correct time\n");
|
151
|
+
} else if (k[i] == ValidFormat) {
|
152
|
+
puts(array[i]);
|
153
|
+
puts(" valid format\n");
|
154
|
+
} else if (k[i] == IncorrectTime) {
|
155
|
+
puts(array[i]);
|
156
|
+
puts(" incorrect time\n");
|
157
|
+
} else if (k[i] == InvalidFormat) {
|
158
|
+
puts(array[i]);
|
159
|
+
puts(" invalid format\n");
|
160
|
+
}
|
161
|
+
}
|
162
|
+
}
|
163
|
+
|
164
|
+
```
|
165
|
+
修正すべき所は多々有ると思いますが、まあ動くので・・・
|
166
|
+
#いろいろなフォーマットのテストをされたほうが良いと思います。
|
1
追記
answer
CHANGED
@@ -1,5 +1,11 @@
|
|
1
|
+
C可変長の配列↓が利用できるのは
|
2
|
+
char timestamps[N][81];
|
3
|
+
int check[N];
|
1
|
-
|
4
|
+
は、特定のコンパイラのみです、
|
5
|
+
[C言語(C11)で可変長の配列を使う方法](https://qiita.com/raccy/items/bfcb62086c5f027d57b6)
|
6
|
+
また、↑にも有りますが、C++14の仕様には含まれていません。
|
7
|
+
|
8
|
+
で、フォーマットチェックは下記で出来るのでは?
|
2
9
|
if(scanf("%d:%d:%d",&h,&m,&s) != 3){
|
3
10
|
エラー処理
|
4
|
-
}
|
11
|
+
}
|
5
|
-
で、フォーマットチェックが出来るのでは?
|