#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 1
// OK代表操作成功
#define ERROR 0
// ERROR代表操作失败
///////////////////////////////////////
//
定义结构体TepletePhone用来存放数据
typedef struct TepletePhone
{
char id[12];
char sex[10];
char name[20];
char adress[20];
char PhoneNum[12];
char Email[20];
struct TepletePhone *next;
}LinkNode,*HeadNode;
//////////////////////////////////
//
将建立好的链表输入到文件中
void write(HeadNode head);
//////////////////////////////////
//
5统计总人数
size_t Cound(void);
//////////////////////////////////
//
2按学号进行删除操作
int Delete(HeadNode head,int num);
//////////////////////////////////
//
4用学号进行查询
int Found(const char *id);
//////////////////////////////////
//
初始化管理界面
void InitGraph(void);
//////////////////////////////////
//
进行选择处理
void Choose(void);
//////////////////////////////////
//
初始化通讯录
void InitNum(HeadNode *head);
/////////////////////////////////
//
1插入操作
int InsertNum(HeadNode head,int num,LinkNode node);
/////////////////////////////////
// 3 查看通讯录
void ShowNum(void);
/////////////////////////////////////
//
主函数
int main(void)
{
InitGraph();
Choose();
return 0;
}
//////////////////////////////////////
//
初始化链表(建立学生信息)
void InitNum(HeadNode *head)
{
*head = (HeadNode)malloc(sizeof(LinkNode));
if (*head == NULL)
{
printf("内存分配失败\n");
exit(EXIT_FAILURE);
}
else
(*head)->next = NULL;
}
/////////////////////////////////////
//
功能1.按位置插入
int InsertNum(HeadNode head,int num,LinkNode node)
{
int i = 0;
LinkNode *ptail,*pnew;
ptail = head;
if (num <= 0)
return ERROR;
while (i < num-1 && ptail != NULL)
{
ptail = ptail->next;
i++;
}
if (ptail == NULL)
return ERROR;
else
{
pnew = (LinkNode *)malloc(sizeof(LinkNode));
if (pnew == NULL)
{
printf("分配内存失败\n");
exit(EXIT_FAILURE);
}
*pnew = node;
pnew->next = ptail->next;
ptail->next = pnew;
return OK;
}
}
///////////////////////////////////////
//
功能3.显示
void ShowNum(void)
{
char show[100];
FILE *fp;
if ((fp = fopen("D:\\B1231.txt","r")) == NULL)
printf("文件打开失败\n");
while (fgets(show,99,fp) != NULL)
printf("%s",show);
printf("\n");
fclose(fp);
}
/////////////////////////////////////
//
初始化界面
void InitGraph(void)
{
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");
}
//////////////////////////////////////
//
用于选择,提供功能
void Choose(void)
{
int number,insert;
char num[20],num1[100];
LinkNode node;
while (1)
{
printf("请输入你需要进行的操作,以回车结束输入:");
scanf("%d",&number);
switch(number)
{
case 1:{
HeadNode head;
InitNum(&head);
FILE *fp;
if ((fp = fopen("D:\\B1231.txt","r")) == NULL)
printf("文件打开失败\n");
fgets(num1,99,fp);
for (number = 0; number < Cound(); number++)
{
fscanf(fp,"%s%s%s%s%s%s",node.id,
node.name,node.sex,node.adress,
node.PhoneNum,node.Email);
if (InsertNum(head,number+1,node) == ERROR)
exit(EXIT_FAILURE);
}
printf("请输入需要新建学生的信息\n");
printf("*请输入学号:");
scanf("%s",node.id);
printf("*请输入姓名:");
scanf("%s",node.name);
printf("*请输入性别:");
scanf("%s",node.sex);
printf("*请输入家庭住址:");
scanf("%s",node.adress);
printf("*请输入电话号码:");
scanf("%s",node.PhoneNum);
printf("*请输入E-mail:");
scanf("%s",node.Email);
printf("*请输入需要插入的位置:");
scanf("%d",&insert);
if (InsertNum(head,insert,node) == ERROR)
{
printf("插入操作失败\n");
exit(EXIT_FAILURE);
}
write(head);
}break;
case 2:{
FILE *fp;
HeadNode head;
InitNum(&head);
if ((fp = fopen("D:\\B1231.txt","r")) == NULL)
printf("文件打不开\n");
fgets(num1,99,fp);
for (number = 0; number < Cound(); number++)
{
fscanf(fp,"%s%s%s%s%s%s",node.id,
node.name,node.sex,node.adress,
node.PhoneNum,node.Email);
if (InsertNum(head,number+1,node) == ERROR)
exit(EXIT_FAILURE);
}
printf("请输入需要删除第几位学生的信息:");
scanf("%d",&insert);
if (Delete(head,insert) == ERROR)
{
printf("删除操作失败\n");
exit(EXIT_FAILURE);
}
write(head);
}break;
case 3: ShowNum(); break;
case 4:
{
printf("请输入需要查询的学号:");
scanf("%s",num);
if (Found(num) == ERROR)
printf("查询失败\n");
}break;
case 5:
printf("通讯录中有%u个学生的信息\n",Cound()); break;
case 6:{
HeadNode head;
InitNum(&head);
FILE *fp;
if ((fp = fopen("D:\\B1231.txt","r")) == NULL)
printf("文件打开失败\n");
fgets(num1,99,fp);
for (number = 0; number < Cound(); number++)
{
fscanf(fp,"%s%s%s%s%s%s",node.id,
node.name,node.sex,node.adress,
node.PhoneNum,node.Email);
if (InsertNum(head,number+1,node) == ERROR)
exit(EXIT_FAILURE);
}
printf("请输入需要更改的学生号:");
char item[20],change[20];
scanf("%s",item);
LinkNode *p = head->next;
for (number = 0; number < Cound(); number++)
if (p != NULL && strcmp(item,p->id) == 0)
break;
else
p = p->next;
printf("请输入需要更改的数据项:");
scanf("%s",item);
printf("请输入新的数据项:");
scanf("%s",change);
int num;
if (strcmp(item,"姓名") == 0)
strcpy(p->name,change);
if (strcmp(item,"学号") == 0)
strcpy(p->id,change);
if (strcmp(item,"性别") == 0)
strcpy(p->sex,change);
if (strcmp(item,"家庭住址") == 0)
strcpy(p->adress,change);
if (strcmp(item,"电话号码") == 0)
strcpy(p->PhoneNum,change);
if (strcmp(item,"E-mail") == 0)
strcpy(p->Email,change);
write(head);
}break;
case 7: return; break;
default: printf("操作失败,"
"请输入正确的操作\n"); break;
}
}
}
//////////////////////////////////////
//
功能4.按学号查询
int Found(const char *id)
{
FILE *fp;
if ((fp = fopen("D:\\B1231.txt","r")) == NULL)
{
printf("文件打开失败\n");
return ERROR;
}
char test[20],found[100];
int flag = 0;
size_t count = 0;
while (fscanf(fp,"%s",test) != EOF)
{
if(strcmp(test,id) == 0)
{
fgets(found,99,fp);
flag = 1;
printf("%s",id);
printf("%s",found);
if (count == Cound()*6)
printf("\n");
//
为了最后一行有换行
break;
}
count++;
}
if (flag == 0)
return ERROR;
else
return OK;
fclose(fp);
}
///////////////////////////////////
//
统计总人数
size_t Cound(void)
{
FILE *fp;
char num[100];
if ((fp = fopen("D:\\B1231.txt","r")) == NULL)
printf("打开文件失败\n");
size_t count = -1;
while (fgets(num,99,fp) != NULL)
count++;
fclose(fp);
return count;
}
///////////////////////////////////////
//
将链表写入文件函数
void write(HeadNode head)
{
FILE *fp;
LinkNode *p = head->next;
fp = fopen("D:\\B1231.txt","w");
fprintf(fp,"%s\t%s\t%s\t%s\t%s\t%s\n",
"学号","姓名","性别","家庭住址",
"电话号码","E-mail");
while (p != NULL)
{
fprintf(fp,"%s\t%s\t%s\t%s\t\t%s\t%s\t",
p->id,p->name,p->sex,p->adress,
p->PhoneNum,p->Email);
if (p->next != NULL)
//
为了最后一行少一个换行
fprintf(fp,"%s","\n");
p = p->next;
}
fclose(fp);
}
//////////////////////////////////////
//
删除节点(删除学生信息)
int Delete(HeadNode head,int num)
{
int i = 0;
LinkNode *pnext,*now;
pnext = head;
while (pnext->next != NULL && i < num-1)
{
pnext = pnext->next;
i++;
}
if (pnext->next == NULL)
return ERROR;
now = pnext->next;
pnext->next = now->next;
free(now);
return OK;
}
你自己做一个文件,改一下