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

来源:互联网 发布:卡盟系统源码 编辑:程序博客网 时间:2024/04/26 09:22

这次的设计用到C语言的单向链表实现。

包括的C语言重点知识有

1.typedef的使用

2.自定义宏的使用

3.单向链表的实现和操作

1.1 设计题目

本课程设计的目的是学习建立链表,使用链表存储结构信息,增加链表结点及删除链表结点等基本操作。实际设计时,可增加数据信息及检索等功能。

1.2 设计要求

1.2.1 功能设计要求

本设计要求实现如下功能:

(1)本设计将重点放在整体设计上,信息只选成员代号和电话。

(2)如果已经有记录,只能在其后追加。

(3)显示整个记录的内容(含新追加的新记录)。

(4)使代号可由6位字符和数字的混合编码组成,如:A201,34011D等。

(5)使电话号码可由18位字符和数字组成,如(86)-551-34443535,1396786678等。

(6)可以删除全部记录,可以随时增加新记录。

(7)可以使用菜单实现增加、删除和显示等功能的选择。

(8)使用宏定义动态申请存储空间。

1.2.2 总体设计

本设计对模块设计的要求如下:

(1)要求使用多文件方式实现链表设计。

(2)要求将它们分成3个模块编制。一个模块负责输入;一个模块负责显示记录的内容;一个模块含有主程序,主程序负责菜单选择和命令处理。

2.设计代码

2.1main.c

#include <stdio.h>#include "record.h"int menu_select(void);void hand_menu(int cmd, ADDR *list_head);int main(int argc, char *argv[]){int cmd = 0;ADDR *list_head;ASK(list_head);list_head->next = NULL;while(1){cmd = menu_select();if(cmd == '0')return 0;hand_menu(cmd, list_head);}}int menu_select(void){int select;printf("    <------通信薄-------->\n");printf("1:添加联系人2:删除联系人\n");printf("3:显示所有联系人0:退出\n");printf("请输入:\n");select = getch();while(select  < '0'|| select>'3'){printf("输入错误,请重新输入:\n");select = getch();}return select;}void hand_menu(int cmd, ADDR *list_head){switch(cmd){case '1':add_person(list_head);break;case '2':list_head = del_person(list_head);break;case '3':dis_person(list_head);break;default:break;}}


2.2 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

2.3 opre.c

#include <stdio.h>#include "record.h"void add_person(ADDR *node){ADDR *new_p; ASK(new_p);new_p->next = NULL;printf("请输入姓名:");scanf("%s", new_p->data.name);printf("请输入电话号码:");scanf("%s", new_p->data.tel);while(node->next)node=node->next;node->next = new_p;}void del_person(ADDR *node){char name[8];ADDR *pre = NULL;printf("请输入要删除的名字:");scanf("%s", name);while(node){if(!strcmp(node->data.name, name)){pre->next = node->next;free(node);printf("成功删除!\n");return;}pre = node;node = node->next;}printf("没有找到该名字!\n");}void dis_person(ADDR *node){if(!node)return ;node = node->next;dis_person(node);if(node)printf("姓名:%s号码:%s\n", node->data.name, node->data.tel);}