#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef int bool;
#define true 1
#define false 0
typedef struct student
{
char name[20];
char phone[20];
bool flag;
struct student *next;
}student;
student* Add(student* phead);
student* Find(student *phead);
void Search(student* phead);
void Delete(student *phead);
void Modify(student* phead);
student* Read(student* phead);
void Write(student* phead);
void Show(student *phead);
void Scheclle()
{
char command[20];
student* head = NULL;
printf("===========================================================================\n");
printf("版本号:v1.0\n");
printf("开发者:唐正跃\n");
printf("\n\n\n");
printf("命令名\t功能\n");
printf("add\t增加一条记录\n");
printf("show\t显示所有记录\n");
printf("find\t查找一条记录\n");
printf("help\t显示帮助\n");
printf("delete\t删除一条记录\n");
printf("update\t更新一条记录\n");
printf("export\t数据导出\n");
printf("exit\t应用程序退出\n");
printf("===========================================================================\n");
head = Read(head);
while(true)
{
printf("\n请输入操作命令:>");
scanf("%s", command);
if(strcmp(command, "add") == 0)
{
head = Add(head);
}
else if(strcmp(command, "show") == 0)
{
Show(head);
}
else if(strcmp(command, "update") == 0)
{
Modify(head);
}
else if(strcmp(command, "delete") == 0)
{
Delete(head);
}
else if(strcmp(command, "find") == 0)
{
Search(head);
}
else if(strcmp(command, "exit") == 0)
{
Write(head);
exit(0);
}
else
{
printf("对不起,您输入了错误的指令!\n");
}
}
}
student* Add(student* phead)
{
student* pNode;
pNode = (student*)malloc(sizeof(student));
printf("请输入姓名:");
scanf("%s", pNode->name);
printf("请输入电话号码:");
scanf("%s", pNode->phone);
printf("记录 姓名:%s, 电话号码:%s, 将要添加到数据库中\n", pNode->name, pNode->phone);
pNode->next = NULL;
pNode->flag = true;
if(phead == NULL)
{
phead = pNode;
}
else
{
student* p;
p = phead;
while(p->next)
{
p = p->next;
}
p->next = pNode;
}
return phead;
}
student* Find(student *phead)
{
char name[20];
student *p = NULL;
printf("请输入要查找的姓名:");
scanf("%s", name);
p = phead;
while(p!=NULL)
{
if(strcmp(p->name, name) == 0)
{
return p;
}
else
{
p = p->next;
}
}
return NULL;
}
void Search(student* phead)
{
student* p = Find(phead);
if(p == NULL)
{
printf("查无记录!\n");
}
else
{
printf("姓名:%s\t|电话号码:%s\n", p->name, p->phone);
}
}
void Delete(student *phead)
{
char command[20];
student* p = Find(phead);
if(p!=NULL)
{
printf("确认要删除\"姓名:%s|电话:%s\"的记录吗?(yes/no):", p->name, p->phone);
scanf("%s", command);
if(strcmp(command, "yes") == 0)
{
p->flag = false;
printf("记录删除完毕!\n");
}
}
else
{
printf("查无记录!\n");
}
}
void Modify(student* phead)
{
char name[20];
char phone[20];
student* p = Find(phead);
if(p != NULL)
{
printf("姓名:%s\t|电话号码:%s\n", p->name, p->phone);
printf("请输入新的姓名:");
scanf("%s", name);
printf("请输入新的电话号码:");
scanf("%s", phone);
strcpy(p->name, name);
strcpy(p->phone, phone);
printf("修改记录成功!\n");
}
else
{
printf("查无记录!\n");
}
}
student* Read(student* phead)
{
FILE* fp = fopen("data.dat", "rb");
student* pNode = NULL;
student* p = phead;
if(fp == NULL)
{
printf("打开文件失败!\n");
return NULL;
}
pNode = (student*)malloc(sizeof(student));
while(fread(pNode, sizeof(student), 1, fp)>0)
{
pNode->next = NULL;
if(phead == NULL)
{
phead = pNode;
p = phead;
}
else
{
p->next = pNode;
p = p->next;
}
pNode = (student*)malloc(sizeof(student));
}
fclose(fp);
return phead;
}
void Write(student* phead)
{
FILE *fp;
student *p;
remove("data.dat");
fp = fopen("data.dat", "ab");
if(fp == NULL)
{
printf("生成文件出错, 无法执行保存操作!\n");
return;
}
p = phead;
while(p!=NULL)
{
if(p->flag)
{
fwrite(p, sizeof(student), 1, fp);
}
p = p->next;
}
fclose(fp);
}
void Show(student *phead)
{
student* p = phead;
if(phead == NULL)
{
printf("没有数据可以显示!\n");
return;
}
while(p != NULL)
{
if(p->flag)
{
printf("姓名:%s\t|电话:%s\n", p->name, p->phone);
}
p = p->next;
}
}
int main()
{
Scheclle();
return 0;
}