作业四:老师学生类

来源:互联网 发布:u盘文件怎样移动到mac 编辑:程序博客网 时间:2024/04/29 08:24
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <stdlib.h>
using namespace std;
class student
{
 public:
      int snum;
   string name;
   char sex;
   string subject;
   float score;
 public:
   void add()
   {
    ofstream infile("sdata.txt",ios::app);
    if(infile == NULL)
    {
     cout << "file : data.txt open error!" << endl;
     exit(-1);
    }
    cout<<"please enter 一个学生的:学号,姓名,性别,专业,总分"<<endl;
    string in;
    cin>>in;
    infile<<"/n"+in;
   }
   void display()
   {
    ifstream infile("sdata.txt");
    if(infile == NULL)
    {
     cout << "file : data.txt open error!" << endl;
     exit(-1);
    }
    string line;
    vector< vector<string> > output;
    while(getline(infile, line))
    {
      string word;
      stringstream instream(line);
      vector<string> lineVec;
      while(getline(instream,word,','))
      lineVec.push_back(word);
      output.push_back(lineVec);
    }
    cout<<"学生信息:"<<endl;
    cout<<"学号"<<"姓名"<<"性别"<<"专业"<<"总分"<<endl;
    vector< vector<string> >::const_iterator outIter = output.begin();
    for(; outIter != output.end(); outIter++)
    {
     vector<string>::const_iterator inIter = outIter->begin();
     for(; inIter != outIter->end(); inIter++)
     {
      cout << *inIter;
      if((inIter + 1) != outIter->end())
       cout <<" "<<" ";
      else
       cout << endl;
     }
    }
    cout<<"数据输出完毕!"<<endl;
   }
};
class teacher
{
 public:
      int tnum;
   string name;
   char sex;
   string collage;
   string tsubject;
 public:
   void display()
   {
    ifstream infile("tdata.txt");
    if(infile == NULL)
    {
     cout << "file : data.txt open error!" << endl;
     exit(-1);
    }
    string line;
    vector< vector<string> > output;
    while(getline(infile, line))
    {
      string word;
      stringstream instream(line);
      vector<string> lineVec;
      while(getline(instream,word,','))
      lineVec.push_back(word);
      output.push_back(lineVec);
    }
    cout<<"老师信息:"<<endl;
    cout<<"工号"<<" 姓名 "<<"性别 "<<"院系    "<<"所授课程 "<<endl;
    vector< vector<string> >::const_iterator outIter = output.begin();
    for(; outIter != output.end(); outIter++)
    {
     vector<string>::const_iterator inIter = outIter->begin();
     for(; inIter != outIter->end(); inIter++)
     {
      cout << *inIter;
      if((inIter + 1) != outIter->end())
       cout <<" "<<" ";
      else
       cout << endl;
     }
    }
    cout<<"数据输出完毕!"<<endl;
   }
   void add()
   {
    ofstream infile("tdata.txt",ios::app);
    if(infile == NULL)
    {
     cout << "file : data.txt open error!" << endl;
     exit(-1);
    }
    cout<<"please enter 一个老师的:工号,姓名,性别,院系,所授课程"<<endl;
    string in;
    cin>>in;
    infile<<"/n"+in;
   }
};
void main()
{
 student st;
 teacher te;
 st.display();
 te.display();
 st.add();
 st.display();
 te.add();
 te.display();
}
//sdata.txt//学生数据
008,zk,male,imis,500
009,wh,male,imis,500
010,lqh,male,imis,500
001,yjy,female,imis,500
//tdata.txt//老师数据
211,yj,female,lxy,c++
212,tf,female,lxy,c++
原创粉丝点击