模拟手机短信系统(结构体实现)

本文介绍了一个短信管理系统的详细设计,包括编辑、发送、接收、存储和显示短信的功能。系统使用双向循环链表存储短信,通过链表操作实现短信的编辑、删除、发送和接收。文章深入解析了每个功能模块的实现细节,如时间戳生成、文件读写、用户界面设计等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Source.cpp

#include "com_def.h"
#include"SendMessage.h"
#include"ReceiveMessage.h"
#include<cstdio>
#include<CTime>
using namespace std;
void main(){
	PNode p,head;
	p = (PNode)malloc(sizeof(Node));//初始化收件箱和发件箱链表(双向循环链表)
	p->next = p;
	p->prior=p;

	head=(PNode)malloc(sizeof(Node));//初始化收件箱链表(双向循环链表)
	if(!head)
	{
		cout<<"Error!"<<endl;
		exit(0);
	}
	head->next=head;
	head->prior=head;
	ReadMessage(head);//将收件箱的文件读出来链成双向链表,返回链表的头指针

	int n;
	InitList(p);
	do{
		system("cls");
		cout<<"请选择:"<<endl<<"0.退出"<<endl<<"1.编辑短信"<<endl<<"2.查看发件箱"<<endl<<"3.查看草稿箱"<<endl<<"4.查看收件箱"<<endl;;
		cin>>n;
		switch(n){
			case 0:
				break;
			case 1:
				EditMessage(p,head);
				break;
			case 2:
				OutsendMessage(p);
				break;
			case 3:
				OutdraftMessage(p,head);
				break;
			case 4:
			    OutReciveMessage(head);
				break;
		default:
			cout<<"输入错误!"<<endl;
		}
	}while(n);

	//释放头结点
	free(p);
	free(head);
}

com_def.h

#include<iostream.h>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<cstdio>
#include<time.h>
#include<ctime>
using namespace std;

#define N "00000000000000000000000000000"//宏定义标志信息结点是从文件读取还是从键盘输入
#define sender "XYC"//默认发件人ID为XYC
#define receive "LD"//默认收件人ID为LD




						//收件箱只会收到发给刘丹的信息

string SenderFileName1=sender;//保存收件箱和草稿箱的名字
string ReciveFileName1=receive;//保存收件箱的名字

string SenderFileName=SenderFileName1+"sendfile.txt";//保存收件箱文本的名字
string ReciveFileName=ReciveFileName1+"recivefile.txt";//保存收件箱文本的名字
string DraftFileName=SenderFileName1+"draftfile.txt";//保存草稿箱文本的名字

char *day[] ={ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};//输出周几需要的数组
typedef struct Message{
	char Receiver[10];
	char News[30];
	char Sender[10];
}Message;
typedef struct Time
{
    int sec;  /*秒,正常范围0-59, 但允许至61*/
    int min;  /*分钟,0-59*/
    int hour; /*小时, 0-23*/
    int mday; /*日,即一个月中的第几天,1-31*/
    int mon;  //月, 从一月算起,0-11 1+p->tm_mon;
    int year;  //年, 从1900至今已经多少年 1900+ p->tm_year;
    int wday; /*星期,一周中的第几天, 从星期日算起,0-6*/
}Time;

typedef struct Node{
	int Flag;	//在发送信息时标志草稿箱或者发件箱的短信,在收件箱标志已读未读
	Message Mess;//短信的结构体
	char ShowTime[30];//从文件读取的时间的字符数组
	struct Time time;//存储时间的结构体
	struct Node *prior;//双向循环链表的前驱指针
	struct Node *next;//后继指针
}Node,*PNode;
 

SendMessage.h


void StroeTime(PNode &q)//存储发送或者编辑短信时间的函数
{
	time_t now;
	time(&now);
	struct tm *SendTime;
	SendTime=localtime(&now);

	q->time.year=SendTime->tm_year;
	q->time.mon=SendTime->tm_mon;
	q->time.mday=SendTime->tm_mday;
	q->time.wday=SendTime->tm_wday;
	q->time.hour=SendTime->tm_hour;
	q->time.min=SendTime->tm_min;
	q->time.sec=SendTime->tm_sec;
	strcpy(q->ShowTime,N);
}
int InitList(PNode &L){//将收件箱和草稿箱中的信息链接起来形成一个双向循环链表
						//Flag=1;为收件箱中的已发送内容,flag=2位草稿箱中的内容
//初始化时,如果收件箱或者草稿箱中已有内容,则将其链入链表方便查看

	fstream SendMessage;//将发件箱里面短信写入到链表里面
	SendMessage.open(SenderFileName.c_str(),ios::in);
	while(!SendMessage.eof())
	{
		PNode q = (PNode)malloc(sizeof(Node));
		if(!q)
		{
			cout<<"Error!"<<endl;
			return 0;
		}//申请失败则提示错误
		
		SendMessage.getline(q->ShowTime,80);//从草稿箱中分别读取年月日、周几、时分秒到信息结点


		if(!SendMessage.getline(q->Mess.Receiver,80))//读出收件人的姓名,读到空行则结束
			break;
		if(!SendMessage.getline(q->Mess.News,80))//读出发件人的信息,读到空行则结束
			break;
		if(!SendMessage.getline(q->Mess.Sender,80))//读出收件人姓名,读到空行则结束
			break;
		
		q->Flag=1;//由于是从发送箱中读取,故默认为1
		L->next->prior=q;//双向循环链表的建立
		q->next=L->next;
		q->prior=L;
		L->next=q;
		
	}
	SendMessage.close();

	fstream DraftMessage;//将草稿箱里面短信写入到链表里面
	DraftMessage.open(DraftFileName.c_str(),ios::in);
	while(!DraftMessage.eof())
	{
		PNode q = (PNode)malloc(sizeof(Node));
		if(!q)
		{
			cout<<"Error!"<<endl;
			return 0;
		}//申请失败则提示错误

		DraftMessage.getline(q->ShowTime,80);//从草稿箱中分别读取年月日、周几、时分秒到信息结点
		

		if(!DraftMessage.getline(q->Mess.Receiver,80))//读出收件人的姓名,读到空行则结束
			break;
		if(!DraftMessage.getline(q->Mess.News,80))	//读出发件人的信息,读到空行则结束
			break;
		if(!DraftMessage.getline(q->Mess.Sender,80))//读出收件人姓名,读到空行则结束
			break;

		q->Flag=2;//由于是从草稿箱中读取,故默认为2
		L->next->prior=q;//双向循环链表的建立
		q->next=L->next;
		q->prior=L;
		L->next=q;	

	}
	DraftMessage.close();

	return 1;
}
//编辑短信操作涉及函数
void SendMessage(PNode &p,int s,PNode &head){//用来将短信写入3个文件

	PNode q;
	ofstream ofsend,ofreceive,ofdraft;
	if(s==1){

		ofsend.open(SenderFileName.c_str(),ios::app);
		ofreceive.open(ReciveFileName.c_str(),ios::app);

		ofsend<<(1900 +p->time.year)<<"-"<<(1 + p->time.mon)<<"-"<<p->time.mday<<"  "<<day[p->time.wday]<<" ";
		ofsend<<p->time.hour<<":"<<p->time.min<<":"<<p->time.sec<<endl;
		ofsend<<p->Mess.Receiver<<endl<<p->Mess.News<<endl<<sender<<endl;
		if(!strcmp(p->Mess.Receiver,receive))
		{
			ofreceive<<(1900 +p->time.year)<<"-"<<(1 + p->time.mon)<<"-"<<p->time.mday<<"  "<<day[p->time.wday]<<" ";
			ofreceive<<p->time.hour<<":"<<p->time.min<<":"<<p->time.sec<<endl;

			ofreceive<<sender<<endl<<p->Mess.News<<endl<<p->Mess.Receiver<<endl;
			q=(PNode)malloc(sizeof(Node));
			strcpy(q->Mess.News,p->Mess.News);
			strcpy(q->Mess.Receiver,p->Mess.Receiver);
			strcpy(q->Mess.Sender,p->Mess.Sender);
			q->Flag=p->Flag;
			strcpy(q->ShowTime,N);
			q->time.hour=p->time.hour;
			q->time.mday=p->time.mday;
			q->time.min=p->time.min;
			q->time.mon=p->time.mon;
			q->time.sec=p->time.sec;
			q->time.wday=p->time.wday;
			q->time.year=p->time.year;

			q->next=head->next;//收件箱双向循环链表的建立
			head->next->prior=q;
			q->prior=head;
		    head->next=q;
			q->Flag=0;//设置flag为0表示未读
		}
		ofsend.close();
		ofreceive.close();//分别写入发件箱和收件箱
		cout<<"已写入发件箱"<<endl;
	}
	else if(s==2)
	{
		ofdraft.open(DraftFileName.c_str(),ios::app);

		ofdraft<<(1900 +p->time.year)<<"-"<<(1 + p->time.mon)<<"-"<<p->time.mday<<"  "<<day[p->time.wday]<<" ";
		ofdraft<<p->time.hour<<":"<<p->time.min<<":"<<p->time.sec<<endl;

		ofdraft<<p->Mess.Receiver<<endl<<p->Mess.News<<endl<<sender<<endl;
		
		ofdraft.close(); //写入草稿箱
		cout<<"已写入草稿箱"<<endl;
	}
}

int EditMessage(PNode &L,PNode &head){//用来编辑短信。
	//注:将标记改为了int型,输入s后直接保存在flag中,用于判断时草稿箱还是发件箱
	int s;
	PNode q = (PNode)malloc(sizeof(Node));
	cout<<"依次输入收件人,短信内容:"<<endl;
	cin>>q->Mess.Receiver>>q->Mess.News;
	cout<<"1.发送短信"<<endl<<"2.存入草稿箱"<<endl;
	cin>>s;
	q->Flag=s;
	strcpy(q->Mess.Sender,sender);
	//存入发送时间到发送信息中
	StroeTime(q);
	SendMessage(q,s,head);//写入对应的buff.txt中
	L->next->prior=q;//链入双向循环链表
	q->next=L->next;
	q->prior=L;
	L->next=q;
	system("pause");
	system("cls");
	cout<<"是否继续编写短信:"<<endl;
	cout<<"1.是    2.否"<<endl;
	cin>>s;
	if(s==1)
	{
		EditMessage(L,head);
	}
	return 1;
}
//草稿箱操作涉及函数
int ShowDraftMessage(PNode L)//返回的是草稿箱的短信个数
{
	PNode p=L;
	int i=0;
	while(p->next!=L){//遍历链表输出草稿箱内容
		p = p->next;
		if(p->Flag==2)//flag=2为草稿箱
		{//输出时间
			if(strcmp(p->ShowTime,N))
			{
				cout<<++i<<".接收短信的时间"<<p->ShowTime<<endl;
				cout<<" 发件人: "<<p->Mess.Receiver<<endl;
			}
			else
			{
				cout<<++i<<".编辑草稿箱时间:"<<(1900 +p->time.year)<<"-"<<(1 + p->time.mon)<< "-"<<(p->time.mday);
				cout<<"  "<<day[p->time.wday]<<"  "<<p->time.hour<<":"<<p->time.min<<":"<< p->time.sec<<endl;
				cout<<"   收件人:"<<p->Mess.Receiver<<endl;
			}
		}
	}
	if(!i){//如果草稿箱为空则提示草稿箱为空
		cout<<"草稿箱为空"<<endl;
	}
	system("pause");
	return i;
}

void FindDraftMessage(PNode L,int i)//查找草稿箱中的某条信息,传入参数为链表头结点,查找信息的序号
{
	int j=0;
	PNode p=L;
	while(p->next!=L){//查找草稿箱中的第i条信息
		p = p->next;
		if(p->Flag == 2){
			j++;
			if(j==i)
			{
				//输出时间
			if(strcmp(p->ShowTime,N))
			{
				cout<<i<<".接收短信的时间"<<p->ShowTime<<endl;
				cout<<" 发件人: "<<p->Mess.Receiver<<endl;
			}
			else
			{
				cout<<i<<".编辑草稿箱时间:"<<(1900 +p->time.year)<<"-"<<(1 + p->time.mon)<< "-"<<(p->time.mday);
				cout<<"  "<<day[p->time.wday]<<"  "<<p->time.hour<<":"<<p->time.min<<":"<< p->time.sec<<endl;
				cout<<"收件人:"<<p->Mess.Receiver<<"短信内容为:"<<p->Mess.News <<endl;	
			}
			}
		}		
	}
	system("pause");
}

int DelateDraftMessage(PNode L,int i){//清空草稿箱,将链表重新写入草稿箱

	int j=0;
	PNode p=L,q;
	ofstream ofdraft(DraftFileName.c_str());
	ofdraft.clear();//清空草稿箱

	while(p->prior!=L){//遍历链表
		p = p->prior;
		if((p->Flag == 2)){//删除链表里面草稿箱的某条信息
			j++;
			if(j == i){
				q=p->next ;
				p->next->prior=p->prior;
				p->prior->next=p->next;

			//	p->prior ->next = p->next ;
			//	p->next ->prior = p->prior;
				//q=p->prior ;
				free(p);
				p=q;
			}
			else
			{
				 if(strcmp(p->ShowTime,N))
				{
					ofdraft<<p->ShowTime<<endl;
					ofdraft<<p->Mess.Receiver<<endl<<p->Mess.News<<endl<<sender<<endl;	//除了删除节点,原来其他的草稿箱短信重新写入	
				}
				else{

					ofdraft<<(1900 +p->time.year)<<"-"<<(1 + p->time.mon)<<"-"<<p->time.mday<<"  "<<day[p->time.wday]<<" ";
					ofdraft<<p->time.hour<<":"<<p->time.min<<":"<<p->time.sec<<endl;

					ofdraft<<p->Mess.Receiver<<endl<<p->Mess.News<<endl<<sender<<endl;	//除了删除节点,原来其他的草稿箱短信重新写入
				}
			}
		}


	}
	
	ofdraft.close(); //关闭草稿箱
	return 1;
}
//删除发送后草稿箱内的信息
void WriteDraft(PNode L)
{
	PNode p=L;
	ofstream ofdraft(DraftFileName.c_str());
	ofdraft.clear();//清空草稿箱
	while(p->prior!=L)
	{
		p=p->prior;
		if(L->Flag==2)
		{
			ofdraft<<(1900 +p->time.year)<<"-"<<(1 + p->time.mon)<<"-"<<p->time.mday<<"  "<<day[p->time.wday];
			ofdraft<<p->time.hour<<":"<<p->time.min<<":"<<p->time.sec<<endl;
		
			ofdraft<<p->Mess.Receiver<<endl<<p->Mess.News<<endl<<sender<<endl;	//除了删除节点,原来其他的草稿箱短信重新写入		
		}
	}
	ofdraft.close(); //关闭草稿箱
}

void DraftToSendbuff(PNode L,int i,PNode head){//将草稿箱里的短信发送给发送箱
	int j=0,flag;
	PNode p=L;
	while(p->next!=L){//遍历链表输出发件箱内容
		p = p->next;
		if(p->Flag == 2){
			j++;
			if(j==i)
			{
				p->Flag=1;//修改信息的状态,变为已发送状态
				cout<<"草稿信息:收件人: "<<p->Mess.Receiver<<" 发送信息:"<<p->Mess.News<<endl;
				cout<<"1.修改草稿后发送"<<endl<<"2.直接发送草稿信息"<<endl;
				cin>>flag;
				if(flag==1)
				{
					cout<<"依次输入收件人,短信内容:"<<endl;
					cin>>p->Mess.Receiver>>p->Mess.News;
				
					//发送草稿箱的短信时间
					StroeTime(p);
					SendMessage(p,1,head);//写入发送箱中
					WriteDraft(L);
					cout<<"发送成功"<<endl;
				}
				else if(flag==2)
				{
				
					//发送草稿箱的短信时间
										
					StroeTime(p);
					SendMessage(p,1,head);//写入发送箱中
					WriteDraft(L);
					cout<<"发送成功"<<endl;
				}
			}
		}		
	}
	system("pause");
}

//清空草稿箱内的内容,同时清空链表释放内存
void EmptyDraftFile(PNode &L)
{
	ofstream EmptyFile;//清空收件箱内的内容
	EmptyFile.open(DraftFileName.c_str());
	EmptyFile.clear();
	EmptyFile.close();
	
	PNode p,q;
	p=L;
	while(p->next != L)//将收件箱信息链表内的结点清空,同时保留头结点形成空的循环链表
	{
		p=p->next;
		if(p->Flag == 2)
		{
			q=p->prior;
			p->next->prior=p->prior;
			p->prior->next=p->next;
			free(p);
			p=q;
		}
	}
	
}
int OutdraftMessage(PNode L,PNode head){//对草稿箱的具体操作
	PNode p=L;
	int i=0,j=0,choice=2;//默认不将草稿箱里的短信发送给发送箱

	i=ShowDraftMessage(L);//显示草稿箱里的短信

	do{
		system("cls");
		cout<<"1.查找短信"<<endl<<"2.删除短信"<<endl<<"3.发送短信"<<endl<<"4.清空草稿箱"<<endl<<"0.返回"<<endl;
		cin>>choice;
		switch(choice){
			case 1:
				cout<<"请输入想查找的短信(输入序号即可)"<<endl;
				cin>>j;
				if(j<1||j>i){
					cout<<"没有该短信"<<endl;
				}
				else{
					FindDraftMessage(L,j);
				}
				break;
			case 2:
				cout<<"请输入想删除的短信(输入序号即可)"<<endl;
				cin>>j;
				if(j<1||j>i){
					cout<<"没有该短信"<<endl;
				}
				else{
					DelateDraftMessage(L,j);//删除draftbuff.txt里面的记录
					ShowDraftMessage(L);//显示草稿箱里的短信
				}
				break;
			case 3:
				cout<<"请输入想发送的短信(输入序号即可)"<<endl;
				cin>>j;
				if(j<1||j>i){
					cout<<"没有该短信"<<endl;
				}
				else{
					DraftToSendbuff(L,j,head);//草稿发送给发送箱,并且将草稿箱里面这个信息的记录删除,在发送箱里面增加该信息记录
					ShowDraftMessage(L);//显示草稿箱里的短信
				}
				break;
			case  4:
				EmptyDraftFile(L);
				ShowDraftMessage(L);
				break;
			case 0:
				break;
			default:
				cout<<"输入错误!"<<endl;
			}//switch
	}while(choice);
	cout<<endl;
	return 1;
}
//发件箱操作函数
int ShowsendMessage(PNode L)//返回的是发件箱的短信个数
{
	PNode p=L;
	int i=0;
	while(p->next!=L){//遍历链表输出发件箱内容
		p = p->next;
		if(p->Flag==1)//输出时间
		{
			if(strcmp(p->ShowTime,N))
			{
				cout<<++i<<".发送短信时间:"<<p->ShowTime<<endl;
				cout<<"   收件人:"<<p->Mess.Receiver<<endl;
			}
			else{
				cout<<++i<<".发送短信时间:"<<(1900 +p->time.year)<<"-"<<(1 + p->time.mon)<< "-"<<(p->time.mday);
				cout<<"  "<<day[p->time.wday]<<"  "<<p->time.hour<<":"<<p->time.min<<":"<< p->time.sec<<endl;
				cout<<"   收件人:"<<p->Mess.Receiver<<endl;
			}
		}
	}
	if(!i){//如果发件箱为空则提示发件箱为空
		cout<<"发件箱为空"<<endl;
	}
	system("pause");
	return i;
}

void FindSendMessage(PNode L,int i)//查找发件箱中的某条短信
{
	int j=0;
	PNode p=L;
	while(p->next!=L){//查找发件箱中的某条短信
		p = p->next;
		if(p->Flag == 1){
			j++;
			if(j==i)
			{
				//输出时间
				if(strcmp(p->ShowTime,N))
				{
					cout<<i<<".发送短信时间:"<<p->ShowTime<<endl;
					cout<<"收件人:"<<p->Mess.Receiver<<" 发送短信内容:"<<p->Mess.News <<endl;
				}
				else{
					cout<<i<<".编辑草稿箱时间:"<<(1900 +p->time.year)<<"-"<<(1 + p->time.mon)<< "-"<<(p->time.mday);
					cout<<"  "<<day[p->time.wday]<<"  "<<p->time.hour<<":"<<p->time.min<<":"<< p->time.sec<<endl;
					cout<<"收件人:"<<p->Mess.Receiver<<" 发送短信内容:"<<p->Mess.News <<endl;
				}
			}	
		}		
	}
	system("pause");
}

int DelateSendMessage(PNode L,int i){//清空发送箱,将链表重新写入发送箱
	int j=0;
	PNode p=L,q;
	ofstream ofsend(SenderFileName.c_str());
	ofsend.clear();//清空发送箱

	while(p->prior!=L){//遍历链表
		p = p->prior;
		if((p->Flag == 1)){//删除链表里面发送箱的某条信息
			j++;
			if(j == i){
				p->next ->prior = p->prior ;
				p->prior ->next = p->next;
				q=p->next ;
				free(p);
				p=q;
			}
			else
			{
				if(strcmp(p->ShowTime,N))
				{
					ofsend<<p->ShowTime<<endl;
					ofsend<<p->Mess.Receiver<<endl<<p->Mess.News<<endl<<sender<<endl;	//除了删除节点,原来其他的发送箱短信重新写入
	
				}
				else{
	
					ofsend<<(1900 +p->time.year)<<"-"<<(1 + p->time.mon)<<"-"<<p->time.mday<<"  "<<day[p->time.wday]<<" ";
					ofsend<<p->time.hour<<":"<<p->time.min<<":"<<p->time.sec<<endl;
					ofsend<<p->Mess.Receiver<<endl<<p->Mess.News<<endl<<sender<<endl;	//除了删除节点,原来其他的发送箱短信重新写入
				}
			}
		}
		
	}
	
	ofsend.close(); //写入草稿箱
	return 1;
}
//清空发件箱内的内容,同时清空链表释放内存
void EmptySenderFile(PNode &L)
{
	ofstream EmptyFile;//清空收件箱内的内容
	EmptyFile.open(SenderFileName.c_str());
	EmptyFile.clear();
	EmptyFile.close();
	
	PNode p,q;
	p=L;
	while(p->next != L)//将收件箱信息链表内的结点清空,同时保留头结点形成空的循环链表
	{
		p=p->next;
		if(p->Flag == 1)
		{
			q=p->prior;
			p->next->prior=p->prior;
			p->prior->next=p->next;
			free(p);
			p=q;
		}
	}
	
}
int OutsendMessage(PNode L){//发件箱的具体操作
	PNode p=L;
	int i=0,j=0,choice=2;

	i=ShowsendMessage(L);//显示发件箱里的短信

	do{
		system("cls");
		cout<<"1.查找短信"<<endl<<"2.删除短信"<<endl<<"3.清空发件箱"<<endl<<"0.返回"<<endl;
		cin>>choice;
		switch(choice){
			case 1:
				cout<<"请输入想查找的短信(输入序号即可)"<<endl;
				cin>>j;
				if(j<1||j>i){
					cout<<"没有该短信"<<endl;
				}
				else{
					FindSendMessage(L,j);
				}
				break;
			case 2:
				cout<<"请输入想删除的短信(输入序号即可)"<<endl;
				cin>>j;
				if(j<1||j>i){
					cout<<"没有该短信"<<endl;
				}
				else{
					DelateSendMessage(L,j);//删除sendbuff.txt里面的记录
					ShowsendMessage(L);//显示发件箱里的短信
				}
				break;
			case 3:
				EmptySenderFile(L);
				ShowsendMessage(L);
				break;
			case 0:
				break;
			default:
				cout<<"输入错误!"<<endl;
			}//switch
	}while(choice);
	cout<<endl;
	return 1;
}

ReceiveMessage.h

//读取收件箱内的内容,用双向链表存储起来,实现阅读,查找,删除功能,存入草稿箱的功能
//Flag 在此处表示已读未读标记

int ReadMessage(PNode &head)//从收件箱读出所有发给自己ID的信息,链成双向链表
{
	ifstream ReadMessage;
	PNode ptr;

	ReadMessage.open(ReciveFileName.c_str());
	while(!ReadMessage.eof())
	{
		ptr=(PNode)malloc(sizeof(Node));
		if(!ptr)
		{
			cout<<"Error!"<<endl;
			return 0;
		}

		ReadMessage.getline(ptr->ShowTime,80);//从草稿箱中分别读取年月日、周几、时分秒到信息结点
		
		if(!ReadMessage.getline(ptr->Mess.Sender,80))//读出发件人的姓名,读到空行则结束
			break;
		if(!ReadMessage.getline(ptr->Mess.News,80))//读出发件人的信息,读到空行则结束
			break;
		if(!ReadMessage.getline(ptr->Mess.Receiver,80))//读出收件人姓名,读到空行则结束
			break;

			ptr->next=head->next;//双向循环链表的建立
			head->next->prior=ptr;
			ptr->prior=head;
			head->next=ptr;
			ptr->Flag=0;//设置flag为0表示未读

	}

	ReadMessage.close();
	return 0;
}

int ReadAllMessage(PNode &head)//显示在收件箱中所有短信,返回信息条数
{
	PNode q;
	int i=0;
	q=head;
	while(q->next != head)//遍历双向循环链表,读出信息时给信息添加序号显示到屏幕上
	{					//同时将结构体内的flag设为1标为已读
		q=q->next;
		if(strcmp(q->ShowTime,N))
			{
				cout<<++i<<".发送短信时间:"<<q->ShowTime<<endl;
				cout<<"   发件人:"<<q->Mess.Receiver<<endl;
			}
		else{
				cout<<++i<<".接收短信时间:"<<(1900 +q->time.year)<<"-"<<(1 + q->time.mon)<< "-"<<(q->time.mday);
				cout<<"  "<<day[q->time.wday]<<"  "<<q->time.hour<<":"<<q->time.min<<":"<< q->time.sec<<endl;
				cout<<"   发件人:"<<q->Mess.Receiver<<endl;
		}
	}
	if(i==0)//判断收件中的内容是否为空
	{
		cout<<"收件箱为空!"<<endl;
		return 0;
	}
	return i;
}

int  ReadOneMessage(PNode &head,int choice)//查找某条短信,同时标为已读
{
	int i=0;
	PNode p;
	p=head;
	while(i<choice  && p->next!=head)
	{
		p=p->next;
		i++;
	}
	if(i==0)//判断收件中的内容是否为空
	{
		cout<<"收件箱为空!"<<endl;
		return 0;
	}
	if(i == choice)//退出循环时i 和choice 相等则查找到信息并且输出信息
	{
		if(strcmp(p->ShowTime,N))
		{
		
		cout<<"第"<<choice<<"条短信接收的时间:"<<p->ShowTime<<endl;
		cout<<"               发件人:"<<p->Mess.Sender<<" 发送信息:"<<p->Mess.News<<endl;
		}
		else{
			cout<<++i<<".接收短信时间:"<<(1900 +p->time.year)<<"-"<<(1 + p->time.mon)<< "-"<<(p->time.mday);
			cout<<"  "<<day[p->time.wday]<<"  "<<p->time.hour<<":"<<p->time.min<<":"<< p->time.sec<<endl;
			cout<<"    收件人:"<<p->Mess.Receiver<<endl;
		}
		p->Flag=1;
		return 1;
	}
	else//退出循环时i 和choice 不相等则查找到信息并且输出信息
	{
		cout<<"无该序号短信"<<endl;
		return 0;
	}
}
//删除双向循环链表的某条已阅读信息,已读则删除删除双向链表里的节点,同时删除收件箱内txt的该条信息的内容
//否则如果未阅读则先阅读后才能删除结点,文件内的信息,并释放节点内存
int DeleteMessage(PNode &head,int Slect)
{
	ofstream WriteFile;
	WriteFile.open(ReciveFileName.c_str());
	WriteFile.clear();
	PNode p=head;
	PNode q;
	int i=0;
	while(p->prior != head)
	{
		p=p->prior;
		i++;
		if(i==Slect)
		{
			q= p->next;
			p->next->prior= p->prior;
			p->prior->next = p->next;
			free(p);
			p=q;

		}
		else
		{
	    	if(strcmp(p->ShowTime,N))
			{
				WriteFile<<p->ShowTime<<endl;
				WriteFile<<p->Mess.Receiver<<endl<<p->Mess.News<<endl<<sender<<endl;	//除了删除节点,原来其他的发送箱短信重新写入
	
			}
			else{
	
				WriteFile<<(1900 +p->time.year)<<"-"<<(1 + p->time.mon)<<"-"<<p->time.mday<<"  "<<day[p->time.wday]<<" ";
				WriteFile<<p->time.hour<<":"<<p->time.min<<":"<<p->time.sec<<endl;
				WriteFile<<p->Mess.Receiver<<endl<<p->Mess.News<<endl<<sender<<endl;	//除了删除节点,原来其他的发送箱短信重新写入
			}
		}
	}
	if(i==0)
	{
		cout<<"短信息为空,无可删除短信"<<endl;
		return 0;
	}
	 if(i < Slect )
	{
		cout<<"无该条短信"<<endl;
		return 0;
	}
	 WriteFile.close();
	 return 1;
}
//清空收件箱内的内容,同时清空链表释放内存
void EmptyReciverFile(PNode &head)
{
	ofstream EmptyFile;//清空收件箱内的内容
	EmptyFile.open(ReciveFileName.c_str());
	EmptyFile.clear();
	EmptyFile.close();
	
	PNode p,q;
	p=head->next;
	while(p != head)//将收件箱信息链表内的结点清空,同时保留头结点形成空的循环链表
	{
		q=p->next;
		free(p);
		p=q;
	}
	head->next=head;
	head->prior=head;
	cout<<"收件箱已清空!"<<endl;
}
void OutReciveMessage(PNode &head)//收件箱中可能进行的操作,查看、删除信息
{
	int Choice,k;
	k=ReadAllMessage(head);//读出所有信息
	system("pause");	
	do
	{
		system("cls");
		cout<<"请选择:"<<endl<<"1.查找短信"<<endl<<"2.删除短信"<<endl<<"3.清空收件箱"<<endl;
		cout<<"0.退出"<<endl;
		cin>>Choice;
		switch(Choice)
		{
			case 1:
				cout<<"请输入查看短信的序号:";
				cin>>Choice;
				if(Choice<1 && Choice > k)
				{
					cout<<"输入错误!"<<endl;
				}
				ReadOneMessage(head,Choice);//读出(查找)某条信息
				system("pause");
				break;
			case 2:
				cout<<"请输入要删除短信的序号:";
				cin>>Choice;
				if(Choice<1 && Choice >k)
				{
					cout<<"输入错误!"<<endl;
				}
				if(DeleteMessage(head,Choice))//删除收件箱中的某条信息
				{
					cout<<"删除后收件箱中的信息:"<<endl;
					ReadAllMessage(head);
				}
				system("pause");
				break;
			case 3:EmptyReciverFile(head);
				system("pause");
				break;
			case 0:break;
			default:
				cout<<"error!"<<endl;break;
		}
	}while(Choice);

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邪三一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值