ProchXciss 2022-06-13 11:09 采纳率: 100%
浏览 70
已结题

求具体解释C++代码

源代码来自
https://blog.csdn.net/aitaozi11/article/details/111867705?spm=1001.2014.3001.5506
源代码修改了一部分,学校展示用了一下,别人说我解释的不准确,还有部分内容看不太懂,求具体解释一下代码


#include <string> 
#include <iostream> 
#include <fstream> 
#include <iomanip> 
#include <memory.h> 
#include <stdio.h> 
#include <conio.h> 
#include <stdlib.h> 
using namespace std;
class employee
{
public:
    string m_Code;//员工代码
    string m_Name;//员工姓名
    unsigned short int m_Birthday;//员工生日
    string m_Sex;//性别
    string m_Address;//家庭住址
    string m_Status;//婚姻状况
    string m_Post;//职称
    string m_Level;//工资等级
    unsigned int m_Wage;//员工工资
    //链表节点的指针域--- 
    employee* Next;
public:
    employee* Create(employee* Head);
    void Rel(employee* Head);
    employee* Add(employee* Head);
    bool Search(employee* Head);
    employee* Search_Unique_Front(employee* Head);
    void Display_List(employee* Head);
    void Display_Node(employee* pNode);
    employee* Modify(employee* Head);
    employee* Del(employee* Head);
    void Save_ByFile(employee* Head, fstream& ofile);
    employee* Sort(employee* Head);
};
employee* employee::Create(employee* Head)
{//创建一个带头节点的空链表。 
    Head = new employee;
    if (!Head)
    {
        cout << "分配内存失败!" << endl;
        return NULL;
    }
    Head->m_Code = "";
    Head->m_Name = "";
    Head->m_Birthday = 0;
    Head->m_Sex = "";
    Head->m_Address = "";
    Head->m_Status = "";
    Head->m_Post = "";
    Head->m_Level = "";
    Head->m_Wage = 0;
    Head->Next = NULL;
    return Head;
}
void employee::Rel(employee* Head)
{//释放链表。 
    employee* ptr;//声明一个操作用的指针。 
    while (Head != NULL)
    {
        ptr = Head;
        Head = Head->Next;
        delete ptr;//释放节点资源。 
    }
}

employee* employee::Add(employee* Head)
{//前插法添加数据。 
    employee* pNew;// 声明一个新节点。 
    char again;
    string code, name, sex, address, status, post, level;
    unsigned short int birthday;
    unsigned int wage;
    do
    {
        pNew = new employee;
        //数据域。
        cout << "请输入职工代码:";
        cin >> code;
        cout << endl << "请输入职工姓名:";
        cin >> name;
        cout << endl << "请输入职工出生年份:";
        cin >> birthday;
        while (cin.fail())//判断输入是否正确
        {
            cout << "请输入正确的日期格式。" << endl;
            cin.clear();
            fflush(stdin);
            cin >> birthday;
        }
        cout << endl << "请输入职工性别:";
        cin >> sex;
        cout << endl << "请输入职工家庭住址:";
        cin >> address;
        cout << endl << "请输入职工婚姻状况:";
        cin >> status;
        cout << endl << "请输入职工职称:";
        cin >> post;
        cout << endl << "请输入职工工资等级:";
        cin >> level;
        cout << endl << "请输入职工工资:";
        cin >> wage;
        while (cin.fail())
        {
            cout << "请输入正确的工资数据。" << endl;
            cin.clear();
            fflush(stdin);
            cin >> wage;
        }
        cout << endl;
        pNew->m_Code = code;
        pNew->m_Name = name;
        pNew->m_Birthday = birthday;
        pNew->m_Sex = sex;
        pNew->m_Address = address;
        pNew->m_Status = status;
        pNew->m_Post = post;
        pNew->m_Level = level;
        pNew->m_Wage = wage;
        //指针域。 
        pNew->Next = Head->Next;
        Head->Next = pNew;
        cout << "数据添加成功!是否继续添加?(Y/N)" << endl;
        cin >> again;
    } while (again == 'Y' || again == 'y');
    return Head;
}
bool employee::Search(employee* Head)
{//查询同时满足“姓名”和“性别”的职工信息。 
    employee* ptr;
    string sex;
    string name;
    ptr = Head->Next;
    cout << "请输入性别:";
    cin >> sex;
    cout << endl << "请输入姓名:";
    cin >> name;
    cout << endl << "----------------查询结果------------------" << endl;
    while (ptr)
    {
        if ((ptr->m_Name == name) && (ptr->m_Sex == sex))
        {
            Display_Node(ptr);//打印满足条件的节点。 
            return true;
        }
        ptr = ptr->Next;//查询下一节点。 
    }
    cout << "无此职工的信息。" << endl;
    return false;
}
employee* employee::Search_Unique_Front(employee* Head)
{
    employee* ptr;
    string code;
    ptr = Head->Next;
    cout << "请输入职工代码:";
    cin >> code;
    cout << endl << "----------------查询结果------------------" << endl;
    while (ptr)
    {
        if (ptr->m_Code == code)
            return ptr;
        ptr = ptr->Next;
    }
    return ptr;
}
void employee::Display_List(employee* Head)
{
    employee* ptr;
    ptr = Head->Next;
    cout << "================================所有职工信息===============================" << endl;
    while (ptr)
    {
        Display_Node(ptr);
        ptr = ptr->Next;
    }
}

void employee::Display_Node(employee* pNode)
{//在标准输出设备上输出。 
    cout << setw(10) << left << pNode->m_Code
        << setw(10) << left << pNode->m_Name
        << setw(10) << left << pNode->m_Birthday
        << setw(10) << left << pNode->m_Sex
        << setw(10) << left << pNode->m_Address
        << setw(10) << left << pNode->m_Status
        << setw(10) << left << pNode->m_Post
        << setw(10) << left << pNode->m_Level
        << setw(10) << left << pNode->m_Wage << endl;//setw(10)表示占10个字符位置。 
}
employee* employee::Modify(employee* Head)
{// 修改单一个节点。 
    employee* ptr;
    ptr = Search_Unique_Front(Head);
    string code, name, sex, address, status, post, level;
    unsigned short int birthday;
    unsigned  int wage;
    if (ptr)
    {
        cout << "-------你现在可以修改此职工的信息了-------" << endl;
        //数据域。 
        cout << "请输入职工代码:";
        cin >> code;
        cout << endl << "请输入职工姓名:";
        cin >> name;
        cout << endl << "请输入职工出生年份:";
        cin >> birthday;
        while (cin.fail())
        {
            cout << "请输入正确的日期格式。" << endl;
            cin.clear();
            fflush(stdin);
            cin >> birthday;
        }
        cout << endl << "请输入职工性别:";
        cin >> sex;
        cout << endl << "请输入职工家庭住址:";
        cin >> address;
        cout << endl << "请输入职工婚姻状况:";
        cin >> status;
        cout << endl << "请输入职工职称:";
        cin >> post;
        cout << endl << "请输入职工工资等级:";
        cin >> level;
        cout << endl << "请输入职工工资:";
        cin >> wage;
        while (cin.fail())
        {
            cout << "请输入正确的工资数据。" << endl;
            cin.clear();
            fflush(stdin);
            cin >> wage;
        }
        cout << endl;
        ptr->m_Code = code;
        ptr->m_Name = name;
        ptr->m_Birthday = birthday;
        ptr->m_Sex = sex;
        ptr->m_Address = address;
        ptr->m_Status = status;
        ptr->m_Post = post;
        ptr->m_Level = level;
        ptr->m_Wage = wage;
    }
    else
    {
        cout << "没找到此职工的记录,无法修改。" << endl;
    }
    return Head;
}
employee* employee::Del(employee* Head)
{
    string code;
    employee* parentptr;
    employee* ptr_front;
    //ptr_front=Search_Unique_Front(Head); 
    cout << "请输入职工代码:";
    cin >> code;
    parentptr = Head;
    ptr_front = Head->Next;
    while (ptr_front)
    {
        if (ptr_front->m_Code == code)
        {
            parentptr->Next = ptr_front->Next;
            delete ptr_front;
            return Head;
        }
        parentptr = ptr_front;
        ptr_front = ptr_front->Next;

    }
    return Head;
}

void employee::Save_ByFile(employee* Head, fstream& ofile)
{
    employee* pNode;
    pNode = Head->Next;
    ofile.clear();//清除文件结束状态。 
    while (pNode)
    {
        ofile << setw(10) << left << pNode->m_Code
            << setw(10) << left << pNode->m_Name
            << setw(10) << left << pNode->m_Birthday
            << setw(10) << left << pNode->m_Sex
            << setw(10) << left << pNode->m_Address
            << setw(10) << left << pNode->m_Status
            << setw(10) << left << pNode->m_Post
            << setw(10) << left << pNode->m_Level
            << setw(10) << left << pNode->m_Wage << endl;//setw(10)表示占10个字符位置。 
        pNode = pNode->Next;
    }
    cout << "数据文件保存成功!" << endl;
}

employee* employee::Sort(employee* Head)
{//我创建的是带头节点的链表。用直接插入法。 
    if ((Head->Next == NULL) || (Head->Next->Next == NULL))//此步条件判断非常有价值。 
    {
        cout << "数据节点数少于2个,不用排序!" << endl;
        return Head;
    }
    //-----------第二步; 
    employee* ptr;
    employee* ptr_F;
    employee* ptr_N;
    ptr = Head->Next->Next;
    ptr_F = Head;
    Head->Next->Next = NULL;//到此,分成了两个链表。 
    //第三步。 
    while (ptr)
    {
        ptr_N = ptr->Next;
        ptr_F = Head;//ptr_F的归位。 
        while (ptr_F->Next)
        {
            if (ptr->m_Wage > ptr_F->Next->m_Wage)
            {
                ptr->Next = ptr_F->Next;
                ptr_F->Next = ptr;
                break;

            }
            else
            {
                ptr_F = ptr_F->Next;
            }
        }
        if (ptr_F->Next == NULL)
        {
            ptr->Next = ptr_F->Next;
            ptr_F->Next = ptr;//表示插到有序链表的最后面了。 
        }

        ptr = ptr_N;//归位,准备下一次排序。 

    }
    cout << "从高到低,排序成功!" << endl;
    return Head;
}

int main()
{
    employee* st = new employee();
    st = st->Create(st);
    fstream iofile;
    iofile.open("d:\\iofile.txt", ios_base::in | ios_base::out | ios_base::app);//文件以三种方式打开。 
    if (!iofile)
    {
        cout << "打开文件失败!" << endl;
        return -1;
    }
    int menu;
    while (1)
    {
        cout << "*****************************************************" << endl;
        cout << "*====================菜单选顶=======================*" << endl;
        cout << "*                                                   *" << endl;
        cout << "*    1.注册职工 2.修改信息 3.删除信息 4.信息查询    *" << endl;
        cout << "*    5.保存文件 6.工资排行 7.信息显示 0.退出系统    *" << endl;
        cout << "*                                                   *" << endl;
        cout << "*****************************************************" << endl;
        cout << endl << "请选择相应操作菜单项:";
        cin >> menu;
        while (cin.fail())
        {
            cout << "请选择正确的菜单选项。" << endl;
            cin.clear();
            fflush(stdin);
            cin >> menu;
        }
        switch (menu)
        {
        case 0:
            cout << "成功退出系统!" << endl;
            return 0;
        case 1:
            st = st->Add(st);
            break;
        case 2:
            st = st->Modify(st);
            break;
        case 3:
            st = st->Del(st);
            break;
        case 4:
            st->Search(st);
            break;
        case 5:
            st->Save_ByFile(st, iofile);
            break;
        case 6:
            st->Sort(st);
            break;
        case 7:
            st->Display_List(st);
            break;
        default:
            cout << "请选择正确的菜单项进行操作。多谢合作!" << endl;
        }
    }
    st->Rel(st);
    iofile.close();
    return 0;
}
  • 写回答

2条回答 默认 最新

  • sinJack 2022-06-13 12:45
    关注

    会代码,还不知道代码意思。。
    老师说你哪里解释不对呢,你的解释呢?代码中的部分注释吗

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月21日
  • 已采纳回答 6月13日
  • 创建了问题 6月13日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度