C++ 实验1-2 学生通讯录管理系统

一、注意

   Xcode:C++写入数据的文件为啥在项目下没看到呢?

  原来默认放在:/Users/username(这里填入自己的用户名)/Library/Developer/Xcode/DerivedData/项目

                          名/Build/Products/Debug/student.txt

二、实现程序:

    1.student.h

#ifndef student_h
#define student_h
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct Node {
    string id; // 学生的学号
    string name; // 姓名
    string birthday; // 出生日期:如1995-01-01
    int sex; // 1:表示是男生,0:表示是女生
    string phone; // 电话
    string address; // 地址
    Node *next;
};

Node *Create(); // 新建同学录
bool InsertStudent(Node *head); // 向学生通讯录插入学生信息
bool DeleteStudent(Node *head, string id); // 在通讯录中删除学生信息
Node *ReadFromFile(); // 从文件中读取通讯录信息
bool WriteToFile(Node *head); // 向文件写入学生通讯录信息
bool FindStudent(Node *head, string id); // 在通讯录中查询学生信息(按学号查询)
void PrintStudent(Node *head); // 在屏幕中输出全部学生信息

#endif /* student_h */

 

2.student.cpp

#include "student.h"
//string id; // 学生的学号
//string name; // 姓名
//string birthday; // 出生日期:如1995-01-01
//int sex; // 1:表示是男生,0:表示是女生
//string phone; // 电话
//string address; // 地址

// 新建学生通讯录信息
// 如:20192101011 ChanJose 2000-01-01 1 15626453333 广东广州
Node *Create() {
    int i = 1;
    char ch = 'Y'; // 是否继续输入学生通讯录信息
    Node *head = new Node(); // 头结点,不存放数据
    if(NULL == head) {
        cerr << "内存空间分配失败" << endl;
        exit(1);
    }
    head->next = NULL;
    while(ch == 'Y') {
        cout << "请输入第" << i << "位学生的学号、姓名、出生日期、姓别(1:表示是男生,0:表示是女生)、电话和地址(以空格隔开):" << endl;
        Node *newNode = new Node(); // 新建结点
        if(NULL == newNode) {
            cerr << "内存空间分配失败" << endl;
            exit(1);
        }
        cin >> newNode->id >> newNode->name >> newNode->birthday >> newNode->sex >> newNode->phone >> newNode->address;
        newNode->next = NULL;
        // 采用头插入
        newNode->next = head->next; // 新结点的next保存首结点(头结点的下一个结点)
        head->next = newNode; // 头指针往前移
        i++;
        cout << "是否继续输入学生的通讯录信息?(Y/N):";
        cin >> ch;
    }
    return head;
}

// 向学生通讯录插入学生信息
bool InsertStudent(Node *head) {
    cout << "请输入学生的学号、姓名、出生日期、姓别(1:表示是男生,0:表示是女生)、电话和地址(以空格隔开):" << endl;
    Node *newNode = new Node(); // 新建结点
    if(NULL == newNode) {
        cerr << "内存空间分配失败" << endl;
        exit(1);
    }
    cin >> newNode->id >> newNode->name >> newNode->birthday >> newNode->sex >> newNode->phone >> newNode->address;
    newNode->next = head->next; // 新结点的next保存首结点(头结点的下一个结点)
    head->next = newNode; // 头指针往前移
    return true; // 插入成功
}

// 在通讯录中删除学生信息
bool DeleteStudent(Node *head, string id) {
    Node *prev = head;
    Node *p = head->next;
    
    while(p != NULL && p->id != id) { // 也可以用strcmp,但不知道为什么我引入了#include <cstring>还是发现找不到strcmp函数
        prev = p;
        p = p->next;
    }
    if(p == NULL)
        return false;
    prev->next = p->next;
    delete p;
    p = NULL;
    return true;
}

// 从文件中读取通讯录信息
Node *ReadFromFile() {
    Node *head = new Node(); // 头结点,不存放数据
    if(NULL == head) {
        cerr << "内存空间分配失败" << endl;
        exit(1);
    }
    ifstream inFile;
    inFile.open("student.txt", ios::in); // 以读的方式打开文件
    if(!inFile.is_open())
    {
        cout << "Error: opening file fail" << endl;
        exit(1);
    }
    Node *newNode = new Node(); // 新建结点
    if(NULL == newNode) {
        cerr << "内存空间分配失败" << endl;
        exit(1);
    }
    while(!inFile.eof()) { // 当未到文件尾
        Node *newNode = new Node(); // 新建结点
        if(NULL == newNode) {
            cerr << "内存空间分配失败" << endl;
            exit(1);
        }
        // 从文件中输入数据
        inFile >> newNode->id >> newNode->name >> newNode->birthday >> newNode->sex >> newNode->phone >> newNode->address;
        newNode->next = head->next; // 新结点的next保存首结点(头结点的下一个结点)
        head->next = newNode; // 头指针往前移
    }
    inFile.close(); // 关闭文件
    return head;
}

// 向文件写入学生通讯录信息
bool WriteToFile(Node *head) {
    Node *p = head->next;
    
    // 以写模式打开文件
    ofstream outFile;
    outFile.open("student.txt", ios::out);
    if(!outFile)
    {
        cout << "Error: opening file fail" << endl;
        exit(1);
    }
    while(p != NULL) {
        // 写入文件
        outFile << p->id << " " << p->name << " " << p->birthday << " " << p->sex << " " << p->phone << " " << p->address << endl;
        p = p->next; // 往后移
    }
    return true;
}

// 在通讯录中查询学生信息(按学号查询)
bool FindStudent(Node *head, string id) {
    Node *p = head->next;
    
    while(p != NULL && p->id != id) { // 也可以用strcmp,但不知道为什么我引入了#include <cstring>还是发现找不到strcmp函数
        p = p->next;
    }
    if(p == NULL)
        return false;
    return true;
}

// 在屏幕中输出全部学生信息
void PrintStudent(Node *head) {
    Node *p = head->next;
    
    while(p != NULL) {
        cout << "学号:" << p->id << " 姓名:" << p->name << " 出生日期:" << p->birthday;
        if(p->sex == 1)
            cout << " 性别:男";
        else
            cout << " 性别:女";
        cout << " 电话:" << p->phone << " 地址:" << p->address << endl;
        p = p->next;
    }
}

3.main.cpp

扫描二维码关注公众号,回复: 4815451 查看本文章
//  测试数据: 20192101011 ChanJose 2000-01-01 1 15626453333 广东广州
//           20192101012 juan 2000-01-02 0 15626453334 广东广州
//           20192101013 feng 2000-01-03 0 15626453335 广东广州
#include <iostream>
#include "student.h"
using namespace std;

int main(int argc, const char * argv[]) {
    int choice;
    string id;
    bool finished = false;
    
    Node *head = NULL;
    while(!finished) {
        cout << "\n*********菜单*********\n";
        cout << "1:新建学生通讯录\n";
        cout << "2:向学生通讯录插入学生信息\n";
        cout << "3:在通讯录删除学生信息\n";
        cout << "4:从文件中读取通讯录信息\n";
        cout << "5:向文件写入通讯录信息\n";
        cout << "6:在通讯录中查询学生信息(按学号查询)\n";
        cout << "7:在屏幕中输出全部学生信息\n";
        cout << "8:退出\n";
        cout << "请输入选择(1-8):\n";
        cin >> choice;
        switch(choice) {
            case 1:
                head = Create(); // 新建学生通讯录
                break;
            case 2:
                if(InsertStudent(head)) // 向学生通讯录插入学生信息
                    cout << "插入学生信息成功!" << endl;
                else
                    cout << "插入学生信息失败!" << endl;
                break;
            case 3: // 在通讯录删除学生信息
                cout << "请输入要删除的学生通讯录信息的学号:";
                cin >> id;
                if(DeleteStudent(head, id))
                    cout << "删除成功!" << endl;
                else
                    cout << "删除失败!" << endl;
                break;
            case 4: // 从文件中读取通讯录信息
                head = ReadFromFile(); 
                break;
            case 5: // 向文件写入通讯录信息
                WriteToFile(head); 
                break;
            case 6: // 在通讯录中查询学生信息(按学号查询)
                cout << "请输入要查找的学生信息的学号:";
                cin >> id;
                if(FindStudent(head, id))
                    cout << "查找成功!" << endl;
                else
                    cout << "查找失败!" << endl;
                break;
            case 7: // 在屏幕中输出全部学生信息
                PrintStudent(head);
                break;
            case 8:
                finished = true;
                break;
            default:
                cout << "输入选择错误,请重新输入!" << endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/chuanzhouxiao/article/details/85722520