码迷,mamicode.com
首页 > 其他好文 > 详细

实验7

时间:2018-06-17 19:05:13      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:iostream   push   换行   ons   eof   ftime   string   getc   int   

#include<iostream>
#include<fstream>
#include<cstring>
#include<vector>
#include<ctime>
#include<cstdlib>
using namespace std;
class stu{
	private:
		int order;
		string id,name,cla;
	public:
		stu(const stu &p);
		stu(int orderx,string idx,string namex,string clax);
		void print();
		int geto(){return order;}
		string geti(){return id;}
		string getn(){return name;}
		string getc(){return cla;}
};
stu::stu(const stu &p){
	order=p.order;
	id=p.id;
	name=p.name;
	cla=p.cla;
}
stu::stu(int orderx,string idx,string namex,string clax):order(orderx),id(idx),name(namex),cla(clax){}
void stu::print(){
	cout<<order<<" "<<id<<" "<<name<<" "<<cla<<endl;
}
int main(){
	vector<stu> stus;
	int order;
	string id,name,cla;
	char ch;//读取上一行的换行符 
	string filename;
	cout<<"输入要打开的班级"<<endl;
	cin>>filename; 
	ifstream fin(filename);
	if(!fin){
		cout<<"fail to open list.txt"<<endl;
		return 1;
	}
	while(fin>>order>>id>>name>>cla){
		fin.get(ch);
        stus.push_back(stu(order,id,name,cla)) ;
}
		fin.close();
    time_t t = time(0); 
    char tmp[64]; 
    strftime( tmp, sizeof(tmp), "%Y%m%d%H%M%S.txt",localtime(&t) );
    string filename2=tmp;
	ofstream fout(filename2);
	if(!fout){
		cout<<"fail to open"<<endl;
		return 1;
	}
	srand(time(NULL));
	int n;
	cout<<"继续点名输入1,否则输入2"<<endl; 
	while(cin>>n){
		if(n==1){
	int j=rand()%stus.size();
    
	stu exm(stus[j]);
	exm.print();
	fout<<exm.geto()<<" "<<exm.geti()<<" "<<exm.getn()<<" "<<exm.getc()<<endl;
}
    else
    
    break;
	}
	fout.close() ;
	return 0;
} 

  技术分享图片技术分享图片

实验7

标签:iostream   push   换行   ons   eof   ftime   string   getc   int   

原文地址:https://www.cnblogs.com/-19990406-whhw/p/9193361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!