文章标题

通讯录
内容文件名 telephone number list.txt

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
struct TEL{
    char name[10];
    char sex[5];
    char number[20];
    char others[100];   
};

struct TEL phone[5000];
int amount;


void read(){
FILE *fp;
fp=fopen("telephone number list.txt","r");
if(fp==NULL){
 printf("Open time error!  0000017\n ");
 return;
}
char temp[1000];
while(fgets(temp,1000,fp)>0){
    temp[ strlen(temp)-1 ]=0;
    char *k1=strstr(temp," ");
    *k1=0;
    strcpy(phone[amount].name,temp);
    char *k2=strstr(k1+1," ");
    *k2=0;
    strcpy(phone[amount].sex ,k1+1);
    char *k3=strstr(k2+1," ");
    *k3=0;
    strcpy(phone[amount].number,k2+1);
    char *k4=strstr(k3+1,":");
    strcpy(phone[amount].others,k4+1);
    amount++; 
}
fclose(fp);
}

void write_in()
{FILE *fp;
fp=fopen("telephone number list.txt","w");
if(fp==NULL){
 printf("Open time error!\n");
 return;
}
int i;
for(i=0;i<amount;i++)
fprintf(fp,"%s %s %s 备注:%s\n",phone[i].name,phone[i].sex,
                                phone[i].number,phone[i].others);
fclose(fp);
return;
}
void edit()
{int i,n,m,ok=0;
 char temp[100],temp2[100],y='y';
 while(y=='y'){
 printf("1.修改联系人\n");
 printf("2.删除联系人\n");
 printf("请输入编辑选项:");
 scanf("%d",&n);
 if(n==1)
    {   ok=0;
        printf("请输入要修改的联系人的姓名(全名):");
        scanf("%s",temp);
        getchar();
        for(i=0;i<amount;i++)
            if(strcmp(phone[i].name,temp)==0)
                {ok=1;break;}
        if(ok==0)
        printf("找不到该联系人\n");
        if(ok==1){
            printf("1.修改该联系人姓名\n");
            printf("2.修改该联系人性别\n");
            printf("3.修改该联系人电话号码\n");
            printf("4.修改该联系人备注\n");
            printf("请输入要修改的项目序号:");
            scanf("%d",&m);
            switch(m){
                case 1:
                    printf("请输入新姓名:");
                    scanf("%s",temp2);
                    getchar();
                    strcpy(phone[i].name,temp2);
                    printf("修改成功!\n");break;
                case 2: 
                    printf("请输入新性别:");
                    scanf("%s",temp2);
                    getchar();
                    strcpy(phone[i].sex,temp2);
                    printf("修改成功!\n");break;
                case 3:
                    printf("请输入新的电话号码:");
                    scanf("%s",temp2);
                    getchar();
                    strcpy(phone[i].number,temp2);
                    printf("修改成功!\n");break;
                case 4:
                    printf("请输入新备注:");
                    scanf("%s",temp2);
                    getchar();
                    strcpy(phone[i].others,temp2);
                    printf("修改成功!\n");break;
            } 
        }

    }
 if(n==2){ok=0; 
    printf("请输入要删除的联系人的姓名(全名):");
        scanf("%s",temp);
        getchar();
        for(i=0;i<amount;i++)
            if(strcmp(phone[i].name,temp)==0)
                {ok=1;break;}
        if(ok==0)
        printf("找不到该联系人\n");
        if(ok){
            for(;i<amount-1;i++)
                {strcpy(phone[i].name,phone[i+1].name);
                 strcpy(phone[i].sex,phone[i+1].sex);
                 strcpy(phone[i].number,phone[i+1].number);
                 strcpy(phone[i].others,phone[i+1].others);}
            amount--;
            printf("删除成功!\n");
        }
 }
 printf("是否继续编辑联系人?(y/n)   ");
 scanf("%c",&y);
}
write_in();
system("cls");
return;
}

void write()
{char n;
while(n-'n'){
printf("请输入联系人姓名:");
scanf("%s",phone[amount].name);
printf("请输入联系人性别:");
scanf("%s",phone[amount].sex);
printf("请输入联系人电话号码:");
scanf("%s",phone[amount].number);
printf("请输入联系人备注:");
scanf("%s",phone[amount].others);
amount++;
printf("是否继续录入?(y/n)");
getchar();
scanf("%c",&n);}
write_in();
system("cls");
return;

}



void list()
{int i;
for(i=0;i<amount;i++)
printf("%8s %2s %11s 备注:%s\n",phone[i].name,phone[i].sex,phone[i].number,phone[i].others);
system("pause");
system("cls");
return;
}

void search()
{int n,i,ok=0;
 char temp[100],c='y';
 while(c=='y'){

 printf("1.按姓名搜索\n");
 printf("2.按性别搜索\n");
 printf("3.按电话号码搜索\n");
 printf("4.按备注内容搜索\n");
printf("请输入搜索方式:"); 
scanf("%d",&n);
getchar();
switch(n){
    case 1:
        ok=0;
        printf("请输入要查找的姓名字段:");
        scanf("%s",temp);
        getchar();
        for(i=0;i<amount;i++)
            if(strstr(phone[i].name,temp)!=NULL)
                {printf("%8s %2s %11s 备注:%s\n",phone[i].name,phone[i].sex,phone[i].number,phone[i].others);
                 ok=1;}
        if(!ok)
        printf("没有找到含有该姓名字段的联系人\n");
        break;
    case 2:
        ok=0;
        printf("请输入性别(男/女):");
        scanf("%s",temp);
        getchar();
        for(i=0;i<amount;i++)
            if(strstr(phone[i].sex,temp)!=NULL)
                {printf("%8s %2s %11s 备注:%s\n",phone[i].name,phone[i].sex,phone[i].number,phone[i].others);
                 ok=1;}
        if(!ok)
        printf("没有找到此性别的联系人或性别输入错误\n");
        break;
    case 3:
        ok=0;
        printf("请输入电话号码字段:");
        scanf("%s",temp);
        getchar();
        for(i=0;i<amount;i++)
            if(strstr(phone[i].number,temp)!=NULL)
                {printf("%8s %2s %11s 备注:%s\n",phone[i].name,phone[i].sex,phone[i].number,phone[i].others);
                 ok=1;}
        if(!ok)
        printf("没有找到含有此号码字段的联系人\n");
        break;
    case 4:
        ok=0;
        printf("请输入备注的字段:");
        scanf("%s",temp);
        getchar();
        for(i=0;i<amount;i++)
            if(strstr(phone[i].others,temp)!=NULL)
                {printf("%8s %2s %11s 备注:%s\n",phone[i].name,phone[i].sex,phone[i].number,phone[i].others);
                 ok=1;}
        if(!ok)
        printf("没有含有此备注字段的联系人\n");
        break;
}
printf("是否重新搜索?(y/n)");
scanf("%c",&c);
getchar();
}
system("cls");
return; 
}


int choose(void)
{printf("------------通讯录-----------\n");
 printf("                 made by Drug\n");
 printf("1.录入联系人\n");
 printf("2.全部列出联系人\n");
 printf("3.搜索联系人\n");
 printf("4.编辑联系人\n");
 printf("0.结束\n");
 int i;
 printf("请输入操作序号:"); 
 scanf("%d",&i);
 getchar();
 if(i==1) write();
 if(i==2) list();
 if(i==3) search();
 if(i==4) edit();
 if(i==0) return 0;
}

int main()
{
 printf("读取通讯录文件中.....\n"); 
 read();
 printf("读取完成!\n");
 Sleep(1000);
 system("cls");
 while(choose());
 return 0; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值