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"
#define receive "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;
int min;
int hour;
int mday;
int mon;
int year;
int wday;
}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){
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;
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;
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){
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;
}
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;
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);
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)
{
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){
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;
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);
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;
}
}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);
ShowsendMessage(L);
}
break;
case 3:
EmptySenderFile(L);
ShowsendMessage(L);
break;
case 0:
break;
default:
cout<<"输入错误!"<<endl;
}
}while(choice);
cout<<endl;
return 1;
}
ReceiveMessage.h
int ReadMessage(PNode &head)
{
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;
}
ReadMessage.close();
return 0;
}
int ReadAllMessage(PNode &head)
{
PNode q;
int i=0;
q=head;
while(q->next != head)
{
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)
{
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
{
cout<<"无该序号短信"<<endl;
return 0;
}
}
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);
}