| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 756 人关注过本帖
标题:分享<通讯录>程序vc编写通过
只看楼主 加入收藏
经典
Rank: 2
等 级:论坛游民
帖 子:17
专家分:10
注 册:2010-7-14
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:7 
分享<通讯录>程序vc编写通过
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//********************************************
typedef struct person                   //通讯录结构中结点的定义
{
    char name[10];                        //姓名
    char addr[30];                      //地址
    char offphnum[15];                  //办公电话
    char hmphnum[15];                   //家庭电话
    char mbphnum[15];                   //移动电话
    struct person *next;               
}listnode,*listlink;
//********************************************
struct add_person                      //通讯录记录结构
{
    char name[10];                        //姓名
    char addr[30];                      //地址
    char offphnum[15];                  //办公电话
    char hmphnum[15];                   //家庭电话
    char mbphnum[15];                   //移动电话                  
};
//******************函数声明******************
listlink readfp();                      //将文件的信息读出并转存到链表中
listlink create();                      //建立通讯录
void show(listlink head);               //显示所有的信息
void Delete(listlink head);             //删除一条记录的信息   
void Find(listlink head);               //查找记录
void Iuput(listlink head);              //向通讯录追加一条个人信息
void Alter(listlink head);              //修改一个人信息
void Save(listlink head);               //保存信息
//********************************************
main()
{
    listlink head=NULL;                 //设置链表头结点为空
    int sel;
    head=readfp();                     // 将文件的信息读出并转存到链表中
    if(head==NULL)
        head=create();                 //为空,记录通讯录首条信息
    do
    {
        printf("\n\n\t\t     欢迎使用通讯录");
        printf("\n\t\t 1.显示所有信息        2.删除一条信息");
        printf("\n\t\t 3.查找一条信息        4.追加一条信息");
        printf("\n\t\t 5.修改一条信息");
        printf("\n\t\t 0.保存并退出");
        printf("\n\t\t     请输入(0-5)选择:");
        scanf("%d",&sel);             //读取输入的选择数字
        printf("\n");
        switch(sel)
        {
        case 1:
            show(head);              //显示所有信息
            break;
        case 2:
            Delete(head);           //删除一条信息
            break;
        case 3:
            Find(head);             //查找一条信息
            break;
        case 4:
            Iuput(head);            //追加一条信息               
            break;
        case 5:
            Alter(head);           //修改一条信息
            break;
        case 0:
            Save(head);           //保存并退出
            break;
        }
    }while(1);

}
//*************将文件的信息读出并转存如链表中************
listlink readfp()                     
{
    FILE *fp;                                   //定义文件指针
    struct add_person persons;
    listnode *s;
    listlink head=NULL,end=NULL;
    fp=fopen("people.txt","rb");
    if(fp==NULL)
    {
        printf("不能打开文件\n");
        return head;
    }
    fread(&persons,sizeof(struct add_person),1,fp);
    while(!feof(fp))
    {
        s=(listnode*)malloc(sizeof(listnode));
        strcpy(s->name,persons.name);
        strcpy(s->addr,persons.addr);
        strcpy(s->offphnum,persons.offphnum);
        strcpy(s->hmphnum,persons.hmphnum);
        strcpy(s->mbphnum,persons.mbphnum);
        s->next=NULL;
        if(head==NULL)
            head=end=s;
        else
        {
            end->next=s;
            end=s;
        }
        fread(&persons,sizeof(struct add_person),1,fp);
    }
    return head;
}
//**********建立通讯录,并将信息存入文件************************
listlink create()
{
    listlink s;
    listlink head=NULL,end=NULL;
    while(1)
    {
        s=(listlink)malloc(sizeof(listnode));
        printf("\n\t 建立通讯录\n");
        printf("\n\n\t 姓名:('#'结束)\n\t 姓名:");
        scanf("%s",s->name);
        if(strcmp(s->name,"#")==0)
            break;
        printf("\n 地址:\t 办公电话:\t 家庭电话:\t 移动电话:\n");
        scanf("%s %s %s %s",s->addr,s->offphnum,s->hmphnum,s->mbphnum);
        s->next=NULL;
        if(head==NULL)
            head=end=s;
        else
        {
            end->next=s;
            end=s;
        }
    }
    return(head);
}
//********************显示所有的信息*******************************
void show(listlink head)
{
    listnode *p1;
    p1=head;
    while(p1!=NULL)
    {
        printf("%s\t",p1->name);
        printf("%s\t",p1->addr);
        printf("%s\t",p1->offphnum);
        printf("%s\t",p1->hmphnum);
        printf("%s\n",p1->mbphnum);
        p1=p1->next;
    }
}
//*******************删除一条记录的信息******************************
void Delete(listlink head)
{
    listlink p1,p2;
    char name1[10];
    p1=p2=head;
    if(p1==NULL)
    {
        printf("没有记录\n");
        return;
    }
    printf("\n 请输入要删除的名字:");
    scanf("%s",name1);                               //读取输入的名字
    while(strcmp(p1->name,name1)!=0&&p1->next!=NULL) //查找没结束
    {
        p2=p1;
        p1=p1->next;
    }
    if(strcmp(p1->name,name1)==0)                    //判断输入的名字是否存在
    {
        if(p1==head)
            p1=p1->next;
        else
            p2->next=p1->next;
        free(p1);
    }
    else
        printf("  要删除的记录不存在!\n");
}
//*******************查找指定记录信息的函数********************
void Find(listlink head)
{
    listlink p1;
    char name1[10];
    while(1)
    {
        p1=head;
        printf("\n 输入查找人的名字:('#'结束)\n 姓名:");
        scanf("%s",name1);
        if(strcmp(name1,"#")==0)
            return;
        while(strcmp(name1,p1->name)!=0&&p1->next!=NULL)
            p1=p1->next;
        if(strcmp(name1,p1->name)!=0)
            printf("\n 不存在这个记录!");
        else
        {
            printf("\t 姓名:%s",p1->name);
            printf("\t 地址:%s",p1->name);
            printf("\t 办公电话:%s",p1->offphnum);
            printf("\t 家庭电话:%s",p1->hmphnum);
            printf("\t 移动电话:%s\n",p1->mbphnum);
        }
    }
}
//****************向通讯录追加一条个人信息*******************
void Iuput(listlink head)
{
    listlink s,end;
    s=(listlink)malloc(sizeof(listnode));
    do
    {
        printf("\n\t 追加一条个人信息!\n\t 姓名:('#'结束)\n\t 姓名:");
        scanf("%s",s->name);
        if(strcmp(s->name,"#")==0)
            return;
        end=head;
        if(end==NULL)
            break;
        while((strcmp(end->name,s->name)!=0)&&end->next!=NULL)
            end=end->next;
    }while(strcmp(end->name,s->name)==0);
    printf("\n 地址:\t 办公电话:\t 家庭电话:\t 移动电话\n");
    scanf("%s %s %s %s",s->addr,s->offphnum,s->hmphnum,s->mbphnum);
    s->next=NULL;
    end->next=s;
}
//*****************修改一个人信息***************************
void Alter(listlink head)
{
    listlink p1;
    char name1[10],phnum1[15],phnum2[15],phnum3[15],add1[30];
    printf("\n 输入要修改人的名字:");
    scanf("%s",name1);
    p1=head;
    while(strcmp(name1,p1->name)!=0&&p1->next!=NULL)
        p1=p1->next;
    if(strcmp(name1,p1->name)!=0)
    {
        printf("\n 记录不存在!");
        return;
    }
    else
    {
        printf("\n 输入要修改人的新信息!");
        printf("\n 地址:\t 办公电话:\t 家庭电话:\t 移动电话\n");
        scanf("%s %s %s %s",add1,phnum1,phnum2,phnum3);
        strcpy(p1->name,name1);
        strcpy(p1->addr,add1);
        strcpy(p1->offphnum,phnum1);
        strcpy(p1->hmphnum,phnum2);
        strcpy(p1->mbphnum,phnum3);
    }
}
//********************保存信息***********************
void Save(listlink head)
{
    FILE *fp;
    static struct add_person persons;
    listlink p1;
    fp=fopen("people.txt","wb");
    for(p1=head;p1!=NULL;p1=p1->next)
    {
        strcpy(persons.name,p1->name);
        strcpy(persons.addr,p1->addr);
        strcpy(persons.offphnum,p1->offphnum);
        strcpy(persons.hmphnum,p1->hmphnum);
        strcpy(persons.mbphnum,p1->mbphnum);
        fwrite(&persons,sizeof(struct add_person),1,fp);
    }
    fclose(fp);
}






搜索更多相关主题的帖子: 分享 编写 通讯录 
2010-07-23 11:26
经典
Rank: 2
等 级:论坛游民
帖 子:17
专家分:10
注 册:2010-7-14
收藏
得分:0 
已测试通过,增加了注释
2010-07-23 11:27
sunyh1999
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:14
帖 子:1178
专家分:3032
注 册:2009-5-17
收藏
得分:5 
做的很不错,顶一个

欢迎来到我的博客:http://blog..cn/noisunyuhong
2010-07-23 11:29
sunyh1999
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:14
帖 子:1178
专家分:3032
注 册:2009-5-17
收藏
得分:0 
我这里也有一个通讯录,LZ好参考一下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct
     {
         char score;                /*编号*/
         char name[10];             /*姓名*/
         char num[15];              /*号码*/
         char adds[20];                 /*住址*/
     }Person;

Person pe[80];                        
int menu_select()
     {
         char c;
         do{
         system("color 07");
         system("cls");
         printf("\t\t *****通讯录***** \n");                              
         printf("\t\t┌———————┐\n");                  
         printf("\t\t│ 1. 添加记录   │\n");
         printf("\t\t│ 2. 显示记录   │\n");
         printf("\t\t│ 3. 删除记录   │\n");
         printf("\t\t│ 4. 查询记录   │\n");
         printf("\t\t│ 5. 修改记录   │\n");
         printf("\t\t│ 6. 保存记录   │\n");
         printf("\t\t│ 0. 退出程序   │\n");
         printf("\t\t└———————┘\n");
         printf("\t\t请您选择(0-6):");
         c=getchar();  
         }while(c<'0'||c>'6');
         return(c-'0');
     }

int Input(Person per[],int n)
{
     int i=0;
     char sign,x[10];                     
     while(sign!='n'&&sign!='N')
     {
         printf("\t编号:");
         scanf("\t%d",&per[n+i].score);
         printf("\t姓名:");
         scanf("\t%s",per[n+i].name);
         printf("\t电话号码:");
         scanf("\t%s",per[n+i].num);
         printf("\t通讯住址:");
         scanf("\t%s",per[n+i].adds);
         gets(x);   
         printf("\n\t是否继续添加?(Y/N)");
         scanf("\t%c",&sign);
         i++;
     }
     return(n+i);
}

void Display(Person per[],int n)                                         
{
     int i;
     printf("----------------------------------------------------------------------\n");                           /*格式*/
     printf("编号   姓名     电话号码      通讯地址\n");
     printf("----------------------------------------------------------------------\n");
     for(i=1;i<n+1;i++)                                                            
     {
         printf("%-5d%-8s%-13s%-15s%\n",per[i-1].score,per[i-1].name,per[i-1].num,per[i-1].adds);
         if(i>1&&i%10==0)                                                         
         {
             printf("\t-----------------------------------\n");
             printf("\t");
             system("pause");
             printf("\t-----------------------------------\n");
         }
     }
     printf("----------------------------------------------------------------------\n");
     system("pause");
}



int Delete_a_record(Person per[],int n)
{
     char s[20];
     int i=0,j;
     printf("\t请输入想删除记录中的名字:");
     scanf("%s",s);
     while(strcmp(per[i].name,s)!=0&&i<n) i++;
     if(i==n)
     {
         printf("\t通讯录中没有此人!\n");
         return(n);
     }
     for(j=i;j<n-1;j++)
     {
         strcpy(per[j].num,per[j+1].num);
         strcpy(per[j].name,per[j+1].name);
         strcpy(per[j].adds,per[j+1].adds);
         per[j].score=per[j+1].score;
     }
     printf("\t\t\t已经成功删除!\n");
     return(n-1);
}

void Query_a_record(Person per[],int n)
{
   
     int m;
     printf("\t\n请选择查询方式:\n");
     printf("\t┌——————┐\n");
     printf("\t│1------姓名 │\n");
     printf("\t│2------电话 │\n");
     printf("\t│3------地址 │\n");
     printf("\t│4------返回 │\n");
     printf("\t└——————┘\n");
     printf("请选择:");
     scanf("%d",&m);
     while(m!=1&&m!=2&&m!=3&&m!=4)
     {
         printf("输入错误,请重新选择:");
         scanf("%d",&m);
     }

     if(m==1)
     {
         char s[20];
         int i=0;
         printf("\t请输入想查询的姓名:");
         scanf("\t%s",s);
         while(strcmp(per[i].name,s)!=0&&i<n) i++;
         if(i==n)
         {
             printf("\t通讯录中没有此人!\n");
             return;
         }
         printf("\t此人编号: %d\n",per[i].score);
         printf("\t电话号码: %s\n",per[i].num);
         printf("\t通讯地址: %s\n",per[i].adds);
     } ;

     if(m==2)
     {
         char s[20];
         int i=0;
         printf("\t请输入想查询的电话:");
         scanf("\t%s",s);
         while(strcmp(per[i].num,s)!=0&&i<n) i++;
         if(i==n)
         {
             printf("\t通讯录中没有此人!\n");
             return;

         }
         printf("\t此人编号: %d\n",per[i].score);
         printf("\t此人姓名: %s\n",per[i].name);
         printf("\t通讯地址: %s\n",per[i].adds);
     } ;

     if(m==3)
     {
         char s[20];
         int i=0;
         printf("\t请输入想查询的地址:");
         scanf("\t%s",s);
         while(strcmp(per[i].adds,s)!=0&&i<n) i++;
         if(i==n)
         {
             printf("\t通讯录中没有此人!\n");
             return;
         }
         printf("\t此人编号: %d\n",per[i].score);
         printf("\t此人姓名: %s\n",per[i].name);
         printf("\t电话号码: %s\n",per[i].num);

     } ;
}


void Change(Person per[],int n)
{
     char s[20];
     int i=0;
     printf("\t请输入想修改的记录中的名字:");
     scanf("%s",s);
     while(strcmp(per[i].name,s)!=0&&i<n) i++;
     if(i==n)
     {
         printf("\t通讯录中没有此人!\n");
         return;
     }
     printf("\t编号:");
     scanf("\t%d",&per[i].score);
     printf("\t姓名:");
     scanf("\t%s",per[i].name);
     printf("\t电话号码:");
     scanf("\t%s",per[i].num);
     printf("\t通讯住址:");
     printf("\t修改成功!");
}


void WritetoText(Person per[],int n)            
{
     int i=0;
     FILE *fp;                                            /*定义文件指针*/
     char filename[20];                                   /*定义文件名*/
     printf("\t保存到文件\n");                           /*输入文件名*/
     printf("\t请输入所保存的文件名:");
     scanf("\t%s",filename);
     if((fp=fopen(filename,"w"))==NULL)                  
     {
         printf("\t无法打开文件\n");
         system("pause");
         return;
     }
     fprintf(fp,"******************************************通讯录******************************************\n");
     fprintf(fp,"编号      姓名       电话号码            通讯地址\n");
     fprintf(fp,"------------------------------------------------------------------------------------------\n");
     while(i<n)

     {
         fprintf(fp,"%-3d\t%-6s\t%-13s\t%-20s\t%-20s\n",per[i].score,per[i].name,per[i].num,per[i].adds);
         i++;
     }
     fprintf(fp,"------------------------------------------------------------------------------------------\n");
     fprintf(fp,"***************************************共有%d条记录****************************************\n",n);
     fclose(fp);                                          /*关闭文件*/
     printf("保存成功!\n");
}

void main()                                          /*主函数*/
{
     int n=0;
     for(;;)
     {
         switch(menu_select())
         {
         case 1:
         printf("\n\t添加记录到通讯录\n");                          /*添加记录*/
         n=Input(pe,n);
         break;

         case 2:
         printf("\n\t\t\t     通讯录记录表\n");                       /*显示记录*/
         Display(pe,n);
         break;
        
         case 3:
         printf("\n\t从通讯录中删除记录\n");
         n=Delete_a_record(pe,n);                                   /*删除记录*/
         printf("\t");
         system("pause");
         break;

         case 4:
         printf("\n\t在通讯录中查找记录\n");
         Query_a_record(pe,n);                                       /*查找记录*/
         printf("\t");
         system("pause");
         break;
        
         case 5:
         printf("\n\t修改通讯录中的记录\n");
         Change(pe,n);                                              /*修改数据*/
         printf("\t");
         system("pause");
         break;

         case 6:
         printf("\n\t保存功能\n");
         WritetoText(pe,n);                                          /*保存数据*/
         printf("\t");
         system("pause");
         break;

         case 0:
         printf("\n\t\t谢谢使用,再见!\n");                          /*结束程序*/
         printf("\n\t\t");
         system("pause");
         exit(0);
         }
     }
}

欢迎来到我的博客:http://blog..cn/noisunyuhong
2010-07-23 11:31
经典
Rank: 2
等 级:论坛游民
帖 子:17
专家分:10
注 册:2010-7-14
收藏
得分:0 
你这个更完整点,谢谢了
我这个是已链表方式做的
2010-07-23 11:39
conquerorbzm
Rank: 2
等 级:论坛游民
帖 子:35
专家分:33
注 册:2010-7-23
收藏
得分:5 
nb啊
2010-07-23 13:23
wangzg1126
Rank: 1
等 级:新手上路
帖 子:1
专家分:5
注 册:2010-7-24
收藏
得分:5 
都很牛逼
2010-07-24 02:00
vs303791832
Rank: 2
来 自:地球
等 级:论坛游民
帖 子:87
专家分:12
注 册:2010-6-23
收藏
得分:5 
回个

小白无敌,哈哈.......我成功了
2010-07-24 04:13
快速回复:分享<通讯录>程序vc编写通过
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053662 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved