如何解决C ++如何将多个主题输出到一个文本文件?
使用STL的MULTIMAP类将地图添加到办公时间项目中。 假设教授使用每个上学的学生的名字 小时作为地图中的键值,并输入他/她询问的主题 关于。 100个工作小时后,他产生了个性化 以文本文件的形式为每个学生报告,描述如何 他们多次参加了上班时间的100次迭代, 他们上班时间来的话题。
我们以前曾制定过一个程序,该程序可以在办公时间生成随机数量的学生,为每个学生分配一个优先级并相应地调整他们的位置,并为他们提供一个随机的时间,让他们花1到10分钟的时间办公室。它运行了这些办公时间中的100个,然后输出每次会话访问办公室的平均学生人数,平均等待时间,在办公室花费的平均时间以及教授在1小时内的平均时间必须留下来。
所有这些都很好。
在最近一次的作业中,我们必须使用多图为每个学生分配一个ID编号(从1到10),每个编号代表一个特定的学生。它还为他们分配一个从文本文件中选择的随机主题。这也很好。我只是通过让程序将每个学生及其主题输出到编译器中的一长串列表中来进行测试的,它可以按预期工作。
我遇到的麻烦是他希望我们为每个学生创建报告文件,其中包含他们在100个办公时间课程中询问的每个主题的列表。
因此基本上,它将输出一个名为“ Student 1.txt”的文件,其中包含以下内容:
Maps
MuliMaps
B-Trees
Trees
BinaryTrees
BinarySearchTrees
Queues
Queues
Heaps
Trees
我可以为10个学生中的每一个生成文本文件,但是当我打开它们时,它们全部为空白并且不包含任何主题。
这是主要代码。我要查看的主要区域是createReports()函数,因为该函数应该负责创建和写入文本文件:
#include <iostream>
#include <queue> //used for queue and priority queue
#include <ctime> //used for the time(NULL) function.
#include <map> //used for the multimap
#include <fstream> //used to read from and print to txt files
using namespace std;
class Student{
public:
Student();
int timeInOffice,arrivalTime,waitTime,priority;
string studentName,topic;
};
string getTopic(){
vector<string> topicoption;
fstream file;
file.open("topics.txt");
for (string s; file >> s; ) topicoption.push_back(s);
string chosenTopic = topicoption[rand()% topicoption.size()];
return chosenTopic;
} //this picks a random topic from the topics.txt file
Student::Student(){
timeInOffice = rand()%10+1;
priority = rand()%10+1;
studentName = "Student " + to_string(rand()%10+1) + " Report";
topic = getTopic();
} //this assigns a priority,student number,topic,and time in office to each student.
bool operator<(const Student &lhs,const Student &rhs){
if (lhs.priority < rhs.priority){
return true;
}else{
return false;
}
} //this deals with the priority of each student and aligns them accordingly
void createReports(multimap<string,string> officeLineMap){
multimap<string,string>::const_iterator i;
for (i = officeLineMap.begin(); i != officeLineMap.end(); i++) {
ofstream myfile(i->first + ".txt");
myfile.open (i->first + ".txt");
myfile << i->second << endl;
myfile.close();
}
} //this prints the reports for each student
int main(){
int numOfStudents;
queue<Student> officeLine;
double avgWaitFinal=0,avgTimeOver1Hr=0,avgWait=0,avgTimeInOffice=0;
priority_queue<Student> priorityLine;
int const officeHour = 60; //this represents the one hour of office hours
multimap<string,string> report;
srand(time(NULL));
for(int j = 0; j < 100; j++){
while(!priorityLine.empty()){
priorityLine.pop();
}
while(!officeLine.empty()){
officeLine.pop();
}
for(int currTime = 0; currTime<officeHour; currTime++){ //currTime is the current time that a student enters or leaves
if((rand()%10+1) <= 1){
Student st;
report.insert(pair<string,string>(st.studentName,st.topic));
st.arrivalTime = currTime;
priorityLine.push(st);
}
}
if(priorityLine.empty()){
cout << "No Students have shown up for office hours :(" << endl;
exit(true);
}
Student st = priorityLine.top();
int timeWaited = 0,students = 0,timeOver = 0,totalTimeInOffice = 0,currTime = st.arrivalTime;
while(!priorityLine.empty()){
st = priorityLine.top();
priorityLine.pop();
if(currTime - st.arrivalTime < 0){
currTime = st.arrivalTime;
st.waitTime = 0;
}else{
st.waitTime = currTime - st.arrivalTime;
}
currTime += st.timeInOffice;
officeLine.push(st);
students++;
timeWaited += st.waitTime;
totalTimeInOffice += st.timeInOffice;
}
avgWait = timeWaited/students;
avgWaitFinal += avgWait;
avgTimeInOffice += totalTimeInOffice/students;
numOfStudents += students;
Student st2 = officeLine.back();
int timeLeft = st2.arrivalTime + st2.waitTime + st2.timeInOffice; //this is the time that a student leaves office hours
timeOver = (timeLeft < officeHour) ? 0 : timeLeft - officeHour;
avgTimeOver1Hr += timeOver;
}
createReports(report);
cout << "Average Number of Students That Visited: " << numOfStudents/100 << " students."<<endl;
cout << "Average Wait Time: " << avgWaitFinal/100 << " minutes." << endl;
cout << "Average Time In Office: " << avgTimeInOffice/100 << " minutes." << endl;
cout << "Average Time Over 1 Hour: " << avgTimeOver1Hr/100 << " minutes." << endl;
return 0;
}
这是topic.txt文件,getTopic()函数从该文件中选择主题:
Maps
MuliMaps
B-Trees
Trees
BinaryTrees
BinarySearchTrees
Queues
Heaps
PriorityQueues
Sorting
Searching
Graphs
Stacks
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。