回复 4楼 wp231957
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
char name[20];
char telephone[15];
char address[50];
char note[20];
struct node *next;
}book;
char choose;
int initiate(book **h);
void finput(book *h);
void foutput(book *h);
int main() /*操作界面*/
{
book *h;
initiate(&h);
while(1)
{
printf("\n");
printf("***************************************\n");
printf("*
欢迎使用通讯录管理系统
*\n");
printf("*
您有以下选择:
*\n");
printf("*
1--初始化
*\n");
printf("*
2--输入
*\n");
printf("*
5--输出
*\n");
printf("*
6--退出系统
*\n");
printf("***************************************\n");
printf("请输入您的选择:");
scanf("%c",&choose);
printf("\n");
switch (choose)
{
case '1':initiate(&h);break;
case '2':finput(h);break;
case '5':foutput(h);break;
case '6':return 0;
default:printf("输入错误,请重新选择!\n");
}
}
}
int initiate(book **h) /*初始化*/
{
*h=(book*)malloc(sizeof(book));
if (*h==NULL)
{
printf("初始化失败!\n");
return 0;
}
(*h)->next=NULL;
printf("完成初始化!\n");
return 1;
}
void finput(book *h) /*文件读取数据*/
{
book *p,*q;
FILE *fp;
char infile[40];
printf("输入文件名:");
gets(infile);
if((fp=fopen(infile,"r"))==NULL)
{
printf("无法打开文件%s\n",infile);
exit(1);
}
q=h;
while(!feof(fp))
{
p=q;
fgets(p->name,21,fp);
fgets(p->telephone,16,fp);
fgets(p->address,51,fp);
fgets(p->note,21,fp);
if ((q=(book*)malloc(sizeof(book)))==NULL)
{
printf("新结点空间申请失败!\n");
exit(0);
}
p->next=q;
}
p->next=NULL;
fclose(fp);
}
void foutput(book *h) /*通讯录文件整体输出*/
{
FILE *fp;
char outfile[40];
book *p;
int i=0;
p=h;
printf("输出文件名:");
gets(outfile);
if((fp=fopen(outfile,"w"))==NULL)
{
printf("无法打开文件%s\n",outfile);
exit(1);
}
while (p!=NULL)
{
i++;
fprintf(fp,"\n");
fprintf(fp,"当前通讯录:\n");
fprintf(fp,"\n");
fprintf(fp,"联系人%d:\n",i);
fprintf(fp,"姓名:%s\n",p->name);
fprintf(fp,"电话号码:%s\n",p->telephone);
fprintf(fp,"地址:%s\n",p->address);
fprintf(fp,"备注:%s\n",p->note);
fprintf(fp,"\n");
p=p->next;
}
fclose(fp);
}