项目:基于链表的通信录管理
意义:对于一个通信录来说,要管理联系人的信息,包括编号,姓名,性别,电话。开发其系统主要为了帮助用户提高通讯录有管理效率,节约资源,提高信息的精确度
模块:
一级菜单内容
1> 注册模块:完成用户信息的注册用于登录管理系统,将注册信息存入文件
2> 登录模块:使用输入的登录账号和密码与文件存储信息对比
3> 退出系统
二级菜单内容:
3> 创建模块create:创建链表完成对通信录的存储
4> 添加数据add:添加通信录的信息放入链表中
5> 查找信息find:可以通过姓名进行查找
6> 修改信息update:可以修改联系人信息并保存,修改联系人信息有包括了对联系人编号,姓名,性别,电话号码的分别修改,也可以同时对编号,姓名,性别,电话号码修改;
7> 删除信息delete:可根据输入的姓名进行删除
8> 插入信息insert:将给定的信息以及插入位置信息完成插入
9> 展示信息show:将通讯录的所有信息进行展示
10> 导出信息export:将通讯录信息导出到文件中
11> 按照姓名将通讯录排序
12> 返回上一级
辅助功能:
13> 初始化链表init:将链表进行初始化
14> 获取链表长度getLength:
15> 统计性别;
project1.h
#ifndef __PROJECT1_H__
#define __PROJECT1_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct User{
char username[20];
char password[20];
}user;
typedef struct userNode{
union{
int len;
user data;
};
struct userNode *next;
}*userlist;
typedef struct Contact{
int id;
char name[20];
char gender;
int phone;
}contact;
typedef struct contactNode{
union{
int len;
contact data;
};
struct contactNode *next;
}*contactlist;
userlist create_user_list();
userlist create_user_node();
int insert_user_info(userlist,char *,char *);
void load_info_into_list(userlist);
int save_info_into_file(userlist,char *,char *);
int delete_user_info(userlist);
void output_userlist(userlist);
int if_log_in(userlist,char *,char *);
int find_user_in_file(char *,char *);
userlist free_user_space(userlist);
void show_first_menu(userlist,userlist);
void show_second_menu(userlist);
contactlist create_contact_list();
contactlist create_contact_node(contactlist);
int insert_contact(contactlist,int, char *,char,int);
int insert_contact_by_position(contactlist,int,int,char *,char ,int );
contactlist search_by_name(contactlist,char *);
int update_contact_by_name(contactlist,char *);
int delete_contact_by_name(contactlist,char *);
int sort_contact(contactlist);
contactlist free_contact_space(contactlist);
void load_info_into_contactlist(contactlist);
void save_contact_into_file(contactlist);
void output_contactlist(contactlist);
void save_all_into_file(userlist);
#endif
project1_user.c
#include "project1.h"
userlist create_user_list(){
userlist head = (userlist)malloc(sizeof(struct userNode));
if( head == NULL){
printf("User creation failed.\n");
return NULL;
}
head->len = 0;
head->next = NULL;
return head;
}
userlist create_user_node(){
userlist node = (userlist)malloc(sizeof(struct userNode));
if (node == NULL){
printf("User node creation failed.\n");
return NULL;
}
strcpy(node->data.username,"");
strcpy(node->data.password,"");
node->next = NULL;
return node;
}
int insert_user_info(userlist head,char *name, char *pwd){
if(head == NULL){
printf("The user list doesn't exist.\n");
return -1;
}
userlist node = create_user_node();
if(node == NULL){
printf("User info insertion failed");
return -1;
}
strcpy(node->data.username,name);
strcpy(node->data.password,pwd);
node->next = head->next;
head->next = node;
head->len++;
return 0;
}
int delete_user_info(userlist head){
if(head == NULL || head->next == NULL){
printf("User list is empty or doesn't exist\n");
return -1;
}
userlist p = head->next;
head->next = p->next;
free(p);
p = NULL;
head->len--;
return 0;
}
void output_userlist(userlist head){
if(head == NULL || head->next == NULL){
printf("User list is empty or doesn't exist\n");
return;
}
userlist p = head;
while(p->next){
p = p->next;
printf("username: %-8s\n",p->data.username);
printf("password: %-8s\n",p->data.password);
}
}
int if_log_in(userlist head,char *name,char *pwd){
if(head == NULL || head->next == NULL){
printf("Failed to load user info");
return -1;
}
userlist p = head;
while(p->next){
p = p->next;
if(strcmp(p->data.username,name) == 0){
if(strcmp(p->data.password,pwd) == 0){
printf("Welcome back, %s.\n",p->data.username);
userlist q = head;
while(q->next != p){
q = q->next;
}
q->next = p->next;
free(p);
p = NULL;
head->len--;
save_all_into_file(head);
return 0;
}else{
printf("Password is incorrect.\n");
return -1;
}
}
}
printf("The user doesn't exist.\n");
return -1;
}
int find_user_in_file(char *uname,char *upwd){
FILE *fp;
fp = fopen("user.txt","r");
if(fp == NULL){
printf("File not found.\n");
return -1;
}
char s1[20],s2[20];
while(fscanf(fp,"%s %s",s1,s2)!=EOF){
if(strcmp(uname,s1)==0){
return 0;
}
}
fclose(fp);
fp = NULL;
return -1;
}
userlist free_user_space(userlist head){
if(head == NULL){
return NULL;
}
userlist p = head->next;
int num = head->len;
for(int i = 0; i < num;i++){
delete_user_info(head);
}
free(head);
head = NULL;
return head;
}
void load_info_into_list(userlist head){
if(head == NULL){
printf("The user list doesn't exist.\n");
return;
}
FILE *fp;
fp = fopen("user.txt","r");
if(fp == NULL){
printf("File not found.\n");
return;
}
char temp1[20];
char temp2[20];
while(fscanf(fp,"%s %s",temp1,temp2) != EOF){
insert_user_info(head,temp1,temp2);
}
//output_userlist(head);
fclose(fp);
fp = NULL;
}
int save_info_into_file(userlist head,char *s1,char *s2){
FILE *fp;
fp = fopen("user.txt","a");
if(fp == NULL){
printf("File not found.\n");
return -1;
}
int ifExist = find_user_in_file(s1,s2);
if(ifExist == 0){
printf("User already exist.\n");
return -1;
}
fprintf(fp,"%s %s\n",s1,s2);
insert_user_info(head,s1,s2);
fclose(fp);
fp = NULL;
return 0;
}
void save_all_into_file(userlist head){
FILE *fp;
fp = fopen("user.txt","w");
userlist p = head;
while(p->next){
p = p -> next;
fprintf(fp,"%s %s\n",p->data.username,p->data.password);
}
fclose(fp);
fp = NULL;
}
project1_contact.c
#include "project1.h"
contactlist create_contact_list(){
contactlist head = (contactlist)malloc(sizeof(struct contactNode));
if( head == NULL){
printf("Contact list creation failed.\n");
return NULL;
}
head->len = 0;
head->next = NULL;
return head;
}
contactlist create_contact_node(contactlist head){
contactlist node = (contactlist)malloc(sizeof(struct contactNode));
if( node == NULL){
printf("Contact node creation failed.\n");
return NULL;
}
node->data.id = 0;
strcpy(node->data.name,"");
node->data.gender = ' ';
node->data.phone = 0;
node->next = NULL;
return node;
}
int insert_contact(contactlist head,int contactId,char *contactName,char contactGender,int contactPhone){
if(head == NULL){
printf("Contact list doesn't exist.\n");
return -1;
}
contactlist newNode = create_contact_node(head);
if(newNode == NULL){
printf("New contact node creation failed.\n");
return -1;
}
newNode->data.id = contactId;
strcpy(newNode->data.name,contactName);
newNode->data.gender = contactGender;
newNode->data.phone = contactPhone;
contactlist p = head;
while(p->next){
p = p->next;
}
newNode->next = p->next;
p->next = newNode;
head->len++;
return 0;
}
int delete_contact(contactlist head){
if(head == NULL || head->next == NULL){
printf("Contact list is empty or doesn't exist.\n");
return -1;
}
contactlist p = head->next;
head->next = p->next;
free(p);
p = NULL;
head->len--;
return 0;
}
int insert_contact_by_position(contactlist head,int pos,int contactId,char *contactName,char contactGender,int contactPhone){
if(head == NULL || pos < 1 || pos > head->len + 1){
printf("Invalid for insertion\n");
return -1;
}
contactlist newNode = create_contact_node(head);
if(newNode == NULL){
printf("New contact node creation failed.\n");
return -1;
}
newNode->data.id = contactId;
strcpy(newNode->data.name,contactName);
newNode->data.gender = contactGender;
newNode->data.phone = contactPhone;
contactlist p = head;
for(int i = 0;i < pos -1;i++){
p = p->next;
}
newNode->next = p->next;
p->next = newNode;
head->len++;
return 0;
}
contactlist search_by_name(contactlist head,char *s){
if(head == NULL || head->next == NULL){
printf("The contact list doesn't exist or is empty\n");
return NULL;
}
contactlist p = head;
while(p->next){
p = p->next;
if(strcmp(p->data.name,s) == 0){
printf("User name %s is found.\n",s);
printf("ID is %d\n",p->data.id);
printf("Gender is %c\n",p->data.gender);
printf("Phone number is %d\n",p->data.phone);
return p;
}
}
printf("The user doesn't exist.\n");
return NULL;
}
int update_contact_by_name(contactlist head,char *s){
contactlist node = search_by_name(head,s);
if(node == NULL){
return -1;
}else{
int tid;
char tg;
int tphone;
printf("Please enter an ID:\n");
scanf("%d",&tid);
printf("Please enter a name:\n");
scanf("%s",s);
printf("Please enter a gender.\n");
scanf("%c",&tg);
printf("Please enter a phone number:\n");
scanf("%d",&tphone);
node->data.id = tid;
strcpy(node->data.name,s);
node->data.gender = tg;
node->data.phone = tphone;
printf("Record saved successfully\n");
return 0;
}
}
int delete_contact_by_name(contactlist head,char *s){
contactlist node = search_by_name(head,s);
if(node == NULL){
return -1;
}else{
contactlist p = head;
while(p->next!= node){
p = p->next;
}
p->next = node->next;
free(node);
node = NULL;
printf("The contact is deleted.\n");
return 0;
}
}
void output_contactlist(contactlist head){
if(head == NULL || head->next == NULL){
printf("The contact list is empty or doesn't exist.\n");
return;
}
contactlist p = head;
while(p->next){
p = p->next;
printf("Id is:%d\n",p->data.id);
printf("Name is:%s\n",p->data.name);
printf("Gender is:%c\n",p->data.gender);
printf("Phone number is:%d\n",p->data.phone);
}
}
void load_info_into_contactlist(contactlist head){
if(head == NULL){
printf("The contact list doesn't exist.\n");
return;
}
FILE *fp;
fp = fopen("contact.txt","r");
if(fp == NULL){
printf("File not found.\n");
return;
}
int tid;
char tname[20];
char tg;
int tphone;
while(fscanf(fp,"%d %s %c %d",&tid,tname,&tg,&tphone) != EOF){
insert_contact(head,tid,tname,tg,tphone);
}
fclose(fp);
fp = NULL;
printf("Successfully loaded.\n");
}
void save_contact_into_file(contactlist head){
if(head == NULL || head->next == NULL){
printf("The contact list doesn't exist or is empty.\n");
return;
}
FILE *fp;
fp = fopen("contact.txt","w");
if(fp == NULL){
printf("File not found.\n");
return;
}
contactlist p = head;
while(p->next){
p = p->next;
fprintf(fp,"%d %s %c %d\n",p->data.id,p->data.name,p->data.gender,p->data.phone);
}
fclose(fp);
fp = NULL;
printf("Successfully saved.\n");
}
int sort_contact(contactlist head){
if(head == NULL || head->len < 2){
printf("The list is sorted or doesn't exist.\n");
return -1;
}
int num = head->len;
contactlist p = head;
for(int i = 1; i < num;i++){
int count = 0;
p = p->next;
contactlist q = head;
for(int j = 0;j < num - i;j++){
q = q -> next;
if(strcmp(q->data.name,q->next->data.name)>0){
struct Contact temp = q->data;
q->data = q->next->data;
q->next->data = temp;
count++;
}
}
if(count == 0){
break;
}
}
printf("The list is sorted.\n");
return 0;
}
contactlist free_contact_space(contactlist head){
if(head == NULL){
return NULL;
}
int num = head->len;
for (int i = 0; i < num; i++){
delete_contact(head);
}
free(head);
head = NULL;
return head;
}
project1.c
#include "project1.h"
void show_second_menu(userlist temp){
getchar();
char input;
int ifQuit = 0;
int pos;
int ID;
char str[20];
char g;
int pNumber;
contactlist myContactList = create_contact_list();
while(1){
printf("Please select an option to continue:\n");
printf("1.Create module.\n");
printf("2.Insert info.\n");
printf("3.Search info by name.\n");
printf("4.Update info by name.\n");
printf("5.Delete info by name.\n");
printf("6.Insert info by position.\n");
printf("7.Show info.\n");
printf("8.Export info into file.\n");
printf("9.Sort info by name.\n");
printf("0.Back to previous menu.\n");
scanf("%c",&input);
switch(input)
{
case '1':
load_info_into_contactlist(myContactList);
getchar();
break;
case '2':
printf("Please enter an ID:\n");
scanf("%d",&ID);
printf("PLease enter a name:\n");
scanf("%s",str);
printf("Please enter gender:\n");
scanf(" %c",&g);
printf("Please enter phone number:\n");
scanf("%d",&pNumber);
int s1 = insert_contact(myContactList,ID,str,g,pNumber);
if(s1 == 0){
printf("Inserted successfully\n");
}
getchar();
break;
case '3':
printf("Please enter a name:\n");
scanf("%s",str);
search_by_name(myContactList,str);
getchar();
break;
case '4':
printf("Please enter a name:\n");
scanf("%s",str);
update_contact_by_name(myContactList,str);
getchar();
break;
case '5':
printf("PLease enter a name:\n");
scanf("%s",str);
delete_contact_by_name(myContactList,str);
getchar();
break;
case '6':
printf("Please enter a position:\n");
scanf("%d",&pos);
printf("Please enter an ID:\n");
scanf("%d",&ID);
printf("PLease enter a name:\n");
scanf("%s",str);
printf("Please enter gender:\n");
scanf(" %c",&g);
printf("Please enter phone number:\n");
scanf("%d",&pNumber);
int s2 = insert_contact_by_position(myContactList,pos,ID,str,g,pNumber);
if(s2 == 0){
printf("Inserted successfully\n");
}
getchar();
break;
case '7':
output_contactlist(myContactList);
getchar();
break;
case '8':
save_contact_into_file(myContactList);
getchar();
break;
case '9':
sort_contact(myContactList);
getchar();
break;
case '0':
ifQuit = 1;
save_all_into_file(temp);
break;
default:
printf("Invalid choice,please re-enter.\n");
getchar();
break;
}
if(ifQuit){
break;
}
}
myContactList = free_contact_space(myContactList);
return;
}
void show_first_menu(userlist head,userlist temp){
char input;
char temp1[20];
char temp2[20];
while(1){
int ifQuit = 0;
printf("*******User Menu*******\n");
printf("1.Registration.\n");
printf("2.Log in.\n");
printf("3.Exit.\n");
scanf("%c",&input);
switch (input)
{
case '1':printf("*******Registration*******\n");
printf("Please enter a username:\n");
scanf("%s",temp1);
printf("Please enter a password\n");
scanf("%s",temp2);
if(save_info_into_file(head,temp1,temp2) == 0){
insert_user_info(temp,temp1,temp2);
printf("User info registered successfully.\n");
}
getchar();
break;
case '2':
printf("*******Log in*******\n");
printf("Please enter your user name:\n");
scanf("%s",temp1);
printf("Please enter your password:\n");
scanf("%s",temp2);
if(if_log_in(head,temp1,temp2) == 0){
show_second_menu(temp);
}
getchar();
break;
case '3':
ifQuit = 1;
getchar();
break;
default:
printf("A wrong option.Re-enter please.\n");
break;
}
if(ifQuit){
printf("Exited.\n");
break;
}
}
}
project1_main.c
#include "project1.h"
int main(int argc, const char *argv[])
{
userlist userList = create_user_list();
userlist temp = create_user_list();
load_info_into_list(userList);
load_info_into_list(temp);
show_first_menu(userList,temp);
userList = free_user_space(userList);
temp = free_user_space(temp);
return 0;
}