当前位置:编程学习 > C/C++ >>

菜鸟修炼C语言小设计之——通讯录(二)

 

通讯录在通讯录(一)的基础上作了一些完善,添加了保存联系人的功能。

主要涉及的C语言重要知识点有:

文件流的操作

代码:

main.c

 

#include <stdio.h> 

#include "record.h" 

 

int menu_select(void); 

void hand_menu(int cmd, int *flag); 

 

int main(int argc, char *argv[]) 

    int cmd = 0; 

    int flag = 1; 

     

    while(1){ 

        cmd = menu_select(); 

        if(cmd == '0') 

            return 0; 

             

        hand_menu(cmd, &flag); 

    } 

 

int menu_select(void) 

    int select; 

     

    printf("    <------通信薄-------->\n"); 

    printf("1:添加联系人     2:删除联系人\n"); 

    printf("3:显示所有联系人   4:保存\n"); 

    printf("0:退出\n"); 

    printf("请输入:\n"); 

     

    select = getch(); 

    while(select  < '0'|| select>'4') { 

        printf("输入错误,请重新输入:\n"); 

        select = getch(); 

    } 

    return select; 

 

void hand_menu(int cmd, int *flag) 

    static ADDR *list_head = NULL; 

    if(1 == *flag){ 

        list_head = init_person(list_head); 

        *flag  = 0; 

    }  

     

    switch(cmd){ 

        case '1': 

            list_head  = add_person(list_head); 

            break; 

        case '2': 

            list_head = del_person(list_head); 

            break; 

        case '3': 

            dis_person(list_head); 

            break; 

        case '4': 

            save_person(list_head); 

            break; 

             

        default: 

            break; 

    } 

}<strong> 

</strong> 

 

record.h

 

#ifndef _RECORD_H_ 

#define _RECORD_H_ 

typedef struct{ 

    char name[8]; 

    char tel[20]; 

}DATA; 

 

typedef struct node{ 

    DATA data; 

    struct node *next; 

}ADDR; 

 

#define ASK(p) do{\ 

    p = (ADDR *)malloc(sizeof(ADDR));\ 

    if(p==NULL){printf("malloc memory failed!");exit(-1);}\ 

}while(0) 

 

#endif 

opre.c

 

#include <stdio.h> 

#include "record.h" 

 

#define FILE_NAME "phonebook.dat" 

 

ADDR* add_person(ADDR *list_head) 

    ADDR *head = list_head; 

    ADDR *node = list_head; 

    ADDR *new_p; 

     

    ASK(new_p); 

    new_p->next = NULL; 

     

    printf("请输入姓名:"); 

    scanf("%s", new_p->data.name); 

    printf("请输入电话号码:"); 

    scanf("%s", new_p->data.tel); 

     

    if(!node){ 

        head = new_p; 

        return head; 

    } 

     

    while(node->next) 

        node=node->next; 

         

    node->next = new_p; 

    return head; 

 

ADDR *del_person(ADDR *list_head) 

    ADDR *node = list_head; 

    ADDR *head = list_head; 

     

    char name[8]; 

    ADDR *pre = node; 

     

    printf("请输入要删除的名字:"); 

    scanf("%s", name); 

     

    if(!strcmp(head->data.name, name)){ 

        pre = head; 

        head = head->next; 

        free(pre); 

        return head; 

    } 

     

    while(node){ 

        if(!strcmp(node->data.name, name)){ 

            pre->next = node->next; 

            free(node); 

            printf("成功删除!\n"); 

            return; 

        } 补充:软件开发 , C语言 ,

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,