Задача: Считать числа из файла, разделенные пробелом, посчитать их НОД и вывести количество чисел в файле
Первое отлично работает, а вот второе, выдает на одно число больше, если в файле постатить перевод на новую строку, как с этим справится? Какая проверка должны быть?
| Код | #include <iostream> #include <fstream>
using namespace std;
int ae(int a, int b) { a=abs(a); b=abs(b);
while (a!=b) (a>b)?a=a-b:b=b-a; return b; }
int main() { int pr,nxt,t=1; ifstream in("in.txt"); if (!in) { cout << "Cannot open input file.\n"; return 1; }
in >> pr; while (!in.eof()) { in>>nxt; t++; pr = ae(nxt,pr); } in.close();
ofstream out("out.txt"); if(!out) { cout << "Cannot open output file.\n"; return 1; }
out << pr << " " << t ; out.close();
return 0; }
|
|