Bài tập là cài đặt sổ địa chỉ bằng danh sách nối đơn bao gồm:
Tên, địa chỉ, email, sdt cơ quan, dt nhà riêng, dt di động. Trong đó email và các sdt là không hạn chế.
Viết chương trình minh họa một số thao tác thêm sửa tìm kiếm. Tạo file đọc ghi dữ liệu.
Mình còn làm thiếu tạo file đọc ghi dữ liệu nữa. Bạn nào giúp mình với. Code đã làm:




#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <malloc.h>
#include <string.h>

//Khai bao kieu du lieu/////////////////////////////////////////////////////////
struct email
{
char address[30];
email *next;
};

struct phone
{
char number[15];
phone *next;
char type[30];
};

struct contact
{
char ten[30];
char dia_chi[50];
email *mail;
phone *sdt;
contact *next;
};
////////////////////////////////////////////////////////////////////////////////


//Khoi tao danh ba//////////////////////////////////////////////////////////////
contact *create_contact_list ()
{
contact *c;
c=(contact*)malloc(sizeof(contact));
c->next=NULL;
return c;// c tro den phan tu dau tien trong danh ba
}
////////////////////////////////////////////////////////////////////////////////


//Hien thi thong tin ve contact duoc tro boi current////////////////////////////
void display_contact(contact *current)
{
printf(" Ten : %s\n",current->next->ten);
printf(" Dia chi : %s\n",current->next->dia_chi);
email *temp_email;phone *temp_phone;
temp_email=(email*)malloc(sizeof(email));
temp_phone=(phone*)malloc(sizeof(phone));
temp_email=current->next->mail;temp_phone=current->next->sdt;
int count=0;
while ((temp_email)!=NULL) {count++;printf(" Dia chi email %d : %s\n",count,temp_email->address);temp_email=temp_email->next;}
count=0;
while ((temp_phone)!=NULL) {count++;printf(" So dien thoai %d (%s) : %s\n",count,temp_phone->type,temp_phone->number);temp_phone=temp_phone->next;}
}
////////////////////////////////////////////////////////////////////////////////


//Them contact vao dau danh ba//////////////////////////////////////////////////
void insert_contact(contact *first)
{
contact *temp;
temp=(contact*)malloc(sizeof(contact));
temp->mail=(email*)malloc(sizeof(email));
temp->sdt=(phone*)malloc(sizeof(phone));

temp->next=first->next;
fflush(stdin);
printf(" Nhap ten : ");gets(temp->ten);
printf(" Nhap dia chi : ");gets(temp->dia_chi);
printf(" Nhap email : ");gets(temp->mail->address);temp->mail->next=NULL;
printf(" Nhap so dien thoai : ");gets(temp->sdt->number);temp->sdt->next=NULL;
printf(" So dien thoai nay thuoc loai (co quan, nha rieng, di dong,...) : ");gets(temp->sdt->type);
first->next=temp;
}
////////////////////////////////////////////////////////////////////////////////


//Xoa contact duoc tro boi current//////////////////////////////////////////////
void delete_contact(contact *current)
{
contact *temp;
temp=(contact*)malloc(sizeof(contact));
temp=current->next;
current->next=temp->next;
free(temp);
}
////////////////////////////////////////////////////////////////////////////////


//Tim kiem contact theo ten, bat dau tu vi tri tro boi first////////////////////
contact *search_contact(char name[],contact *first)
{
contact *temp;
temp=(contact*)malloc(sizeof(contact));
temp=first;
int result=0;
while (temp->next!=NULL)
{
if (strcmp(temp->next->ten,name) == 0 ) {result++;break;}
else temp=temp->next;
}
if (result==0) temp=NULL;
return temp;//Ket qua la con tro tro toi nut chua gia tri name
}
////////////////////////////////////////////////////////////////////////////////


//Edit thong tin ve so dien thoai tai contact duoc tro boi current//////////////
void edit_phone(contact *current,int option)
{
char new_number[15],new_type[30];int stt;int count;
phone *temp;temp=(phone*)malloc(sizeof(phone));
phone *temp2;temp2=(phone*)malloc(sizeof(phone));
switch(option)
{
case 1://Them so dien thoai
printf(" Nhap so dien thoai can them:");fflush(stdin);
gets(new_number);
printf(" So dien thoai nay thuoc loai (co quan, nha rieng, di dong,...) : ");gets(new_type);
strcpy(temp->number,new_number);
strcpy(temp->type,new_type);
temp->next=current->next->sdt;
current->next->sdt=temp;
printf(" Da them so dien thoai\n");
printf("\n Thong tin sau khi sua\n");display_contact(current);
break;
case 2://Sua so dien thoai
printf(" Sua so dien thoai thu:");scanf("%d",&stt);
if (stt>=1)
{
count=1;
temp=current->next->sdt;
while ((count<stt) && (temp->next != NULL)) {count++;temp=temp->next;}
if (count!=stt) printf("\n So thu tu vua nhap khong hop le\n");
else
{printf(" Nhap so dien thoai moi:");fflush(stdin);gets(new_number);
strcpy(temp->number,new_number);
printf(" Da sua so dien thoai\n");
printf("\n Thong tin sau khi sua\n");display_contact(current);
}
} else printf("\n So thu tu vua nhap khong hop le\n");
break;
case 3://Xoa so dien thoai
printf(" Xoa so dien thoai thu:");scanf("%d",&stt);
if (stt>1)
{
count=1;
temp=current->next->sdt;
while ((count<stt-1) && (temp->next != NULL)) {count++;temp=temp->next;}
if ((count!=stt-1) || (temp->next==NULL)) printf("\n So thu tu vua nhap khong hop le\n");
else
{temp2=temp->next;
temp->next=temp2->next;
free(temp2);
printf(" Da xoa so dien thoai\n");
printf("\n Thong tin sau khi sua\n");display_contact(current);
}
} else if (stt=1)
{temp2=current->next->sdt;
current->next->sdt=temp2->next;
free(temp2);
printf(" Da xoa so dien thoai\n");
printf("\n Thong tin sau khi sua\n");display_contact(current);
} else printf("\n So thu tu vua nhap khong hop le\n");
break;
}
}
////////////////////////////////////////////////////////////////////////////////


//Edit thong tin ve email tai contact duoc tro boi current//////////////////////
void edit_email(contact *current,int option)
{
char new_email[30];int stt;int count;
email *temp;temp=(email*)malloc(sizeof(email));
email *temp2;temp2=(email*)malloc(sizeof(email));
switch(option)
{
case 1://Them dia chi email
printf(" Nhap dia chi email can them:");fflush(stdin);
gets(new_email);
strcpy(temp->address,new_email);
temp->next=current->next->mail;
current->next->mail=temp;
printf(" Da them dia chi email\n");
printf("\n Thong tin sau khi sua\n");display_contact(current);
break;
case 2://Sua dia chi email
printf(" Sua dia chi email thu:");scanf("%d",&stt);
if (stt>=1)
{
count=1;
temp=current->next->mail;
while ((count<stt) && (temp->next != NULL)) {count++;temp=temp->next;}
if (count!=stt) printf("\n So thu tu vua nhap khong hop le\n");
else
{printf(" Nhap dia chi email moi:");fflush(stdin);
gets(new_email);
strcpy(temp->address,new_email);
printf(" Da sua dia chi email\n");
printf("\n Thong tin sau khi sua\n");display_contact(current);
}
} else printf("\n So thu tu vua nhap khong hop le\n");
break;
case 3://Xoa dia chi email
printf(" Xoa dia chi email thu:");scanf("%d",&stt);
if (stt>1)
{
count=1;
temp=current->next->mail;
while ((count<stt-1) && (temp->next != NULL)) {count++;temp=temp->next;}
if ((count!=stt-1) || (temp->next==NULL)) printf("\n So thu tu vua nhap khong hop le\n");
else
{temp2=temp->next;
temp->next=temp2->next;
free(temp2);
printf(" Da xoa dia chi email\n");
printf("\n Thong tin sau khi sua\n");display_contact(current);
}
} else if (stt=1)
{temp2=current->next->mail;
current->next->mail=temp2->next;
free(temp2);
printf(" Da xoa dia chi email\n");
printf("\n Thong tin sau khi sua\n");display_contact(current);
} else printf("\n So thu tu vua nhap khong hop le\n");
break;
}
}
////////////////////////////////////////////////////////////////////////////////


//Chuong trinh chinh////////////////////////////////////////////////////////////
int main()
{
contact *first;
first=create_contact_list();

int ch;char name[30];contact *result;
result=(contact*)malloc(sizeof(contact));
while (1)
{
printf("\nDANH BA DIEN THOAI\n\n");
printf(" 1.Them contact vao danh ba\n 2.Xoa contact khoi danh ba\n 3.Sua thong tin contact\n 4.Tim kiem contact\n 5.Exit\n");
printf(" Chon chuc nang:");scanf("%d",&ch);printf("\n");
switch(ch)
{
case 1: insert_contact(first);
printf("\n Da them contact moi vao danh ba\n");
break;
case 2: printf(" Nhap ten contact can xoa:");fflush(stdin);gets(name);
result=search_contact(name,first);
if (result!=NULL)
{delete_contact(result);
printf("\n Da xoa contact khoi danh ba\n");}
else printf("\n CONTACT NOT FOUND !!!\n");
break;
case 3: printf(" Nhap ten contact can sua:");fflush(stdin);gets(name);
result=search_contact(name,first);
if (result==NULL) printf("\n CONTACT NOT FOUND !!!\n");
else {printf("\n Thong tin ve contact can sua\n");display_contact(result);
int loop=1;
while (loop==1)
{
printf("\n 1.Sua ten\n 2.Sua dia chi\n 3.Them/sua/xoa so dien thoai\n 4.Them/sua/xoa dia chi email\n 5.Quay ve man hinh chinh\n");
int ch_contact;
printf(" Chon chuc nang:");scanf("%d",&ch_contact);printf("\n");
switch(ch_contact)
{
case 1:char new_ten[30];
printf(" Nhap ten moi:");fflush(stdin);gets(new_ten);
strcpy(result->next->ten,new_ten);
printf("\n Da sua ten\n");
printf("\n Thong tin sau khi sua\n");display_contact(result);
break;
case 2:char new_dia_chi[50];
printf(" Nhap dia chi moi:");fflush(stdin);gets(new_dia_chi);
strcpy(result->next->dia_chi,new_dia_chi);
printf("\n Da sua dia chi\n");
printf("\n Thong tin sau khi sua\n");display_contact(result);
break;
break;
case 3:printf(" 1.Them so dien thoai\n 2.Sua so dien thoai\n 3.Xoa so dien thoai\n 4.Cancel\n");
int ch_info;
printf(" Chon chuc nang:");scanf("%d",&ch_info);printf("\n");
switch(ch_info)
{
case 1:edit_phone(result,1);
break;
case 2:edit_phone(result,2);
break;
case 3:edit_phone(result,3);
break;
case 4:break;
default:printf("\n Wrong choice\n");getch();

}
break;
case 4:printf(" 1.Them dia chi email\n 2.Sua dia chi email\n 3.Xoa dia chi email\n 4.Cancel\n");
printf(" Chon chuc nang:");scanf("%d",&ch_info);printf("\n");
switch(ch_info)
{
case 1:edit_email(result,1);
break;
case 2:edit_email(result,2);
break;
case 3:edit_email(result,3);
break;
case 4:break;
default:printf("\n Wrong choice\n");getch();

}
break;
case 5:loop=0;break;
default: printf("\nWrong choice\n");getch();
}
}
}
break;
case 4: printf(" Nhap ten contact can tim kiem:");fflush(stdin);gets(name);
result=search_contact(name,first);
if (result!=NULL) {printf("\n Thong tin ve contact can tim\n");display_contact(result);}
else printf("\n CONTACT NOT FOUND !!!\n");
break;
case 5: printf("\n Bye Bye!!! \n");getch();exit(0);break;
default: printf("Wrong choice\n");getch();
}
}
getch();
}
////////////////////////////////////////////////////////////////////////////////