通讯录

大笑.h文件

#ifndef __CONTACTS__
#define __CONTACTS__

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#define NAME_SIZE 10
#define SEX_SIZE 5
#define TEL_SIZE 12
#define ADDRE_SIZE 20
#define START 10

enum          //把不同的操作用枚举的方式把它们的可能取值列出来
{
                EXIT,
                ADD,
                DELETE,
                FIND,
                ALTER,
                SHOW,
                CLEAR,
                SORT,
                SAVE
};

typedef struct Peo     //因为通讯录里一个人的信息包括很多方面,不同的类型,所以把人定义为一个新的结构体类型
{
                 char name[ NAME_SIZE ];
                 char sex[ SEX_SIZE ];
                 int age;
                 char tel[ TEL_SIZE ];
                 char addre[ ADDRE_SIZE ];
} Peo ;

typedef struct Contact
{
                 int size;
                 Peo peo[];
} Contact ,* Pcon ;

void meun();
void init_conatct( Pcon pcon);
void load_contact( Pcon pcon);
void add_conatct( Pcon pcon);
void dele_conatct( Pcon pcon);
void find_conatct( Pcon pcon);
void alter_conatct( Pcon pcon);
void show_conatct( Pcon pcon);
void clear_conatct( Pcon pcon);
void sort_conatct( Pcon pcon);
void save_contact( Pcon pcon);

#endif

害羞.c文件

#include "contact.h"

void meun()
{
                printf( "        ********通讯录********\n" );
                printf( "********1、添加联系人        2、删除指定联系人*********\n" );
                printf( "********3、查找指定联系人    4、修改指定联系人*********\n" );
                printf( "********5、显示所有联系人    6、清空所有联系人*********\n" );
                printf( "********7、按名字排序联系人  8、保存联系人并退出*******\n" );
                printf( "********0、退出程序                             *******\n" );
                printf( "请选择:>" );
}

void init_conatct( Pcon pcon )
{
                 pcon ->size = 0;
                 pcon = ( Pcon )malloc( START * sizeof ( pcon ->peo) + sizeof ( Pcon ));   //为通讯录分配动态内存
}

void add_mem( Pcon pcon )
{
                 if ( pcon ->size == 1000)
                {
                                printf( "通讯录已满!\n" );
                                 return ;
                }
                 int num = pcon ->size / 10 + 1;
                 pcon = ( Pcon )malloc(num * START * sizeof ( pcon ->peo) + sizeof ( Pcon ));
}

void load_contact( Pcon pcon )
{
                 char ch = ' ' ;
                init_conatct( pcon );
                 char p[5] = { 0 };
                 FILE *fread = fopen( "contact.txt" , "r" );
                 if (fopen == NULL )
                {
                                printf( "打开文件失败!\n" );
                                exit(0);
                }
                ch = fgetc(fread);
                 while (ch!= EOF )
                {
                                fseek(fread, -1, SEEK_CUR );
                                add_mem( pcon );         
                                fscanf(fread, "%s%s%d%s%s" , pcon ->peo[ pcon ->size].name, pcon ->peo[ pcon ->size].sex, & pcon ->peo[ pcon ->size].age,\
                                                   pcon ->peo[ pcon ->size].tel, pcon ->peo[ pcon ->size].addre); //注意结构体指针变量对成员的引用方法
                                       //fscanf():从文件里以格式化的方式读取数据到内存的函数
                                 pcon ->size++;              
                                ch = fgetc(fread);          //因为fscanf是读到上一行的'\n'截止,所以得先把'\n'读了,使fread指向下一行
                                ch = fgetc(fread);          //判断现在fread所指向的这一行是不是空行(EOF)
                }
                fclose(fread);
}

void save_contact( Pcon pcon )
                 int i = 0;
                 FILE *fwriter = fopen( "contact.txt" , "w" );
                 if (fopen == NULL )
                {
                                printf( "打开文件失败!\n" );
                                exit(0);
                }
                 //fprintf(fwriter, "%-4s  %-4s  %-4s  %-8s  %-8s\n", "姓名", "性别", "年龄", "电话", "住址");
                 while (i < pcon ->size)
                {             
                                fprintf(fwriter, "%-4s  %-4s  %-4d  %-8s  %s\n" , pcon ->peo[i].name, pcon ->peo[i].sex, pcon ->peo[i].age,\
                                                                 pcon ->peo[i].tel,  pcon ->peo[i].addre);
                                        //fprintf():从内存里以格式化的方式输出数据到文件的函数
                                i++;

                }
                fclose(fwriter);
                printf( "联系人信息保存成功!\n" );
                exit(0);
}

void swap( Peo * peo1 , Peo * peo2 )    //排序时交换两个人的信息
{
                 Peo temp = * peo1 ;
                * peo1 = * peo2 ;
                * peo2 = temp;
}

void add_conatct( Pcon pcon )
{
                add_mem( pcon );
                printf( "请输入要插入的联系人的信息:\n" );
                printf( "姓名:" );
                scanf( "%s" , pcon ->peo[ pcon ->size].name);
                printf( "性别:" );
                scanf( "%s" , pcon ->peo[ pcon ->size].sex);
                printf( "年龄:" );
                scanf( "%d" , & pcon ->peo[ pcon ->size].age);
                printf( "电话:" );
                scanf( "%s" , pcon ->peo[ pcon ->size].tel);
                printf( "住址:" );
                scanf( "%s" , pcon ->peo[ pcon ->size].addre);
                 pcon ->size++;
                printf( "插入成功!\n" );
                system( "cls" );
}

void dele_conatct( Pcon pcon )
{
                 int i = 0;
                 char p[10] = "" ;
                printf( "请输入要删除的联系人的姓名:\n" );
                scanf( "%s" , p);
                 for (i = 0; i < pcon ->size; i++)
                {
                                 if (!strcmp(p, pcon ->peo[i].name))
                                {
                                                 break ;
                                }
                }
                 if (i == pcon ->size)
                {
                                printf( "通讯录不存在该联系人,无法删除!\n" );
                                 return ;
                }
                 while (i < ( pcon ->size - 1))
                {
                                 pcon ->peo[i] = pcon ->peo[i + 1];
                                i++;
                }
                 pcon ->size--;
                printf( "删除成功!\n" );
                system( "cls" );
}


void find_conatct( Pcon pcon )
{
                 int i = 0;
                 char p[10] = "" ;
                printf( "请输入要查找的联系人的姓名:\n" );
                scanf( "%s" , p);
                 for (i = 0; i < pcon ->size;)
                {
                                 if (!strcmp( pcon ->peo[i].name, p))

                                {
                                                printf( "%-6s%-6s%-6s%-16s%s\n" , "姓名" , "性别" , "年龄" , "电话" , "住址" );
                                                printf( "%-6s%-6s%-6d%-16s%s\n" , pcon ->peo[i].name, pcon ->peo[i].sex, pcon ->peo[i].age, \
                                                                                 pcon ->peo[i].tel, pcon ->peo[i].addre);
                                                 return ;
                                }
                                i++;         //因为上面循环里的判断用到i了,要是把i++写在循环条件里,那么会出错
                }
                printf( "通讯录内不存在该联系人!\n" );
                 return ;
}

void alter_conatct( Pcon pcon )
{
                 int i = 0;
                 char p[10] = "" ;
                printf( "请输入要修改的联系人的姓名:\n" );
                scanf( "%s" , p);
                 for (i = 0; i < pcon ->size; )
                {
                                 if (!strcmp( pcon ->peo[i].name, p))
                                {
                                                printf( "请输入修改后的联系人的信息:\n" );
                                                printf( "姓名:" );
                                                scanf( "%s" , pcon ->peo[i].name);
                                                printf( "性别:" );
                                                scanf( "%s" , pcon ->peo[i].sex);
                                                printf( "年龄:" );
                                                scanf( "%d" , & pcon ->peo[i].age);
                                                printf( "电话:" );
                                                scanf( "%s" , pcon ->peo[i].tel);
                                                printf( "住址:" );
                                                scanf( "%s" , pcon ->peo[i].addre);
                                                printf( "修改成功!\n" );
                                                system( "cls" );
                                                 return ;
                                }
                                i++;         //因为上面循环里的判断用到i了,要是把i++写在循环条件里,那么会出错
                }
                printf( "通讯录内不存在该联系人!\n" );
                system( "cls" );
}

void show_conatct( Pcon pcon )
{
                 int i = 0;
                 if ( pcon ->size == 0)
                {
                                printf( "通讯录内没有任何信息!\n" );
                                system( "cls" );
                                 return ;
                }
                printf( "%-6s%-6s%-6s%-16s%s\n" , "姓名" , "性别" , "年龄" , "电话" , "住址" );
                 for (i = 0; i < pcon ->size; i++)
                {
                                printf( "%-6s%-6s%-6d%-16s%s\n" , pcon ->peo[i].name, pcon ->peo[i].sex, pcon ->peo[i].age,\
                                                    pcon ->peo[i].tel, pcon ->peo[i].addre);
                }
}

void clear_conatct( Pcon pcon )
{
                 pcon ->size = 0;
                printf( "联系人已被清空!\n" );
                system( "cls" );
}

void sort_conatct( Pcon pcon )
{
                 int i = 0;
                 int j = 0;
                 for (i = 0; i < pcon ->size - 1; i++)
                {
                                 for (j = 0; j < pcon ->size - i - 1; j++)
                                {
                                                 if (strcmp( pcon ->peo[j].name, pcon ->peo[j+1].name) > 0)  //这个必须指明结果是大于0还是小于0,因为
                                                                                                                     //不指明的话,不管大于0还是小于0都会进到if
                                                                                                                                                                                                                                                                                 //判断语句里去
                                                {
                                                                swap(& pcon ->peo[j], & pcon ->peo[j + 1]);
                                                }
                                }
                }
                printf( "排序成功!\n" );
                system( "cls" );
}
生气测试部分

#define _CRT_SECURE_NO_WARNINGS 1

#include "contact.h"

int main()
{
                 Contact pcon = { 0 };
                load_contact(&pcon);
                 while (1)
                {
                                 int input = 0;
                                meun();
                                scanf( "%d" , &input);
                                 switch (input)
                                {
                                 case ADD:;
                                                add_conatct(&pcon);
                                                 break ;
                                 case DELETE:;
                                                dele_conatct(&pcon);
                                                 break ;
                                 case FIND:
                                                find_conatct(&pcon);
                                                 break ;
                                 case ALTER:;
                                                alter_conatct(&pcon);
                                                 break ;
                                 case SHOW:;
                                                show_conatct(&pcon);
                                                 break ;
                                 case CLEAR:;
                                                clear_conatct(&pcon);
                                                 break ;
                                 case SORT:;
                                                sort_conatct(&pcon);
                                                 break ;
                                 case SAVE:;
                                                save_contact(&pcon);
                                                 break ;
                                 case EXIT:;
                                                exit(0);
                                                 break ;
                                }
                }
                system( "pause" );
                 return 0;
}




你可能感兴趣的:(C语言,项目,通讯录)