简体   繁体   English

C ++输入/输出

[英]C++ Input/output

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int a , b , c , i , n;
    int d = 0;
 ifstream myfile;
 myfile.open("Duomenys1.txt");
 myfile >> n;
 for (int i = 0; i < n; i++ )
 {
     myfile >> a >> b >> c;
     d +=  (a + b + c)/3 ;
 }
ofstream myotherfile;
myotherfile.open ("Rezultatai1.txt");
myotherfile << d;
myotherfile.close();
myotherfile.close();
return 0;
}

The programs should read 3 (3 is n) rows of numbers (5 7 4 ; 9 9 8; 8 7 8), rows are summed up separately and given 3 different averages (7 ; 9 ; 8) in the Rezultatai1.txt file. 程序应读取3(3为n)行数字(5 7 4; 9 9 8; 8 7 8),分别对行进行累加并在Rezultatai1.txt文件中给出3个不同的平均值(7; 9; 8) 。 But I only get -2143899376 result. 但是我只得到-2143899376结果。

The problem isn't the huge number, I need the program to give every row's average number separately in the output file, so that in the output file its written (7 ; 9 ; 8) 问题不是很大的数字,我需要程序在输出文件中分别给出每一行的平均数,以便在输出文件中写入它的(7; 9; 8)

You must make one output per line and you must use floating point arithmetic followed by rounding if you want rounded averages. 如果要取四舍五入平均值,则必须每行输出一个,并且必须使用浮点运算然后进行四舍五入。

#include <iostream>
#include <iostream>
#include <cmath>

int main()
{
  const int numbers_per_lines = 3;
  std::ofstream output("Rezultatai1.txt");
  std::ifstream input("Duomenys1.txt");
  int number_of_lines;
  input >> number_of_lines;
  for(int i=0; i<number_of_lines; ++i) {
    double sum=0;
    for(int num=0; num<numbers_per_line; ++num) {
      double x;
      input >> x;
      sum += x;
    }
    output << i << ' ' << std::round(sum/numbers_per_line) << std::endl;
  }
}

Two problems: First of all you don't do any rounding, instead since you use integer arithmetic the result is truncated . 两个问题:首先,您不进行任何舍入,而是因为使用整数运算,结果将被截断 There are a couple of ways to do rounding, one of the simple is to use floating point arithmetic, and use eg std::round (or std::lround ) to round to nearest integer value. 舍入有两种方法,一种简单的方法是使用浮点算术,并使用例如std::round (或std::lround舍入到最接近的整数值。 Like eg 像例如

d = std::round((a + b + c) / 3.0);

Notice the use of the floating point literal 3.0 when dividing. 注意除法时使用浮点字面量3.0

The second problem is that you don't write the averages, you sum all averages and write the sum. 第二个问题是您不写平均值,而是对所有平均值求和并求和。 This can be fixed by simple write the average in the loop instead of after the loop, and use plain assignment instead of increase-and-assign. 可以通过在循环中而不是循环后简单地写入平均值,并使用普通分配而不是增量分配来解决此问题。

I'd suggest this 我建议这个

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    freopen("Duomenys1.txt", "r", stdin);    // Reopen stream with different file
    freopen("Rezultatai1.txt", "w", stdout);
    int n, a, b, c;
    cin >> n;
    while (n--) {
        cin >> a >> b >> c;
        cout << (a + b + c) / 3 << endl;
    }
    return 0;
}

Input 输入

3
5 7 4
9 9 8
8 7 8

Output 产量

5
8
7

See DEMO . 参见DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM