使用I/O流以文本方式建立一个文件test1.txt,写入字符"已成功写入文件!",用其他字处理程序(例如Windows记事本程序Notepad)打开,看看是否正确写入。
本题比较简单,只需掌握C++中基本的I/O输入输出方式即可,这里重要的是掌握C++中流的概念。这一部分可以参考 C++ Prime Plus(第6版)中文版 第17章相关内容。简单理解流相当于管道,在文件(硬件也可视为文件)和程序之间传递字节数据。因此文件流相关的类fstream对应的重载如<<和标准输入输出流iostream中的用法类似。则代码如下
#include<cstdio>#include<fstream>#include<iostream>#include<sstream>#include<string>#include<vector>usingnamespacestd;intmain(){cout<<"Please input the filename you want to open: ";stringfilename;cin>>filename;ifstreamfin(filename);stringbackup=filename+".back_up";ofstreamfout(backup);intnumber=1;stringsline;while(true){getline(fin,sline);if(fin.eof())break;fout<<number<<" "<<sline<<endl;number++;}fin.close();fout.close();fout.open(filename);fin.open(backup);while(true){getline(fin,sline);if(fin.eof())break;fout<<sline<<endl;}fin.close();fout.close();// 删除临时文件
constchar*filerename=backup.c_str();if(remove(filerename)==0){cout<<"Add line number success!";}else{cout<<"Remove backup file error!";}return0;}
#include<cstdio>#include<fstream>#include<iostream>#include<map>#include<sstream>#include<string>#include<utility>#include<vector>usingnamespacestd;namespacezqy19281235{structSTUDENT{intsno;stringname;stringspecialty;intaverage;};// 有默认形参,使用引用变量
voidadd_stu(map<int,structSTUDENT*>&students,intsno,stringname,stringspecialty,intaverage=60){structSTUDENT*new_stu=new(structSTUDENT);new_stu->sno=sno;new_stu->name=name;new_stu->specialty=specialty;new_stu->average=average;students.insert(make_pair(sno,new_stu));}voiddel_stu(map<int,structSTUDENT*>&students,intsno){// 释放内存
delete(students.find(sno)->second);students.erase(sno);cout<<"Delete success!";}// 默认全部显示
voidlook_up_stu(map<int,structSTUDENT*>&students){for(autostu:students){cout<<"Sno = "<<stu.second->sno<<" Name = "<<stu.second->name<<" Specialty = "<<stu.second->specialty<<" Average = "<<stu.second->average<<endl;}}// 重载的显示函数
voidlook_up_stu(map<int,structSTUDENT*>&students,intflag,intmin=0,intmax=100){for(autostu:students){if(stu.second->average>=min&&stu.second->average<=max){cout<<"Sno = "<<stu.second->sno<<" Name = "<<stu.second->name<<" Specialty = "<<stu.second->specialty<<" Average = "<<stu.second->average<<endl;}}}}// namespace zqy19281235
intmain(){charselect;cout<<"Welcome to my student manage system!!!!!"<<endl;map<int,zqy19281235::STUDENT*>students;while(true){cout<<"Please select the operation you want:"<<endl<<"\t1.add a student.\n"<<"\t2.delete a student\n"<<"\t3.display students infomation\n"<<"\tq for quit\n"<<"your select:";cin>>select;switch(select){case'1':{intnumber,average;stringaver;stringname,specialty;cout<<"The student number:";cin>>number;cout<<"The student name:";cin>>name;cout<<"The student specialty:";cin>>specialty;cin.get();cout<<"The student average(can be nothing):";getline(cin,aver);if(aver.length()==0){zqy19281235::add_stu(students,number,name,specialty);}else{average=stoi(aver);zqy19281235::add_stu(students,number,name,specialty,average);}break;}case'2':{intnumber;cout<<"The student number:";cin>>number;zqy19281235::del_stu(students,number);break;}case'3':{stringmin,max;cout<<"Enter the min average of the students:";cin.get();getline(cin,min);cout<<"Enter the max average of the students:";getline(cin,max);if(min.length()==0&&max.length()==0){zqy19281235::look_up_stu(students);break;}elseif(max.length()==0){zqy19281235::look_up_stu(students,1,stoi(min));}elseif(min.length()==0){zqy19281235::look_up_stu(students,1,0,stoi(max));}else{zqy19281235::look_up_stu(students,1,stoi(min),stoi(max));}break;}case'q':{return0;}default:break;}}}
#include<cstdio>#include<fstream>#include<iostream>#include<map>#include<sstream>#include<string>#include<vector>usingnamespacestd;classdate{public:stringbirthday;};classPerson{public:stringpno;stringsex;stringidno;Person();Person(constPerson&obj);voidadd(stringx,stringy,stringz,stringxx);inlinevoiddisplay();~Person();private:datedate_;};Person::Person(){}Person::~Person(){}Person::Person(constPerson&obj){cout<<"call copy function";pno=obj.pno;sex=obj.sex;idno=obj.idno;date_.birthday=obj.date_.birthday;}voidPerson::add(stringx,stringy,stringz,stringxx="00000000"){pno=x;sex=y;idno=xx;date_.birthday=z;}inlinevoidPerson::display(){cout<<"Info of this Person is: "<<endl<<"No: "<<pno<<"\tSex: "<<sex<<"\tBirthday:"<<date_.birthday<<"\tIdno"<<idno<<endl;}
// MyString.cpp
#include"MyString.h"#include<cstring>usingnamespacestd;MyString::MyString(constchar*s){length=strlen(s);str=newchar[length+1];str[length]='\0';//安全拷贝
strncpy(str,s,length+1);}MyString::~MyString(){};char*MyString::get_string(){returnstr;}voidMyString::set_string(char*s){length=strlen(s);str=newchar[length+1];str[length]='\0';//安全拷贝
strncpy(str,s,length+1);}intMyString::get_length(){returnlength;}voidMyString::append(char*s){length=length+strlen(s);char*str_temp=newchar[length+1];str_temp[length]='\0';strncpy(str_temp,str,length+1);strncat(str_temp,s,length+1);deletestr;str=str_temp;}intmain(intargc,char*argv[]){char*first="This is first string";char*second="This is second";MyStringmystring(first);cout<<"Length: "<<mystring.get_length()<<endl;cout<<"String is : "<<mystring.get_string()<<endl;// 添加第二字符串
mystring.append(second);cout<<"Length: "<<mystring.get_length()<<endl;cout<<"String is : "<<mystring.get_string()<<endl;return0;}
#include<cstdio>#include<fstream>#include<iostream>#include<map>#include<sstream>#include<string>#include<utility>#include<vector>usingnamespacestd;structSTUDENT{intsno;stringname;stringspecialty;intaverage;};classStu_info{public:Stu_info(){};~Stu_info(){};// 有默认形参,使用引用变量
voidadd_stu(map<int,structSTUDENT*>&students,intsno,stringname,stringspecialty,intaverage=60){structSTUDENT*new_stu=new(structSTUDENT);new_stu->sno=sno;new_stu->name=name;new_stu->specialty=specialty;new_stu->average=average;students.insert(make_pair(sno,new_stu));}voiddel_stu(map<int,structSTUDENT*>&students,intsno){// 释放内存
delete(students.find(sno)->second);students.erase(sno);cout<<"Delete success!";}// 默认全部显示
voidlook_up_stu(map<int,structSTUDENT*>&students){for(autostu:students){cout<<"Sno = "<<stu.second->sno<<" Name = "<<stu.second->name<<" Specialty = "<<stu.second->specialty<<" Score = "<<stu.second->average<<endl;}}// 重载的显示函数
voidlook_up_stu(map<int,structSTUDENT*>&students,intflag,intmin=0,intmax=100){for(autostu:students){if(stu.second->average>=min&&stu.second->average<=max){cout<<"Sno = "<<stu.second->sno<<" Name = "<<stu.second->name<<" Specialty = "<<stu.second->specialty<<" Score = "<<stu.second->average<<endl;}}}};intmain(){charselect;cout<<"Welcome to my student manage system!!!!!"<<endl;Stu_info*stu_info=newStu_info();map<int,STUDENT*>students;while(true){cout<<"\nPlease select the operation you want:"<<endl<<"\t1.add a student.\n"<<"\t2.delete a student\n"<<"\t3.display students infomation\n"<<"\tq for quit\n"<<"your select:";cin>>select;switch(select){case'1':{intnumber,average;stringaver;stringname,specialty;cout<<"The student number:";cin>>number;cout<<"The student name:";cin>>name;cout<<"The student specialty:";cin>>specialty;cin.get();cout<<"The student score(can be nothing):";getline(cin,aver);if(aver.length()==0){stu_info->add_stu(students,number,name,specialty);}else{average=stoi(aver);stu_info->add_stu(students,number,name,specialty,average);}break;}case'2':{intnumber;cout<<"The student number:";cin>>number;stu_info->del_stu(students,number);break;}case'3':{stringmin,max;cout<<"Enter the min score of the students:";cin.get();getline(cin,min);cout<<"Enter the max score of the students:";getline(cin,max);if(min.length()==0&&max.length()==0){stu_info->look_up_stu(students);break;}elseif(max.length()==0){stu_info->look_up_stu(students,1,stoi(min));}elseif(min.length()==0){stu_info->look_up_stu(students,1,0,stoi(max));}else{stu_info->look_up_stu(students,1,stoi(min),stoi(max));}break;}case'q':{return0;}default:break;}}}
// MyString.h
#include<cstdio>#include<fstream>#include<iostream>#include<map>#include<sstream>#include<string>#include<vector>usingnamespacestd;classMyString{private:char*str;intlength;public:MyString();MyString(char*s);MyString(constMyString&obj);MyString&operator=(constMyString&obj);char*get_string();voidset_string(char*s);intget_length();voidappend(char*s);voidappend(constMyString&obj);~MyString();};// MyString.cpp
#include"MyString.h"#include<cstdio>#include<cstring>#include<fstream>#include<iostream>#include<map>#include<sstream>#include<string>#include<utility>#include<vector>usingnamespacestd;MyString::MyString(){cout<<"MyString类的默认构造函数被调用"<<endl;}MyString::MyString(char*s){length=strlen(s);str=newchar[length+1];str[length]='\0';//安全拷贝
strncpy(str,s,length+1);cout<<"MyString类的有参构造函数被调用,当前字符串为:"<<str<<endl;}MyString::~MyString(){cout<<"MyString类的析构函数被调用,当前字符串为:"<<str<<endl;deletestr;};MyString::MyString(constMyString&obj){set_string(obj.str);cout<<"MyString类的复制构造函数被调用,当前字符串为:";cout<<obj.str<<endl;}MyString&MyString::operator=(constMyString&obj){set_string(obj.str);cout<<"MyString类的赋值运算符被调用,当前字符串为:";cout<<obj.str<<endl;return*this;}char*MyString::get_string(){returnstr;}voidMyString::set_string(char*s){length=strlen(s);str=newchar[length+1];str[length]='\0';//安全拷贝
strncpy(str,s,length+1);}intMyString::get_length(){returnlength;}voidMyString::append(char*s){length=length+strlen(s);char*str_temp=newchar[length+1];str_temp[length]='\0';strncpy(str_temp,str,length+1);strncat(str_temp,s,length+1);deletestr;str=str_temp;}voidMyString::append(constMyString&obj){this->append(obj.str);}intmain(intargc,char*argv[]){MyStringstr;str.set_string("I love C++, ");cout<<"字符串长度:"<<str.get_length()<<"\t"<<str.get_string()<<endl;str.append("yeah!");cout<<"字符串长度:"<<str.get_length()<<"\t"<<str.get_string()<<endl;{MyStringstr("I like C++ programming!");MyStringstr2(str),str3=str;}MyStringstr2;cout<<str.get_string()<<endl;str2=str;str2.append(str2);cout<<str2.get_string()<<endl;return0;}
#include"MyString.h"#include<cstdio>#include<cstring>#include<fstream>#include<iostream>#include<map>#include<sstream>#include<string>#include<utility>#include<vector>usingnamespacestd;MyString::MyString(){cout<<"MyString类的默认构造函数被调用"<<endl;}MyString::MyString(char*s){length=strlen(s);str=newchar[length+1];str[length]='\0';//安全拷贝
strncpy(str,s,length+1);cout<<"MyString类的有参构造函数被调用,当前字符串为:"<<str<<endl;}MyString::~MyString(){cout<<"MyString类的析构函数被调用,当前字符串为:"<<str<<endl;deletestr;};MyString::MyString(constMyString&obj){set_string(obj.str);cout<<"MyString类的复制构造函数被调用,当前字符串为:";cout<<obj.str<<endl;}MyString&MyString::operator=(constMyString&obj){set_string(obj.str);cout<<"MyString类的赋值运算符被调用,当前字符串为:";cout<<obj.str<<endl;return*this;}char*MyString::get_string(){returnstr;}voidMyString::set_string(char*s){length=strlen(s);str=newchar[length+1];str[length]='\0';//安全拷贝
strncpy(str,s,length+1);}intMyString::get_length(){returnlength;}voidMyString::append(char*s){length=length+strlen(s);char*str_temp=newchar[length+1];str_temp[length]='\0';strncpy(str_temp,str,length+1);strncat(str_temp,s,length+1);deletestr;str=str_temp;}voidMyString::append(constMyString&obj){this->append(obj.str);}// CStudent类
classCStudent{public:intsno;MyStringname;MyStringspecialty;intscore;CStudent();CStudent(CStudent&&)=default;CStudent(constCStudent&)=default;CStudent&operator=(CStudent&&)=default;CStudent&operator=(constCStudent&)=default;~CStudent();private:};classCStudentList{public:CStudentList(){};// CStudentList的拷贝构造函数
CStudentList(constCStudentList&obj){for(autostu:obj.students){CStudent*stu_temp=newCStudent();stu_temp->sno=stu.second->sno;// 使用了MyString的拷贝构造函数
stu_temp->name=stu.second->name;stu_temp->score=stu.second->score;stu_temp->specialty=stu.second->specialty;students.insert(make_pair(stu.second->sno,stu_temp));}}// 赋值运算符函数
CStudentList&operator=(constCStudentList&obj){for(autostu:obj.students){CStudent*stu_temp=newCStudent();stu_temp->sno=stu.second->sno;// 使用了MyString的拷贝构造函数
stu_temp->name=stu.second->name;stu_temp->score=stu.second->score;stu_temp->specialty=stu.second->specialty;students.insert(make_pair(stu.second->sno,stu_temp));this->students.insert(make_pair(stu.second->sno,stu_temp));}return*this;}~CStudentList(){};map<int,CStudent*>students;// 有默认形参,使用引用变量
voidadd_stu(intsno,char*name,char*specialty,intscore=60){CStudent*student=newCStudent();student->sno=sno;student->score=score;student->name.set_string(name);student->specialty.set_string(specialty);students.insert(make_pair(sno,student));}voiddel_stu(intsno){// 释放内存
delete(students.find(sno)->second);students.erase(sno);cout<<"Delete success!";}// 默认全部显示
voidlook_up_stu(){for(autostu:students){cout<<"Sno = "<<stu.second->sno<<" Name = "<<stu.second->name.get_string()<<" Specialty = "<<stu.second->specialty.get_string()<<" Score = "<<stu.second->score<<endl;}}// 重载的显示函数
voidlook_up_stu(intflag,intmin=0,intmax=100){for(autostu:students){if((stu.second->score)>=min&&stu.second->score<=max){cout<<"Sno = "<<stu.second->sno<<" Name = "<<stu.second->name.get_string()<<" Specialty = "<<stu.second->specialty.get_string()<<" Score = "<<stu.second->score<<endl;}}}// 根据学号修改学生信息
voidchange_stu(intsno,char*name,char*specialty,intscore=60){intkeycount=students.count(sno);if(keycount!=1){cout<<"Don't have this student, please add it to system!!"<<endl;return;}else{students.find(sno)->second->sno=sno;students.find(sno)->second->name.set_string(name);students.find(sno)->second->specialty.set_string(specialty);students.find(sno)->second->score=score;cout<<"Update success!"<<endl;return;}}};intmain(){charselect;cout<<"Welcome to my student manage system!!!!!"<<endl;CStudentList*stu_info=newCStudentList();while(true){cout<<"\nPlease select the operation you want:"<<endl<<"\t1.add a student.\n"<<"\t2.delete a student\n"<<"\t3.display students information\n"<<"\t4.change student information\n"<<"\tq for quit\n"<<"your select:";cin>>select;switch(select){case'1':{intnumber,average;stringaver;stringname,specialty;cout<<"The student number:";cin>>number;cout<<"The student name:";cin>>name;cout<<"The student specialty:";cin>>specialty;cin.get();cout<<"The student score(can be nothing):";getline(cin,aver);if(aver.length()==0){stu_info->add_stu(number,(char*)name.data(),(char*)specialty.data());}else{average=stoi(aver);stu_info->add_stu(number,(char*)name.data(),(char*)specialty.data(),average);}break;}case'2':{intnumber;cout<<"The student number:";cin>>number;stu_info->del_stu(number);break;}case'3':{stringmin,max;cout<<"Enter the min score of the :";cin.get();getline(cin,min);cout<<"Enter the max score of the :";getline(cin,max);if(min.length()==0&&max.length()==0){stu_info->look_up_stu();break;}elseif(max.length()==0){stu_info->look_up_stu(1,stoi(min));}elseif(min.length()==0){stu_info->look_up_stu(1,0,stoi(max));}else{stu_info->look_up_stu(1,stoi(min),stoi(max));}break;}case'4':{intnumber,average;stringaver;stringname,specialty;cout<<"The student number:";cin>>number;cout<<"The student name:";cin>>name;cout<<"The student specialty:";cin>>specialty;cin.get();cout<<"The student score(can be nothing):";getline(cin,aver);if(aver.length()==0){stu_info->change_stu(number,(char*)name.data(),(char*)specialty.data());}else{average=stoi(aver);stu_info->change_stu(number,(char*)name.data(),(char*)specialty.data(),average);}break;}case'q':{return0;}default:break;}}}
#include"MyString.cpp"#include<cstdio>#include<cstring>#include<fstream>#include<iostream>#include<iterator>#include<map>#include<ostream>#include<sstream>#include<string>#include<utility>#include<vector>usingnamespacestd;// CStudent类
classCStudent{public:intsno;MyStringname;MyStringspecialty;intscore;CStudent(){};~CStudent(){};// Cstudent类输入输出重载
friendostream&operator<<(ostream&os,CStudent&stu){os<<stu.name<<" "<<stu.score<<" "<<stu.sno<<" "<<stu.specialty<<endl;returnos;}friendistream&operator>>(istream&os,CStudent&stu){os>>stu.name>>stu.score>>stu.sno>>stu.specialty;returnos;}private:};classCStudentList{public:CStudentList(){};// CStudentList的拷贝构造函数
CStudentList(constCStudentList&obj){for(autostu:obj.students){CStudent*stu_temp=newCStudent();stu_temp->sno=stu.second->sno;// 使用了MyString的拷贝构造函数
stu_temp->name=stu.second->name;stu_temp->score=stu.second->score;stu_temp->specialty=stu.second->specialty;students.insert(make_pair(stu.second->sno,stu_temp));}}// 赋值运算符函数
CStudentList&operator=(constCStudentList&obj){for(autostu:obj.students){CStudent*stu_temp=newCStudent();stu_temp->sno=stu.second->sno;// 使用了MyString的拷贝构造函数
stu_temp->name=stu.second->name;stu_temp->score=stu.second->score;stu_temp->specialty=stu.second->specialty;students.insert(make_pair(stu.second->sno,stu_temp));this->students.insert(make_pair(stu.second->sno,stu_temp));}return*this;}~CStudentList(){for(autostu:students){delete&stu.second->name;delete&stu.second->specialty;}};// students数据
map<int,CStudent*>students;// CStudentList重载加法运算符
CStudentListoperator+(constCStudentList&obj){CStudentListnewstudent;newstudent.students=students;newstudent.students.insert(obj.students.begin(),obj.students.end());returnnewstudent;}// 重载下标运算符
CStudent*operator[](inti){if(i>=students.size()){cout<<"超过最大索引"<<endl;returnstudents.begin()->second;}map<int,CStudent*>::iteratoriter=students.begin();for(intj=0;j!=i;j++){iter++;}returniter->second;}// 重载输出输入函数
friendostream&operator<<(ostream&os,CStudentList&stulist){os<<stulist.students.size()<<" ";for(autostu:stulist.students){os<<*stu.second;}returnos;}friendistream&operator>>(istream&os,CStudentList&stulist){intsize;os>>size;for(inti=0;i<size;i++){CStudent*newstudent=newCStudent;os>>*newstudent;stulist.students.insert(make_pair(newstudent->sno,newstudent));}returnos;}// 有默认形参,使用引用变量
voidadd_stu(intsno,char*name,char*specialty,intscore=60){CStudent*student=newCStudent();student->sno=sno;student->score=score;student->name.set_string(name);student->specialty.set_string(specialty);students.insert(make_pair(sno,student));}voiddel_stu(intsno){// 释放内存
delete(students.find(sno)->second);students.erase(sno);cout<<"Delete success!";}// 默认全部显示
voidlook_up_stu(){for(autostu:students){cout<<"Sno = "<<stu.second->sno<<" Name = "<<stu.second->name.get_string()<<" Specialty = "<<stu.second->specialty.get_string()<<" Score = "<<stu.second->score<<endl;}}// 重载的显示函数
voidlook_up_stu(intflag,intmin=0,intmax=100){for(autostu:students){if((stu.second->score)>=min&&stu.second->score<=max){cout<<"Sno = "<<stu.second->sno<<" Name = "<<stu.second->name.get_string()<<" Specialty = "<<stu.second->specialty.get_string()<<" Score = "<<stu.second->score<<endl;}}}// 根据学号修改学生信息
voidchange_stu(intsno,char*name,char*specialty,intscore=60){intkeycount=students.count(sno);if(keycount!=1){cout<<"Don't have this student, please add it to system!!"<<endl;return;}else{students.find(sno)->second->sno=sno;students.find(sno)->second->name.set_string(name);students.find(sno)->second->specialty.set_string(specialty);students.find(sno)->second->score=score;cout<<"Update success!"<<endl;return;}}};intmain(){charselect;cout<<"Welcome to my student manage system!!!!!"<<endl;CStudentList*stu_info=newCStudentList();while(true){cout<<"\nPlease select the operation you want:"<<endl<<"\t1.add a student.\n"<<"\t2.delete a student\n"<<"\t3.display students information\n"<<"\t4.change student information\n"<<"\t5.write to a file\n"<<"\t6.read from a file\n"<<"\tq for quit\n"<<"your select:";cin>>select;switch(select){case'1':{intnumber,average;stringaver;stringname,specialty;cout<<"The student number:";cin>>number;cout<<"The student name:";cin>>name;cout<<"The student specialty:";cin>>specialty;cin.get();cout<<"The student score(can be nothing):";getline(cin,aver);if(aver.length()==0){stu_info->add_stu(number,(char*)name.data(),(char*)specialty.data());}else{average=stoi(aver);stu_info->add_stu(number,(char*)name.data(),(char*)specialty.data(),average);}break;}case'2':{intnumber;cout<<"The student number:";cin>>number;stu_info->del_stu(number);break;}case'3':{stringmin,max;cout<<"Enter the min score of the :";cin.get();getline(cin,min);cout<<"Enter the max score of the :";getline(cin,max);if(min.length()==0&&max.length()==0){stu_info->look_up_stu();break;}elseif(max.length()==0){stu_info->look_up_stu(1,stoi(min));}elseif(min.length()==0){stu_info->look_up_stu(1,0,stoi(max));}else{stu_info->look_up_stu(1,stoi(min),stoi(max));}break;}case'4':{intnumber,average;stringaver;stringname,specialty;cout<<"The student number:";cin>>number;cout<<"The student name:";cin>>name;cout<<"The student specialty:";cin>>specialty;cin.get();cout<<"The student score(can be nothing):";getline(cin,aver);if(aver.length()==0){stu_info->change_stu(number,(char*)name.data(),(char*)specialty.data());}else{average=stoi(aver);stu_info->change_stu(number,(char*)name.data(),(char*)specialty.data(),average);}break;}case'5':{cout<<"please enter the file name:";stringfilename;cin>>filename;ofstreamoutfile;outfile.open(filename);outfile<<*stu_info;outfile.close();break;}case'6':{ifstreaminfile;cout<<"please enter the file name:";stringfilename;cin>>filename;infile.open(filename);infile>>*stu_info;infile.close();break;}case'q':{return0;}default:break;}}}
武士降生
输出样例: 004 blue lion 5 born with strength 5,2 lion in red headquarter
表示在 4点整,编号为5的蓝魔lion武士降生,它降生时生命值为5,降生后蓝魔司令部里共有2个lion武士。(为简单起见,不考虑单词的复数形式)注意,每制造出一个新的武士,都要输出此时司令部里共有多少个该种武士。
如果造出的是dragon,那么还要输出一行,例:
It has a arrow,and it’s morale is 23.34
表示该dragon降生时得到了arrow,其士气是23.34(为简单起见,本题中arrow前面的冠词用a,不用an,士气精确到小数点后面2位,四舍五入)
如果造出的是ninjia,那么还要输出一行,例:
It has a bomb and a arrow
表示该ninjia降生时得到了bomb和arrow。
如果造出的是iceman,那么还要输出一行,例:
It has a sword
表示该iceman降生时得到了sword。
如果造出的是lion,那么还要输出一行,例:
It’s loyalty is 24
表示该lion降生时的忠诚度是24。
司令部停止制造武士
输出样例: 010 red headquarter stops making warriors
表示在 10点整,红方司令部停止制造武士
输出事件时:
首先按时间顺序输出;
同一时间发生的事件,先输出红司令部的,再输出蓝司令部的。
输入
第一行是一个整数,代表测试数据组数。
每组测试数据共两行。
第一行,一个整数M。其含义为: 每个司令部一开始都有M个生命元( 1 <= M <= 10000)
第二行:五个整数,依次是 dragon 、ninja、iceman、lion、wolf 的初始生命值。它们都大于0小于等于10000
输出
对每组测试数据,要求输出从0时0分开始,到双方司令部都停止制造武士为止的所有事件。
对每组测试数据,首先输出"Case:n" n是测试数据的编号,从1开始
接下来按恰当的顺序和格式输出所有事件。每个事件都以事件发生的时间开头,时间以小时为单位,有三位。
样例输入
1
20
3 4 5 6 7
样例输出
Case:1
000 red iceman 1 born with strength 5,1 iceman in red headquarter
It has a bomb
000 blue lion 1 born with strength 6,1 lion in blue headquarter
It’s loyalty is 14
001 red lion 2 born with strength 6,1 lion in red headquarter
It’s loyalty is 9
001 blue dragon 2 born with strength 3,1 dragon in blue headquarter
It has a arrow,and it’s morale is 3.67
002 red wolf 3 born with strength 7,1 wolf in red headquarter
002 blue ninja 3 born with strength 4,1 ninja in blue headquarter
It has a sword and a bomb
003 red headquarter stops making warriors
003 blue iceman 4 born with strength 5,1 iceman in blue headquarter
It has a bomb
004 blue headquarter stops making warriors
#include<cstdio>#include<fstream>#include<iomanip>#include<iostream>#include<map>#include<sstream>#include<string>#include<utility>#include<vector>usingnamespacestd;// 构造战士信息哈希表
map<int,structwarrior*>warriors;vector<string>warrior_name={"dragon","ninja","iceman","lion","wolf"};vector<string>tool={"sword","bomb","arrow"};classown_warrior{public:intnumber;intlife_value;// 表示是否拥有武器
inthas_tool;vector<int>tools;voidmetamake(intlife,intwarrior_num){number=warrior_num;life_value=life;}};classdragon:publicown_warrior{public:// 士气
floatmorale;voidmake(intlife,intwarrior_num,intmeta_life,stringcolor,intsize){metamake(life,warrior_num);has_tool=true;inttool_num=warrior_num%3;tools.push_back(tool_num);morale=(float)meta_life/(float)life;cout<<color<<" "<<"dragon "<<warrior_num<<" born with strength "<<life<<","<<size<<" "<<"dragon"<<" in "<<color<<" headquarter"<<endl;cout<<"It has a "<<tool[tool_num]<<",and it's morale is "// 设定小数精度
<<setprecision(3)<<morale<<endl;}};classlion:publicown_warrior{public:// 忠诚度
intloyalty;// 制造战士的函数,各个战士的该函数均为此用处
voidmake(intlife,intwarrior_num,intmeta_life,stringcolor,intsize){metamake(life,warrior_num);loyalty=meta_life;cout<<color<<" "<<"lion "<<warrior_num<<" born with strength "<<life<<","<<size<<" "<<"lion"<<" in "<<color<<" headquarter"<<endl;cout<<"It's loyalty is "<<loyalty<<endl;}};classninja:publicown_warrior{public:voidmake(intlife,intwarrior_num,intmeta_life,stringcolor,intsize){metamake(life,warrior_num);has_tool=true;tools.push_back(warrior_num%3);tools.push_back((warrior_num+1)%3);cout<<color<<" "<<"ninja "<<warrior_num<<" born with strength "<<life<<","<<size<<" "<<"ninja"<<" in "<<color<<" headquarter"<<endl;cout<<"It has a "<<tool[warrior_num%3]<<" and a "<<tool[(warrior_num+1)%3]<<endl;}};classiceman:publicown_warrior{public:voidmake(intlife,intwarrior_num,intmeta_life,stringcolor,intsize){metamake(life,warrior_num);has_tool=true;tools.push_back(warrior_num%3);cout<<color<<" "<<"iceman "<<warrior_num<<" born with strength "<<life<<","<<size<<" "<<"iceman"<<" in "<<color<<" headquarter"<<endl;cout<<"It has a "<<tool[warrior_num%3]<<endl;}};classwolf:publicown_warrior{public:voidmake(intlife,intwarrior_num,intmeta_life,stringcolor,intsize){metamake(life,warrior_num);cout<<color<<" "<<"wolf "<<warrior_num<<" born with strength "<<life<<","<<size<<" "<<"wolf"<<" in "<<color<<" headquarter"<<endl;}};structwarrior{stringname;intlife_value;};// 司令部类
classheadquarter{public:intmeta_life;vector<int>order;intwarrior_now;boolover;stringcolor;intwarrior_num;map<string,vector<void*>*>warrior_info;headquarter(int,vector<int>,string);headquarter(headquarter&&)=default;headquarter(constheadquarter&)=default;headquarter&operator=(headquarter&&)=default;headquarter&operator=(constheadquarter&)=default;~headquarter();voidmake_warrior(int);voidjudge_make();private:};// 构造函数,初始化生命元和战士信息
headquarter::headquarter(intlife,vector<int>my_order,stringmy_color){meta_life=life;order=my_order;color=my_color;warrior_num=1;over=false;warrior_now=0;for(inti=0;i<warrior_name.size();i++){vector<void*>*temp_vector=newvector<void*>;warrior_info.insert(make_pair(warrior_name[i],temp_vector));}}headquarter::~headquarter(){}// 制造战士
voidheadquarter::make_warrior(intwarr_now){intlife_value=warriors[order[warr_now]]->life_value;stringname=warriors[order[warr_now]]->name;meta_life-=life_value;// 为新战士分配内存空间放入对应的map中
if(name=="dragon"){dragon*newdragon=newdragon;warrior_info[name]->push_back(newdragon);newdragon->make(life_value,warrior_num,meta_life,color,warrior_info[name]->size());}elseif(name=="ninja"){ninja*newninja=newninja;warrior_info[name]->push_back(newninja);newninja->make(life_value,warrior_num,meta_life,color,warrior_info[name]->size());}elseif(name=="iceman"){iceman*newiceman=newiceman;warrior_info[name]->push_back(newiceman);newiceman->make(life_value,warrior_num,meta_life,color,warrior_info[name]->size());}elseif(name=="lion"){lion*newlion=newlion;warrior_info[name]->push_back(newlion);newlion->make(life_value,warrior_num,meta_life,color,warrior_info[name]->size());}elseif(name=="wolf"){wolf*newwolf=newwolf;warrior_info[name]->push_back(newwolf);newwolf->make(life_value,warrior_num,meta_life,color,warrior_info[name]->size());}warrior_num++;warrior_now=(warr_now+1)%(warriors.size());// cout << warriors.size() << " " << warrior_now << endl;
return;}// 判能否制造下一个战士,不能则继续制造下一个,直到都不能则结束.
voidheadquarter::judge_make(){if(warriors[order[warrior_now]]->life_value<=meta_life){make_warrior(warrior_now);return;}else{for(inti=1;i<warriors.size();i++){if(warriors[order[(warrior_now+i)%warriors.size()]]->life_value<=meta_life){warrior_now=(warrior_now+i)%warriors.size();make_warrior(warrior_now);return;}}over=true;cout<<color<<" headquarter stops making warriors"<<endl;return;}}intmain(intargc,char*argv[]){intcase_no=0;intlength=5;vector<int>red_order={2,3,4,1,0};vector<int>blue_order={3,0,1,2,4};//处理每组数据
while(cin>>case_no){inttime=0;intcase_life=20;// 读入司令部的元生命值
cin>>case_life;warriors.clear();// 读入各个战士的生命值
for(inti=0;i<length;i++){intwa_life=0;cin>>wa_life;structwarrior*new_warrior=newstructwarrior;new_warrior->life_value=wa_life;new_warrior->name=warrior_name[i];warriors.insert(make_pair(i,new_warrior));}headquarter*red_headquarter=newheadquarter(case_life,red_order,"red");headquarter*blue_headquarter=newheadquarter(case_life,blue_order,"blue");cout<<"Case:"<<case_no<<endl;while(!red_headquarter->over||!blue_headquarter->over){if(!red_headquarter->over){printf("%03d ",time);red_headquarter->judge_make();}if(!blue_headquarter->over){printf("%03d ",time);blue_headquarter->judge_make();}time++;}}return0;}