找到最小的最大奇数和偶数来自一组数据

时间:2016-11-01 21:04:20

标签: c++ algorithm loops numbers

我需要找到最小的最大奇数和偶数来自一组数据。

数据文件示例

2

1 2 4 5 6 7 8

2 5 6 7 4 2 1

第一行给出了需要读取的行数(在示例中为2)

这就是我试过的

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>


using namespace std;

int main()

{

    ifstream infile;
    ofstream outfile;

    infile.open("input.txt");
    outfile.open("output.txt");

    int max = INT_MIN, min = INT_MAX, num_even = 0, num_odd = 0;
    int number, sum_even=0;
    int counter = 0;
    cout << "Test\n\n";
    cout << setw(15)<<left<<"Line";
    cout << setw(15) << left << "Smallest #";
    cout << setw(15) << left << "Largest #";
    cout << setw(15) << left << "# of Odds";
    cout << setw(15) << left << "Sum of evens\n\n";

    for (infile>>counter; counter>=1; counter--)
    {
        while (infile >> number)
        {

            if (number % 2 == 0) {
                num_even++;
                sum_even = sum_even + num_even;
            }

            else
                num_odd++;

            if (number > max)
                max = number;

            if (number < min)
                min = number;
        }
        cout << setw(15) << left << counter;
        cout << setw(15) << left << min;
        cout << setw(15) << left << max ;
        cout << setw(15) << left << num_odd;
        cout << setw(15) << left << sum_even << endl;
    }

    infile.close();
    outfile.close();
    system("pause");
    return 0;
}

它只是将数据作为一个完整的集合读取而不是逐行

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<climits>

using namespace std;

int main()
{
    ifstream infile;
    ofstream outfile;

    infile.open("input.txt");
    outfile.open("output.txt");

    int counter = 0;

    cout << "Test\n\n";
    infile >> counter;
    string s;
    getline(infile, s, '\n' ); // get the '\n' after the first number
    for (int times = 1; times <= counter; times++)
    {
        // Initialize your variables
        int max = INT_MIN, min = INT_MAX, num_even = 0, num_odd = 0;
        int number, sum_even=0;

        // print the headers
        cout << setw(15) << left <<"Line";
        cout << setw(15) << left << "Smallest #";
        cout << setw(15) << left << "Largest #";
        cout << setw(15) << left << "# of Odds";
        cout << setw(15) << left << "Sum of evens\n";

        // get the next line, ending with '\n' (you'll have to check if your line endings depending on linux/windows)
        getline(infile, s, '\n' );
        // string stream
        istringstream is( s );

        // for each number in the string, make your validations
        while ( is >> number ) 
        {
            if (number % 2 == 0) {
                num_even++;
                // is this what are you after? couse you had 'num_even' here
                sum_even += number;
            }
            else
                num_odd++;

            if (number > max)
                max = number;

            if (number < min)
                min = number;
        }

        // print results
        cout << setw(15) << left << times;
        cout << setw(15) << left << min;
        cout << setw(15) << left << max ;
        cout << setw(15) << left << num_odd;
        cout << setw(15) << left << sum_even << endl;
    }

    infile.close();
    outfile.close();
    system("pause");
    return 0;
}

编辑:更改代码以省略使用向量