C++有什么方法可以对文本文件的部分进行分类,然后单独使用它们?

发布于 2025-01-31 03:07:09 字数 578 浏览 2 评论 0原文

我目前正在尝试做我的大学作业,我们现在正在研究C ++的文件流和向量,我们的任务很长,但我只是尝试解释我的主要问题:

我们需要阅读多个文本文件并获取信息并存储它们在向量中, 无论如何,在我们的第一个文本文件中,我们需要像这样阅读的部分:

作业
00011234 84
00012341 90
00012481 100

中期
00011234 55
00012341 99
00012481 50
(学生ID,等级)

等...在我们的任务中,我们需要获得10%的家庭作业等级,20%的中期成绩和最终成绩的30%,然后获得与学生ID相关的成绩的总和。

我的问题是:有什么方法可以通过迭代或对这些信息进行分组,并使用它们来获取相关百分比? (顺便说一句,我们还没有学习类/结构/指针,因此禁止在我们的任务中使用它们,只有向量,fstream,sstream,string库。) 此外,学生有多个家庭作业等级,或者只有1个作业等级等(我们需要占据百分比并总和该学生的所有等级)

I am currently trying to do my college homework, we are studying file streams and vectors in C++ right now, our task is long but I will just try to explain my main problem:

We need to read multiple text files and get information and store them in vectors,
anyways, in our first text file, there are parts we need to read like this:

HOMEWORK
00011234 84
00012341 90
00012481 100

MIDTERM
00011234 55
00012341 99
00012481 50
(student Id, grade)

etc... In our task, we need to get 10% of homework grade, 20% of midterm grade and 30% of final grade, and then take the sum of grades related with student id's.

My question is: is there any way to read by iteration or group these informations, and use them to take related percentages?
(btw we didn't learn classes/structs/pointers yet, so it's forbidden to use them in our task, just vectors, fstream, sstream, string libraries.)
Also, there are multiple homework grades for a student, or only 1 homework grade etc. (We need to take percentages and sum all grades of that student)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冧九 2025-02-07 03:07:09

我不知道是否有一种使用getline函数在特定行之前使用的方法。

有很多方法可以解决这个问题,但是一个简单的方法是使用break关键字在满足特定条件时退出循环。

std::ifstream file_stream ("file.text");
std::string line_text;

while(getline(file_stream, line_text)) {
 if(line_text == "MIDTERM") { // or whatever other test makes sense for your situation.
    // This will make the program skip to immediately after the loop.
    break;
 }

 // Handle otherwise
}

I don't know if there is a way to use getline function until a specific line.

There are many ways of going about that, but a simple one is to use the break keyword to exit out of a loop when a certain condition is met.

std::ifstream file_stream ("file.text");
std::string line_text;

while(getline(file_stream, line_text)) {
 if(line_text == "MIDTERM") { // or whatever other test makes sense for your situation.
    // This will make the program skip to immediately after the loop.
    break;
 }

 // Handle otherwise
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文