回答編集履歴

1

駅名を出現順に、乗降客数を3桁区切りで表示するバージョン追加

2017/04/29 07:02

投稿

naomi3
naomi3

スコア1105

test CHANGED
@@ -37,3 +37,91 @@
37
37
  }
38
38
 
39
39
  ```
40
+
41
+
42
+
43
+ 解決済になっていますけど、駅名を出現順に、乗降客数を3桁区切りで表示するバージョンを示しておきます。
44
+
45
+ ```bash
46
+
47
+ #!/usr/bin/awk -f
48
+
49
+ function putCommaAtEvery3Digits(input, output) {
50
+
51
+ for (output = ""; input >= 1000; input = int(input / 1000)) {
52
+
53
+ output = sprintf(",%03d", input % 1000) output;
54
+
55
+ }
56
+
57
+ output = input output;
58
+
59
+
60
+
61
+ return output;
62
+
63
+ }
64
+
65
+
66
+
67
+
68
+
69
+ function registerStation(station, stationIndex) {
70
+
71
+ for (stationIndex = 0; stationIndex < stationCount; stationIndex++) {
72
+
73
+ if (stationList[stationIndex] == station) {
74
+
75
+ return stationIndex;
76
+
77
+ }
78
+
79
+ }
80
+
81
+
82
+
83
+ stationList[stationCount++] = station;
84
+
85
+ return stationCount;
86
+
87
+ }
88
+
89
+
90
+
91
+
92
+
93
+ {
94
+
95
+ station = $1;
96
+
97
+ registerStation(station);
98
+
99
+
100
+
101
+ passengers = $2;
102
+
103
+ gsub(/,/, "", passengers);
104
+
105
+
106
+
107
+ passengersAtStation[station] += passengers;
108
+
109
+ }
110
+
111
+
112
+
113
+
114
+
115
+ END {
116
+
117
+ for (stationIndex = 0; stationIndex < stationCount; stationIndex++) {
118
+
119
+ station = stationList[stationIndex];
120
+
121
+ printf("%s\t%s\n", station, putCommaAtEvery3Digits(passengersAtStation[station]));
122
+
123
+ }
124
+
125
+ }
126
+
127
+ ```