MOOC清华《程序设计基础》第8章:以二进制文件存储链表

#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[20];
}; 

struct Node      //定义链表结点 
{
	Log_info log;     //数据域 
	Node* next;     //指针域 
};

int Hash(const char* str) 
{
	int sum = 0;
	for(int i = 0; i < strlen(str); i++)
		sum ^= str[i];
	return sum;
}

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)
{
	while(list)  
	{  
    	cout << list->log.tm.year << '/' << list->log.tm.month << '/' << list->log.tm.day << ' ';
		cout << list->log.tm.hour << '/' << list->log.tm.minute << '/' << list->log.tm.second << ' ';  
		cout << list->log.id << ' ';
		cout << list->log.op << endl;
		list = list->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");  //“list.tab”就是一个二进制文件 
	
	//释放哈希表内存
	Release(list_tab);
	
	//从文件中读入哈希表内容 
	LoadHashTab(list_tab, "list.tab");
	
	//输出哈希表内容
	Output(list_tab);
	
	//释放哈希表内存
	Release(list_tab); 
	
	return 0; 
}

控制台窗口显示的内容如下(摘录)。根据运行时间的长度,可以大致判断出控制台窗口其实输出了两遍。一遍是直接从源程序构建的哈希表中输出的,另一遍是从保存到二进制文件中的哈希表输出的。



然后可执行程序所在根目录下出现了一个“list.tab”的二进制文件,将其扩展名修改为txt后,打开显示如下:


由此启发,可修改源代码,将输出的内容输出到文本文件中。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值