用链表写职工管理系统

职工信息管理系统:

  1. 存在一个数据文件,用来存储职工各种信息:职工号,姓名,年龄,性别,
    邮编,部门,工资

  2. 可以注册新职工;

  3. 允许修改职工信息

  4. 允许删除职工信息;

4,按照按照姓名和部门查询职工信息;

  1. 可以按照工资多少进行排名,

  2. 可以浏览所有职工信息;

.有一个主界面,供选择和调用上述选项。

.用C++中,文件和链表实现

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;

typedef struct node
{
    int ID;
    char name[20];
    int age;
    char sex[20];
    char postcodes[20];
    char department[20];
    int salary;
    struct node *next;   // 结点指针
}t;

typedef t *PNode;     // 重命名结点指针类型

class Opt{
public:
    int Creat_list_tail(PNode h);
    int Display(PNode h);
    int Search(PNode h, char *p);
    int Del(PNode h, char *p);  
    int Change(PNode h, char *p);
    int Sort(PNode h);
    int Quit(PNode h);
}s;

PNode head_node;
int MARK;
int RUN;

//INSERT————添加通讯录好友 尾插法
int Opt::Creat_list_tail(PNode h)
{
    cout<<"#1";
    if (h == NULL)
    {
        return -1;
    }
    PNode node = new t;

    cout<<"输入职工号";
    cin>>node->ID;
    cin.ignore(100, '\n');
    cout<<"输入姓名";
    cin>>node->name;
    cin.ignore(100, '\n');
    cout<<"输入年龄";
    cin>>node->age;
    cin.ignore(100, '\n');
    cout<<"输入性别";
    cin>>node->sex;
    cin.ignore(100, '\n');
    cout<<"输入邮编";
    cin>>node->postcodes;
    cin.ignore(100, '\n');
    cout<<"输入部门";
    cin>>node->department;
    cin.ignore(100, '\n');
    cout<<"输入工资";
    cin>>node->salary;
    cin.ignore(100, '\n');

    node->next = NULL;

    PNode temp = h;
    while (temp->next)
    {
        temp = temp->next;
    }
    temp->next = node;

    return 0;
}
int Opt::Display(PNode h)
{
    if (h == NULL)
    {
        return -1;
    }
    PNode temp = h->next;  // 链表第一个结点指针
    while (temp)
    {
        cout<<"职工号";
        cout<<temp->ID<<endl;
        cout<<"姓名";
        cout<<temp->name<<endl;
        cout<<"年龄";
        cout<<temp->age<<endl;
        cout<<"性别";
        cout<<temp->sex<<endl;
        cout<<"邮编";
        cout<<temp->postcodes<<endl;
        cout<<"部门";
        cout<<temp->department<<endl;
        cout<<"工资";
        cout<<temp->salary<<endl;
        temp = temp->next;
    }
    return 0;
}

int Opt::Search(PNode h, char *p)
{
    if (h == NULL)
    {
        return -1;
    }
    PNode temp = h->next;
    int flag = 1;
    while(temp)
    {   
        if(strcmp(temp->name, p) == 0)
        {
            flag = 0;
            cout<<"输入职工号";
            cout<<temp->ID<<endl;
            cout<<"输入姓名";
            cout<<temp->name<<endl;
            cout<<"输入年龄";
            cout<<temp->age<<endl;
            cout<<"输入性别";
            cout<<temp->sex<<endl;
            cout<<"输入邮编";
            cout<<temp->postcodes<<endl;
            cout<<"输入部门";
            cout<<temp->department<<endl;
            cout<<"输入工资";
            cout<<temp->salary<<endl;
            break;
        }
        temp = temp->next;
    }
    if (flag)
    {
        printf("\t没有此联系人!\n");
    }
    return 0;
} 
int Opt::Del(PNode h, char *p)
{
    //1搜索联系人
    if (h == NULL)
    {
        return -1;
    }
    PNode temp = h->next;   

    int i;
    int name_count = 0;
    int a[20];
    while(temp)
    {
        for(i = 0; i < 21; i++)
        {
            if(*(p+i) != temp->name[i])
            {
                break;
            }
            else if( (*(p+i) == '\0') && (temp->name[i] == '\0') )
            {
                a[name_count] = temp->ID;   
                name_count++;
                break;
            }
        }
        temp = temp->next;
    }   

    //2删除联系人
    PNode front = NULL;

    if (name_count == 0)
    {
        cout<<"\t没有此联系人!"<<endl;
    }   
    else
    {
        printf("\t查找到联系人%d个\n",name_count);
        printf("\t选择联系人删除:\n");
        i = 0;
        while(i < name_count )
        {
            printf("\t\tID%d: %08d\n",i+1,a[i]);
            i++;
        }
        int choose;
        printf("\t输入要删除好友ID:");
        cin>>choose;
        cin.ignore(100, '\n');
        temp = h->next;

        if(temp->next == NULL)
        {
            free(temp);
            h->next = NULL;
        }
        else
        {   
            while(temp)
            {                       
                if(temp->ID == choose)
                {                   
                    PNode m = front->next;
                    front->next = m->next;
                    free(m);
                    m=NULL;                                                     
                } 
                front=temp;
                temp = temp->next;              
            }
        }
    }

    return 0;
} 
int Opt::Change(PNode h, char *p)
{
    //1搜索联系人
    if (h == NULL)
    {
        return -1;
    }
    PNode temp = h->next;   

    int i;
    int name_count = 0;
    int a[20];
    while(temp)
    {
        for(i = 0; i < 21; i++)
        {
            if(*(p+i) != temp->name[i])
            {
                break;
            }
            else if( (*(p+i) == '\0') && (temp->name[i] == '\0') )
            {
                a[name_count] = temp->ID;   
                name_count++;
                break;
            }
        }
        temp = temp->next;
    }
    //2修改联系人
    PNode front = NULL;

    if (name_count == 0)
    {
        cout<<"\t没有此联系人!"<<endl;
    }   
    else
    {
        printf("\t查找到联系人%d个\n",name_count);
        printf("\t选择联系人修改:\n");
        i = 0;
        while(i < name_count )
        {
            printf("\t\tID%d: %08d\n",i+1,a[i]);
            i++;
        }
        int choose;
        printf("\t输入要修改好友ID:");
        cin>>choose;
        cin.ignore(100, '\n');
        temp = h->next;
        while(temp)
        {                       
            if(temp->ID == choose)
            {                   
                cout<<"输入职工号";
                cin>>temp->ID;
                cin.ignore(100, '\n');
                cout<<"输入姓名";
                cin>>temp->name;
                cin.ignore(100, '\n');
                cout<<"输入年龄";
                cin>>temp->age;
                cin.ignore(100, '\n');
                cout<<"输入性别";
                cin>>temp->sex;
                cin.ignore(100, '\n');
                cout<<"输入邮编";
                cin>>temp->postcodes;
                cin.ignore(100, '\n');
                cout<<"输入部门";
                cin>>temp->department;
                cin.ignore(100, '\n');
                cout<<"输入工资";
                cin>>temp->salary;  
                cin.ignore(100, '\n');
                return 0;
            } 
            front=temp;
            temp = temp->next;              
        }
    }

    return 1;
}
int Opt::Sort(PNode h)
{
    if (h == NULL)
    {
        return -1;
    }
    PNode temp = h->next;
    int count = 0;
    int maxsalary = 0;
    int t = 0;
    while(temp)
    {
        count++;
        if(maxsalary < temp->salary) 
        {
            maxsalary = temp->salary;
        }
        temp = temp->next;
    }
    cout<<"总人数为"<<count<<endl;
    cout<<"最高薪为"<<maxsalary<<endl;
    int i = 0, j = 0;
    int flag = 0;
    while(count != 0)
    {
        temp = h->next;
        t = maxsalary;
        maxsalary = 0;
        while(temp)
        {
            //cout<<"#1";

            if(temp->salary == t)//打印当前薪水的人的信息
            {
                cout<<"职工号";
                cout<<temp->ID<<endl;
                cout<<"姓名";
                cout<<temp->name<<endl;
                cout<<"年龄";
                cout<<temp->age<<endl;
                cout<<"性别";
                cout<<temp->sex<<endl;
                cout<<"邮编";
                cout<<temp->postcodes<<endl;
                cout<<"部门";
                cout<<temp->department<<endl;
                cout<<"工资";
                cout<<temp->salary<<endl<<endl;
                //system("pause");
            }
            else if((maxsalary < temp->salary) && (temp->salary < t))//找到下一个的人的薪水
            {
                maxsalary = temp->salary;
                flag = 1;
            }
            temp = temp->next;
        }
        if(flag != 1)
        {
            count = 0;
        }
        flag = 0;
    }


    return 0;

}

int Opt::Quit(PNode h)
{
    PNode temp = h->next;
    int count = 0;
    fstream outfile("stud.txt",ios::out);
    if(!outfile)
    {
        cerr<<"open error!"<<endl;
        abort( );
    }
    while(temp)
    {
        outfile.write((char *)temp,sizeof(t));
        temp = temp->next;
    }    
    outfile.close( );
    RUN = 0;
    return 0;
}
void System_init()
{
    RUN = 1;
    MARK = 0;
    system("color A4");
    head_node = new t;
    head_node->next = NULL;   // 空链表

    fstream infile("stud.txt",ios::in);
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        abort( );
    }
    PNode temp = new t;
    if(infile.read((char *)temp,sizeof(t)))
    {       
        head_node->next = temp;         
    }
    else
    {
        return;
    }
    PNode end = head_node->next;
    while(1)
    {   
        PNode temp = new t;
        if(infile.read((char *)temp,sizeof(t)))
        {                   
            end->next = temp;
            end = end->next;
            end->next = NULL;
        }
        else
        {
            free(temp);
            infile.close( );
            break;
        }
    }
}

void welcome()
{

}
void Menu(int menu)
{
    switch (menu)
    {
    case 0://Main menu
        {
            cout<<"Main menu"<<endl;
            cout<<"1-------------------------注册新职工"<<endl;
            cout<<"2-------------------------修改职工信息"<<endl;
            cout<<"3-------------------------删除职工信息"<<endl;
            cout<<"4-------------------------按照按照姓名和部门查询职工信息"<<endl;
            cout<<"5-------------------------按照工资多少进行排名"<<endl;
            cout<<"6-------------------------浏览所有职工信息"<<endl;
            cout<<"9-------------------------退出程序"<<endl;
            break;
        }   
    case 1://Register~
        {
            system("cls");
            cout<<"注册新职工"<<endl;
            break;
        }
    case 2://Change
        {
            system("cls");
            cout<<"修改职工信息"<<endl;
            break;
        }
    case 3://Delete~
        {
            system("cls");
            cout<<"删除职工信息"<<endl;
            break;
        }
    case 4://Search~
        {
            system("cls");
            cout<<"按照按照姓名和部门查询职工信息"<<endl;
            break;
        }
    case 5://Sort_salary
        {
            system("cls");
            cout<<"按照工资多少进行排名"<<endl;
            break;
        }
    case 6://Display~
        {
            system("cls");
            cout<<"浏览所有职工信息"<<endl;
            break;
        }
    case 9://quit~
        {
            system("cls");
            cout<<"退出程序"<<endl;
            break;
        }
    }
}
int Choose()
{
    cout<<"请输入选择";
    char choose[10];
    cin>>choose;
    cin.ignore(100, '\n');
    //fflush(stdin);
    return (int)*choose - 48;
}
void Task(int choose)
{
    cout<<"choose = "<<choose<<endl;
    Menu(choose);
    switch(choose)
    {
    case 0:
        {
            break;
        }
    case 1:
        {   
            if(s.Creat_list_tail(head_node))
                cout<<"ERROR1"<<endl;
            system("pause");
            system("cls");
            Menu(0);
            break;
        }
    case 2:
        {
            char name[20];
            cout<<"输入姓名";
            cin>>name;
            cin.ignore(100, '\n');
            if(s.Change(head_node,name))
                cout<<"不存在该员工信息"<<endl;
            system("pause");
            system("cls");
            Menu(0);
            break;
        }
    case 3:
        {
            char name[20];
            cout<<"输入姓名";
            cin>>name;
            cin.ignore(100, '\n');
            if(s.Del(head_node,name))
                cout<<"ERROR3"<<endl;
            system("pause");
            system("cls");
            Menu(0);
            break;
        }
    case 4:
        {
            char name[20];
            cout<<"输入姓名";
            cin>>name;
            cin.ignore(100, '\n');
            if(s.Search(head_node,name))
                cout<<"ERROR4"<<endl;
            system("pause");
            system("cls");
            Menu(0);
            break;
        }
    case 5:
        {
            if(s.Sort(head_node))
                cout<<"ERROR5"<<endl;
            system("pause");
            system("cls");
            Menu(0);
            break;
        }
    case 6:
        {
            if(s.Display(head_node))
                cout<<"ERROR6"<<endl;
            system("pause");
            system("cls");
            Menu(0);
            break;
        }
    case 9:
        {
            if(s.Quit(head_node))
                cout<<"ERROR9"<<endl;
            break;
        }
    }

}
int main()
{
    System_init();
    Menu(0);
    while (RUN)
    {
        Task(Choose());

        //system ("pause");
    }
    return 0;
}
学生包含以下信息项:学号、姓名、学院、班级、高数成绩、英语成绩、C语言成绩、总分、平均分。 系统的主要功能包括: 1. 创建学生成绩信息文件,根据提示输入学生的各项信息,计算出总分和平均分,然后按学号对学生信息进行排序,并将排序后的学生成绩信息存储到一个二进制文件中。 2. 增加学生信息,在原有学生信息文件的基础上增加新的学生成绩信息,要求:增加后的学生信息仍按学号排序,并继续保存至原文件。 3. 删除学生信息,提示用户输入要进行删除操作的学号,如果在文件中有该信息存在,则将该学号所对应的学生信息删除,否则输出提示信息,并提示用户选择是否继续进行删除操作。 4. 修改学生信息,提示用户输入要进行修改操作的学号,如果在文件中有该息存在,则将提示用户输入该学号对应的要修改的选项,结果保存至原文件,并提示用户选择是否继续进行修改操作。 5. 按不同条件对学生信息进行查询操作,输出满足条件的学生信息。 (1) 按学号查询,输入一个学号,输出对应的学生信息。 (2) 按姓名查询,包括精确查询(输入全名),模糊查询(输入姓)。 (3) 按学院查询,输入学院名称,输出该学院的全部学生的信息。 (4) 按班级查询,输入班级名称,输出该班级的全部学生的信息。 6. 按不同条件对学生成绩进行统计工作。 (1) 按总分对学生信息进行排序(由高到低),输出排序后的信息,并将排序后的学生信息存放到一个新的二进制文件中。 (2) 按平均分统计各个分数段的学生人数(不及格,60-69,70-79,80-89,90-100)。 (3) 分别找出3门课程成绩最高的学生,并输出他们的信息。 (4) 分别统计出3门课程的不及格率,并输出。
职工管理系统可以使用链表来实现。链表是一种数据结构,它由一些列节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。 在职工管理系统中,每个职工可以看作一个节点,每个节点包含该职工的信息和一个指向下一个职工的指针。可以使用单向链表、双向链表或循环链表来实现。 下面是一个使用单向链表实现职工管理系统的示例代码: ```c++ #include <iostream> using namespace std; // 定义职工类 class Employee { public: int id; // 职工编号 string name; // 姓名 int age; // 年龄 int salary; // 工资 }; // 定义链表节点类 class Node { public: Employee emp; // 职工信息 Node* next; // 指向下一个节点的指针 }; // 定义链表类 class LinkedList { public: LinkedList() : head(NULL) {} ~LinkedList(); void insert(Employee emp); // 插入节点 void remove(int id); // 删除节点 void display(); // 显示链表中所有节点 private: Node* head; // 链表头指针 }; LinkedList::~LinkedList() { Node* current = head; while (current != NULL) { Node* temp = current; current = current->next; delete temp; } } void LinkedList::insert(Employee emp) { Node* newNode = new Node; newNode->emp = emp; newNode->next = NULL; if (head == NULL) { head = newNode; } else { Node* current = head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } void LinkedList::remove(int id) { if (head == NULL) { return; } if (head->emp.id == id) { Node* temp = head; head = head->next; delete temp; return; } Node* current = head; while (current->next != NULL) { if (current->next->emp.id == id) { Node* temp = current->next; current->next = temp->next; delete temp; return; } current = current->next; } } void LinkedList::display() { Node* current = head; while (current != NULL) { cout << "ID: " << current->emp.id << endl; cout << "Name: " << current->emp.name << endl; cout << "Age: " << current->emp.age << endl; cout << "Salary: " << current->emp.salary << endl; cout << endl; current = current->next; } } int main() { LinkedList list; // 添加职工 Employee emp1 = { 1, "Amy", 25, 3000 }; list.insert(emp1); Employee emp2 = { 2, "Bob", 30, 4000 }; list.insert(emp2); Employee emp3 = { 3, "Charlie", 35, 5000 }; list.insert(emp3); // 显示职工列表 list.display(); // 删除职工 list.remove(2); // 再次显示职工列表 list.display(); return 0; } ``` 在这个示例中,我们定义了一个职工类 Employee,其中包含职工的编号、姓名、年龄和工资等信息。我们还定义了一个链表节点类 Node,其中包含一个职工对象和一个指向下一个节点的指针。 在链表类 LinkedList 中,我们定义了插入节点、删除节点和显示链表中所有节点的方法。其中,插入节点方法是将一个新节点插入到链表的末尾;删除节点方法是根据职工编号删除指定节点;显示链表中所有节点方法是遍历链表并输出每个节点的职工信息。 在主函数中,我们创建了三个职工对象并插入到链表中,然后显示链表中所有节点,并删除了编号为 2 的职工节点,最后再次显示链表中所有节点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值