下面是我的代码。我的算法有问题。它显示输入文件中整数的最后一个最大值和最小值。价值观。有人可以看看并告诉我我做错了什么吗?
#include "cstdlib"
#include "iostream"
#include "fstream"
using namespace std;
int main()
{
fstream instream;
ofstream outstream;
instream.open("num.txt");
if(instream.fail())
{
cout<<"The input file failed to open\n";
exit(1);
}
outstream.open("output.txt");
if(outstream.fail())
{
cout<<"The output file failed to open";
exit(1);
}
int next, largest, smallest;
largest = 0;
smallest = 0;
while(instream>>next)
{
largest = next;
smallest = next;
if(largest<next)
{
largest = next;
}
if(smallest>next)
{
smallest = next;
}
}
outstream<<"The largest number is: "<<largest<<endl;
outstream<<"The smallest number is: "<<smallest<<endl;
instream.close();
outstream.close();
return 0;
}