从键盘输入n个职工的信息,并将其保存在D:/info文件中。

2)已知一个职工的信息为:职工号、工资、姓名,从键盘输入n个职工的信息,并将其保存在D:/info文件中。
【要求】
(1)使用链表实现。
(2)n可以从键盘输入。
(3)定义create 函数实现链表的创建
(4)定义writeInfo函数实现链表的保存。

以下是实现上述要求的C++代码:

#include <iostream>
#include <fstream>
using namespace std;

struct Employee {
    int id;
    double salary;
    string name;
    Employee* next;
};

Employee* create(int n) {
    Employee* head = nullptr;
    Employee* tail = nullptr;
    for (int i = 0; i < n; i++) {
        Employee* emp = new Employee;
        cout << "请输入第" << i + 1 << "个职工的信息:" << endl;
        cout << "职工号:";
        cin >> emp->id;
        cout << "工资:";
        cin >> emp->salary;
        cout << "姓名:";
        cin >> emp->name;
        emp->next = nullptr;
        if (head == nullptr) {
            head = emp;
            tail = emp;
        }
        else {
            tail->next = emp;
            tail = emp;
        }
    }
    return head;
}

void writeInfo(Employee* head) {
    ofstream outfile("D:/info.txt");
    Employee* p = head;
    while (p != nullptr) {
        outfile << p->id << " " << p->salary << " " << p->name << endl;
        p = p->next;
    }
    outfile.close();
}

int main() {
    int n;
    cout << "请输入职工的个数:";
    cin >> n;
    Employee* head = create(n);
    writeInfo(head);
    cout << "职工信息已保存到D:/info.txt文件中。" << endl;
    return 0;
}

在上述代码中,我们定义了一个结构体Employee,表示职工的信息,其中包括职工号、工资和姓名,以及一个指向下一个职工的指针next。然后,我们定义了create函数,用于创建包含n个职工信息的链表。在create函数中,我们使用循环读入n个职工的信息,并将其保存在链表中。最后,我们定义了writeInfo函数,用于将链表中的职工信息保存到D:/info.txt文件中。在main函数中,我们首先从键盘输入职工的个数n,然后调用create函数创建链表,最后调用writeInfo函数将职工信息保存到文件中。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// 声明职工信息结构体
struct Employee {
    int id;
    double salary;
    string name;
    Employee* next;
};

// 定义函数,根据输入创建链表
Employee* create(int n) {
    Employee* head = nullptr;  // 头指针初始化为空
    Employee* tail = nullptr;  // 尾指针初始化为空

    for (int i = 0; i < n; i++) {
        Employee* node = new Employee;  // 动态分配一个新节点
        cout << "请输入第" << i + 1 << "个员工的信息:" << endl;
        cin >> node->id >> node->salary >> node->name;
        node->next = nullptr;  // 新节点的下一节点指针设为空

        if (head == nullptr) {   // 链表中不存在数据,当前节点为头节点
            head = node;
            tail = node;
        } else {                 // 链表中已存在数据
            tail->next = node;
            tail = node;
        }
    }
    return head;
}

// 定义函数,将链表保存至文件
void writeInfo(Employee* head) {
    ofstream outfile;  // 创建输出文件流对象
    outfile.open("D:/info.txt", ios::out);

    while (head != nullptr) {
        outfile << head->id << "\t" << head->salary << "\t" << head->name << endl;
        head = head->next;
    }
    outfile.close();   // 关闭文件流
}

// 主函数
int main() {
    int n;
    cout << "请输入待输入的职工个数:" << endl;
    cin >> n;

    Employee* head = create(n);
    writeInfo(head);

    return 0;
}

#include <iostream>
#include <fstream>
using namespace std;

struct Employee {
    int emp_id;
    double salary;
    string name;
    Employee* next;
};

Employee* create(int n) {
    Employee* head = nullptr;
    Employee* current = nullptr;
    
    for (int i = 0; i < n; i++) {
        Employee* newEmployee = new Employee;
        
        cout << "Enter details for employee " << i+1 << ":" << endl;
        cout << "Employee ID: ";
        cin >> newEmployee->emp_id;
        cout << "Salary: ";
        cin >> newEmployee->salary;
        cout << "Name: ";
        cin >> newEmployee->name;
        
        newEmployee->next = nullptr;
        
        if (head == nullptr) {
            head = newEmployee;
            current = newEmployee;
        } else {
            current->next = newEmployee;
            current = newEmployee;
        }
    }
    
    return head;
}

void writeInfo(Employee* head) {
    ofstream outputFile("D:/info.txt");
    
    if (!outputFile.is_open()) {
        cout << "Error opening file!" << endl;
        return;
    }
    
    Employee* current = head;
    
    while (current != nullptr) {
        outputFile << "Employee ID: " << current->emp_id << endl;
        outputFile << "Salary: " << current->salary << endl;
        outputFile << "Name: " << current->name << endl;
        outputFile << endl;
        
        current = current->next;
    }
    
    outputFile.close();
    cout << "Employee information saved to D:/info.txt" << endl;
}

int main() {
    int n;
    cout << "Enter the number of employees: ";
    cin >> n;
    
    Employee* head = create(n);
    
    writeInfo(head);
    
    return 0;
}