-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelcoDataCheckAndQuery.cpp
More file actions
191 lines (173 loc) · 6.05 KB
/
TelcoDataCheckAndQuery.cpp
File metadata and controls
191 lines (173 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
Description
Write a C program to perform some queries on a telco data (comming from stdin)
with the following format:
The first block of data consists of lines (terminated by a line containing #),
each line (number of lines can be up to 100000) is under the form:
call <from_number> <to_number> <date> <from_time> <end_time>
which is a call from the phone number <from_number> to a phone number
<to_number> on <date>, and starting at time-point <from_time>, terminating at
time-point <end_time>
<from_number> and <to_number> are string of 10 characters (a phone number is
correct if it contains only digits 0,1,...,9, otherwise, the phone number is
incorrect)
<date> is under the form YYYY-MM-DD (for example 2022-10-21)
<from_time> and <to_time> are under the form hh:mm:ss (for example, 10:07:23)
The second block consists of queries (terminated by a line containing #), each
query in a line (number of lines can be up to 100000) and belongs to one of the
following types:
?check_phone_number: print to stdout (in a new line) value 1 if no phone number
is incorrect
?number_calls_from <phone_number>: print to stdout (in a new line) the number of
times a call is made from <phone_number>
?number_total_calls: print to stdout (in a new line) the total number of calls
of the data
?count_time_calls_from <phone_number>: print to stdout (in a new line) the total
time duration (in seconds) the calls are made from <phone_number>
Example
Input
call 0912345678 0132465789 2022-07-12 10:30:23 10:32:00
call 0912345678 0945324545 2022-07-13 11:30:10 11:35:11
call 0132465789 0945324545 2022-07-13 11:30:23 11:32:23
call 0945324545 0912345678 2022-07-13 07:30:23 07:48:30
#
?check_phone_number
?number_calls_from 0912345678
?number_total_calls
?count_time_calls_from 0912345678
?count_time_calls_from 0132465789
#
*/
#include<bits/stdc++.h>
using namespace std;
bool isIncorrectPhone(string phoneNumer) {
if(phoneNumer.length() != 10) return true;
for (int i = 0; i < 10; i++)
{
if(!(phoneNumer[i] >= '0' && phoneNumer[i] <= '9')) return true;
}
return false;
}
// ftime, etime has format hh:mm:ss
int countTime(string ftime, string etime) {
int startTime = 3600 * ((ftime[0]-'0')*10 + (ftime[1]-'0'))
+ 60 * ((ftime[3]-'0')*10 + (ftime[4]-'0'))
+ 1 * ((ftime[6]-'0')*10 + (ftime[7]-'0'));
int endTime = 3600 * ((etime[0]-'0')*10 + (etime[1]-'0'))
+ 60 * ((etime[3]-'0')*10 + (etime[4]-'0'))
+ 1 * ((etime[6]-'0')*10 + (etime[7]-'0'));
return endTime - startTime;
}
void TelcoDataCheckAndQueryUsingCinForUserInput() {
unordered_map<string, int> numberCalls, timeCall;
int incorrectPhones = 0;
int nbTotalCalls = 0;
string type;
string fPhoneNumber, toPhoneNumber, day, ftime, etime;
do
{
cin >> type;
if (type == "#") continue;
if (type == "call")
{
cin >> fPhoneNumber >> toPhoneNumber >> day >> ftime >> etime;
++nbTotalCalls;
if (isIncorrectPhone(fPhoneNumber) || isIncorrectPhone(toPhoneNumber))
{
incorrectPhones++;
}
numberCalls[fPhoneNumber]++;
timeCall[fPhoneNumber] += countTime(ftime, etime);
}
} while (type != "#");
do
{
string numberForQuery;
cin >> type;
if (type == "#") continue;
if (type == "?check_phone_number")
{
if(incorrectPhones == 0) cout << 1 << endl;
else cout << 0 << endl;
}
if (type == "?number_calls_from")
{
cin >> numberForQuery;
cout << numberCalls[numberForQuery] << endl;
}
if (type == "?number_total_calls")
{
cout << nbTotalCalls << endl;
}
if (type == "?count_time_calls_from")
{
cin >> numberForQuery;
cout << timeCall[numberForQuery] << endl;
}
} while (type != "#");
}
void TelcoDataCheckAndQueryUsingScanfForUserInput() {
unordered_map<string, pair<int, int>> phoneData; //pair.first = number_calls_from <string>; pair.second = count_time_calls_from <string>
char type[23];
char fromNumb[11], toNumb[11], date[11], ftime[9], etime[9];
int incorrectPhones = 0;
int nbTotalCalls = 0;
do
{
scanf("%s", type);
if (strcmp(type, "#") == 0)
{
continue;
}
if (strcmp(type, "call") == 0)
{
scanf("%s %s %s %s %s", fromNumb, toNumb, date, ftime, etime);
if (isIncorrectPhone(fromNumb) || isIncorrectPhone(toNumb))
{
incorrectPhones++;
} else
{
nbTotalCalls ++;
if (phoneData.find(fromNumb) == phoneData.end())
{
phoneData[fromNumb] = {0, 0};
}
phoneData[fromNumb].first += 1;
phoneData[fromNumb].second += countTime(ftime, etime);
}
}
} while (strcmp(type, "#") != 0);
char k[11];
do
{
scanf("%s", type);
if (strcmp(type, "#") == 0)
{
continue;
}
if (strcmp(type, "?check_phone_number") == 0)
{
printf("%d\n", incorrectPhones == 0 ? 1 : 0);
} else if (strcmp(type, "?number_calls_from") == 0)
{
scanf("%s", k);
printf("%d\n", phoneData[k].first);
} else if (strcmp(type, "?number_total_calls") == 0)
{
printf("%d\n", nbTotalCalls);
} else if (strcmp(type, "?count_time_calls_from") == 0)
{
scanf("%s", k);
printf("%d\n", phoneData[k].second);
}
} while (strcmp(type, "#") != 0);
}
int main() {
/*
this version of code has performance problem when user input data volume is large
cin slower than scanf => using scanf can improve performance
*/
// TelcoDataCheckAndQueryUsingCinForUserInput();
TelcoDataCheckAndQueryUsingScanfForUserInput();
return 0;
}