-
Notifications
You must be signed in to change notification settings - Fork 4
/
print_contact_table.cpp
56 lines (48 loc) · 1.48 KB
/
print_contact_table.cpp
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
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Contact {
string first_name;
string last_name;
string telephone;
string email;
};
const vector<int> column_width{10, 10, 15, 20};
void print_line();
void print_row(const vector<string>& row);
void print_table(const vector<Contact>& contacts);
int main() {
vector<Contact> contacts{
{"Alice", "Smith", "12345678", "alice@foxmail.com"},
{"Bob", "Jones", "23456789", "bob@gmail.com"},
{"Cindy", "Williams", "34567890", "cindy@sina.com"},
{"Dale", "Brown", "45678901", "dale@hotmail.com"},
{"Eric", "Taylor", "56789012", "eric@yahoo.com"},
{"Frank", "Wilson", "67890123", "frank@sohu.com"},
{"Grace", "Thomas", "78901234", "grace@qq.com"},
{"Helen", "Johnson", "89012345", "helen@163.com"},
};
print_table(contacts);
return 0;
}
void print_line() {
for (int w : column_width)
cout << '+' << string(w, '-');
cout << '+' << endl;
}
void print_row(const vector<string>& row) {
for (int i = 0; i < row.size(); ++i)
cout << '|' << setw(column_width[i]) << row[i];
cout << '|' << endl;
}
void print_table(const vector<Contact>& contacts) {
cout << left;
print_line();
print_row({"first_name", "last_name", "telephone", "email"});
print_line();
for (const Contact& c : contacts)
print_row({c.first_name, c.last_name, c.telephone, c.email});
print_line();
}