C ++:使用*

时间:2016-01-27 14:47:54

标签: c++ arrays c++11 charts

我正在用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 : **

2 个答案:

答案 0 :(得分:0)

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

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;
}

可以优化,但我相信你可以做到。

答案 1 :(得分:0)

这是我的解决方案:

#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 maxValue验证用于识别&#34;更多&#34;情况。输出:

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