c++ 未从input.txt文件读取输入文件

vcirk6k6  于 2023-01-10  发布在  其他
关注(0)|答案(1)|浏览(125)
#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
using namespace std;

typedef long long int ll;

int main()
{
    istream in("input.txt", "r", stdin);
    ofstream out("output.txt", "w", stdout);
    int t;
    in >> t;
    while (t--)
    {
        ll n;
        in >> n;
        ll arr[n];
        for (int i = 0; i < n; i++)
        {
            in >> arr[i];
        }
        sort(arr, arr + n);
        double sum = 0;
        for (int i = 0; i < n - 1; i++)
        {
            sum += arr[i];
        }
        double ans = (sum / (n - 1)) + arr[n - 1];
        out << setprecision(9) << ans << endl;
    }
    return 0;
}

请注意,我还更改了tasks.json文件。
我按照@john和@PepjinKramer的说法修改了代码,但是我的输入没有从input.txt文件中读取,所以output.txt中没有显示任何内容。
我该如何解决这个问题?

jk9hmnmh

jk9hmnmh1#

以下是一些反馈,展示了如何减少大量C++代码中的“货物崇拜”内容:

// #include <bits/stdc++.h> <== NO not standard C++
#include <algorithm> // for sort
#include <iostream>
#include <fstream>
#include <vector> // for resizable arrays
#include <numeric> // for accumulate
#include <iomanip> // for setprecission

// using namespace std; <== NO will become a problem in big projects (nameclashes0

// typedef long long int ll; <== NO just use standard types in code e.g. std::size_t or std::int64_t

int main()
{
    //istream in("input.txt", "r", stdin);      // reading from file doesn't need anything from stdin
    //ofstream out("output.txt", "w", stdout);  // writing to a file doesn't need anything from stdout

    // std::ifstream input{ "input.txt" };
    // std::ofstream output{ "output.txt" };

    // for testing manual input
    auto& input = std::cin;
    auto& output = std::cout;
    
    std::size_t number_of_averages; // give variables more meaningful names then just 't'

    std::cout << "input number of averages to calculate = ";
    input >> number_of_averages;

    for(std::size_t n = 0; n < number_of_averages; ++n )
    {
        //ll n; NO sizes of containters should be std::size_t not long long
        //ll n;
        //in >> n;

        std::size_t number_of_values;
        std::cout << "input number of values = ";
        input >> number_of_values;

        // ll arr[n]; // NO C++ doesn NOT have variable length arrays, use std::vector
        std::vector<double> values(number_of_values);

        // for (int i = 0; i < n; i++) // Do NOT use index based loops if you don't have to (out of bound bugs)
        //{
        // use range based for loops and learn about (const) references
        for(auto& value : values)
        {
            std::cout << "input number : ";
            input >> value;
        }

        // sort(arr, arr + n); // No use iteraters
        std::sort(values.begin(), values.end());

        // NO for summing collections use <algorithm> accumulate
        /*
        double sum = 0;
        for (int i = 0; i < n - 1; i++)
        {
            sum += arr[i];
        }
        */
        auto sum = std::accumulate(values.begin(), values.end(), 0.0);
        auto average = sum / static_cast<double>(values.size());

        std::cout << "average = ";
        output << std::setprecision(9) << average << std::endl;
    }
    return 0;

}

相关问题