Finall...
This commit is contained in:
@@ -3,18 +3,15 @@
|
||||
//#define FILENAME "contacts.dat"
|
||||
#define LEN sizeof(struct Contact)
|
||||
|
||||
FILE * open_c(char s[])
|
||||
FILE * open_c(char s[]) //打开FILENAME文件并返回FILE*类型,打开方式取决于传入的字符串
|
||||
{
|
||||
extern char FILENAME[20];
|
||||
return fopen(FILENAME,s);
|
||||
}
|
||||
|
||||
void close_c(FILE *fp)
|
||||
{
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
struct Contact* copyc()
|
||||
|
||||
struct Contact* copyc() //打开文件,并在内存中克隆出一个相同的结构体
|
||||
{
|
||||
FILE *con;
|
||||
extern char FILENAME[20];
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include<stdio.h>
|
||||
|
||||
int count_total()
|
||||
int count_total() //从文件中获取链表,返回链表中结构体的数目
|
||||
{
|
||||
FILE *con;
|
||||
extern char FILENAME[20];
|
||||
@@ -16,7 +16,7 @@ int count_total()
|
||||
return total;
|
||||
}
|
||||
|
||||
int count_male()
|
||||
int count_male() //从文件中获取链表,返回链表中成员gender为"Male"的结构体的数目
|
||||
{
|
||||
FILE *con;
|
||||
extern char FILENAME[20];
|
||||
@@ -32,13 +32,13 @@ int count_male()
|
||||
return male;
|
||||
}
|
||||
|
||||
void print_total()
|
||||
void print_total() //以表格的形式打印出文件中结构体的信息
|
||||
{
|
||||
int total=count_total(),male=count_male();
|
||||
printf(" ___________________\n");
|
||||
printf("|%-10s|%8d|\n","Total",total);
|
||||
printf("|%-10s|%8d|\n","Female",total-male);
|
||||
printf("|%-10s|%8d|\n","Male",male);
|
||||
printf("|%-10s|%8d|\n","总数",total);
|
||||
printf("|%-10s|%8d|\n","女性",total-male);
|
||||
printf("|%-10s|%8d|\n","男性",male);
|
||||
printf("|__________|________|\n\n");
|
||||
char t[5];
|
||||
printf("Press enter to continue...\n");
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
|
||||
int delete_menu()
|
||||
{
|
||||
printf("------------DELETE------------\n\n");
|
||||
printf("0 - Back.\n");
|
||||
printf("1 - Search by name.\n");
|
||||
printf("2 - Search by tel.\n");
|
||||
printf("3 - Search by E-mail.\n");
|
||||
printf("------------删除------------\n\n"); //打印删除菜单并返回用户的选择值
|
||||
printf("0 - 返回.\n");
|
||||
printf("1 - 按姓名查找.\n");
|
||||
printf("2 - 按电话查找.\n");
|
||||
printf("3 - 按E-mail查找.\n");
|
||||
char c[10];
|
||||
do
|
||||
{
|
||||
@@ -19,21 +19,21 @@ int delete_menu()
|
||||
return c[0] - '0';
|
||||
}
|
||||
|
||||
int confirm()
|
||||
int confirm() //提示是否确认删除操作
|
||||
{
|
||||
char s[10];
|
||||
printf("Are you SURE to remove the contact?(Y/N) ");
|
||||
printf("确认删除?(Y/N) ");
|
||||
gets(s);
|
||||
if(strcmp(s,"Y")&&strcmp(s,"y"))
|
||||
{
|
||||
printf("\nDelete canceled.\n");
|
||||
printf("\n取消删除.\n");
|
||||
return 0;
|
||||
}
|
||||
else return 1;
|
||||
}
|
||||
|
||||
|
||||
struct Contact* delete_by_name(char name[],struct Contact* head)
|
||||
struct Contact* delete_by_name(char name[],struct Contact* head) //传入链表的头指针,删除name成员名与传入的字符串相同的结构体
|
||||
{
|
||||
while(head!=NULL)
|
||||
{
|
||||
@@ -41,11 +41,11 @@ struct Contact* delete_by_name(char name[],struct Contact* head)
|
||||
if(strcmp((head->next)->name,name)==0)return head;
|
||||
head=head->next;
|
||||
}
|
||||
printf("Sorry,no such content in the contacts.\n\n");
|
||||
printf("内容不存在.\n\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct Contact* delete_by_tel(char tel[],struct Contact* head)
|
||||
struct Contact* delete_by_tel(char tel[],struct Contact* head) ////传入链表的头指针,删除tel成员名与传入的字符串相同的结构体
|
||||
{
|
||||
while(head!=NULL)
|
||||
{
|
||||
@@ -53,11 +53,11 @@ struct Contact* delete_by_tel(char tel[],struct Contact* head)
|
||||
if(strcmp((head->next)->tel,tel)==0)return head;
|
||||
head=head->next;
|
||||
}
|
||||
printf("Sorry,no such content in the contacts.\n\n");
|
||||
printf("内容不存在.\n\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct Contact* delete_by_email(char email[],struct Contact* head)
|
||||
struct Contact* delete_by_email(char email[],struct Contact* head) //传入链表的头指针,删除email成员名与传入的字符串相同的结构体
|
||||
{
|
||||
while(head!=NULL)
|
||||
{
|
||||
@@ -65,11 +65,11 @@ struct Contact* delete_by_email(char email[],struct Contact* head)
|
||||
if(strcmp((head->next)->email,email)==0)return head;
|
||||
head=head->next;
|
||||
}
|
||||
printf("Sorry,no such countent in the contacts.\n\n");
|
||||
printf("内容不存在.\n\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct Contact* deletec(struct Contact *head) //·µ»ØÖ¸Õë
|
||||
struct Contact* deletec(struct Contact *head) //调用以上三个函数实现删除
|
||||
{
|
||||
int c;
|
||||
char s[30];
|
||||
@@ -77,43 +77,43 @@ struct Contact* deletec(struct Contact *head) //
|
||||
if (c == 0)return NULL;
|
||||
else if (c == 1)
|
||||
{
|
||||
printf("Please enter the name: ");
|
||||
printf("请输入姓名: ");
|
||||
gets(s);
|
||||
if(strcmp(s,head->name)!=0)return delete_by_name(s,head);
|
||||
showc(head);
|
||||
if(!confirm())return NULL;
|
||||
head=head->next;
|
||||
writec(head,"wb");
|
||||
printf("Delete successfully!\n\n");
|
||||
printf("删除成功!\n\n");
|
||||
return NULL;
|
||||
}
|
||||
else if (c == 2)
|
||||
{
|
||||
printf("Please enter the tel: ");
|
||||
printf("请输入电话: ");
|
||||
gets(s);
|
||||
if(strcmp(s,head->tel)!=0)return delete_by_tel(s,head);
|
||||
showc(head);
|
||||
if(!confirm())return NULL;
|
||||
head=head->next;
|
||||
writec(head,"wb");
|
||||
printf("Delete successfully!\n\n");
|
||||
printf("删除成功!\n\n");
|
||||
return NULL;
|
||||
}
|
||||
else if (c == 3)
|
||||
{
|
||||
printf("Please enter the e-mail: ");
|
||||
printf("请输入E-mail: ");
|
||||
gets(s);
|
||||
if(strcmp(s,head->email)!=0)return delete_by_email(s,head);
|
||||
showc(head);
|
||||
if(!confirm())return NULL;
|
||||
head=head->next;
|
||||
writec(head,"wb");
|
||||
printf("Delete successfully!\n\n");
|
||||
printf("删除成功!\n\n");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void removec()
|
||||
void removec() //将菜单界面与删除操作结合
|
||||
{
|
||||
struct Contact *head,*p1,*p2;
|
||||
head=copyc();
|
||||
@@ -124,7 +124,5 @@ void removec()
|
||||
p2=p1->next;
|
||||
(p1->next)=(p2->next);
|
||||
writec(head,"wb");
|
||||
printf("Delete successfully!\n\n");
|
||||
printf("删除成功!\n\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
|
||||
int edit_menu()
|
||||
{
|
||||
printf("-------------EDIT-------------\n\n");
|
||||
printf("0 - Back.\n");
|
||||
printf("1 - Search by name.\n");
|
||||
printf("2 - Search by tel.\n");
|
||||
printf("3 - Search by E-mail.\n");
|
||||
printf("-------------编辑-------------\n\n"); //打印编辑菜单并返回用户选择值
|
||||
printf("0 - 返回.\n");
|
||||
printf("1 - 按姓名查找.\n");
|
||||
printf("2 - 按电话查找.\n");
|
||||
printf("3 - 按E-mail查找.\n");
|
||||
char c[10];
|
||||
do
|
||||
{
|
||||
@@ -16,14 +16,14 @@ int edit_menu()
|
||||
return c[0] - '0';
|
||||
}
|
||||
|
||||
int edit_menu2()
|
||||
int edit_menu2() //打印编辑二级菜单并返回用户选择值
|
||||
{
|
||||
printf("------------EDIT------------\n\n");
|
||||
printf("0 - Cancel.\n");
|
||||
printf("1 - Edit name.\n");
|
||||
printf("2 - Edit gender.\n");
|
||||
printf("3 - Edit tel.\n");
|
||||
printf("4 - Edit e-mail.\n");
|
||||
printf("------------编辑------------\n\n");
|
||||
printf("0 - 取消.\n");
|
||||
printf("1 - 更改姓名.\n");
|
||||
printf("2 - 更改性别.\n");
|
||||
printf("3 - 更改电话.\n");
|
||||
printf("4 - 更改E-mail.\n");
|
||||
char c[10];
|
||||
do
|
||||
{
|
||||
@@ -34,40 +34,40 @@ int edit_menu2()
|
||||
return c[0] - '0';
|
||||
}
|
||||
|
||||
struct Contact* edit_by_name(char name[],struct Contact* head)
|
||||
struct Contact* edit_by_name(char name[],struct Contact* head) //传入链表头指针和字符串,找到文件中name成员的值与字符串相同的结构体,并返回其地址,若不存在则返回NULL
|
||||
{
|
||||
while(head!=NULL)
|
||||
{
|
||||
if(strcmp(head->name,name)==0)return head;
|
||||
head=head->next;
|
||||
}
|
||||
printf("Sorry,no such content in the contacts.\n\n");
|
||||
printf("内容不存在.\n\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct Contact* edit_by_tel(char tel[],struct Contact* head)
|
||||
struct Contact* edit_by_tel(char tel[],struct Contact* head) //传入链表头指针和字符串,找到文件中tel成员的值与字符串相同的结构体,并返回其地址,若不存在则返回NULL
|
||||
{
|
||||
while(head!=NULL)
|
||||
{
|
||||
if(strcmp(head->tel,tel)==0)return head;
|
||||
head=head->next;
|
||||
}
|
||||
printf("Sorry,no such content in the contacts.\n\n");
|
||||
printf("内容不存在.\n\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct Contact* edit_by_email(char email[],struct Contact* head)
|
||||
struct Contact* edit_by_email(char email[],struct Contact* head) //传入链表头指针和字符串,找到文件中email成员的值与字符串相同的结构体,并返回其地址,若不存在则返回NULL
|
||||
{
|
||||
while(head!=NULL)
|
||||
{
|
||||
if(strcmp(head->email,email)==0)return head;
|
||||
head=head->next;
|
||||
}
|
||||
printf("Sorry,no such content in the contacts.\n\n");
|
||||
printf("内容不存在.\n\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct Contact* editc(struct Contact* head) //·µ»ØÖ¸Õë
|
||||
struct Contact* editc(struct Contact* head) //调用以上函数实现编辑操作
|
||||
{
|
||||
int c;
|
||||
char s[30];
|
||||
@@ -75,25 +75,25 @@ struct Contact* editc(struct Contact* head) //
|
||||
if (c == 0)return NULL;
|
||||
else if (c == 1)
|
||||
{
|
||||
printf("Please enter the name: ");
|
||||
printf("请输入姓名:");
|
||||
gets(s);
|
||||
return edit_by_name(s,head);
|
||||
}
|
||||
else if (c == 2)
|
||||
{
|
||||
printf("Please enter the tel: ");
|
||||
printf("请输入电话:");
|
||||
gets(s);
|
||||
return edit_by_tel(s,head);
|
||||
}
|
||||
else if (c == 3)
|
||||
{
|
||||
printf("Please enter the e-mail: ");
|
||||
printf("请输入E-mail: ");
|
||||
gets(s);
|
||||
return edit_by_email(s,head);
|
||||
}
|
||||
}
|
||||
|
||||
void edit()
|
||||
void edit() //将删除功能与菜单界面结合
|
||||
{
|
||||
struct Contact *head,*p;
|
||||
char s[30];
|
||||
@@ -106,11 +106,11 @@ void edit()
|
||||
if(c==0)return;
|
||||
else if(c==1)
|
||||
{
|
||||
printf("Enter the new name:");
|
||||
printf("请输入新的姓名:");
|
||||
gets(s);
|
||||
strcpy(p->name,s);
|
||||
writec(head,"wb");
|
||||
printf("Update successfully!\n\n");
|
||||
printf("更改成功!\n\n");
|
||||
char t[5];
|
||||
printf("Press enter to continue...");
|
||||
gets(t);
|
||||
@@ -119,35 +119,35 @@ void edit()
|
||||
{
|
||||
do
|
||||
{
|
||||
printf("Enter the new gender [0(Male)/1(Female)]:");
|
||||
printf("请输入新的性别[0(男)/1(女)]:");
|
||||
gets(s);
|
||||
}while(strcmp(s,"0")&&strcmp(s,"1"));
|
||||
if(strcmp(s,"0")==0)strcpy(p->gender,"Male");
|
||||
else if(strcmp(s,"1")==0)strcpy(p->gender,"Female");
|
||||
if(strcmp(s,"0")==0)strcpy(p->gender,"男");
|
||||
else if(strcmp(s,"1")==0)strcpy(p->gender,"女");
|
||||
writec(head,"wb");
|
||||
printf("Update successfully!\n\n");
|
||||
printf("更改成功!\n\n");
|
||||
char t[5];
|
||||
printf("Press enter to continue...");
|
||||
gets(t);
|
||||
}
|
||||
else if(c==3)
|
||||
{
|
||||
printf("Enter the new tel:");
|
||||
printf("请输入新的电话:");
|
||||
gets(s);
|
||||
strcpy(p->tel,s);
|
||||
writec(head,"wb");
|
||||
printf("Update successfully!\n\n");
|
||||
printf("更改成功!\n\n");
|
||||
char t[5];
|
||||
printf("Press enter to continue...");
|
||||
gets(t);
|
||||
}
|
||||
else if(c==4)
|
||||
{
|
||||
printf("Enter the new e-mail:");
|
||||
printf("请输入新的E-mail:");
|
||||
gets(s);
|
||||
strcpy(p->email,s);
|
||||
writec(head,"wb");
|
||||
printf("Update successfully!\n\n");
|
||||
printf("更改成功!\n\n");
|
||||
char t[5];
|
||||
printf("Press enter to continue...");
|
||||
gets(t);
|
||||
|
||||
@@ -13,21 +13,21 @@
|
||||
|
||||
char FILENAME[20];
|
||||
|
||||
void print_menu()
|
||||
void print_menu() //打印主菜单
|
||||
{
|
||||
printf("------------MENU------------\n\n");
|
||||
printf("Contacts\n\n");
|
||||
printf("0 - Quit.\n");
|
||||
printf("1 - Add contact(s).\n");
|
||||
printf("2 - Remove a contact.\n");
|
||||
printf("3 - Edit a contact.\n");
|
||||
printf("4 - Search a contact.\n");
|
||||
printf("5 - Sort contacts.\n");
|
||||
printf("6 - Count contacts.\n");
|
||||
printf("7 - Show contacts.\n");
|
||||
printf("------------通讯录------------\n\n");
|
||||
//printf("Contacts\n\n");
|
||||
printf("0 - 退出.\n");
|
||||
printf("1 - 添加.\n");
|
||||
printf("2 - 删除.\n");
|
||||
printf("3 - 编辑.\n");
|
||||
printf("4 - 查找.\n");
|
||||
printf("5 - 排序.\n");
|
||||
printf("6 - 统计.\n");
|
||||
printf("7 - 显示.\n");
|
||||
}
|
||||
|
||||
int selectc()
|
||||
int selectc() //向用户询问选项并将其返回
|
||||
{
|
||||
print_menu();
|
||||
char c[10];
|
||||
@@ -39,7 +39,7 @@ int selectc()
|
||||
return c[0]-'0';
|
||||
}
|
||||
|
||||
void func()
|
||||
void func() //调用各项功能
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
@@ -53,15 +53,12 @@ void func()
|
||||
else if(choice==4)
|
||||
{
|
||||
showc(findc());
|
||||
//system("pause");
|
||||
//sleep(2);
|
||||
char t[5];
|
||||
printf("Press enter to continue...");
|
||||
printf("按回车继续...");
|
||||
gets(t);
|
||||
}
|
||||
else if(choice==5)sortc();
|
||||
else if(choice==5)sortsort();
|
||||
else if(choice==6)print_total();
|
||||
else if(choice==7)readc();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
|
||||
//定义用户结构体
|
||||
struct usr
|
||||
{
|
||||
char username[20];
|
||||
@@ -9,19 +11,20 @@ struct usr
|
||||
|
||||
char FILENAME[20];
|
||||
|
||||
//登陆函数,登陆成功返回1,否则返回0
|
||||
int sign_in()
|
||||
{
|
||||
char usr[20],password[20];
|
||||
printf("Username: ");
|
||||
printf("用户名:");
|
||||
gets(usr);
|
||||
printf("Password: ");
|
||||
printf("密码:");
|
||||
gets(password);
|
||||
|
||||
FILE* info;
|
||||
info=fopen("log.dat","rb");
|
||||
if(info==NULL)
|
||||
{
|
||||
printf("Please sign up first.\n\n");
|
||||
printf("请先注册\n\n");
|
||||
return 0;
|
||||
}
|
||||
struct usr* p;
|
||||
@@ -35,17 +38,18 @@ int sign_in()
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
printf("No such account or wrong password.\n\n");
|
||||
printf("密码错误.\n\n");
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
//注册函数
|
||||
int sign_up()
|
||||
{
|
||||
char username[20],password[20];
|
||||
printf("Username: ");
|
||||
printf("用户名:");
|
||||
gets(username);
|
||||
printf("Password: ");
|
||||
printf("密码:");
|
||||
gets(password);
|
||||
struct usr newone;
|
||||
strcpy(newone.username,username);
|
||||
@@ -60,7 +64,7 @@ int sign_up()
|
||||
{
|
||||
if(strcmp(p->username,newone.username)==0)
|
||||
{
|
||||
printf("Account already exists.\n\n");
|
||||
printf("用户名已存在\n\n");
|
||||
fclose(info);
|
||||
return 0;
|
||||
}
|
||||
@@ -73,11 +77,12 @@ int sign_up()
|
||||
}
|
||||
|
||||
|
||||
//登陆和注册
|
||||
int login()
|
||||
{
|
||||
printf("0 - Exit.\n");
|
||||
printf("1 - Sign in.\n");
|
||||
printf("2 - Sign up.\n");
|
||||
printf("0 - 退出.\n");
|
||||
printf("1 - 登陆.\n");
|
||||
printf("2 - 注册.\n");
|
||||
char c[6];
|
||||
char ch[]="012";
|
||||
do
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
|
||||
void print_title()
|
||||
void print_title() //打印表格的第一行
|
||||
{
|
||||
printf(" ____________________________________________________________________\n");
|
||||
printf("|%-10s|%-10s|%-15s|%-30s|\n","Name","Gender","Tel","E-mail");
|
||||
printf("|%-10s|%-10s|%-15s|%-30s|\n","姓名","性别","电话","E-mail");
|
||||
}
|
||||
void readc()
|
||||
void readc() //从文件中读取结构体,并以表格的形式打印出来
|
||||
{
|
||||
FILE *con;
|
||||
struct Contact *p;
|
||||
@@ -13,7 +13,7 @@ void readc()
|
||||
con=fopen(FILENAME,"rb");
|
||||
if(con==NULL)
|
||||
{
|
||||
printf("Sorry,your contacts are empty.\n\n");
|
||||
printf("请先建立通讯录.\n\n");
|
||||
return ;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ void readc()
|
||||
printf("|__________|__________|_______________|______________________________|\n");
|
||||
printf("\n");
|
||||
char t[5];
|
||||
printf("Press enter to continue...\n");
|
||||
printf("按回车键继续...\n");
|
||||
gets(t);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<stdlib.h>
|
||||
#define CHOICES "0123"
|
||||
|
||||
int search_menu()
|
||||
int search_menu() //打印搜索菜单,返回用户的选择值
|
||||
{
|
||||
printf("------------SEARCH------------\n\n");
|
||||
printf("0 - Back.\n");
|
||||
printf("1 - Search by name.\n");
|
||||
printf("2 - Search by tel.\n");
|
||||
printf("3 - Search by E-mail.\n");
|
||||
printf("------------查找------------\n\n");
|
||||
printf("0 - 返回.\n");
|
||||
printf("1 - 按姓名查找.\n");
|
||||
printf("2 - 按电话查找.\n");
|
||||
printf("3 - 按E-mail查找.\n");
|
||||
char c[10];
|
||||
do
|
||||
{
|
||||
@@ -20,40 +19,40 @@ int search_menu()
|
||||
return c[0] - '0';
|
||||
}
|
||||
|
||||
struct Contact* search_by_name(char name[],struct Contact* head)
|
||||
struct Contact* search_by_name(char name[],struct Contact* head) //传入链表头指针和字符串,找到文件中name成员的值与字符串相同的结构体,并返回其地址,若不存在则返回NULL
|
||||
{
|
||||
while(head!=NULL)
|
||||
{
|
||||
if(strcmp(head->name,name)==0)return head;
|
||||
head=head->next;
|
||||
}
|
||||
printf("Sorry,no such content in the contacts.\n\n");
|
||||
printf("\n内容不存在.\n\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct Contact* search_by_tel(char tel[],struct Contact* head)
|
||||
struct Contact* search_by_tel(char tel[],struct Contact* head) //传入链表头指针和字符串,找到文件中tel成员的值与字符串相同的结构体,并返回其地址,若不存在则返回NULL
|
||||
{
|
||||
while(head!=NULL)
|
||||
{
|
||||
if(strcmp(head->tel,tel)==0)return head;
|
||||
head=head->next;
|
||||
}
|
||||
printf("Sorry,no such content in the contacts.\n\n");
|
||||
printf("\n内容不存在.\n\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct Contact* search_by_email(char email[],struct Contact* head)
|
||||
struct Contact* search_by_email(char email[],struct Contact* head) //传入链表头指针和字符串,找到文件中email成员的值与字符串相同的结构体,并返回其地址,若不存在则返回NULL
|
||||
{
|
||||
while(head!=NULL)
|
||||
{
|
||||
if(strcmp(head->email,email)==0)return head;
|
||||
head=head->next;
|
||||
}
|
||||
printf("Sorry,no such content in the contacts.\n\n");
|
||||
printf("\n内容不存在.\n\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct Contact* findc() //·µ»ØÖ¸Õë
|
||||
struct Contact* findc() //调用以上四个函数实现查找功能
|
||||
{
|
||||
int c;
|
||||
char s[30];
|
||||
@@ -63,34 +62,33 @@ struct Contact* findc() //
|
||||
if (c == 0)return NULL;
|
||||
else if (c == 1)
|
||||
{
|
||||
printf("Please enter the name: ");
|
||||
printf("请输入姓名:");
|
||||
gets(s);
|
||||
return search_by_name(s,head);
|
||||
}
|
||||
else if (c == 2)
|
||||
{
|
||||
printf("Please enter the tel: ");
|
||||
printf("请输入电话:");
|
||||
gets(s);
|
||||
return search_by_tel(s,head);
|
||||
}
|
||||
else if (c == 3)
|
||||
{
|
||||
printf("Please enter the e-mail: ");
|
||||
printf("请输入E-mail: ");
|
||||
gets(s);
|
||||
return search_by_email(s,head);
|
||||
}
|
||||
}
|
||||
|
||||
void showc(struct Contact* p)
|
||||
void showc(struct Contact* p) //传入结构体指针,将该指针指向的结构体的成员以表格的形式打印
|
||||
{
|
||||
if(p!=NULL)
|
||||
{
|
||||
printf(" _________________________________________\n");
|
||||
printf("|%-10s|%-30s|\n","Name",p->name);
|
||||
printf("|%-10s|%-30s|\n","Gender",p->gender);
|
||||
printf("|%-10s|%-30s|\n","Tel",p->tel);
|
||||
printf("|%-10s|%-30s|\n","姓名",p->name);
|
||||
printf("|%-10s|%-30s|\n","性别",p->gender);
|
||||
printf("|%-10s|%-30s|\n","电话",p->tel);
|
||||
printf("|%-10s|%-30s|\n","E-mail",p->email);
|
||||
printf("|__________|______________________________|\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,31 @@
|
||||
#include<stdio.h>
|
||||
|
||||
|
||||
int print_sort()
|
||||
int sort_menu()
|
||||
{
|
||||
printf("0 - 返回\n");
|
||||
printf("1 - 按姓名排序.\n");
|
||||
printf("2 - 按电话排序.\n");
|
||||
char c[10];
|
||||
do
|
||||
{
|
||||
printf("\nChoice:");
|
||||
gets(c);
|
||||
} while (strlen(c)!=1||strchr("012", c[0])==NULL);
|
||||
printf("\n");
|
||||
return c[0] - '0';
|
||||
}
|
||||
|
||||
int print_sort() //确认是否倒序排序
|
||||
{
|
||||
char c[10];
|
||||
printf("Reverse?(Y/N):");
|
||||
printf("倒序?(Y/N):");
|
||||
gets(c);
|
||||
if(strcmp(c,"y")==0||strcmp(c,"Y")==0)return 1;
|
||||
else return 0;
|
||||
}
|
||||
|
||||
void sortc()
|
||||
void sortc() //排序功能
|
||||
{
|
||||
int tot=count_total();
|
||||
FILE *con=NULL;
|
||||
@@ -63,3 +78,65 @@ void sortc()
|
||||
printf("\n排序完成.\n\n");
|
||||
}
|
||||
|
||||
|
||||
void sorttel() //排序功能
|
||||
{
|
||||
int tot=count_total();
|
||||
FILE *con=NULL;
|
||||
con=open_c("rb");
|
||||
struct Contact c[tot];
|
||||
struct Contact tem;
|
||||
int i,j;
|
||||
for(i=0;i<tot;i++)
|
||||
{
|
||||
fread(c+i,LEN,1,con);
|
||||
}
|
||||
fclose(con);
|
||||
con=NULL;
|
||||
if(print_sort()==0)
|
||||
{
|
||||
for(i=0;i<tot;i++)
|
||||
{
|
||||
for(j=i+1;j<tot;j++)
|
||||
{
|
||||
if(strcmp(c[i].tel,c[j].tel)>0)
|
||||
{
|
||||
tem=c[i];
|
||||
c[i]=c[j];
|
||||
c[j]=tem;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(i=0;i<tot;i++)
|
||||
{
|
||||
for(j=i+1;j<tot;j++)
|
||||
{
|
||||
if(strcmp(c[i].tel,c[j].tel)<0)
|
||||
{
|
||||
tem=c[i];
|
||||
c[i]=c[j];
|
||||
c[j]=tem;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
con=open_c("wb");
|
||||
for(i=0;i<tot;i++)
|
||||
{
|
||||
fwrite(c+i,LEN,1,con);
|
||||
}
|
||||
fclose(con);
|
||||
printf("\n排序完成.\n\n");
|
||||
}
|
||||
|
||||
void sortsort()
|
||||
{
|
||||
int i;
|
||||
i=sort_menu();
|
||||
if(i==0)return;
|
||||
else if(i==1)sortc();
|
||||
else if(i==2)sorttel();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include<stdio.h>
|
||||
|
||||
|
||||
struct Contact
|
||||
struct Contact //¶¨Òå½á¹¹Ìå
|
||||
{
|
||||
char name[10];
|
||||
char gender[10];
|
||||
@@ -9,4 +8,3 @@ struct Contact
|
||||
char email[30];
|
||||
struct Contact *next;
|
||||
};
|
||||
|
||||
|
||||
@@ -6,24 +6,24 @@
|
||||
|
||||
|
||||
|
||||
struct Contact* add()
|
||||
struct Contact* add() //在内存中构造出动态链表,并向其中添加元素,返回新链表的头指针
|
||||
{
|
||||
char sex[5];
|
||||
struct Contact *head=NULL,*p1,*p2;
|
||||
p1=(struct Contact*)malloc(sizeof(struct Contact));
|
||||
p2=p1;
|
||||
printf("*Input will be finished when name is 0.\n\n");
|
||||
printf("Name:");
|
||||
printf("姓名:");
|
||||
gets(p1->name);
|
||||
if(strcmp(p1->name,"0")==0)return NULL;
|
||||
do
|
||||
{
|
||||
printf("Gender[0(Male)/1(Female)]:");
|
||||
printf("性别[0(男)/1(女)]:");
|
||||
gets(sex);
|
||||
}while(strcmp(sex,"0")&&strcmp(sex,"1"));
|
||||
if(strcmp(sex,"0")==0)strcpy(p1->gender,"Male");
|
||||
else strcpy(p1->gender,"Female");
|
||||
printf("Tel:");
|
||||
if(strcmp(sex,"0")==0)strcpy(p1->gender,"男");
|
||||
else strcpy(p1->gender,"女");
|
||||
printf("电话:");
|
||||
gets(p1->tel);
|
||||
printf("E-mail:");
|
||||
gets(p1->email);
|
||||
@@ -36,17 +36,17 @@ struct Contact* add()
|
||||
else p2->next=p1;
|
||||
p2=p1;
|
||||
p1=(struct Contact*)malloc(sizeof(struct Contact));
|
||||
printf("Name:");
|
||||
printf("姓名:");
|
||||
gets(p1->name);
|
||||
if(strcmp(p1->name,"0")==0)break;
|
||||
do
|
||||
{
|
||||
printf("Gender[0(Male)/1(Female)]:");
|
||||
printf("性别[0(男)/1(女)]:");
|
||||
gets(sex);
|
||||
}while(strcmp(sex,"0")&&strcmp(sex,"1"));
|
||||
if(strcmp(sex,"0")==0)strcpy(p1->gender,"Male");
|
||||
else strcpy(p1->gender,"Female");
|
||||
printf("Tel:");
|
||||
if(strcmp(sex,"0")==0)strcpy(p1->gender,"男");
|
||||
else strcpy(p1->gender,"女");
|
||||
printf("电话:");
|
||||
gets(p1->tel);
|
||||
printf("E-mail:");
|
||||
gets(p1->email);
|
||||
@@ -56,7 +56,7 @@ struct Contact* add()
|
||||
return head;
|
||||
}
|
||||
|
||||
void writec(struct Contact* head,char s[])
|
||||
void writec(struct Contact* head,char s[]) // 传入头指针,将结构体数据写入文件中,写入方式由传入的字符串决定
|
||||
{
|
||||
FILE *con;
|
||||
extern char FILENAME[20];
|
||||
@@ -68,7 +68,4 @@ void writec(struct Contact* head,char s[])
|
||||
}
|
||||
fclose(con);
|
||||
printf("\n");
|
||||
//sleep(1);
|
||||
char t[5];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user