[C/C++]结课作业-小型通讯录带文件保存(链表)

15 篇文章 6 订阅

  •  个人主页:北·海
  •  🎐CSDN新晋作者
  •  🎉欢迎 👍点赞✍评论⭐收藏
  • ✨收录专栏:C/C++
  • 🤝希望作者的文章能对你有所帮助,有不足的地方请在评论区留言指正,大家一起学习交流!🤗
#include <iostream>
#include <cstring>
#include <fstream>
#include <windows.h>
#include <string.h>
using namespace std;
//通讯录类  联系人类  链表类   管理类
class Address_Book;
//联系人结构体
struct Contacts{
    char name[20];
    char phone[20];
    char address[20];

    Contacts* next;
    Contacts(char name1[20]="",char phone1[20]="",char address1[20]=""){
        strcpy(name,name1);
        strcpy(phone,phone1);
        strcpy(address,address1);
        next = nullptr;
    }
};
//通讯录结构体
struct Address_Books{
    char name[20];
    Contacts* Head_Contacts;//联系人头指针

    Address_Books* next;//通讯录第一个节点地址

    Address_Books(char name1[20]){
        strcpy(name,name1);
        Head_Contacts = nullptr;
        next = nullptr;
    }

};
//联系人类
class Contact{
public:
    //获取联系人个数
    static int Get_Contacts_Count(Contacts**Contact_Head);
    //增加联系人信息
    static void Insert_Contact(Contacts**Contact_Head);
    //删除联系人信息
    static void Delete_Contact(Contacts**Contact_Head);
    //查询联系人信息
    static void Search_Contact(Contacts**Contact_Head);
    //修改联系人信息
    static void Revise_Contact(Contacts**Contact_Head);
    //联系人管理界面
    static void Manage_Contact_Show();
    //输出联系人信息
    static void Show_Contact(Contacts**Contact_Head);

};
//通讯录类
class Address_Book{
protected:
    char name[20];//通讯录名
public:
    static struct Address_Books* Head;//通讯录首地址
    // Address_Book(char name1[20]="");
    //获取通讯录节点数量
    static int Get_Address_Count(Address_Books* head);
    //创建通讯录
    static void Creat_Address_Book();
    //删除通讯录
    static void Delete_Address_Book(Address_Books *head);
    //修改通讯录名字
    static void Revise_Address_Book(Address_Books *head);
    //查询通讯录
    static void Search_Address_Book(Address_Books *head);
    //通讯录管理界面
    static void Manage_Address_Book_Show();
    //显示所有通讯录
    static void Show__Address_Book(Address_Books *head);
    //打开通讯录
    static void Open_Address_Book(Address_Books* head);
};
//管理类(允许整个程序)
class Manage:public Address_Book{
public:
    Manage();
};
//读写文件类
class Open_Write_File{
public:
    //读文件
    static void READ_FILE(Address_Books** Head);
    //写入通讯录,联系人信息写入文件
    static void Write_FILE(Address_Books* Head);
};
//获取联系人数量
int Contact::Get_Contacts_Count(Contacts **Contact_Head) {
    Contacts* Curr_Contact  = *Contact_Head;
    int Contact_Count =0;
    while(Curr_Contact != nullptr)
    {
        Contact_Count++;
        Curr_Contact = Curr_Contact->next;
    }
    return Contact_Count;
}
//增添联系人
void Contact::Insert_Contact(Contacts **Contact_Head) {
    //创建联系人
    char choose[2];
    char Contact_Name[20];
    char Contact_Phone[20];
    char Contact_Address[20];
    char c;;
    while(true) {
        cout << "Enter the contact name:";
        cin >> Contact_Name;
        cout << "Enter the contact phone:";
        cin >> Contact_Phone;
        cout << "Enter the contact address:";
        cin >> Contact_Address;
        cout << "Save or not?(y/n):";
        cin >> choose;
        if (strcmp(choose, "Y") == 0 || strcmp(choose, "y") == 0) {
            Contacts *NEW_NODE = new Contacts(Contact_Name,Contact_Phone,Contact_Address);//创建节点
            //插入节点
            if (*Contact_Head == nullptr) {
                *Contact_Head = NEW_NODE;
            } else {
                Contacts* Curr_Contact = *Contact_Head;
                while (Curr_Contact->next != nullptr) {
                    Curr_Contact = Curr_Contact->next;
                }
                Curr_Contact->next = NEW_NODE;
            }

            cout << "Successfully saved!" << endl;
        } else {
            cout << "Return successful!" << endl;
            break;
        }
        cout<<"Do you want to continue creating(y/n):";
        cin>>c;
        if(c == 'y'||c=='Y')continue;
        else return;
    }
}
//删除联系人
void Contact::Delete_Contact(Contacts **head) {
    char Delete_Contact_Name[20];
    char c;
    int Flag =0;
    if(Get_Contacts_Count(head) == 0) cout<<"Address book is empty!"<<endl;
    else{
        while (true) {
            cout << "Enter the address book name to delete (0 exits):";
            cin >> Delete_Contact_Name;
            struct Contacts* Curr_Contact = *head;
            if (strcmp(Delete_Contact_Name,"0") == 0) break;
            while (Curr_Contact != nullptr) {
                if (strcmp(Delete_Contact_Name, Curr_Contact->name) == 0) {
                    cout << "Found, delete (y/n)";
                    cin >> c;
                    if (c == 'y' || c == 'Y') {
                        Contacts *kill = Curr_Contact;
                        if (kill == *head)//删除头节点
                            *head = kill->next;
                        else {//不是头节点
                            Contacts *begin_head = *head;
                            while (begin_head->next != kill) {
                                begin_head = begin_head->next;
                            }
                            begin_head->next = kill->next;
                        }
                        delete kill;
                        Flag = 1;
                        cout << "Successfully deleted name of" << Delete_Contact_Name << " Contact" << endl;
                        break;
                    }
                    else{
                        cout << "Delete operation canceled!" << endl;
                        Flag = 2;
                    }
                }
                Curr_Contact = Curr_Contact->next;
            }
            if(Flag == 0) cout<<"The contact was not found!"<<endl;
            cout << "Do you want to continue deleting(y/n):";
            cin >> c;
            if (c == 'y' || c == 'Y') {
                if(Get_Contacts_Count(head) == 0) {
                    cout<<"Address book is empty!"<<endl;
                    return;
                }
                continue;
            } else break;
        }
    }
}
//查询联系人
void Contact::Search_Contact(Contacts **head) {
    char Search_Contact_Name[20];
    int flag = 0;
    if(Get_Contacts_Count(head)==0) cout<<"Address book is empty!"<<endl;
    else {
        Show_Contact(head);//显示所有联系人
        while (true) {
            int i=1;
            Contacts * Curr_Contact = *head;
            cout << "Enter the contact name to query (0 exit):";
            cin >> Search_Contact_Name;
            if (strcmp(Search_Contact_Name, "0") == 0) return;
            cout << "---------------------------------------" << endl;
            cout << "\tName" << "\t" << "cell-phone number" << "\t\t" << "Address"  << endl;
            while (Curr_Contact != nullptr) {
                if (strcmp(Curr_Contact->name, Search_Contact_Name) == 0) {
                    flag = 1;;
                    cout<<i<<". "<<Curr_Contact->name<<"\t"<<Curr_Contact->phone<<"\t"<<Curr_Contact->address<<endl;
                }
                i++;
                Curr_Contact = Curr_Contact->next;
            }
            cout << "---------------------------------------" << endl;
            if (flag == 0) cout << "The contact information was not found!" << endl;
        }
    }
}
//修改联系人信息
void Contact::Revise_Contact(Contacts **head) {
    char old_name[20];
    while(true) {
        Contacts* curr = *head;
        cout << "Enter the contact name you want to modify (0 exit)";
        cin >> old_name;
        if(strcmp(old_name,"0")==0) return;
        while(curr != nullptr){
            if (strcmp(old_name,curr->name)== 0) {
                cout<<"Found:"<<old_name<<"Contact"<<endl;
                cout<<"Enter a new contact name:";
                cin>>curr->name;
                cout<<"Enter a new contact phone number:";
                cin>>curr->phone;
                cout<<"Enter a new contact address:";
                cin>>curr->address;
                cout << "Successfully modified!" << endl;
                break;
            }
            curr =curr->next;
        }
    }
}
//显示所有联系人
void Contact::Show_Contact(Contacts **head) {
    Contacts* Curr_Contact = *head;
    char Name[20];
    int i =1;
    if(Curr_Contact == nullptr){
        cout<<"Address book is empty!"<<endl;
    }
    else {
        cout << "--------------contact list-------------------------" << endl;
        cout << "\tName" << "\t" << "cell-phone number" << "\t\t" << "Address"  << endl;
        cout << "-------------------------------------------------" << endl;
        while (Curr_Contact != nullptr) {
            cout<<i<<". "<<Curr_Contact->name<<"\t"<<Curr_Contact->phone<<"\t"<<Curr_Contact->address<<endl;
            Curr_Contact = Curr_Contact->next;
            i++;
        }
        cout << "-------------------------------------------------" << endl;
    }
}
//联系人管理界面
void Contact::Manage_Contact_Show() {
    cout << endl;
    cout << "*********************************************" << endl;
    cout << "**             **联系人管理界面**          **" << endl;
    cout << "**             1、添加联系人信息           **" << endl;
    cout << "**             2、删除联系人信息           ** " << endl;
    cout << "**             3、查询联系人信息           **" << endl;
    cout << "**             4、修改联系人信息           **" << endl;
    cout << "**             5、显示所有联系人信息       **" << endl;
    cout << "**             0、退出                     **" << endl;
    cout << "*********************************************" << endl;
    cout << endl;
}
//通讯录头指针
struct Address_Books* Address_Book::Head = nullptr;
//创建通讯录
void Address_Book::Creat_Address_Book() {
    //创建通讯录
    Address_Books* curr = Head;
    char choose[2];
    char Name[20];
    char c;
    while(true) {

        cout << "Enter the address book name:";
        cin >> Name;
        while(curr != nullptr){
            if(strcmp(Name,curr->name)==0){
                cout<<"Reenter the address book name:";
                cin>>Name;
                curr = Head;
                continue;
            }
            curr =curr->next;
        }//判断是否重名
        cout << "Save or not?(y/n):";
        cin >> choose;
        if (strcmp(choose, "Y") == 0 || strcmp(choose, "y") == 0) {
            Address_Books *NEW_NODE  = new Address_Books (Name);
            //插入节点
            if (Head == nullptr) {
                Head = NEW_NODE;
            } else {
                Address_Books *curr = Head;
                while (curr->next != nullptr) {
                    curr = curr->next;
                }
                curr->next = NEW_NODE;
            }
            cout << "Successfully saved!" << endl;
        } else {
            cout << "Return successful!" << endl;
            break;
        }
        cout<<"Do you want to continue creating (y/n):";
        cin>>c;
        if(c == 'y'||c=='Y')continue;
        else return;
    }
}
//通讯录管理界面
void Address_Book::Manage_Address_Book_Show() {
    cout << endl;
    cout << "*********************************************" << endl;
    cout << "**             **通讯录管理界面**          **" << endl;
    cout << "**             1、新建通讯录               **" << endl;
    cout << "**             2、删除通讯录               ** " << endl;
    cout << "**             3、查询通讯录               **" << endl;
    cout << "**             4、修改通讯录名字           **" << endl;
    cout << "**             5、显示所有通讯录           **" << endl;
    cout << "**             6、打开指定通讯录           **" << endl;
    cout << "**             0、退出(保存数据)           **" << endl;
    cout << "*********************************************" << endl;
    cout << endl;

}//通讯录管理界面
//删除通讯录
void Address_Book::Delete_Address_Book(Address_Books *head) {
    char Delete_Name[20];
    char c;
    int Flag =0;
    if(Get_Address_Count(Head) == 0) cout<<"Address book is empty!"<<endl;
    else{
        while (true) {
            cout << "Enter the address book name to delete (0 exits):";
            cin >> Delete_Name;
            struct Address_Books* curr = Head;
            if (strcmp(Delete_Name,"0") == 0) break;
            while (curr != nullptr) {
                if (strcmp(Delete_Name, curr->name) == 0) {
                    cout << "Found, delete (y/n)";
                    cin >> c;
                    if (c == 'y' || c == 'Y') {
                        Address_Books *kill = curr;
                        const char *remove_file = kill->name;
                        remove(remove_file);//删除文件
                        if (kill == Head)//删除头节点
                            Head = kill->next;
                        else {//不是头节点
                            Address_Books *begin_head = Head;
                            while (begin_head->next != kill) {
                                begin_head = begin_head->next;
                            }
                            begin_head->next = kill->next;
                        }
                        delete kill;
                        Flag = 1;
                        cout << "Successfully deleted the name of:" << Delete_Name << " Address Book " << endl;
                        break;
                    }
                    else{
                        cout << "Delete operation canceled!" << endl;
                        Flag = 2;
                    }
                }
                curr = curr->next;
            }
            if(Flag == 0) cout<<"The address book was not found!"<<endl;
            cout << "Do you want to continue deleting (y/n):";
            cin >> c;
            if (c == 'y' || c == 'Y') {
                if(Get_Address_Count(Head) == 0) {
                    cout<<"Address book is empty!"<<endl;
                    return;
                }
                continue;
            } else break;
        }
    }
}
//修改通讯录
void Address_Book::Revise_Address_Book(Address_Books *head) {
    char old_name[20];
    while(true) {
        Address_Books* curr = head;;
        cout << "Enter the name of the address book you want to modify (0 exit):";
        cin >> old_name;
        if(strcmp(old_name,"0")==0) return;
        while(curr != nullptr){
            if (strcmp(old_name,curr->name)== 0) {
                cout<<"Enter a new address book name:";
                cin>>curr->name;
                cout << "The address book has been found and successfully modified!" << endl;
                break;
            }
            curr =curr->next;
        }
    }
}
//查询通讯录
void Address_Book::Search_Address_Book(Address_Books *head) {
    char Name[20];
    int flag = 0;
    if(Get_Address_Count(head)==0) cout<<"Address book is empty!"<<endl;
    else {
        Show__Address_Book(head);//显示所有通讯录
        while (true) {
            int i=1;
            Address_Books * curr = head;
            cout << "Please enter the address book name to query (0 exit):";
            cin >> Name;
            if (strcmp(Name, "0") == 0) return;
            while (curr != nullptr) {
                if (strcmp(curr->name, Name) == 0) {
                    flag = 1;
                    cout << i << " ." << curr->name << endl;
                }
                i++;
                curr = curr->next;
            }
            if (flag == 0) cout << "The information was not found!" << endl;
        }
    }

}
//显示所有通讯录
void Address_Book::Show__Address_Book(Address_Books* head) {
    Address_Books* curr = head;
    char Name[20];
    int i =1;
    if(curr == nullptr){
        cout<<"Address book is empty!"<<endl;
    }
    else {
        cout << "---------------Address Book List-------------------------" << endl;

        while (curr != nullptr) {
            cout<<i<<". "<<curr->name<<endl;
            curr = curr->next;
            i++;
        }
        cout << "--------------------------------------------------" << endl;
    }
}
//获取通讯录数量
int Address_Book::Get_Address_Count(Address_Books*head) {
    Address_Books*curr =head;
    int count  = 0;
    while(curr!= nullptr)
    {
        count++;
        curr = curr->next;
    }
    return  count;
}
//进入通讯录
void Address_Book::Open_Address_Book(Address_Books*head) {
    //进入通讯录
    int c,choose;
    char Name[20];
    while(true) {
        Address_Books *curr = head;
        Show__Address_Book(head);
        cout << "Please enter the name of the address book you want to open (0 exit):";
        cin >> Name;
        if (strcmp(Name, "0") == 0) return;
        while (curr != nullptr) {
            if (strcmp(Name, curr->name) == 0) {
                Address_Books* Open = curr;//要打开的通讯录
                while (true) {
                    Contact::Manage_Contact_Show();
                    cout << "Please enter the operation number:";
                    cin >> c;
                    if(c == 0) break;
                    switch (c) {//插入联系人头节点的地址
                        case 1:
                            Contact::Insert_Contact(&(Open->Head_Contacts));
                            break;
                        case 2:
                            Contact::Delete_Contact(&(Open->Head_Contacts));
                            break;
                        case 3:
                            Contact:: Search_Contact(&(Open->Head_Contacts));
                            break;
                        case 4:
                            Contact:: Revise_Contact(&(Open->Head_Contacts));
                            break;
                        case 5:
                            Contact::Show_Contact(&(Open->Head_Contacts));
                            break;
                        default:
                            cout << "Input error, re input:";
                            cin >> choose;
                    }
                }
            }
            curr = curr->next;
        }
    }
}
//数据写入文件
void Open_Write_File::Write_FILE(Address_Books *Head) {
    //循环每一个通讯录创建一个文件 在将联系人信息写入
    Address_Books* curr =Head;
    char File_Name[20];
    //先保存通讯录名到File_Name.txt中
    ofstream File_Name_fp ("File_Name.txt",ios::out);//打开文件
    ofstream fp;
    cout<<"Saving the file...."<<endl;
    if(!File_Name_fp.is_open()) {
        cout << "File opening failed!" << endl;
        return;
    }
    while(curr != nullptr){
        File_Name_fp<<curr->name<<endl;
        curr =curr->next;
    }
    File_Name_fp.close();//关闭文件
    cout<<"File saved!"<<endl;
    //保存联系人信息
    curr  = Head;
    while(curr != nullptr){//循环每个通讯录
        fp.open(curr->name,ios::out);//创建通讯录文件夹
        //写入文件
        if(!fp.is_open()){
            cout<<"File opening failed!"<<endl;
            fp.close();
            return;
        }
        Contacts* Curr_Contact=curr->Head_Contacts;
        while(Curr_Contact!= nullptr) {
            fp.write((char *)Curr_Contact,sizeof(Contacts));
           // fp<<Curr_Contact->name<<"#"<<Curr_Contact->phone<<"#"<<Curr_Contact->address<<"#"<<Curr_Contact->next<<endl;
            Curr_Contact =Curr_Contact->next;
        }
        fp.close();
        curr = curr->next;
    }
}
//数据读入链表
void Open_Write_File::READ_FILE(Address_Books **Head) {
    //写入文件  从文件中读通讯录文件名 进行遍历
    //先读入通讯录名存入字符数组里
    //遍历通讯录名下的联系人进行创建节点保存
    Address_Books*ADDRESS_BOOK = nullptr;
    Address_Books* CURR = nullptr;
    char File_Name[20];//存放文件名
    ifstream read("File_Name.txt");//读取通讯录名
    ifstream Read_Address_Book;//读通讯录里的信息
    if (!read.is_open()) {
        read.close();
        return;
    }
    char c;
    read >> c;//读出一个字符
    if (read.eof()) {
        cout << "File is empty!" << endl;
        return;
    }//如果没有读到则跳出循环;
    read.seekg(0, ios::beg);
    while (read >> File_Name) {//按行读取
        //读取到了一个通讯录名,创建通讯录节点
        ADDRESS_BOOK = new Address_Books(File_Name);//创建一个通讯录节点
        CURR = *Head;
        if (CURR == nullptr) {//插入头节点
            *Head = ADDRESS_BOOK;;
        } else {
            while (CURR->next != nullptr) {
                CURR = CURR->next;
            }
            CURR->next = ADDRESS_BOOK;
        }
    }
    read.close();

    //存入联系人
    Address_Books *CURR_ADDRESS = *Head;//通讯录头指针

    while (CURR_ADDRESS != nullptr) {//循环每个通讯录
        ifstream fp_read_contact(CURR_ADDRESS->name,ios::in);
        if (!fp_read_contact.is_open()) {
            cout << "file open failed!" << endl;
            fp_read_contact.close();
            return;
        }
        fp_read_contact.seekg(0, ios::end);//移动到文件末尾
        long end = fp_read_contact.tellg();//记录文件最后的位置
        fp_read_contact.seekg(0, ios::beg);//移动到开头位置
       unsigned long Get_Count_Node = end/sizeof(Contacts);//得到节点个数
        Contacts *contact = new Contacts[Get_Count_Node];//创建Get_Count_Node个节点
               for(int i = 0;i<Get_Count_Node;i++){
                   fp_read_contact.read((char *)&contact[i], sizeof(Contacts));//读取到TEMP_CONTACT
                   if(i == 0 ){
                       CURR_ADDRESS->Head_Contacts = &contact[0];
                   }
                   else{
                       contact[i-1].next = &contact[i];
                   }
        }
        fp_read_contact.close();
        CURR_ADDRESS = CURR_ADDRESS->next;
    }


}
//运行
Manage::Manage() {
    Open_Write_File::READ_FILE(&Address_Book::Head);//先读通讯录,创建通讯录
    Manage_Address_Book_Show();
    while(true)
    {
        cout<<"Please enter an action:";
        int choose;
        cin>>choose;
        if(choose>=0&& choose<= 6) {
            switch (choose) {
                case 1:
                    Creat_Address_Book();
                    break;
                case 2:
                    Delete_Address_Book(Head);
                    break;
                case 3:
                    Search_Address_Book(Head);
                    break;
                case 4:
                    Revise_Address_Book(Head);
                    break;
                case 5:
                    Show__Address_Book(Head);
                    break;
                case 6:
                    Open_Address_Book(Head);
                    break;
                case 0:
                    Open_Write_File::Write_FILE(Head);//程序结束时候将链表内容写入文件内
                    //exit(0);
                    return;
                default:
                    cout << "Input error!"<<endl;
                    continue;

            }
            Manage_Address_Book_Show();
        }
        else
        {
            cout<<"Input error!"<<endl;
            continue;
        }
    }
}
int main()
{
    Manage StartUp;//运行
}
//给文件名加后缀
//用继承  多态

 运行环境:Clion

可以创建多个通讯录分类存储联系人

待优化:文件设计后缀

复制粘贴即可用

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北·海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值