Demo entry 6694315

txt

   

Submitted by anonymous on Jan 22, 2018 at 16:24
Language: C++. Code size: 10.6 kB.

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<stdlib.h>
using namespace std;
#define M 500    //可以存信息总量

int N=0;         //职工总量
class employee
{
public:
    employee( );

    void Set();//初始化信息,第一次对信息的录入

    void add( ); //添加1职工信息

    void del( );//删除某职工信息

    void change( );//修改某职工信息

    void Search( );//查询某职工信息

    void display();//显示全部职工信息
private:
    string num,name,sex,age,wage,xueli,address,telephone;
};
employee::employee( ){ };
void employee::Set( )
{
    int i;
    string setnum,setname,setsex,setage,setwage,setxueli,setaddress,settelephone;
    ofstream outfile;
    //---打开文1件---
    outfile.open("t.txt",ios::out);
    if(!outfile)
    {
        cerr<<" open error"<<endl;
        exit(1);
    }

    //--依次录入相应个数的职工信息---
    for( i=0; i<M; i++)
    {
        cout<<"当职工号输入为'0'时,停止输入!!!"<<endl;
        cout<<" 请输入职工号:"<<endl;
        cin>>setnum;
        num=setnum;
        if(setnum=="0")
            break;
        if(setnum!="0")
        {
            N++;
            outfile<<setnum<<"  ";
            cout<<" 请输入姓名:"<<endl;
            cin>>setname;
            name=setname;
            outfile<<setname<<"  ";
            cout<<" 请输入性别:"<<endl;
            cin>>setsex;
            sex=setsex;
            outfile<<setsex<<"  ";
            cout<<" 请输入年龄:"<<endl;
            cin>>setage;
            age=setage;
            outfile<<setage<<endl;
            cout<<" 请输入工资:"<<endl;
            cin>>setwage;
            wage= setwage;
            outfile<<setwage<<endl;
            cout<<" 请输入学历:"<<endl;
            cin>>setxueli;
            xueli= setxueli;
            outfile<<setxueli<<"  ";
            cout<<" 请输入家庭住址:"<<endl;
            cin>>setaddress;
            address= setaddress;
            outfile<<setaddress<<endl;
            cout<<" 请输入电话:"<<endl;
            cin>>settelephone;
            telephone=settelephone;
            outfile<<settelephone<<endl;
        }
    }
//--- 关闭文件----
    outfile.close( );
}
void employee::add( )
{
    string addnum,addname,addsex,addage,addwage,addxueli,addaddress,addtelephone;
    //--以追加的方式录入信息,直接将信息追加到以前文件的末尾---
    ofstream outfile("t.txt",ios::out|ios::app);
    if(!outfile)
    {
        cerr<<" open error"<<endl ;
        exit(1);
    }
    N=N+1;
//---依次输入相应的数据,并且追加到之前的文件中去----
    cout<<" 请输入您要添加的数据"<<endl ;
    cout<<"请输入职工号:"<<endl;
    cin>>addnum;
    outfile<<addnum<<"  ";
    cout<<"请输入姓名:"<<endl;
    cin>>addname;
    outfile<<addname<<"  ";
    cout<<"请输入性别:"<<endl;
    cin>>addsex;
    outfile<<addsex<<"  ";
    cout<<"请输入年龄:"<<endl;
    cin>>addage;
    outfile<<addage<<endl ;
    cout<<"请输入工资:"<<endl;
    cin>>addwage;
    outfile<<addwage<<endl ;
    cout<<"请输入学历:"<<endl;
    cin>>addxueli;
    outfile<<addxueli<<"  ";
    cout<<"请输入家庭住址:"<<endl;
    cin>>addaddress;
    outfile<<addaddress<<endl ;
    cout<<"请输入电话:"<<endl;
    cin>>addtelephone;
    outfile<<addtelephone<<endl ;
    outfile.close( );
    cout<<"已添加成功!"<<endl;
}
void employee::del( )
{
    int k,i;
    string num[M],name[M],sex[M],age[M],wage[M],xueli[M],address[M],telephone[M];
    ifstream infile("t.txt",ios::in);
    if(!infile)
    {
        cerr<<" open error"<<endl ;
        exit(1);
    }
       cout<<" 学号  姓名  性别 年龄  工资   学历   家庭住址    电话"<<endl ;
    for( i=0; i<N; i++)
    {
        cout<<"  ";
        infile>>num[i];
        cout<< setw(10)<<num[i];
        infile>>name[i];
        cout<<setw(10)<<name[i];
        infile>>sex[i];
        cout<<setw(8)<<sex[i];
        infile>>age[i];
        cout<< setw(8)<<age[i];
        infile>>wage[i];
        cout<< setw(8)<<wage[i];
        infile>>xueli[i];
        cout<<setw(8)<<xueli[i];
        infile>>address[i];
        cout<<setw(10)<<address[i];
        infile>>telephone[i];
        cout<< setw(16)<<telephone[i]<<endl ;
    }
    cout<<"你想删除第几个?"<<endl;
    cin>>k;
    k=k-1;
    infile.close();
    ofstream outfile("t.txt",ios::out);
    if( !outfile)
    {
        cerr<<" open error"<<endl;
        exit(1); //退出程序
    }
//---将数组存取的信息输出到文件中去---
    for(i=0; i<N; i++)
    {
        if(i!=k)
        {
            outfile<<num[i]<<"  ";
            outfile<<name[i]<<"  ";
            outfile<<sex[i]<<"  ";
            outfile<<age[i]<<"  ";
            outfile<<wage[i]<<"  ";
            outfile<<xueli[i]<<"  ";
            outfile<<address[i]<<"  ";
            outfile<<telephone[i]<<"  "<<endl;
        }
    }
    outfile.close( );
    N=N-1;
    cout<<" 已删除成功!"<<endl;
}
void employee::change( )
{
    int i,k;
    string num[M],name[M],sex[M],age[M],wage[M],xueli[M],address[M],telephone[M];
    ifstream infile("t.txt",ios::in);
    if( !infile)
    {
        cerr<<" open error"<<endl ;
        exit(1);
    }
    cout<<"  学号  姓名   性别   年龄  工资  学历  家庭住址   电话"<<endl ;
    for( i=0; i<N ; i++)
    {
        cout<<"    ";
        infile>>num[i];
        cout<<setw(10)<<num[i];
        infile>>name[i];
        cout<<setw(10)<<name[i] ;
        infile>>sex[i];
        cout<<setw(8)<<sex[i];
        infile>>age[i];
        cout<<setw(8)<<age[i];
        infile>>wage[i];
        cout<<setw(8)<<wage[i];
        infile>>xueli[i];
        cout<<setw(8)<<xueli[i];
        infile>>address[i] ;
        cout<<setw(10)<<address[i];
        infile>>telephone[i];
        cout<<setw(16)<<telephone[i]<<endl;
    }
    infile.close( );
    ofstream outfile("t.txt",ios::out);
    if(!outfile)
    {
        cerr<<" open error"<<endl;
        exit(1);
    }
    cout<<" 你想修改第几条数据?"<<endl;
    cin>>k;
    k=k-1;
    cout<<"请输入新的数据:"<<endl;
    cout<<"请输入职工号:"<<endl;
    cin>>num[k] ;
    cout<<"请输入姓名:"<<endl;
    cin>>name[k] ;
    cout<<"请输入性别:"<<endl;
    cin>> sex[k] ;
    cout<<"请输入年龄:"<<endl;
    cin>> age[k] ;
    cout<<"请输入工资:"<<endl;
    cin>> wage[k] ;
    cout<<"请输入学历:"<<endl;
    cin>>xueli[k];
    cout<<"请输入家庭住址:"<<endl;
    cin>>address[k];
    cout<<"请输入电话:"<<endl;
    cin>> telephone[k] ;
    for( i=0; i<N ; i++)
    {
        outfile<<num[i]<<"  ";
        outfile<<name[i]<<"  ";
        outfile<<sex[i]<<"  ";
        outfile<<age[i]<<"  ";
        outfile<<wage[i]<<"  ";
        outfile<<xueli[i]<<"  ";
        outfile<<address[i]<<"  ";
        outfile<<telephone[i]<<"  "<<endl;
    }

    outfile.close( );
    cout<<"已修改成功!"<<endl;
}
void employee::Search( )
{
    int i,w=0;
    char ch;
    string searchwage,searchxueli,num[M],name[M],sex[M],age[M],wage[M],xueli[M],address[M],telephone[M],b[M][8];
    cout<<"m.按工资查找     n.按学历查找"<<endl;
    ifstream infile("t.txt",ios::in);
    if(!infile)
    {
        cerr<<" open error"<<endl ;
        exit( 1);
    }
    for(i=0; i<N; i++)
    {
        infile>>num[i];
        b[i][0]=num[i];
        infile>>name[i];
        b[i][1]=name[i];
        infile>>sex[i];
        b[i][2]=sex[i];
        infile>>age[i];
        b[i][3]=age[i];
        infile>>wage[i];
        b[i][4]=wage[i];
        infile>>xueli[i];
        b[i][5]=xueli[i];
        infile>>address[i];
        b[i][6]=address[i];
        infile>>telephone[i];
        b[i][7]=telephone[i];
    }
    cout<<"请输入m或者n!"<<endl ;
    cin>>ch;
    if(ch=='m')
    {
        cout<<"请输入工资!"<<endl;
        cin>>searchwage;
        for(i=0; i<N; i++)
            if(wage[i]==searchwage)
            {
                w= 1;
                cout<<" 学号 姓名  性别  年龄 工资 学历 家庭住址  电话"<<endl ;

                cout<<"  ";
                cout<<setw(10)<<b[i][0];
                cout<<setw(10)<<b[i][1];
                cout<<setw(8)<<b[i][2];
                cout<<setw(8)<<b[i][3];
                cout<<setw(8)<<b[i][4];
                cout<<setw(8)<<b[i][5];
                cout<<setw(10)<<b[i][6];
                cout<<setw(16)<<b[i][7]<<endl;
            }
        if(w==0)
            cout<<" 无此人信息!"<<endl;
    }
    else if( ch=='n')
    {
        cout<<"请输入学历!"<<endl;
        cin>>searchxueli;
        for(i=0; i<N; i++)
            if(xueli[i]==searchxueli)
            {
                w= 1;
                cout<<" 学号 姓名 性别  年龄  工资  学历 家庭住址  电话"<<endl ;

                cout<<"  ";
                cout<<setw(10)<<b[i][0];
                cout<<setw(10)<<b[i][1];
                cout<<setw(8)<<b[i][2];
                cout<<setw(8)<<b[i][3];
                cout<<setw(8)<<b[i][4];
                cout<<setw(8)<<b[i][5];
                cout<<setw(10)<<b[i][6];
                cout<<setw(16)<<b[i][7]<<endl;
            }
        if(w==0)
            cout<<" 无此人信息!"<<endl;
    }
}
void employee::display( )
{
    int k;
    string num,name,sex,age,wage,xueli,address,telephone;
    ifstream infile("t.txt",ios::in);
    if(!infile)
    {
        cerr<<" open error"<<endl ;
        exit(1);
    }
    cout<<" 学号  姓名  性别   年龄   工资   学历   家庭住址     电话"<<endl ;
    for( k=0; k<N; k++)
    {
        cout<<"  ";
        infile>>num;
        cout<<setw(10)<<num;
        infile>> name;
        cout<<setw(10)<<name;
        infile>> sex;
        cout<<setw(8)<<sex;
        infile>> age;
        cout<<setw(8)<<age;
        infile>> wage;
        cout<<setw(8)<<wage;
        infile>>xueli;
        cout<<setw(8)<<xueli;
        infile>>address;
        cout<<setw(10)<<address ;
        infile>>telephone;
        cout<<setw(16)<<telephone<<endl;
    }
    infile.close();
}
void menu()
{
    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;
}

int  main( )
{
    employee e;
    char ch;
    menu();
    cin>>ch;
    while(ch!='0')
    {
        system("cls");
        switch(ch)
        {
        case'1':
            e.Set( );
            break;
        case'2':
            e.add( );
            break;
        case'3':
            e.del( );
            break;
        case'4':
            e.change( );
            break;
        case'5':
            e.Search( );
            break;
        case'6':
            e.display( );
            break;
        case'0':
            cout<<"退出系统!";
            break;
        default:
            cout<<" 没有此选项!"<<endl;
            break;
        }
        menu( );
        cin>>ch;
    }
}

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).