通讯录程序优化
来源:互联网 发布:中原地产 知乎 编辑:程序博客网 时间:2024/05/29 03:05
接再上次的通讯录程序,上次程序中还存在一些bug,本次修改:
1. 我将print输出函数单独列出,不再与其他函数共同使用,使程序更加简洁,界面也比较清晰;
2. 添加empty清空函数,之前未将通讯录程序中的数据清空,会造成一些系统垃圾;
3. 优化界面;
4. 在退出时添加询问的语句,避免因为操作失误而退出;
优化后的程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define T 1
#define F -1
typedef int bo;
typedef char Type;
struct Address
{
struct Address* next;
struct Address* prior;
int id;
Type name[20];
Type num[12];
};
typedef struct Address* address;
int mycmp(char *str1, char *str2);
void mycpy(char *s, char *p);
int init(address *head);
int length(address head);
int insert_tail(address head, Type *name, Type *num);
int delete_num(address head, Type *num);
int delete_name(address head, Type *name);
int delete_id(address head, int id);
int update_id(address head, int id, Type *name, Type *num);
int update_num(address head, Type *name, Type *old_num, Type *new_num);
int update_name(address head, Type *old_name, Type *new_name, Type *num);
int query_id(address head, int id);
int query_num(address head, Type *num);
int query_name(address head, Type *name);
void rank_name(address head);
void rank_num(address head, int begin, int end);
address unit(address head, int index);
void print(address head);
void empty(address head);
int main()
{
int i;
int sum;
int ret;
Type name[20];
Type num[12];
Type new_name[20];
Type new_num[12];
Type old_name[20];
Type old_num[12];
address head = NULL;
ret = init(&head);
if (NULL == head)
{
return F;
}
printf("/****************************insert, delete, update, query, rank********************************/\n");
int control = 1;
int delete;
int update;
int query;
int rank;
while (control != 0)
{
printf("if you want to end scanf 0, insert scanf 1, delete scanf 2, query scanf 3, update scanf 4, rank scanf 5, empty scanf 6, print scanf 7\n");
printf("please scanf : ");
scanf("%d", &control);
if (0 == control)
{
printf("if you want to finish? Y 1/ N 0\n");
int finish;
scanf("%d", &finish);
if (finish == 1)
{
break;
}
else
{
printf("if you want to end scanf 0, insert scanf 1, delete scanf 2, query scanf 3, update scanf 4, rank scanf 5, empty scanf 6, print scanf 7\n");
printf("please scanf : ");
scanf("%d", &control);
}
}
switch (control)
{
case 1 : printf("you want to insert how many telephone number : ");
scanf("%d", &sum);
for (i = 0; i < sum; i++)
{
printf("[%d] the name, number : ", i + 1);
scanf("%s %s", name, num);
insert_tail(head, name, num);
}
printf("***************************************************************************************\n");
break;
case 2 : printf("you want to choose delete by id 1, num, 2, name 3 : ");
scanf("%d", &delete);
switch (delete)
{
case 1 : printf("delete by id, please choose the id : ");
int id;
scanf("%d", &id);
delete_id(head, id);
printf("***************************************************************************************\n");
break;
case 2 : printf("delete by num, please choose the num : ");
scanf("%s", num);
delete_num(head, num);
printf("***************************************************************************************\n");
break;
case 3 : printf("delete by name, please choose the name : ");
scanf("%s", name);
delete_name(head, name);
printf("***************************************************************************************\n");
break;
default : printf("no !\n");
printf("***************************************************************************************\n");
break;
}
break;
case 3 : printf("you want to choose query by id 1, num, 2, name 3 : ");
scanf("%d", &query);
switch (query)
{
case 1 : printf("query by id, please choose the id : ");
int id;
scanf("%d", &id);
query_id(head, id);
printf("***************************************************************************************\n");
break;
case 2 : printf("query by num, please choose the num : ");
scanf("%s", num);
query_num(head, num);
printf("***************************************************************************************\n");
break;
case 3 : printf("query by name, please choose the name : ");
scanf("%s", name);
query_name(head, name);
printf("***************************************************************************************\n");
break;
default : printf("no !\n");
printf("***************************************************************************************\n");
break;
}
break;
case 4 : printf("you want to choose update by id 1, num, 2, name 3 : ");
scanf("%d", &update);
switch (update)
{
case 1 : printf("update by id, please choose the id name, num : ");
int id;
scanf("%d %s %s", &id, name, num);
update_id(head, id, name, num);
printf("***************************************************************************************\n");
break;
case 2 : printf("update by num, please choose the name, the old_num, the new_num : ");
scanf("%s %s %s", name, old_num, new_num);
update_num(head, name, old_num, new_num);
printf("***************************************************************************************\n");
break;
case 3 : printf("update by name, please choose the old_name, the new_name, the num : ");
scanf("%s %s %s", old_name, new_name, num);
update_name(head, old_name, new_name, num);
printf("***************************************************************************************\n");
break;
default : printf("no !\n");
printf("***************************************************************************************\n");
break;
}
break;
case 5 : printf("you want to choose rank by num 1, name 2 : ");
scanf("%d", &rank);
switch (rank)
{
case 1 : printf("%s\n", unit(head, 1)->name);
printf("rank by num\n");
rank_num(head, 1, length(head));
printf("***************************************************************************************\n");
break;
case 2 : printf("rank by name\n");
rank_name(head);
printf("***************************************************************************************\n");
break;
default : printf("no !\n");
printf("***************************************************************************************\n");
break;
}
break;
case 6 : empty(head);
break;
case 7 : print(head);
break;
default : printf("###error, the control is from 0 ~ 7###\n");
break;
}
}
printf("************************************END****************************************************\n");
return 0;
}
void empty(address head)
{
int i;
int len = length(head);
address temp = head;
for (i = 0; i < len; i++)
{
address temp = head->next->next;
free(head->next);
head->next = temp;
temp->prior = head;
}
}
int init(address *head)
{
address newnode = (address)malloc(sizeof(struct Address));
if (NULL == newnode)
{
return F;
}
newnode->prior = newnode;
newnode->next = newnode;
*head = newnode;
return T;
}
void print(address head)
{
int i = 1;
address temp = head;
printf("output :\n");
while (temp->next != head)
{
printf("***************************************************************************************\n");
printf("[%d] :\t", i++);
printf("id = %d\t", temp->next->id);
printf("\tname = %s", temp->next->name);
printf("\tnum = %s\n", temp->next->num);
printf("***************************************************************************************\n");
temp = temp->next;
}
}
int insert_tail(address head, Type* name, Type* num)
{
address newnode = (address)malloc(sizeof(struct Address));
if (NULL == newnode)
{
return F;
}
static int id = 1;
mycpy(newnode->name, name);
mycpy(newnode->num, num);
newnode->id = id;
id++;
newnode->next = head->prior->next;
head->prior->next = newnode;
newnode->prior = head->prior;
head->prior = newnode;
return T;
}
void rank_name(address head)
{
int i;
int j;
int len = length(head);
address temp = head;
char *num = (char *)malloc(sizeof(char) * 20);
char *name = (char *)malloc(sizeof(char) * 20);
for (i = 0; i < len - 1; i++)
{
head = temp;
for (j = i; j < len; j++)
{
if (0 < mycmp(temp->next->name, head->next->name))
{
mycpy(name, head->next->name);
mycpy(head->next->name, temp->next->name);
mycpy(temp->next->name, name);
mycpy(num, head->next->num);
mycpy(head->next->num, temp->next->num);
mycpy(temp->next->num, num);
}
head = head->next;
}
temp = temp->next;
}
}
address unit(address head, int index)
{
int i;
address temp = head;
for (i = 0; i < index; i++)
{
temp = temp->next;
}
return (temp);
}
void rank_num(address head, int begin, int end)
{
if (begin >= end)
{
return;
}
int i = begin;
int j = end;
char name[20];
char num[12];
mycpy(name, unit(head, begin)->name);
mycpy(num, unit(head, begin)->num);
while (i < j)
{
while (i < j && (mycmp(unit(head, j)->num, num) >= 0))
{
j--;
}
mycpy(unit(head, i)->name, unit(head, j)->name);
mycpy(unit(head, i)->num, unit(head, j)->num);
while (i < j && mycmp(unit(head, i)->num, num) <= 0)
{
i++;
}
mycpy(unit(head, j)->name, unit(head, i)->name);
mycpy(unit(head, j)->num, unit(head, i)->num);
}
mycpy(unit(head, i)->name, name);
mycpy(unit(head, i)->num, num);
rank_num(head, begin, i - 1);
rank_num(head, i + 1, end);
}
int length(address head)
{
int cont = 0;
address temp = head;
while (temp->next != head)
{
temp = temp->next;
cont++;
}
return cont;
}
int delete_num(address head, Type *num)
{
int i;
int len = length(head);
printf("%d\n", len);
for (i = 0; i < len; i++)
{
if (mycmp(head->next->num, num) == 0)
{
address temp = head->next->next;
free(head->next);
head->next = temp;
temp->prior = head;
}
else
{
head = head->next;
}
}
return T;
}
int delete_name(address head, Type *name)
{
int i;
int len = length(head);
printf("%d\n", len);
for (i = 0; i < len; i++)
{
if (mycmp(head->next->name, name) == 0)
{
address temp = head->next->next;
free(head->next);
head->next = temp;
temp->prior = head;
}
else
{
head = head->next;
}
}
return T;
}
int delete_id(address head, int id)
{
int i;
if (id < 0 || id > length(head))
{
printf("out of range!\n");
return F;
}
for (i = 1; i < id; i++)
{
head = head->next;
}
address temp = head->next->next;
free(head->next);
head->next = temp;
temp->prior = head;
return T;
}
int update_id(address head, int id, Type *name, Type *num)
{
int i;
if (id < 0 || id > length(head))
{
printf("out of range!\n");
return F;
}
for (i = 1; i < id; i++)
{
head = head->next;
}
mycpy(head->next->name, name);
mycpy(head->next->num, num);
return T;
}
int update_num(address head, Type *name, Type *old_num, Type *new_num)
{
int i;
int len = length(head);
for (i = 0; i < len; i++)
{
if (0 == mycmp(head->next->num, old_num))
{
mycpy(head->next->num, new_num);
mycpy(head->next->name, name);
}
head = head->next;
}
return T;
}
int update_name(address head, Type *old_name, Type *new_name, Type *num)
{
int i;
int len = length(head);
for (i = 0; i < len; i++)
{
if (0 == mycmp(head->next->name, old_name))
{
mycpy(head->next->name, new_name);
mycpy(head->next->num, num);
}
head = head->next;
}
return T;
}
int query_id(address head, int id)
{
int i;
if (id < 0 || id > length(head))
{
printf("out of range!\n");
return F;
}
for (i = 1; i < id; i++)
{
head = head->next;
}
printf("\tid = %d\n", id);
printf("\tname = %s\n", head->next->name);
printf("\tnum = %s\n", head->next->num);
}
int query_num(address head, Type *num)
{
printf("in\n");
int i;
int len = length(head);
for (i = 0; i < len; i++)
{
if (0 == mycmp(head->next->num, num))
{
printf("\tid = %d\n", head->next->id);
printf("\tname = %s\n", head->next->name);
printf("\tnum = %s\n", head->next->num);
}
head = head->next;
}
return T;
}
int query_name(address head, Type *name)
{
int i;
int len = length(head);
for (i = 0; i < len; i++)
{
if (0 == mycmp(head->next->name, name))
{
printf("\tid = %d\n", head->next->id);
printf("\tname = %s\n", head->next->name);
printf("\tnum = %s\n", head->next->num);
}
head = head->next;
}
return T;
}
int mycmp(char *str1, char *str2)
{
if (!str1 || !str2)
{
return;
}
while (*str1 != '\0' || *str2 != '\0')
{
if (*str1 == *str2)
{
str1++;
str2++;
}
else
{
return *str1 > *str2 ? 1 : -1;
}
}
}
void mycpy(char *s, char *p)
{
int cont = 0;
char *temp1 = s;
char *temp2 = p;
if (!s || !p)
{
return;
}
else
{
while (*temp2 != '\0')
{
*temp1 = *temp2;
temp1++;
temp2++;
cont++;
}
*temp1 = *temp2;
}
}
阅读全文
0 0
- 通讯录程序优化
- 通讯录程序
- 简单的通讯录程序
- 手机通讯录模拟程序
- C++通讯录程序
- Python通讯录小程序
- 通讯录程序(链表版)
- 小程序-通讯录
- Android程序:获取手机通讯录并用优化后的listview显示
- android 通讯录读取优化加速
- 一个简单的通讯录程序
- 通讯录管理 程序 VC源代码
- python程序通讯录V1.0
- python程序通讯录V1.1
- 小程序实现通讯录查询
- 2012-1-31--通讯录代码优化-dtsc
- 一个汇编写的通讯录程序
- 华为andriod智能手机通讯录整理程序
- eclipse搭建kafka开发环境
- 深入理解LCD之寄存器配置
- C字符串
- c# class 实现泛型的源码
- 数据库之MySQL(一)
- 通讯录程序优化
- 操作USB设备
- A
- 写给80,90后想学编程的GGMM
- 实体消歧,实体识别,实体融合,知识融合概述
- PyTorch学习系列(十六)——如何使用cuda进行训练?
- C指针与内存
- 【C++】学习笔记四十三——函数模板
- TP从一个模板页面跳到另一个控制器的模板页面