【发布时间】:2015-10-24 03:11:38
【问题描述】:
我有以下 .txt 文件:
23 43 -10
65 1 -1
-3 3 3
400 401 -389
21 6 -6
0 0 0
我需要编写一个程序,它会从文件中读取数据,直到它读取包含三个 0 的行。
然后我需要编写一个函数,它接受三个整数并返回最接近 0 的数字。如果两个或三个值与 0 的距离相同,则返回最接近 0 的第一个数字。
这是我目前所拥有的:
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int findClosest(int, int, int);
int main()
{
ifstream fin;
infile.open("LAB5DATA.TXT");
int a, b, c;
while (infile >> a >> b >> c && a + b + c != 0)
{
int closest = findClosest(a, b, c);
cout << closest << endl;
}
infile.close();
return 0;
}
int findClosest(int a, int b, int c)
{
int differenceA = abs(a - 0);
int differenceB = abs(b - 0);
int differenceC = abs(c - 0);
int closest = differenceA;
if (differenceB < closest)
closest = differenceB;
if (differenceC < closest)
closest = differenceC;
return closest;
}
任何帮助将不胜感激!
【问题讨论】:
-
为您的
findClosest函数付出一些努力,并向我们展示您的尝试。我们不会为你做作业。 -
a + b + c != 0你有没有想过会发生什么,例如。例如,a=3 b=-2 c=-1?
标签: c++ function file visual-c++