c++ 为降雨程序创建并行阵列

hts6caw3  于 2023-04-13  发布在  其他
关注(0)|答案(1)|浏览(147)

我已经写了我的大部分降雨程序和所有的功能工作如何我需要他们。我得到的总,平均,最小和最大降雨量的用户输入使用一个数组。
我有一个用于月份名称的常量字符串数组,但我需要在第116行和第119行显示“month”注解或类似的内容。
另外,有没有一种方法可以让月份名称与用户输入一起显示,而不是1:2:等等?如果不是,那不是最迫切需要解决的问题。
下面是我目前的代码

#include <iostream>
#include <iomanip>
#include <string>
#include <limits>

using namespace std;

const int ARRAY_SIZE = 12;

double inputValidate(double user_number);
void getValues(double []);
double Total(double []);
double average(double);
double GetMinRainfall(double []);
double GetMaxRainfall(double []);
void display(double, double, double, double);

int main()
{
    double rainfall[ARRAY_SIZE];

    cout << "Enter Average Rainfall for Each Month:" << endl;

    getValues(rainfall);
 
    double total_rainfall = Total(rainfall);
    double average_rainfall = average(total_rainfall);
    double minimum_rainfall = GetMinRainfall(rainfall);
    double maximum_rainfall = GetMaxRainfall(rainfall);

    display(total_rainfall, average_rainfall, minimum_rainfall, maximum_rainfall);

    return 0;
}

double inputValidate(double user_number)
{
    while (!(cin >> user_number) || user_number < 0)
    {
        cout << "Error: please enter a positive number." << "Try again: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(),'\n');
    }
    return user_number;
}

void getValues(double array[])
{
    for (int i = 0; i < ARRAY_SIZE; i++)
    {
        cout << (i + 1) << ": ";
        array[i] = inputValidate(array[i]);
    }
}

double Total(double array[])
{
    double total = 0;

    for (int i = 0; i < ARRAY_SIZE; i++)
        total += array[i];

    return total;
}

double average(double total)
{
    return total / ARRAY_SIZE;
}

double GetMinRainfall(double array[]) 
{
    double min = array[0];
    for (int i = 1; i < ARRAY_SIZE; i++)
    {
        if (min > array[i])
            min = array[i]; 
    }
     
    return min;
}

double GetMaxRainfall(double array[])
{
    double max = array[0];
    for (int i = 1; i < ARRAY_SIZE; i++)
    {
        if (max < array[i])
            max = array[i]; 
    }
    
    return  max;
}

void display(double total, double average, double minimum_rainfall, double maximum_rainfall)
{
    const string MONTHS[12] = {
        "January", "February", "March", 
        "April" , "May" , "June",
        "July" , "August" , "September",
        "October", "November", "December"
    }; 

    cout << "Total Rainfall for the year is " << total << " inches.";
    cout << "\nAverage Rainfall for the year is " << average << " inches.";
    cout << "\nThe least rain fell in "<< /* month */ "with " << minimum_rainfall << " inches."; 
     
    cout << "\nThe most rain fell in "<< /* month */ "with "<< maximum_rainfall << " inches.";
}
vd2z7a6w

vd2z7a6w1#

因此,您需要收集每个月的降雨量数据,然后输出哪个月的降雨量最大和最小。总位和平均位不是问题。问题是如何将最小值和最大值与月份名称关联起来。
如果我们进入并行数组的兔子洞,那么索引是关键。在下面,为了简单起见,我将硬编码输入。

#include <iostream>
#include <string>

int main() {
    std::string months[] = {
        "January", "February", "March",
        "April" , "May" , "June",
        "July" , "August" , "September",
        "October", "November", "December"
    };

    double rainfall[] = {
        1.1, 2.2, 3.3,
        0.9, 6.7, 8.4,
        0.3, 7.8, 10.2,
        0.4, 12.1, 5.7
    };

    double min_rain = 0.0, max_rain = 0.0;
    std::string min_month, max_month;

    for (int i = 0; i < 12; i++) {
        if (i == 0 || rainfall[i] < min_rain) {
            min_rain = rainfall[i];
            min_month = months[i];
        }

        if (rainfall[i] > max_rain) {
           max_rain = rainfall[i];
           max_month = months[i];
        }
    }

    std::cout << min_month << " had the least rain." << std::endl
              << max_month << " had the most rain."  << std::endl;

    return 0;
}

这实际上并不是那么糟糕。但是现在让我们想象一下我们想要对它们进行 * 排序 *。现在它变得更加复杂,维护相应的顺序。
但是,如果我们使用聚合数据结构来存储数据呢?现在我们只有一个数组,通过提供一个自定义的lambda形式的比较器,我们可以很容易地使用std::sort进行排序。

#include <iostream>
#include <string>
#include <algorithm>

struct RainData {
    std::string month;
    double rainfall;
};

int main() {
    RainData data[] = {
        {"January", 1.1}, {"February", 2.2}, {"March", 3.3},
        {"April", 0.9}, {"May", 6.7}, {"June", 8.4},
        {"July", 0.3}, {"August", 7.8}, {"September", 10.2},
        {"October", 0.4}, {"November", 12.1}, {"December", 5.7}
    };

    std::sort(
      data, data+12,
      [](auto a, auto b){
        return a.rainfall > b.rainfall;
      }
    );

    std::cout << data[0].month << " had the least." << std::endl
              << data[11].month << " had the most." << std::endl;

    return 0;
}

相关问题