设计一个电话号码管理程序 用C语言
要求:(1) 每条电话号码的信息有:编号,姓名,电话号码,地址,可以用一个结构变量来保存这些信息;
(2) 系统开始时没有任何信息,需要设计信息录入功能,录入后存入文件;
(3) 当用户要看某一电话号码信息时,要求他输入姓名,从文件中读出该电话号码信息,并以以下格式显示该电话号码信息,若找不到该电话号码,则显示“没有此记录”;若用户输入“all”,就显示所有信息
-----------------------------------------TELEPHONE BOOK-------------------------------
| num | name | phonenumber | address |
|----------------------|------------------|--------------------------|------------------------------|
|----------------------|------------------|--------------------------|------------------------------|
(5) 当用户要删除某一学生的信息时,要求他输入姓名,从文件中找出该电话号码,并从文件中将其信息删除。
答案:#include <stdio.h>
#include <conio.h>
#include <io.h>
#include <string.h>
typedef struct _PhoneNumber
{
int id;
char name[16];
char number[16];
char addr[32];
} PhoneNumber;
FILE* file;
PhoneNumber find;
PhoneNumber* Find(char* name)
{
fseek(file,0,SEEK_SET);
while(!feof(file))
{
if(1==fread(&find,sizeof(PhoneNumber),1,file))
{
if(!strcmp(find.name,name))
return &find;
}
}
return NULL;
}
void Print(const PhoneNumber* pn)
{
static char notCalled=1;
if(notCalled)
{
puts("| num | name | phonenumber | address |\n");
notCalled=0;
}
printf("%-15d %-15s %-15s %-15s\n",pn->id,pn->name,pn->number,pn->addr);
}
int Delete(char* name)
{
fseek(file,0,SEEK_SET);
while(!feof(file))
{
if(1==fread(&find,sizeof(PhoneNumber),1,file))
{
if(!strcmp(find.name,name))
{
while(!feof(file))
{
if(1==fread(&find,sizeof(find),1,file))
{
fseek(file,ftell(file)-2*sizeof(PhoneNumber),SEEK_SET);
fwrite(&find,sizeof(PhoneNumber),1,file);
fseek(file,ftell(file)+sizeof(PhoneNumber),SEEK_SET);
}
}
chsize(file->_file,filelength(file->_file)-sizeof(PhoneNumber));
return 1;
}
}
}
return 0;
}
int main()
{
PhoneNumber pn;
int input,id=0,i=0;
long pos;
file=fopen("PhoneNumberInfo.dat","a+b");
if(!file)
{
puts("Load data failed!");
return 1;
}
while(!feof(file))
if(1==fread(&pn,sizeof(PhoneNumber),1,file))
id++;
else
break;
puts("------------------------------TELEPHONE BOOK------------------------------");
puts("1.Add new record.\n2.Find record by name.\n3.Delete record by name.\n0.Exit.");
while(1)
{
scanf("%d",&input);
switch(input)
{
case 1:
pn.id=++id;
printf("Enter name,number,and address(Lilei 13800138000 Beijing):\n");
scanf("%s %s %s",pn.name,pn.number,pn.addr);
if(Find(pn.name))
{
printf("The name %s has Existed!",pn.name);
break;
}
pos=ftell(file);
if(1==fwrite(&pn,sizeof(PhoneNumber),1,file))
puts("New record successfully added.");
else
{
puts("Error adding new record!");
fflush(file);
fseek(file,pos,SEEK_SET);
break;
}
fflush(file);
break;
case 2:
printf("Enter the name:");
scanf("%s",&pn.name);
if(!stricmp("all",pn.name))
{
fseek(file,0,SEEK_SET);
while(!feof(file))
{
if(1==fread(&pn,sizeof(PhoneNumber),1,file))
Print(&pn);
}
}
else if(Find(pn.name))
Print(&find);
else
printf("The name %s does not exist.\n",pn.name);
break;
case 3:
printf("Enter the name:");
scanf("%s",pn.name);
if(Delete(pn.name))
printf("%s was deleted.\n",pn.name);
else
printf("The name %s does not exist.\n",pn.name);
break;
case 0:
break;
default:
fflush(stdin);
puts("Bad command!");
continue;
}
if(!input)
break;
}
fclose(file);
return 0;
}可能还有些问题,有的话,请通知我一下1445089182@qq.com
上一个:C语言问题:输入四个数,输出最大者
下一个:C语言程序设计实验报告怎么写啊