矢量最小值显示为 0
我正在尝试显示向量的最大值和最小值(以及其他内容)。最大值显示正常,但最小值不断出现为 0。唯一一次不为 0 时,它最终会显示数据列表的最终值。
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
fstream file;
file.open("dataWin.txt");
vector<float> dist;
while(file.eof() == false)
{
string miles;
getline(file, miles);
float milesNum = atof(miles.c_str());
dist.push_back(milesNum);
}
float max = 0.0;
float min = 1.0;
float sum = 0.0;
float secHour = 3600;
float lastRecord = 238857;
for(int i = 0; i < dist.size(); i++)
{
if(dist[i] < min)
min = dist[i];
if(dist[i] > max)
max = dist[i];
sum += dist[i];
}
float avg = (float)sum / dist.size();
float lastSeconds = (float)lastRecord / (float)avg;
float deadline = (float)lastSeconds / (float)secHour;
cout << "National Aeronautics and Space Administration - NASA" << endl;
cout << "EYES ONLY" << endl << endl;
cout << "Statistics on solar object 326 Alba" << endl;
cout << "Danger level: HIGH" << endl << endl;
cout << "Maximum travel distance: " << max << " miles per second." << endl;
cout << "Minimum travel distance: " << min << " miles per second." << endl;
cout << "Average travel distance: " << avg << " miles per second." << endl << endl;
cout << "WARNING! OBJECT 326 Alba WILL IMPACT IN " << deadline << " HOURS!" << endl;
file.close();
return 0;
}
如何获取要显示的列表的实际最小值?
I'm trying to display both the maximum and minimum value of a vector (along with other things). The max value displays fine, but the minimum value constantly comes up as 0. The only time it's not 0, it ends up displaying the final value of the list of data.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
fstream file;
file.open("dataWin.txt");
vector<float> dist;
while(file.eof() == false)
{
string miles;
getline(file, miles);
float milesNum = atof(miles.c_str());
dist.push_back(milesNum);
}
float max = 0.0;
float min = 1.0;
float sum = 0.0;
float secHour = 3600;
float lastRecord = 238857;
for(int i = 0; i < dist.size(); i++)
{
if(dist[i] < min)
min = dist[i];
if(dist[i] > max)
max = dist[i];
sum += dist[i];
}
float avg = (float)sum / dist.size();
float lastSeconds = (float)lastRecord / (float)avg;
float deadline = (float)lastSeconds / (float)secHour;
cout << "National Aeronautics and Space Administration - NASA" << endl;
cout << "EYES ONLY" << endl << endl;
cout << "Statistics on solar object 326 Alba" << endl;
cout << "Danger level: HIGH" << endl << endl;
cout << "Maximum travel distance: " << max << " miles per second." << endl;
cout << "Minimum travel distance: " << min << " miles per second." << endl;
cout << "Average travel distance: " << avg << " miles per second." << endl << endl;
cout << "WARNING! OBJECT 326 Alba WILL IMPACT IN " << deadline << " HOURS!" << endl;
file.close();
return 0;
}
How can I get the actual min value of the list to display?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是,当输入结束时,
getline
调用将失败,并且不会更新字符串,然后您将空字符串转换为浮点数,这在atof
中失败调用(如果atof
失败,它将返回0.0
)并将该值推入向量。向量中的最小值现在为0
并且算法正确地将其生成为最小值。如果您要在 C++ 中进行编码,并假设输入的唯一内容是浮点数和空格,则可以通过直接读取浮点数来使代码更简单:
更简单的是,您可以通过使用迭代器和STL 中的算法:
这只是为了展示现有算法可以做什么。在您的特定情况下,我将使用 std::copy 和 std::istream_iterator 来读取输入,但为了处理已读取的数字,我将展开手动循环(这样所有的最小值、最大值和总和都可以在一次中计算出来,上面的 STL 示例在三次中完成)。
The problem is that when input ends the
getline
call will fail and it will not update the string, you are then converting an empty string to a float which is failing in theatof
call (ifatof
fails, it will return0.0
) and pushing that value into the vector. The minimum value in the vector is now0
and the algorithm is correctly yielding it as the minimum.If you are going to code that in C++, and assuming that the only contents of the input are floats and spaces, you can make the code simpler by reading directly into a float:
Even simpler, you can avoid the loop altogether by using iterators and algorithms from the STL:
This is just to show what can be done with existing algorithms. In your particular case I would use the
std::copy
andstd::istream_iterator
s for reading the input, but for processing the already read numbers I would unroll a manual loop (so that all min, max and sum can be calculated in one pass, the STL examples above do it in three passes).这:
应该是:
您从哪里得到需要调用 eof() 的想法?你不这样做,而且你这样做的方式是错误的!
This:
should be:
Where are you picking up the idea you need to call eof() from? You don't, and doing so in the way you are doing it is wrong!