我试图写一个程序,将读取最高和最低的电压,以及它的相关放大器和时间。我能找到最大值,但我得到的是最小值的所有零。我需要帮助。请谢谢你。
txt文件示例:
Time Volt Ampere
0.0001 9.77667 0.147408
0.00015 9.76583 0.147525
0.0002 9.76833 0.147692
0.00025 9.75833 0.147442
0.0003 9.76833 0.147192
0.00035 9.78167 0.1473
0.0004 9.76667 0.147317
0.00045 9.765 0.14715
0.0005 9.75667 0.147
0.00055 9.765 0.14695
0.0006 9.77 0.1471
0.00065 9.7675 0.147417
0.0007 9.7725 0.147417
0.00075 9.755 0.14735
0.0008 9.765 0.147725
0.00085 9.76583 0.147783预期最低产出:
Time = 0.00075 Volt = 9.755 Ampere = 0.14735预期最大产出:
Time= 0.00035 Volt = 9.78167 Ampere = 0.1473int main(void)
{
//Declare Variables
string a, b, c;
double time, volt, ampere;
double maxVolt = 0, maxTime, maxAmpere;
double minVolt = 10, minTime, minAmpere;
//Read from the Volts.txt file
ifstream myFile("D:\\tabit\\Documents\\Volts.txt");
//Check if the file can be opened
if (myFile.is_open())
{
while (myFile >> a >> b >> c)
{
time = atof(a.c_str());
volt = atof(b.c_str());
ampere = atof(c.c_str());
if (volt >= maxVolt)
{
maxTime = time;
maxVolt = volt;
maxAmpere = ampere;
}
if (volt < minVolt)
{
minTime = time;
minVolt = volt;
minAmpere = ampere;
}
}
//Close the file
myFile.close();
}
//Give error message if the file cannot be opened
else return(1);
//Display the Maximum results
cout << "Max Volt: " << maxVolt << endl;
cout << "Max Time: " << maxTime << endl;
cout << "Max Ampere: " << maxAmpere << endl;
//Display the Minimum results
cout << "Min Volt: " << minVolt << endl;
cout << "Min Time: " << minTime << endl;
cout << "Min Ampere: " << minAmpere << endl;
return 0;
}发布于 2018-11-01 20:57:15
您的代码缺少所需的标头--至少是<fstream>和<iostream>。似乎还有一个using namespace std;潜伏在某个地方;这是一个错误的做法,可以巧妙地破坏您的代码,所以我建议您显式地限定您使用的名称(为了节省输入,std是一个非常短的名称)。
在C++中,我们可以声明int main() --这是一个原型,与我们需要编写int main(void)的C语言不同。在C++看来,后者无疑是异乎寻常的。
如果文件打开失败,提前返回可能会更清楚:
if (!myFile) {
std::cerr << "Couldn't open input file";
return 1;
}我对编译后的文件名有点不舒服,这使得程序变得相当不灵活。要么提供名称作为命令行参数,要么简单地从标准输入读取,以便程序可以从任何文件或管道中读取。
如果忽略结果,关闭输入流就没有什么意义了--流的析构函数会为我们完成这个任务,所以让它超出范围就行了。
请注意,std::endl包括输出流的刷新-最好将'\n'写成行结束符(该流将在程序出口处刷新)。
发布于 2018-11-02 03:55:55
using指令.std::pair。0.0和10.0myFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');的第一行(需要#include <limits>)。std::endl发送一个'\n'字符,然后刷新输出缓冲区,除非您知道必须这样做,否则您不需要支付刷新的费用。当您需要它时,显式地使用std::刷新来显示您的意图。RAII会神奇地为您做这件事。把这一切结合起来,它就会:
#include <fstream>
#include <iostream>
#include <limits>
#include <utility>
struct Record
{
double volt{};
double time{};
double ampere{};
};
int main()
{
std::ifstream myFile("records.txt");
if (!myFile) {
std::cerr << "Couldn't open input file";
return 1;
}
myFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Record current{};
auto minmax = std::make_pair<Record, Record>({10.,10.,10.}, {0.,0.,0.});
while (myFile >> current.time >> current.volt >> current.ampere) {
if (current.volt < minmax.first.volt) {
minmax.first = current;
}
if (current.volt > minmax.second.volt) {
minmax.second = current;
}
}
//Display the Minimum results
std::cout << "Min Volt: " << minmax.first.volt << '\n';
std::cout << "Min Time: " << minmax.first.time << '\n';
std::cout << "Min Ampere: " << minmax.first.ampere << '\n';
//Display the Maximum results
std::cout << "Max Volt: " << minmax.second.volt << '\n';
std::cout << "Max Time: " << minmax.second.time << '\n';
std::cout << "Max Ampere: " << minmax.second.ampere << '\n';
return 0;
}注意:您也可以将记录放在向量中。这样,您可以轻松地处理数据(筛选、排序、.)或者用std::minmax找到最小值和最大值
编辑:至少,如果你觉得我活该,告诉我为什么
https://codereview.stackexchange.com/questions/206744
复制相似问题