首页 > 其他 > 详细

结构体写通讯录

时间:2015-05-29 23:17:26      阅读:374      评论:0      收藏:0      [点我收藏+]
     <pre name="code" class="cpp">#ifndef _CONTACT_H_//宏定义通讯录  
#define _CONTACT_H_ //宏定义通讯录  
#define NAME_LEN 30//全局变量  
#define SEX_LEN   5//全局变量  
#define TELP_LEN 12//全局变量  
#define GRO_LEN 15//全局变量  
#define MAX_PER  1000//全局变量  
typedef struct per{  
    char name[NAME_LEN];      
    char sex[SEX_LEN];  
    int age;  
    char telp[TELP_LEN];  
    char gro[GRO_LEN];  
}PER,*PPER;  
typedef struct Contact{  
    PER dhb[MAX_PER];  
    int count;  
}PCON,*Pcon;  
void add(Pcon pcon);//添加函数  
void del(Pcon pcon);//删除函数  
void search(Pcon pcon);//查找函数  
void modify(Pcon pcon);//修改函数  
void show(Pcon pcon);//显示函数  
void clear(Pcon pcon);//清空函数  
void WritetoFile(Pcon pcon);//写入文件
void ReadfromFile(Pcon pcon);//读出文件
#endif
#include <stdio.h>  
//#include "contact.h"  
#include <string.h>  
int find(char *p,Pcon pcon)//在这封装一个find函数,在以后的函数里直接调用  
{  
     int i=0;  
     for(i=0;i<pcon->count;i++)  
     {  
       if(strcmp(p,pcon->dhb[i].name)==0)//strcmp为字符串比较函数  
       {  
           return i;  
       }  
     }  
     return -1;  
}  
void add(Pcon pcon)  
{  
    if(pcon->count<MAX_PER)  
    {  
        printf("请输入联系人名字:");  
        scanf("%s",(pcon->dhb[pcon->count]).name);//结构体中变量间接访问成员  
        printf("请输入年龄:");  
        scanf("%d",&(pcon->dhb[pcon->count].age));  
        printf("请输入性别:");  
        scanf("%s",pcon->dhb[pcon->count].sex);  
        printf("请输入电话:");  
        scanf("%s",pcon->dhb[pcon->count].telp);  
        printf("请输入地址:");  
        scanf("%s",pcon->dhb[pcon->count].gro);  
        pcon->count++;  
        printf("ok!添加成功!\n");  
    }  
    else  
    {  
        printf("电话本已满!退出\n");  
        return ;  
    }  
}  
      
void del(Pcon pcon)  
{  
    char name[NAME_LEN];  
    int k=0;  
    int ret=0;  
    printf("请输入你要删除的人的名字:");  
    scanf("%s",name);  
    ret=find(name,pcon);//调用find函数  
    if(ret==-1)  
    {  
        printf("no exit\n");  
    }  
    else  
    {  
          
        for(k=ret;k<pcon->count-1;k++)  
        {  
        pcon->dhb[k-1]=pcon->dhb[k];  
          
        }  
    }  
    pcon->count --;//删除一个成员后,计数器自减1  
    printf("删除成功");  
}  
void search(Pcon pcon)  
{         
    char name[NAME_LEN];  
    int ret = 0;  
    if(ret == -1)  
    {  
        printf("no exit\n");  
    }  
    else  
    {  
        printf("请输入要查找的联系人的名字:");  
        scanf("%s",name);  
        ret = find(name,pcon);  
        printf("%12s",(pcon->dhb[ret]).name);  
        printf("%12d",(pcon->dhb[ret]).age);  
        printf("%12s",pcon->dhb[ret].sex);  
        printf("%12s",pcon->dhb[ret].telp);  
        printf("%12s\n",pcon->dhb[ret].gro);  
    }  
}  
void modify(Pcon pcon)  
{  
  
    char name[NAME_LEN];  
    int ret=0;  
    printf("请输入你要修改人的名字:");  
    scanf("%s",name);  
    ret=find(name,pcon);  
    if(ret=-1)  
    {  
    printf("no exit\n");  
    }  
    else  
    {  
    printf("请输入修改的联系人的姓名:\n");  
    scanf("%s",pcon->dhb[ret].name);  
    printf("请输入修改的联系人的性别:\n");  
    scanf("%s",pcon->dhb[ret].sex);  
    printf("请输入修改的联系人的年龄:\n");  
    scanf("%d",&pcon->dhb[ret].age);  
    printf("请输入修改的联系人的电话:\n");  
    scanf("%s",pcon->dhb[ret].telp);  
    printf("请输入修改的联系人的住址:\n");  
    scanf("%s",pcon->dhb[ret].gro);  
    printf("修改成功\n");  
    }  
}  
void show(Pcon pcon)  
{  
    int j;  
    for(j=0;j<pcon->count;j++)  
    {  
        printf("%s\n",pcon->dhb[j].name);  
        printf("%s\n",pcon->dhb[j].sex);  
        printf("%d\n",pcon->dhb[j].age);  
        printf("%s\n",pcon->dhb[j].telp);  
        printf("%s\n",pcon->dhb[j].gro);  
    }  
}  
void clear(Pcon pcon)  
{  
    pcon->count=0;//只要设定的电话本里的计数器count归零就达到了清空的作用  
  
}  
void WritetoFile(Pcon pcon)
{
	FILE *fp;
	int i;
	if((fp=fopen("contact.txt","w"))==NULL)
	{
		printf("Fialure to openscore.txt!\n");
		//exit(0);
	}
	for(i=0;i<pcon->count;i++)
    {
		fprintf(fp,"%s%s%d%s%s",pcon->dhb[pcon->count].name,pcon->dhb[pcon->count].sex,pcon->dhb[pcon->count].age,pcon->dhb[pcon->count].telp,pcon->dhb[pcon->count].gro);
	}
	fclose(fp);
}
void ReadfromFile(Pcon pcon)
{
	FILE *fp;
	int i;
	if((fp=fopen("contact.txt","r"))==NULL)
	{
		printf("Failure to open score.txt!\n");
	//	exit(0);
	}
	for(i=0;!feof(fp);i++)
	{
		
		fscanf(fp,"%s",pcon->dhb[pcon->count].name);
		fscanf(fp,"%s",pcon->dhb[pcon->count].sex);
		fscanf(fp,"%d",pcon->dhb[pcon->count].age);
		fscanf(fp,"%s",pcon->dhb[pcon->count].telp);
		fscanf(fp,"%s",pcon->dhb[pcon->count].gro);
	}
	fclose(fp);
}
  
void menu()//菜单选择函数  
{  
    printf("*****************************\n");  
    printf("***     1>add             ***\n");  
    printf("***     2>del             ***\n");  
    printf("***     3>search          ***\n");  
    printf("***     4>modify          ***\n");  
    printf("***     5>show             ***\n");  
    printf("***     6>clear           ***\n");  
	printf("***     7>WritetoFile         ***\n"); 
	printf("***     8>ReadfromFile         ***\n"); 
	printf("*****************************\n");  
}  

int main()//主函数中定义选择语句  
{  
    PCON  con;//此时PCON为类型  
    int input = 1;  
    con.count = 0;//对count初始化为0,说明此时电话本为空  
    while(input)  
    {  
        menu();  
        printf("请输入你的选择>:");  
        scanf("%d",&input);  
        switch(input)  
        {  
        case 1:  
            add(&con);  
            break;  
        case 2:  
            del(&con);  
            break;  
        case 3:  
            search(&con);  
            break;  
        case 4:  
            modify(&con);  
            break;  
        case 5:  
            show(&con);  
            break;  
        case 6:  
            clear(&con);  
            break;  
		case 7:
            WritetoFile(&con);
			break;
		case 8:
			ReadfromFile(&con);
			break;
        default:  
            break;  
        }  
    }  
  
    return 0;  
}  
未完待续


结构体写通讯录

原文:http://blog.csdn.net/juice521cai/article/details/46239047

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!