简 介:
本次课程设计主要是模拟手机短信功能,通过文件操作,链表操作实现对信息的管理功能。
源码:
#include<stdio.h>
#include<stdlib.h>
typedef struct _TIME_
{
int month;
int day;
int year;
}TIME;
typedef struct _MESSAGE_
{
char name[20];
TIME time;
char message[140];
struct _MESSAGE_ * next;
}MESSAGE;
MESSAGE * head_recive;
MESSAGE * head_send;
MESSAGE * head_draft;
FILE * fp_recive;
FILE * fp_send;
FILE * fp_draft;
/******功能函数声明*******/
#if 1
//将新信息加入链表
MESSAGE * add_list(MESSAGE * head);
//打印链表中各结点的信息
MESSAGE * print_list(MESSAGE * head);
//将链表写入文件
int write_file(MESSAGE * head,FILE * fp);
//删除链表中的某个结点
MESSAGE * delete_list(MESSAGE * head);
//查找链表中的某个结点
MESSAGE * search_list(MESSAGE * head);
//修改链表中的某个结点
MESSAGE * chang_list(MESSAGE * head);
//清空链表
MESSAGE * distory_list(MESSAGE * head);
//读取文件中的数据形成链表
MESSAGE * read_file_2_list(MESSAGE * head,FILE * fp);
//显示文件中的数据
int display_file(FILE * fp);
#endif
/******菜单函数声明*******/
#if 1
void menu_download();
void menu_homepage(MESSAGE * head_recive,MESSAGE * head_send,MESSAGE * head_draft,FILE * fp_recive,FILE * fp_send,FILE * fp_draft);
void menu_recivebox(MESSAGE * head_recive,MESSAGE * head_send,MESSAGE * head_draft,FILE * fp_recive,FILE * fp_send,FILE * fp_draft);
void menu_sendbox(MESSAGE * head_recive,MESSAGE * head_send,MESSAGE * head_draft,FILE * fp_recive,FILE * fp_send,FILE * fp_draft);
void menu_draftbox(MESSAGE * head_recive,MESSAGE * head_send,MESSAGE * head_draft,FILE * fp_recive,FILE * fp_send,FILE * fp_draft);
#endif
int main()
{
//头结点申请空间并给他的next赋空
head_recive=(MESSAGE *)malloc(sizeof(MESSAGE));
head_send=(MESSAGE *)malloc(sizeof(MESSAGE));
head_draft=(MESSAGE *)malloc(sizeof(MESSAGE));
head_recive->next=NULL;
head_send->next=NULL;
head_draft->next=NULL;
//函数调用
menu_download();
menu_homepage(head_recive,head_send,head_draft,fp_recive,fp_send,fp_draft);
return 1;
}
//将新信息加入链表
MESSAGE * add_list(MESSAGE * head)
{
MESSAGE * msg;
msg=(MESSAGE *)malloc(sizeof(MESSAGE));
//msg=head;
if(NULL==head || NULL==msg)
{
return NULL;
}
printf("获取新信息:\n");
printf("姓名(号码):\n");
scanf("%s",msg->name);
printf("日期(月 日 年):\n");
scanf("%d%d%d",&(msg->time).month,&(msg->time).day,&(msg->time).year);
printf("信息内容:\n");
scanf("%s",msg->message);
//msg->next=NULL;
msg->next=head->next;
head->next=msg;
return head;
}
//打印链表中各结点的信息
MESSAGE * print_list(MESSAGE * head)
{
MESSAGE * msg;
msg=(MESSAGE *)malloc(sizeof(MESSAGE));
//判断链表是否为空
if(NULL==head||msg==NULL)
{
printf("链表为空!你所要打印的信息不存在!\n");
return head;
}
else
{
msg=head->next;
//循环打印链表中各结点的信息
while(msg!=NULL)
{
printf("%s %d-%d-%d %s\n",msg->name,(msg->time).month,(msg->time).day,(msg->time).year,msg->message);
//指针指向下一个结点
msg=msg->next;
}
}
return head;
}
//删除链表中的某个结点
MESSAGE * delete_list(MESSAGE * head)
{
char name[20];
printf("请输入你所要删除信息的name:\n");
scanf("%s",name);
MESSAGE * msg;
MESSAGE * p;
msg=head;
//判断链表是否为空
if(NULL==head)
{
printf("链表为空,你所要删除的信息不存在!\n");
return head;
}
else
{
//如果找到相应节点,或者已经遍历到尾节点就跳出循环
while(strcmp(name,msg->name)!=0 && msg->next!=NULL)
{
p=msg;
msg=msg->next;
}
//判断是否找到相应节点
if(strcmp(name,msg->name)==0)
{
if(head==msg)
head=msg->next;
else
p->next=msg->next;
printf("你所要删除的信息已删除!\n");
}
else
{
printf("链表中没有你所要删除的信息!\n");
}
}
return head;
}
//查找链表中的某个结点
MESSAGE * search_list(MESSAGE * head)
{
char name[20];
printf("请输入你所要查找信息的name:\n");
scanf("%s",name);
MESSAGE * msg;
MESSAGE * p;
msg=head;
//判断链表是否为空
if(NULL==head)
{
printf("你所要查找的信息不存在!\n");
return head;
}
//如果找到相应节点,或者已经遍历到尾节点就跳出循环
while(strcmp(name,msg->name)!=0 && msg->next!=NULL)
{
p=msg;
msg=msg->next;
}
//判断是否找到相应节点
if(strcmp(name,msg->name)==0)
{
printf("%s %d-%d-%d %s\n",msg->name,(msg->time).month,(msg->time).day,(msg->time).year,msg->message);
}
else
{
printf("你所要查找的信息不存在!\n");
}
return head;
}
//将链表写入文件
int write_file(MESSAGE * head,FILE * fp)
{
MESSAGE * msg;
//判断链表是否为空
if(NULL==head)
{
printf("链表为空!你所要写入文件的信息不存在!\n");
return 0;
}
//判断文件是否打开
if(NULL==fp)
{
return 0;
}
msg=head->next;
//循环把链表中各结点的信息写入文件
while(msg!=NULL)
{
fwrite(msg,sizeof(MESSAGE)-4,1,fp);
//指针指向下一个结点
msg=msg->next;
}
return 1;
}
//修改链表中的某个结点
MESSAGE * chang_list(MESSAGE * head)
{
char name[20];
printf("请输入你所要修改信息的name:\n");
scanf("%s",name);
MESSAGE * msg;
MESSAGE * p;
msg=head;
//判断链表是否为空
if(NULL==head)
{
printf("链表为空,你所要修改的信息不存在!\n");
return head;
}
//如果找到相应节点,或者已经遍历到尾节点就跳出循环
while(strcmp(name,msg->name)!=0 && msg->next!=NULL)
{
p=msg;
msg=msg->next;
}
//判断是否找到相应节点
if(strcmp(name,msg->name)==0)
{
//p=msg->next;
printf("获取修改信息:\n");
printf("姓名(号码):\n");
scanf("%s",msg->name);
printf("日期(月 日 年):\n");
scanf("%d%d%d",&(msg->time).month,&(msg->time).day,&(msg->time).year);
printf("信息内容:\n");
scanf("%s",msg->message);
msg=msg->next;
//printf("%s %d-%d-%d %s\n",msg->name,(msg->time).month,(msg->time).day,(msg->time).year,msg->message);
}
else
{
printf("你所要修改的信息不存在!\n");
}
return head;
}
//清空链表
MESSAGE * distory_list(MESSAGE * head)
{
MESSAGE * msg;
//MESSAGE * p;
msg=head;
//判断链表是否为空
if(NULL==head || msg->next==NULL)
{
printf("链表为空,无需销毁!\n");
return head;
}
else
{
while(msg->next!=NULL)
{
head=msg->next;
free(msg);
msg=head;
}
}
if(msg->next==NULL)
{
printf("链表销毁成功!\n");
head=NULL;
}
return head;
}
//读取文件中的数据形成链表
MESSAGE * read_file_2_list(MESSAGE * head,FILE * fp)
{
if(fp==NULL)
{
printf("文件为空!\n");
return NULL;
}
MESSAGE * msg;
MESSAGE * p;
msg=(MESSAGE *)malloc(sizeof(MESSAGE));
if(msg==NULL)
{
printf("申请空间失败!\n");
}
fread(msg,sizeof(MESSAGE)-4,1,fp);
msg->next=NULL;
while(!feof(fp))
{
p=head->next;
head->next=msg;
msg->next=p;
fread(msg,sizeof(MESSAGE)-4,1,fp);
msg->next=NULL;
}
return head;
}
//显示文件中的数据
int display_file(FILE * fp)
{
if(fp==NULL)
{
printf("文件为空!\n");
return 0;
}
MESSAGE * msg;
msg=(MESSAGE *)malloc(sizeof(MESSAGE));
if(msg==NULL)
{
printf("申请空间失败!\n");
return 0;
}
fread(msg,sizeof(MESSAGE)-4,1,fp);
if(feof(fp))
printf("文件为空!\n");
while(!feof(fp))
{
printf("%s %d-%d-%d %s\n",msg->name,(msg->time).month,(msg->time).day,(msg->time).year,msg->message);
fread(msg,sizeof(MESSAGE)-4,1,fp);
}
return 1;
}
//初始化界面
/*
^^^^^^^^^^^^^^^^^^^^^^^^^
正在初始化…
^^^^^^^^^^^^^^^^^^^^^^^^^
*/
void menu_download()
{
system("clear");
printf("^^^^^^^^^^^^^^^^^^^^^^^^^\n正在初始化…\n^^^^^^^^^^^^^^^^^^^^^^^^^\n");
sleep(3);
}
//主页
/*
*********主 页***********
^^^^^^^^^^^^^^^^^^^^^^^^^
1. 收件箱
2. 发件箱
3. 草稿箱
0. 退 出
^^^^^^^^^^^^^^^^^^^^^^^^^
*/
void menu_homepage(MESSAGE * head_recive,MESSAGE * head_send,MESSAGE * head_draft,FILE * fp_recive,FILE * fp_send,FILE * fp_draft)
{
system("clear");
printf("*********主 页***********\n");
printf("^^^^^^^^^^^^^^^^^^^^^^^^^\n");
printf(" 1. 收件箱\n");
printf(" 2. 发件箱\n");
printf(" 3. 草稿箱\n");
printf(" 0. 退 出\n");
printf("^^^^^^^^^^^^^^^^^^^^^^^^^\n");
int num;
printf("请输入你的选择:\n");
while(scanf("%d",&num)!=0)
{
switch(num)
{
case 1:menu_recivebox(head_recive,head_send,head_draft,fp_recive,fp_send,fp_draft);break;
case 2:menu_sendbox(head_recive,head_send,head_draft,fp_recive,fp_send,fp_draft);break;
case 3:menu_draftbox(head_recive,head_send,head_draft,fp_recive,fp_send,fp_draft);break;
case 0:exit(0);break;
}
printf("请输入你的选择:\n");
}
}
//收件箱
/*
*********收件箱**********
^^^^^^^^^^^^^^^^^^^^^^^^^
1. 新信息
2. 查找信息
3. 删除信息
4. 全部删除
5. 显示所有信息
6. 返回上一层
^^^^^^^^^^^^^^^^^^^^^^^^^
*/
void menu_recivebox(MESSAGE * head_recive,MESSAGE * head_send,MESSAGE * head_draft,FILE * fp_recive,FILE * fp_send,FILE * fp_draft)
{
system("clear");
printf("*********收件箱**********\n");
printf("^^^^^^^^^^^^^^^^^^^^^^^^^\n");
printf(" 1. 收到新信息\n");
printf(" 2. 查找信息\n");
printf(" 3. 删除信息\n");
printf(" 4. 全部删除\n");
printf(" 5. 显示所有信息\n");
printf(" 6. 返回上一层\n");
printf("^^^^^^^^^^^^^^^^^^^^^^^^^\n");
int num;
printf("请输入你的选择:\n");
while(scanf("%d",&num)!=0)
{
switch(num)
{
case 1:fp_recive=fopen("recive.txt","r+");
//read_file_2_list(head_recive,fp_recive);
add_list(head_recive);
write_file(head_recive,fp_recive);
fclose(fp_recive);
break;
case 2:fp_recive=fopen("recive.txt","r");
read_file_2_list(head_recive,fp_recive);
search_list(head_recive);
fclose(fp_recive);
break;
case 3:fp_recive=fopen("recive.txt","w+");
read_file_2_list(head_recive,fp_recive);
delete_list(head_recive);
write_file(head_recive,fp_recive);
fclose(fp_recive);
break;
case 4:fp_recive=fopen("recive.txt","w+");
//read_file_2_list(head_recive,fp_recive);
distory_list(head_recive);
// write_file(head_recive,fp_recive);
fclose(fp_recive);
break;
case 5:fp_recive=fopen("recive.txt","r");
/*read_file_2_list(head_recive,fp_recive);
print_list(head_recive);*/
display_file(fp_recive);
fclose(fp_recive);
break;
case 6:menu_homepage(head_recive,head_send,head_draft,fp_recive,fp_send,fp_draft);break;
}
printf("请输入你的选择:\n");
}
}
//发件箱
/*
*********发件箱**********
^^^^^^^^^^^^^^^^^^^^^^^^^
1. 编辑新信息
2. 未发送(保存到草稿箱)
3. 查找已发送信息
4. 删除信息
5. 全部删除
6. 显示所有信息
7. 返回上一层
^^^^^^^^^^^^^^^^^^^^^^^^^
*/
void menu_sendbox(MESSAGE * head_recive,MESSAGE * head_send,MESSAGE * head_draft,FILE * fp_recive,FILE * fp_send,FILE * fp_draft)
{
system("clear");
printf("*********发件箱**********\n");
printf("^^^^^^^^^^^^^^^^^^^^^^^^^\n");
printf(" 1. 发送新信息\n");
printf(" 2. 未发送(保存到草稿箱)\n");
printf(" 3. 查找已发送信息\n");
printf(" 4. 删除信息\n");
printf(" 5. 全部删除\n");
printf(" 6. 显示所有信息\n");
printf(" 7. 返回上一层\n");
printf("^^^^^^^^^^^^^^^^^^^^^^^^^\n");
int num;
printf("请输入你的选择:\n");
while(scanf("%d",&num)!=0)
{
switch(num)
{
case 1:fp_send=fopen("send.txt","r+");
// read_file_2_list(head_send,fp_send);
add_list(head_send);
write_file(head_send,fp_send);
fclose(fp_send);
break;
case 2:fp_draft=fopen("draft.txt","r+");
add_list(head_draft);
write_file(head_draft,fp_draft);
fclose(fp_draft);
break;
case 3:fp_send=fopen("send.txt","r+");
read_file_2_list(head_send,fp_send);
search_list(head_send);
fclose(fp_send);
break;
case 4:fp_send=fopen("send.txt","w+");
read_file_2_list(head_send,fp_send);
delete_list(head_send);
write_file(head_send,fp_send);
fclose(fp_send);
break;
case 5:fp_send=fopen("send.txt","w+");
//read_file_2_list(head_send,fp_send);
distory_list(head_send);
//write_file(head_send,fp_send);
fclose(fp_send);
break;
case 6:fp_send=fopen("send.txt","r+");
display_file(fp_send);
fclose(fp_send);
break;
case 7:menu_homepage(head_recive,head_send,head_draft,fp_recive,fp_send,fp_draft);break;
}
printf("请输入你的选择:\n");
}
}
//草稿箱
/*
*********草稿箱**********
^^^^^^^^^^^^^^^^^^^^^^^^^
1. 修改信息
2. 发送(保存到发件箱)
3. 查找未发送信息
4. 删除信息
5. 全部删除
6. 显示所有信息
7. 返回上一层
^^^^^^^^^^^^^^^^^^^^^^^^^
*/
void menu_draftbox(MESSAGE * head_recive,MESSAGE * head_send,MESSAGE * head_draft,FILE * fp_recive,FILE * fp_send,FILE * fp_draft)
{
system("clear");
printf("*********草稿箱**********\n");
printf("^^^^^^^^^^^^^^^^^^^^^^^^^\n");
printf(" 1. 修改信息\n");
printf(" 2. 发送(保存到发件箱)\n");
printf(" 3. 查找未发送信息\n");
printf(" 4. 删除信息\n");
printf(" 5. 全部删除\n");
printf(" 6. 显示所有信息\n");
printf(" 7. 返回上一层\n");
printf("^^^^^^^^^^^^^^^^^^^^^^^^^\n");
int num;
printf("请输入你的选择:\n");
while(scanf("%d",&num)!=0)
{
switch(num)
{
case 1:fp_draft=fopen("draft.txt","r+");
//read_file_2_list(head_draft,fp_draft);
chang_list(head_draft);
write_file(head_draft,fp_draft);
fclose(fp_draft);
break;
case 2:fp_send=fopen("send.txt","r+");
fp_draft=fopen("draft.txt","r+");
read_file_2_list(head_draft,fp_draft);
search_list(head_draft);
//add_list(head_send);
write_file(head_send,fp_send);
fclose(fp_draft);
fclose(fp_send);
break;
case 3:fp_draft=fopen("draft.txt","r+");
read_file_2_list(head_draft,fp_draft);
search_list(head_draft);
fclose(fp_draft);
break;
case 4:fp_draft=fopen("draft.txt","w+");
read_file_2_list(head_draft,fp_draft);
delete_list(head_draft);
write_file(head_draft,fp_draft);
fclose(fp_draft);
break;
case 5:fp_draft=fopen("draft.txt","w+");
//read_file_2_list(head_draft,fp_draft);
distory_list(head_draft);
//write_file(head_draft,fp_draft);
fclose(fp_draft);
break;
case 6:fp_draft=fopen("draft.txt","r+");
display_file(fp_draft);
fclose(fp_draft);
break;
case 7:menu_homepage(head_recive,head_send,head_draft,fp_recive,fp_send,fp_draft);
break;
}
printf("请输入你的选择:\n");
}
}