C++ :使用 * 显示数组数据的条形图
C++ : display a bar chart of array data using *
我正在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 : ***
相关文章:
- Qt 图表条形图调整大小崩溃
- 通过显示一行星号来创建条形图,该条形图将读取数据的输入文件
- QT 条形图/QT 商业 5.6 的图形
- 使用星号在条形图中输出负值,以表示三个值的范围
- 二进制文件中的某些数据如图所示,并以奇怪的方式显示OS
- 使用 OpenGL 使用 C++ 以图形方式表示堆积条形图
- 如何在C++中创建 ASCII 艺术条形图
- 使用循环创建条形图
- 使用 char*array 存储条形图的条形图,以便我可以动态制作它并作为字符串返回
- 创建.svg条形图
- Qt.的浮动条形图
- 输出图表/条形图的棒球平均值
- 五家商店的销售条形图
- C++ :使用 * 显示数组数据的条形图
- 销售条形图
- 在QCustomPlot中,如何实时绘制相同数据的折线图和条形图
- 如何在Qt中绘制数据流图
- 如何为矢量<类型*>条形图*赋值*
- 显示条形图(和其他时髦的图形)
- 条形图程序行为奇怪