回答編集履歴
1
数字の文字列と int配列の相互変換コードを追加
test
CHANGED
@@ -35,3 +35,105 @@
|
|
35
35
|
上記の疑問を解消するように質問を編集してください。
|
36
36
|
|
37
37
|
そうすれば、回答に追記をします。
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
**追記**
|
42
|
+
|
43
|
+
> 0~20桁で入れたいです。
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
0桁の数とはどういうものですか?
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
```C
|
52
|
+
|
53
|
+
#include <stdio.h> // printf, scanf, sprintf, sscanf, puts, putchar
|
54
|
+
|
55
|
+
#include <string.h> // strlen, strcpy, memset
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
int toArray(int a[], const char *s)
|
60
|
+
|
61
|
+
{
|
62
|
+
|
63
|
+
char buf[20 + 1], c;
|
64
|
+
|
65
|
+
int len = strlen(s);
|
66
|
+
|
67
|
+
if (len > 20) s += len - 20, len = 20;
|
68
|
+
|
69
|
+
memset(buf, '0', 20 - len);
|
70
|
+
|
71
|
+
strcpy(buf + 20 - len, s);
|
72
|
+
|
73
|
+
return sscanf(buf, "%4d%4d%4d%4d%4d%c", a, a+1, a+2, a+3, a+4, &c) != 5;
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
void toStr(const int a[], char *s)
|
80
|
+
|
81
|
+
{
|
82
|
+
|
83
|
+
int i = 0;
|
84
|
+
|
85
|
+
while (i < 4 && a[i] == 0) i++;
|
86
|
+
|
87
|
+
s += sprintf(s, "%d", a[i++]);
|
88
|
+
|
89
|
+
while (i < 5) s += sprintf(s, "%04d", a[i++]);
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
int main(void)
|
96
|
+
|
97
|
+
{
|
98
|
+
|
99
|
+
int a[5]; char buf[100];
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
while (printf(">> "), scanf("%99s", buf) == 1 && buf[0] != '.') {
|
104
|
+
|
105
|
+
if (toArray(a, buf))
|
106
|
+
|
107
|
+
puts(" Error");
|
108
|
+
|
109
|
+
else {
|
110
|
+
|
111
|
+
for (int i = 0; i < 5; i++) printf(" %04d", a[i]);
|
112
|
+
|
113
|
+
putchar('\n');
|
114
|
+
|
115
|
+
toStr(a, buf);
|
116
|
+
|
117
|
+
printf(" %s\n", buf);
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
```
|
126
|
+
|
127
|
+
20桁を超える場合、下位20桁を配列に入れます。
|
128
|
+
|
129
|
+
エラーにしたいなら変更が必要です。
|
130
|
+
|
131
|
+
負の数は入力されないと仮定して、チェックしていません。
|
132
|
+
|
133
|
+
数字以外の文字があるとエラーになります。
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
コードだけ見せられても分からないところがたくさんあると思いますが、
|
138
|
+
|
139
|
+
自分で調べて、分からないところはコメントで質問してください。
|