称不上项目的小程序1:通讯录

xiaoxiao2021-02-28  164

这是之前刚接触结构体的时候写的一个小程序,而且也是第一次写一个篇幅这么长的程序,很多地方都想得不是很完善,比如main函数中的很多语句完全可以放在.c 函数中去实现,还有main中的整体结构框架可以用switch case 语句来实现,这样程序就不会显得像现在这样臃肿。不过,作为第一次完成的“作品”,至少添加,列举,搜索,删除(包括同名好友的删除)等基本操作还是可以一一实现。至于好友ID号的问题,边上一些觉得应该是用户自己输入,但这样总觉得哪里怪怪的(虽然我也自己动手实现了一下那样的功能),所以还是坚持自己想法,在添加好友的时候,系统自动分配一个独一无二的ID号。

总的来说,做完这样一个小程序还是收获不少吧,对结构体的理解更加深入了。

1、头文件:address.h

#ifndef __ADDRESS_H__ #define __ADDRESS_H__ #define FALSE 0 #define TRUE 1 #define NUM 20 typedef char LinkData; typedef long int LinkInt; typedef struct _node { struct _data { LinkInt id; LinkData name[NUM]; LinkInt tel; LinkData address[NUM]; LinkInt comp_tel; struct _data *next; }data; struct _node *next; }Node; typedef struct _data Data; //创建通讯录 Node* Create_address_list(); //主界面 void interface(); //开头 void begin(); //末尾 void finish(); //尾插 int Insert_Last(Node *h, Data data); //添加好友信息 int Insert(Node *h); //列举好友信息 void Display(Node *h); //搜索好友 int Search(Node* h,LinkData *sname); //删除好友 int Delete_Data(Node* h,LinkData *dname); //LinkData *name #endif

2、address.c

#include <stdlib.h> #include "address.h" #include <stdio.h> #include <string.h> int count = 4; Node* Create_address_list() { Node *address_list = (Node*)malloc(sizeof(Node)/sizeof(char)); if (address_list == NULL) { errno = MALLOC_ERROR; return FALSE; } address_list->next = NULL; return address_list; } void interface() { printf ("\t*************Electronic Address_list Book****************\n"); printf ("\t* *\n"); printf ("\t* 1) Add Friend *\n"); printf ("\t* 2) List Information *\n"); printf ("\t* 3) Search Friend *\n"); printf ("\t* 4) Delete Friend *\n"); printf ("\t* BY LYG *\n"); printf ("\t******************** (q) To Leave ***********************\n\n"); } int Insert(Node *h) { if (h == NULL) { errno = MALLOC_ERROR; return FALSE; } Node* node = Create_address_list(); begin(); //printf ("Input the id :"); //scanf("%d",&node->data.id); node->data.id = count++; printf ("\tInput the name :"); scanf ("%s",&node->data.name); printf ("\tInput the telephone number :"); scanf ("%ld",&node->data.tel); printf ("\tInput the address :"); scanf ("%s",&node->data.address); printf ("\tInput the comp_tel number :"); scanf ("%ld",&node->data.comp_tel); Insert_Last(h, node->data); free (node); printf ("\n\tKeeping Information......\n"); sleep(1); printf ("\n\tAdd Success !\n"); return TRUE; } int Insert_Last(Node *h, Data data) { if (h == NULL) { errno = MALLOC_ERROR; return FALSE; } Node *node = (Node*)malloc(sizeof(Node)/sizeof(char)); if (node == NULL) { errno = MALLOC_ERROR; return FALSE; } node->data = data; node->next = NULL; Node* tmp = h; while (tmp->next) { tmp = tmp->next; } tmp->next = node; return TRUE; } void Display(Node *h) { if (h == NULL) return; Node *tmp = h->next; begin(); printf ("\t ********** The information of friend ************* \n\n"); while (tmp) { printf ("\t ID : d\n", tmp->data.id); printf ("\t name : %s\n", tmp->data.name); printf ("\t phone number : %ld\n", tmp->data.tel); printf ("\t address : %s\n", tmp->data.address); printf ("\t comp_tel number : %ld\n\n", tmp->data.comp_tel); tmp = tmp->next; } } int Search(Node* h,LinkData *sname) { if (h == NULL) { errno = MALLOC_ERROR; return FALSE; } begin(); int tip[100] = {0}; int i = 0; int k = 1; Node *tmp = h->next; while (tmp) { if (0 == strcmp(tmp->data.name,sname)) { tip[i++] = tmp->data.id; k++; } tmp = tmp->next; } if (i == 0) { printf ("\tDo not have any information of this friend\n\n"); return TRUE; } else { for (i = 0; i < k; i++) { tmp = h->next; while (tmp) { if (tmp->data.id == tip[i]) { printf ("\t ID : d\n", tmp->data.id); printf ("\t name : %s\n", tmp->data.name); printf ("\t phone number : %ld\n", tmp->data.tel); printf ("\t address : %s\n", tmp->data.address); printf ("\t comp_tel number : %ld\n\n", tmp->data.comp_tel); } tmp = tmp->next; } } return TRUE; } } int Delete_Data(Node* h, LinkData *dname) { if (h == NULL) { errno = MALLOC_ERROR; return FALSE; } Node *tmp = h; int num = 0; while (tmp->next) { if (0 == strcmp(tmp->next->data.name,dname)) { num++; } tmp = tmp->next; } //if (tmp->next == NULL) // return FALSE; if (num == 0) { begin(); printf ("\tWe can not delete the nonexistent friend\n\n"); finish(); } else if (num == 1) { tmp = h; while (tmp->next) { if (0 == strcmp(tmp->next->data.name,dname)) { Node *p = tmp->next; tmp->next = p->next; free(p); begin(); printf ("\tDelete Success!\n"); printf ("\tWill go to the interface in 3s later......\n"); sleep(3); system ("clear"); interface(); finish(); } tmp = tmp->next; if (tmp == NULL) break; } return TRUE; } else { Search(h,dname); printf ("\tInput the ID of the friend who you want to delete\n"); int ID_num; scanf ("%d", &ID_num); tmp = h; while (tmp->next) { if (tmp->next->data.id == ID_num) { Node *p1 = tmp->next; tmp->next = p1->next; free(p1); begin(); printf ("\tDelete Success!\n"); printf ("\tWill go to the interface in 3s later......\n"); sleep(3); system ("clear"); interface(); finish(); return TRUE; } tmp = tmp->next; } if (tmp->next == NULL) return; } } void begin() { system ("clear"); printf ("\t*************Electronic Address_list Book***************\n\n"); } void finish() { printf ("\n\t******** (q) To Leave ******** (m) To Interface ********\n"); printf ("\tInput The Order(1-4): "); }

3、main.c

#include <stdio.h> #include "address.h" #include <string.h> int main() { Node *address_list = Create_address_list(); if (address_list == NULL) { errno = MALLOC_ERROR; char *str = myStrError(errno); printf ("str: %s\n",str); return FALSE; } Node* node = Create_address_list(); int i; char name1[NUM] = "abc"; char address1[NUM] = "QWE"; for (i = 1; i < 4; i++) { node->data.id = i; strncpy(node->data.name,name1,i); node->data.tel = 58731927+i; strncpy(node->data.address,address1,i); node->data.comp_tel = 58721269+i; Insert_Last(address_list, node->data); } free (node); system ("clear"); interface(); char order[100]; printf("\tInput The Order(1-4): "); scanf ("%s",order); //while (0 != strncmp(order,"q",1)) while(1) { if(0 == strncmp(order,"1",1)) { Insert(address_list); finish(); scanf ("%s",order); } else if(0 == strncmp(order,"2",1)) { Display(address_list); finish(); scanf ("%s",order); } else if(0 == strncmp(order,"3",1)) { printf ("\tInput tne name which you want to search:"); char sname[NUM] = {0}; getchar(); gets(sname); Search(address_list,sname); finish(); scanf ("%s",order); } else if(0 == strncmp(order,"4",1)) { printf ("\tInput tne name which you want to delete:"); char dname[NUM] = {0}; getchar(); gets(dname); Delete_Data(address_list,dname); scanf ("%s",order); } else if(0 == strncmp(order,"m",1)) { system ("clear"); interface(); printf("\tInput The Order(1-4): "); scanf ("%s",order); } else if(0 == strncmp(order,"q",1)) { system ("clear"); interface(); printf ("\tThe system is going to closed.....\n"); printf ("\n\t"); sleep(1); break; } else { finish(); scanf ("%s",order); } } return 0; }

转载请注明原文地址: https://www.6miu.com/read-18368.html

最新回复(0)