电话号码存储系统(包括姓名/地址)

这是一个简单的电话簿管理系统,使用C语言编写,包含添加、修改、删除、查询等功能。系统通过文件读写来保存数据,并能按姓名排序显示所有联系人。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 /*------------------------------------------------------------------------  
    
    Author:                 AaronBai
    Project:                TelephoneSystem
    State:  
    Creation   Date:         2006-11.-2
    Description:               
  ------------------------------------------------------------------------*/  

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <conio.h>
#include<curses.h>


void addsubs();//ADD NEW SUBSCRIBE
void modsubs();//MODIFY SUBSCRIBE
void delsubs();//DELETE SUBSCRIBE
void demand_client();//QUERY SUBSCRIBE
void viewdir();//VIEW ALL SUBSCRIBE
void queryname();//QUERY BY NAME
void querynum();//QUERY BY TELNUMER



struct subscriber
{
char name[20];
char address[30];
long int telno;
};

int  main()
{



int choice=0;
while(choice!=7)
{
/* Display menu */

printf("/nTELEPHONE DIRECTORY SYSTEM");
printf("/n/n1. Add new subscriber details");
printf("/n2. Modify existing subscriber details");
printf("/n3. Delete existing subscriber details");
printf("/n4. Display subscriber details based on telephone number");
printf("/n5. Display subscriber details based on subscriber name");
printf("/n6. View Directory");
printf("/n7. Quit");
printf("/n/nEnter choice: ");

/* Accept menu choice */

scanf("%d", &choice);

/* Invoke appropriate function based on menu choice */

if(choice==1)
addsubs();
else if(choice==2)
modsubs();
else if(choice==3)
delsubs();
else if(choice==4)
querynum();
else if(choice==5)
queryname();
else if(choice==6)
viewdir();
else if(choice==7)
exit(0);
else
{ printf("/n/nYou have entered an invalid menu choice. Please reenter/n.");
getchar();
}

}

}


//ADD NEW SUBSCRIBE
void addsubs()
{
struct subscriber info;
char reply='y';
char save='y';
FILE *fp;
fp=fopen("telefon.dat","a+");
while (reply=='y')
{
system("cls");
printf("/nEnter Subscriber name:");
scanf("%s",info.name);
printf("/nEnter Subscriber address:");
scanf("%s",info.address);
printf("/nEnter Subscriber  telephonum:");
scanf("%ld",&info.telno);
while(info.telno<4000000||info.telno>5000000)
{printf("The telephonum must between 4000000 and 4999999!/n");
 printf("/nEnter Subscriber  telephonum:");
scanf("%ld",&info.telno);}

if (fp!=NULL)
{
fwrite(&info,sizeof(struct subscriber),1,fp);
}
else
{
printf("/nFile open error,/n");
getchar();
return;
}
fclose(fp);




printf("/nContinued or not?(y/n):");
scanf(" %c",&reply);
}

getchar();
}



//MODIFY SUBSCRIBE
void modsubs()
{
struct subscriber info;
FILE *fp;
char amend_name[20];
char reply='y';
char found='y';
char save='y';
int size=sizeof(struct subscriber);
while (reply=='y')
{
found='n';
fp=fopen("telefon.dat","r+w");
if (fp!=NULL)
{
system("cls");
printf("/nPlease input the name you want to modify:");
scanf("%s",amend_name);
while ((fread(&info,size,1,fp))==1)
{
if ((strcmp(amend_name,info.name))==0)
{
found='y';
break;
}
}
if (found=='y')
{

printf("/n==========================================/n");
printf("/nname:%s/n",info.name);
    
printf("/naddress:%s/n",info.address);
printf("/ntelephonum:%s/n",info.telno);
printf("/n==========================================/n");
printf("/nModify the record:/n");
printf("/nname:");
scanf("%s",info.name);
printf("/n家庭住址:");
scanf("%s",info.address);
printf("/ntelephonum:");
scanf("%ld",&info.telno);
printf("/nSave or not?(y/n):");
scanf(" %c",&save);
if (save=='y')
{
fseek(fp,-size,1);
fwrite(&info,sizeof(struct subscriber),1,fp);
}
}
else
{
printf("/nNO SUCH INFO!/n");
}
}
else
{
printf("/nFile open error,/n");
getchar();
return;
}
fclose(fp);
printf("/nContinued or not?(y/n):");
scanf(" %c",&reply);
}

getchar();
}

//DELETE SUBSCRIBE
void delsubs()
{
struct subscriber info[100];
struct subscriber temp_str;
struct subscriber delete_str;
int i=0,j=0;
char reply='y';
char found='y';
char confirm='y';
char delete_name[20];
FILE *fp;
while (reply=='y')
{
system("cls");
fp=fopen("telefon.dat","r");
if (fp!=NULL)
{
i=0;
found='n';
printf("/nPlease input name:");
scanf("%s",delete_name);
while ((fread(&temp_str,sizeof(struct subscriber),1,fp))==1)
{
if ((strcmp(delete_name,temp_str.name))==0)
{
found='y';
delete_str=temp_str;
}//QUERY THE RECORD TO BE DELETED
else
{
info[i]=temp_str;
i++;
}//STORE OTHER RECORD
}
}
else
{
printf("/nFile open error,/n");
getchar();
return;
}
fclose(fp);
if (found=='y')
{
printf("/n==========================================/n");
printf("/nname:%s/n",delete_str.name);
printf("/naddress:%s/n",delete_str.address);
printf("/ntelephonum:%ld/n",delete_str.telno);
printf("/n==========================================/n");
}
else
{
printf("/nNO SUCH INFO,/n");
getchar();
break;
}
printf("/nDelete or not?(y/n):");
scanf(" %c",&confirm);
if (confirm=='y')
{
fp=fopen("telefon.dat","w");
if (fp!=NULL)
{
for(j=0;j<i;j++)
{
fwrite(&info[j],sizeof(struct subscriber),1,fp);
}
printf("/nRecord  Delete !/n");
}
else
{
printf("/nFile open error,/n");
getchar();
return;
}
fclose(fp);
}
printf("/nContinued or not?(y/n):");
scanf(" %c",&reply);
}

getchar();
}




//QUERY BY NAME
void queryname()
{
struct subscriber info;
FILE *fp;
char amend_name[20];
char reply='y';
char found='y';
while (reply=='y')
{
found='n';
fp=fopen("telefon.dat","r+w");
if (fp!=NULL)
{
system("cls");
printf("/nPlease input name:");
scanf("%s",amend_name);
while ((fread(&info,sizeof(struct subscriber),1,fp))==1)
{
if ((strcmp(amend_name,info.name))==0)
{
found='y';
break;
}
}
if (found=='y')
{

printf("/n==========================================/n");
printf("/nname:%s/n",info.name);
printf("/naddress:%s/n",info.address);
printf("/ntelephonum:%ld/n",info.telno);
printf("/n==========================================/n");
}
else
{
printf("/nNO SUCH INFO!/n");
}
}
else
{
printf("/nFile open error,/n");
getchar();
return;
}
fclose(fp);
printf("/nContinued or not?(y/n):");
scanf(" %c",&reply);
}

getchar();
}

//QUERY BY TELNUMER
void querynum()
{
struct subscriber info;
FILE *fp;
long int telephone;
char reply='y';
char found='y';
while (reply=='y')
{
found='n';
fp=fopen("telefon.dat","r+w");
if (fp!=NULL)
{
system("cls");
printf("/nPlease input 你的telephonum:");
scanf("%ld",telephone);
while ((fread(&info,sizeof(struct subscriber),1,fp))==1)
{
//if ((strcmp(telephone,info.telno))==0)
if(telephone-info.telno==0)
{
found='y';
break;
}
}
if (found=='y')
{

printf("/n==========================================/n");
printf("/nname:%s/n",info.name);
printf("/naddress:%s/n",info.address);
printf("/ntelephonum:%ld/n",info.telno);
printf("/n==========================================/n");
}
else
{
printf("/nNO SUCH TELENUMER RECORD!/n");
}
}
else
{
printf("/nFile open error,/n");
getchar();
return;
}
fclose(fp);
printf("/nContinued or not?(y/n):");
scanf(" %c",&reply);
}

getchar();
}




//SORR BY SUBSCRIBE NAME
void sort()
{
FILE *fp;

struct subscriber info,temp[100],a;

int i,j,k;

fp=fopen("telefon.dat","r");

for (i=0;(fread(&info,sizeof(struct subscriber),1,fp))==1;i++)
{
temp[i]=info;
}
for (k=0;k<i-1;k++)
{
for (j=k+1;j<i;j++)
{
if ((strcmp(temp[k].name,temp[j].name))>0)
{
a=temp[k];
temp[k]=temp[j];
temp[j]=a;
}
}
}
fclose(fp);
fp=fopen("telefon.dat","w");
if (fp!=NULL)
{
for (j=0;j<i;j++)
{
fwrite(&temp[j],sizeof(struct subscriber),1,fp);
}
}
fclose(fp);
getch();
}

//VIEW ALL SUBSCRIBE
void viewdir()
{
struct subscriber info;
FILE *fp;
fp=fopen("telefon.dat","r");

sort();
if (fp!=NULL)
{
system("cls");
printf("/nname/t/t/taddress/t/t/ttelephonum/n");
while ((fread(&info,sizeof(struct subscriber),1,fp))==1)
{
printf("/n%-24s",info.name);
printf("%-25s",info.address);
printf("%-12ld/n",info.telno);
}
}
else
{
printf("/nFile open error,/n");
getchar();
return;
}
fclose(fp);

getch();
}
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值