(3)巩固一下C语言基础知识,谁动手,谁收获。

问题12:

编写一个简单的通讯录管理系统。通讯录包括:姓名、通讯地址、邮编、联系电话,现编写一个通讯录管理系统,可以对通讯录进行输入、显示、查找,通讯录保存到一个文件中。(50分)

(提示:可能用到以下库函数:

malloc(),功能:分配size字节的存储区,

用法:(类型 *)malloc(size *sizeof(你要定义的类型));包含于<stdlib.h>库中

feof(),功能:检测文件是否结束

用法: int feof(fp)

FILE *fp;包含于<stdio.h>库中。)

问题12实现代码:

/* author:宋金时 */
/* date:2011/11/03 */

#include "Stdio.h"
#include "Conio.h"

#define N 20
#define FILE_NAME "add_book"
typedef struct addbook
{
    char name[N];
    char add[N];
    long p_code;
    long phone_num;
}a_book;

int g_pos=0;

void init(a_book add[]);
void menu(void);
void input(a_book add[]);
void display(a_book add[]);
int find(a_book add[]);
void save(a_book add[]);
void clear_add(void);
void delete_add(a_book add[]);
void quit_save(a_book add[]);
int main(void)
{
 a_book arr_add[N];
 int m_select;
 init(arr_add);
  while(1)
  {
  menu();
  scanf("%d",&m_select);
  getchar();
  switch(m_select)
  {
    case 1:input(arr_add);break;
    case 2:display(arr_add);break;
    case 3:find(arr_add);break;
    case 4:save(arr_add);break;
    case 5:delete_add(arr_add);break;
    case 6:clear_add();break;
    case 7:quit_save(arr_add);
  }
  }
  getch();
  return 0;
}

void init(a_book add[])
{
    int m_i;
    char m_temp;
    FILE* fp;
    if((fp=fopen(FILE_NAME,"r"))==NULL)
    {
        if((fp=fopen(FILE_NAME,"wb+"))==NULL)
        {
        puts("open file error!\n");
        getch();
        exit(1);
        }
    }
    for(m_i=0;m_i<N;m_i++)
    {
        m_temp = fgetc(fp);
        if(m_temp==EOF)
            break;
        fseek(fp,-1,SEEK_CUR);
        fscanf(fp,"%s %s %ld %ld\n",add[m_i].name,\
        add[m_i].add,&add[m_i].p_code,&add[m_i].phone_num);
    }
    g_pos = m_i;
    puts("init update success!\n");
/*    rewind(fp);*/
    fclose(fp);
}

void menu(void)
{
    puts("1.input a new.\n");
    puts("2.display contents of address book.\n");
    puts("3.find infomation from address book.\n");
    puts("4.save address book.\n");
    puts("5.delete a address book.\n");
    puts("6.clear address book.\n");
    puts("7.save quit.\n");
    puts("please select a number:");
}

void input(a_book add[])
{
    puts("please input name address post code telephone number:\n");
/*    gets(add[g_pos].name);
    gets(add[g_pos].add);*/
    scanf("%s%s%ld%ld",add[g_pos].name,add[g_pos].add,\
    &add[g_pos].p_code,&add[g_pos].phone_num);
    g_pos++;
}

void display(a_book add[])
{
    int m_i;
    save(add);
/*    init(add);*/
    puts("name address postcode telephone\n");
    for(m_i=0;m_i<g_pos;m_i++)
    {
        printf("%s %s %ld %ld\n",add[m_i].name,\
        add[m_i].add,add[m_i].p_code,add[m_i].phone_num);
    }
}

int find(a_book add[])
{
    char m_name[N];
    int m_i;
    puts("please name:");
    gets(m_name);
    for(m_i=0;m_i<g_pos;m_i++)
    {
        if(strcmp(add[m_i].name,m_name)==0)
        {
            puts("name address postcode telephone\n");
            printf("%s %s %ld %ld\n",add[m_i].name,\
        add[m_i].add,add[m_i].p_code,add[m_i].phone_num);
        break;
        }
    }
    if(m_i==g_pos)
        puts("not find!\n");
    return m_i;

}

void save(a_book add[])
{
    int m_i;
    FILE* fp;
    if((fp=fopen(FILE_NAME,"w"))==NULL)
    {
        puts("open file error!\n");
        getch();
        exit(1);
    }
    for(m_i=0;m_i<g_pos;m_i++)
    {
        fprintf(fp,"%s %s %ld %ld\n",add[m_i].name,add[m_i].add,\
        add[m_i].p_code,add[m_i].phone_num);
/*        printf("%ld %ld\n",add[m_i].p_code,add[m_i].phone_num); */
    }
/*    rewind(fp);*/
    fclose(fp);
    puts("save success!\n");
/*    getch();
    exit(1); */
}

void quit_save(a_book add[])
{
    save(add);
    exit(1);
}

void clear_add(void)
{
    FILE* fp;
    if((fp=fopen(FILE_NAME,"wb+"))==NULL)
    {
        printf("not clear!\n");
     }
     else
     {
        g_pos=0;
        fclose(fp);
        printf("clear success!\n");
     }
}

void delete_add(a_book add[])
{
    int m_i = find(add);
    for(;m_i<g_pos;m_i++)
    {
        strcpy(add[m_i].name,add[m_i+1].name);
        strcpy(add[m_i].add,add[m_i+1].add);
        add[m_i].p_code = add[m_i+1].p_code;
        add[m_i].phone_num = add[m_i+1].phone_num;
    }
    g_pos--;
    printf("delete success!\n");
}


你可能感兴趣的:(c,File,delete,语言,FP,通讯录管理)