文本数据处理
第七周“统计记录总数”的参考程序
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream fin("log.txt");
int count = 0;
while (!fin.eof()) {
int year, month, day, hour, minute, second;
char tmp, id[20], operation[10];
fin >> year >> tmp >> month >> tmp >> day;
fin >> hour >> tmp >> minute >> tmp >> second;
fin >> id;
fin >> operation;
count++;
} fin.close();
cout << count << endl;
return 0;
}
第七周“统计活跃用户”的参考程序
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main() {
ifstream fin("log.txt");
int count = 0;
char ids[4500][20];
while (!fin.eof()) {
int year, month, day, hour, minute, second;
char tmp, id[20], operation[10];
fin >> year >> tmp >> month >> tmp >> day;
fin >> hour >> tmp >> minute >> tmp >> second;
fin >> id;
fin >> operation;
strcpy(ids[count], id);
count++;
} fin.close();
int user_count = 0;
for (int i = 0; i < count; i++) {
int found = -1;
for (int j = 0; j < i; j++)
if (strcmp(ids[i], ids[j]) == 0) {
found = j;
break;
}
if (found == -1)
user_count++;
}
cout << user_count << endl;
return 0;
}
第七周“统计活跃用户(省空间)”参考程序
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main() {
ifstream fin("log.txt");
int user_count = 0;
char ids[600][20];
while (!fin.eof()) {
int year, month, day, hour, minute, second;
char tmp, id[20], operation[10];
fin >> year >> tmp >> month >> tmp >> day;
fin >> hour >> tmp >> minute >> tmp >> second;
fin >> id;
fin >> operation;
int found = -1;
for (int i = 0; i < user_count; i++)
if (strcmp(id, ids[i]) == 0) {
found = i;
break;
}
if (found == -1) {
strcpy(ids[user_count], id);
user_count++;
}
}
fin.close();
cout << user_count << endl;
return 0;
}
第七周“统计在线时长”的参考程序
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct Time_t {
int year, month, day;
int hour, minute, second;
};
int TimeDifference(Time_t, Time_t);
int main() {
ifstream fin("log.txt");
int user_count = 0;
char ids[600][20];
bool online[600];
Time_t last_on[600];
int secs[600];
while (!fin.eof()) {
Time_t t;
char tmp, id[20], operation[10];
fin >> t.year >> tmp >> t.month >> tmp >> t.day;
fin >> t.hour >> tmp >> t.minute >> tmp >> t.second;
fin >> id;
fin >> operation;
int found = -1;
for (int i = 0; i < user_count; i++)
if (strcmp(id, ids[i]) == 0) {
found = i;
break;
}
if (found == -1) {
strcpy(ids[user_count], id);
if (strcmp(operation, "LOGIN") == 0) {
online[user_count] = true;
last_on[user_count] = t;
}
else
online[user_count] = false;
secs[user_count] = 0;
user_count++;
}
else {
if (strcmp(operation, "LOGIN") == 0) {
if (!online[found]) {
online[found] = true;
last_on[found] = t;
}
}
else {
if (online[found]) {
online[found] = false;
secs[found] += TimeDifference(last_on[found], t);
}
}
}
}
fin.close();
for (int i = 0; i < user_count; i++)
cout << ids[i] << " " << secs[i] << endl;
return 0;
}
int TimeDifference(Time_t s, Time_t t) {
int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day_count = t.day - s.day;
for (int i = s.month; i < t.month; i++)
day_count += days[i - 1];
int result = day_count * 60 * 60 * 24;
result += (t.hour - s.hour) * 60 * 60;
result += (t.minute - s.minute) * 60;
result += (t.second - s.second);
return result;
}
第七周“统计在线时长(写文件)”参考程序
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct Time_t {
int year, month, day;
int hour, minute, second;
};
int TimeDifference(Time_t, Time_t);
int main() {
ifstream fin("log.txt");
int user_count = 0;
char ids[600][20];
bool online[600];
Time_t last_on[600];
int secs[600];
while (!fin.eof()) {
Time_t t;
char tmp, id[20], operation[10];
fin >> t.year >> tmp >> t.month >> tmp >> t.day;
fin >> t.hour >> tmp >> t.minute >> tmp >> t.second;
fin >> id;
fin >> operation;
int found = -1;
for (int i = 0; i < user_count; i++)
if (strcmp(id, ids[i]) == 0) {
found = i;
break;
}
if (found == -1) {
strcpy(ids[user_count], id);
if (strcmp(operation, "LOGIN") == 0) {
online[user_count] = true;
last_on[user_count] = t;
}
else
online[user_count] = false;
secs[user_count] = 0;
user_count++;
}
else {
if (strcmp(operation, "LOGIN") == 0) {
if (!online[found]) {
online[found] = true;
last_on[found] = t;
}
}
else {
if (online[found]) {
online[found] = false;
secs[found] += TimeDifference(last_on[found], t);
}
}
}
}
fin.close();
for (int i = 0; i < user_count; i++)
cout << ids[i] << " " << secs[i] << endl;
return 0;
}
int TimeDifference(Time_t s, Time_t t) {
int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day_count = t.day - s.day;
for (int i = s.month; i < t.month; i++)
day_count += days[i - 1];
int result = day_count * 60 * 60 * 24;
result += (t.hour - s.hour) * 60 * 60;
result += (t.minute - s.minute) * 60;
result += (t.second - s.second);
return result;
}
非文本数据处理 Hash算法 二进制文件
第八周1
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct Time_t {
int year, month, day;
int hour, minute, second;
};
struct Node {
Time_t tm;
char id[20];
char op[10];
Node* next;
};
int Hash(char* id) {
int res = 0;
for (int i = 0; id[i]; i++) {
res = (res + id[i] * 7) % 256;
}
return res;
}
void Insert(Node* hash_tab[], Node elem) {
int idx = Hash(elem.id);
Node* data = new Node;
*data = elem;
data->next = hash_tab[idx];
hash_tab[idx] = data;
}
void Print(Node* list) {
Node* now = list;
while (now != NULL) {
cout << now->id << " " << now->op << endl;
now = now->next;
}
}
void Output(Node* hash_tab[]) {
for (int i = 0; i < 256; i++) {
if (hash_tab[i] == NULL) continue;
cout << i << ": ";
Print(hash_tab[i]);
cout << endl << endl;
}
}
void Delete(Node* list) {
while (list) {
Node* tmp = list;
list = list->next;
delete tmp;
}
}
void Release(Node* hash_tab[]) {
for (int i = 0; i < 256; i++) {
if (hash_tab[i] == NULL) {
continue;
}
Delete(hash_tab[i]);
hash_tab[i] = NULL;
}
}
int main() {
Node* list_tab[256] = { NULL };
ifstream fin("log.txt");
while (!fin.eof()) {
char tmp;
Node data;
fin >> data.tm.year >> tmp >> data.tm.month >> tmp >> data.tm.day;
fin >> data.tm.hour >> tmp >> data.tm.minute >> tmp >> data.tm.second;
fin >> data.id;
fin >> data.op;
Insert(list_tab, data);
}
fin.close();
Output(list_tab);
Release(list_tab);
return 0;
}
第八周2
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct Time_t {
int year, month, day;
int hour, minute, second;
};
struct Log_info {
Time_t tm;
char id[20];
char op[10];
};
struct Node {
Log_info log;
Node* next;
};
int Hash(char* id) {
int res = 0;
for (int i = 0; id[i]; i++) {
res = (res + id[i] * 7) % 256;
}
return res;
}
void Insert(Node* hash_tab[], Node elem) {
int idx = Hash(elem.log.id);
Node* data = new Node;
*data = elem;
data->next = hash_tab[idx];
hash_tab[idx] = data;
}
void Print(Node* list) {
Node* now = list;
while (now != NULL) {
cout << now->log.id << " " << now->log.op << endl;
now = now->next;
}
}
void Output(Node* hash_tab[]) {
for (int i = 0; i < 256; i++) {
if (hash_tab[i] == NULL) continue;
cout << i << ": ";
Print(hash_tab[i]);
cout << endl << endl;
}
}
void Delete(Node* list) {
while (list) {
Node* tmp = list;
list = list->next;
delete tmp;
}
}
void Release(Node* hash_tab[]) {
for (int i = 0; i < 256; i++) {
if (hash_tab[i] == NULL) {
continue;
}
Delete(hash_tab[i]);
hash_tab[i] = NULL;
}
}
void SaveHashTab(Node* hash_tab[], const char* filename) {
ofstream fout(filename, ios::binary);
for (int i = 0; i < 256; i++) {
Node* p = hash_tab[i];
while (p) {
fout.write((char*)&(p->log), sizeof(p->log));
p = p->next;
}
}
fout.close();
}
void LoadHashTab(Node* hash_tab[], const char* filename) {
ifstream fin(filename, ios::binary);
while (fin) {
Node data;
fin.read((char*)&(data.log), sizeof(data.log));
if (fin.eof()) {
break;
}
Insert(hash_tab, data);
}
fin.close();
}
int main() {
Node* list_tab[256] = { NULL };
ifstream fin("log.txt");
while (!fin.eof()) {
char tmp;
Node data;
fin >> data.log.tm.year >> tmp >> data.log.tm.month >> tmp >> data.log.tm.day;
fin >> data.log.tm.hour >> tmp >> data.log.tm.minute >> tmp >> data.log.tm.second;
fin >> data.log.id;
fin >> data.log.op;
Insert(list_tab, data);
}
fin.close();
Output(list_tab);
SaveHashTab(list_tab, "list.tab");
Release(list_tab);
LoadHashTab(list_tab, "list.tab");
Output(list_tab);
Release(list_tab);
return 0;
}