数据文件输入排序问题 [英] Data File input sorting question

查看:57
本文介绍了数据文件输入排序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我是一名计算机科学专业的学生,​​参加我的第一个C ++课程,这是我第一次在网上发帖。所以,我提前为我缺乏了解而道歉。到目前为止,我没有遇到任何问题,但我现在的任务是抛弃一个循环,所以我希望你们中有一个更有经验的人能指出我正确的方向。


这些是我的作业说明:


1 - 您的作业是提示用户输入磁盘上路径的文件名。如果文件在指定位置不存在,您的程序应该以适当的错误消息退出。


2 - 您的程序应该做的第一件事是向屏幕输出一份副本从磁盘文件读入的数据。这被称为回显输入数据。


3 - 您的程序应该计算并显示男性,女性,社区大学生和大学生的平均分数。将结果显示到两个小数位,然后编写程序以自动将四个计算的平均值列为一行,并为每个结果添加一个合适的标签。


4 - 最后,你的程序应该计算并显示给所有被调查学生的总体平均分数两位小数。



问题:


1 - 我理解循环相当好,但我不知道如何让while循环从数据文件中读取信息,一个列。一次。他们不允许我们做数组,但即使他们这样做,我认为数组逐行而不是逐列。


我有一种感觉,通过使用循环,我可以操纵数据搜索到它将绕过除我需要的数据之外的所有内容,但我不知道如果这是我需要的方向,或者是如何确定如何从特定列读取数据的循环。


2 - 提供的文本文件是这个:


Bailey M CC 68

Harrison F CC 71

Grant M UN 75

彼得森F UN 69

Hsu M UN 79

Bowles M CC 75

Anderson F UN 64

Nguyen F CC 68

Sharp F CC 75

Jones M UN 75

McMillan F UN 80

Gabriel F UN 62


因为我没有输出数据到文件,而是查询文件的输入,我猜我不需要任何outfile命令,但我不确定。我猜老师在那里放了一个名单,以便更难以访问我需要的数据(如性别栏)。如果需要,我很乐意提供更多详细信息,感谢您的时间!这是我的方法的简短模板,它只是到目前为止的性别分数:

Hello everyone!
I am a computer science student taking my first C++ class and this is my first time posting a query online. So, I apologize in advance for my lack of savvy. Up until now, I have not had any problems whatsoever but my current assignment is throwing me for one heck of a loop so I was hoping one of you more experienced folk can point me in the right direction.

These are my assignment instructions:

1 - Your assignment is to prompt the user to enter the filename with the path on disk. If the file does not exist in the specified location, your program should exit with a suitable error message.

2 - The first thing your program should do is output to the screen a copy of the data read in from the disk file. This is known as “echoing” the input data.

3 - Your program should then calculate and display the average score for males, females, community college students, and university students. Display your results to two places of decimals, and write your program to automatically list your four calculated averages one to a line, along with a suitable label for each result.

4 - Finally, your program should calculate and display to two places of decimals the overall average score for all of the students surveyed.


Questions:

1 - I understand loops fairly well but I don''t know how to get the while loop to read info from a data file, one "column" at a time. They are not allowing us to do arrays yet but even if they did, I think arrays work line by line instead of column by column.

I have a feeling though, that through the use of loops, I can potentially manipulate the data search to the point that it will bypass everything except the data I need but I have no idea if that is the direction I need to go or if it is a matter of figuring out how to get a loop to read data from specific columns.

2 - The provided text file is this:

Bailey M CC 68
Harrison F CC 71
Grant M UN 75
Peterson F UN 69
Hsu M UN 79
Bowles M CC 75
Anderson F UN 64
Nguyen F CC 68
Sharp F CC 75
Jones M UN 75
McMillan F UN 80
Gabriel F UN 62

Since I am not outputting data to the file but instead, querying for input from the file, I would guess that I do not need any outfile commands but I am not sure about that. I am guessing the teacher put a list of names in there to make it harder to access the data I need (like the gender column). I am happy to provide more details if needed and thank you for your time! Here is a short template of my approach, it is just the gender scores so far:

展开 | 选择 | Wrap | 行号

推荐答案

好的,我添加了回音。在echo和伪代码上读取的错误


这里有点干净但仍然超级基本:

Ok, I added an echo. The errors read on the echo and the pseudo code

Here is is a bit more clean but still super basic:

展开 | 选择 | Wrap | 行号


展开 | 选择 | Wrap | 行号


每条记录包含4个数据元素:姓名,性别,学校类型和分数。


您可以使用cin>>来阅读这些内容。名称>>性>>等...


然后你测试学校的性别和类型,以添加到你的总数中的一些变量。我也会计算读入的记录。


放置cin>>并且总计逻辑在一个循环上运行,直到你的输入数据用完为止。


你现在拥有了你需要的所有总数,如果你保持一个记录计数你就是一个鸿沟根据记录计算你的总数来获得平均值。


从我看到的,你的程序只需要输入读数和总计逻辑,这看起来过于复杂。


另外,我看到很多双打但数据中只有整数值。我希望你想让3的平均值为1.5。如果是这种情况,那么您违反了重要的数字规则:派生数据不能比源cdata更准确。也就是说,1.5的平均值需要3.0的源数据。


我认为你应该使用int变量。


再次发布并让我知道发生了什么。
Each record contains 4 data elements: name, sex, type of school, and score.

You can read these in using cin >> name >> sex >> etc...

Then you test the sex and type of school to add to some variables that are your totals. I would also count the records read in.

Place the cin>> and the totaling logic onside a loop that runs until you run out of input data.

You now have all of the totals you need and if you have kept a inut record count you an divide your totals by the record count to get the averages.

From what I see, your program just needs chnages to the input reading and totallng logic which appear overly complicated.

Also, I see a lot of doubles but only integer values in the data. I expect you want to make the average of 3 to be 1.5. If that''s the case, you are violating sigificant figures rules: derived data cannot be more accurate than the source cdata. That is, and averge of 1.5 requires source data of 3.0.

I think you should be using int variables.

Post again and let me know what happened.


这篇关于数据文件输入排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆