C++ :使用 * 显示数组数据的条形图

C++ : display a bar chart of array data using *

本文关键字:数据 条形图 数组 显示 使用 C++      更新时间:2023-10-16

我正在C++11中制作一个程序,代表每年的降雨量统计数据。我几乎完成了该计划,但我被困在最后阶段。我需要使用 * 设计/显示条形图,以显示数组中的数据。例如:2015年英国每月降雨量(mm)为:154.3、79.2、95.6、46.3、109.6、55.1、109.5、107.4、54.0、72.2、176.0、230.0。

我希望条形图看起来像这样:

0 - 50 : *
50 - 80 : ****
80 - 110 : ****
110 - 140 : 
140 - more : ***

这是我写的代码,但它给了我不正确的输出

void RainFall::outputBarChart() const {
cout<< "Bar Chart for RainFall Amount:"<< endl;
const size_t frequencySize= 5;
array<unsigned int, frequencySize > frequency = { }; 
for(int mark : amount)
++frequency[mark / 50];
for(size_t count = 0; count < frequencySize; ++count) {
if (count == 0)
cout << " 0-49: ";
else
cout<< count * 50 << "-"<< (count * 50) + 30 << ": ";
       for (unsigned int stars = 0; stars < frequency[count]; ++stars)
       cout<< '*';
       cout << endl;
}
}

它在输出中给了我以下条形图

0 - 49 : *
50 - 80 : *****
100 - 130 : ***
150 - 180 : **
200 - 230 : **

尝试使用矩阵来保存"图表"。像这样:

char chart[][] = char[12][4];
// fill the "chart" with blanks!
for (auto idx=0; idx<12; ++idx)
{
    if (rain[idx] <= 50)
    {
        chart[idx][3] = '*';
    } else if (rain[idx] > 50 && rain[idx] <= 80)
    {
        chart[idx][3] = '*';
        chart[idx][2] = '*';
        chart[idx][1] = '*';
        chart[idx][0] = '*';
    } //etc to cover all cases
}
// now print the matrix
for (auto idxCol=0; idxCol<4; ++idxCol)
{
    for (auto idxLn=0; idxLn<12; ++idxLn)
        std::cout << chart[idxLn][idxCol];
    std::cout << std::endl;
}

它可以优化,但我相信你可以做到这一点。

这是我的解决方案:

#include <iostream>
#include <string>
using namespace std;
struct barChart_t
{
    int minValue = -1;
    int maxValue = -1;
    string counts;
} myBarChart[5];
int main()
{
    // Rainfall data
    float UK_monthlyRainfall_mm[] = 
        {154.3, 79.2, 95.6, 46.3, 109.6, 55.1, 
        109.5, 107.4, 54.0, 72.2, 176.0, 230.0};
    // Initialize desired chart ranges
    myBarChart[0].minValue = 0;
    myBarChart[0].maxValue = 50;
    myBarChart[1].minValue = myBarChart[0].maxValue;
    myBarChart[1].maxValue = 80;
    myBarChart[2].minValue = myBarChart[1].maxValue;
    myBarChart[2].maxValue = 110;
    myBarChart[3].minValue = myBarChart[2].maxValue;
    myBarChart[3].maxValue = 140;
    myBarChart[4].minValue = myBarChart[3].maxValue;
    // Check data ranges and increment counter  
    for (int month = 0; month < 12; month++)
    {
        for (int line = 0; line < 5; line++)
        {
            if (myBarChart[line].maxValue != -1)
            {
                if ((UK_monthlyRainfall_mm[month] >= myBarChart[line].minValue) && 
                    (UK_monthlyRainfall_mm[month] <= myBarChart[line].maxValue))
                    myBarChart[line].counts = myBarChart[line].counts + '*';
            }
            else if (UK_monthlyRainfall_mm[month] >= myBarChart[line].minValue)
            {
                myBarChart[line].counts = myBarChart[line].counts + '*';
            }
        }
    }
    // Print chart
    for (int line = 0; line < 5; line++)
    {
        if (myBarChart[line].maxValue != -1)
            cout << myBarChart[line].minValue << " - " << myBarChart[line].maxValue << " : " << myBarChart[line].counts << endl;
        else
            cout << myBarChart[line].minValue << " - " << "more" << " : " << myBarChart[line].counts << endl;
    }
    return 0;
}

如您所见,我使用了一个结构来保存每行所需的信息,并使用了一个结构数组来保存条形图。-1 最大值验证用于识别"更多"情况。输出:

0 - 50 : *
50 - 80 : ****
80 - 110 : ****
110 - 140 :
140 - more : ***