c++ - C++ 中的垂直条形图

标签 c++ graph bar-chart

好的,我正在尝试根据文件中的值制作垂直条形图。下面的代码在某种程度上是有效的,并且水平打印,但每行一个星号,这意味着有空格(很明显)。不是寻找填鸭式的答案,只是朝着正确的方向努力。

using namespace std;

int main()  
{
int counter;
cout<<"Please enter a number"<< "\n";
counter=0;

char *fname = "C:/Users/Jordan Moffat/Desktop/coursework/problem2.txt";
int x;

ifstream infile(fname);

while (infile >> x)
{ 
    if (x==0 && x<=10){
        cout<<"*"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\n";
        }
    else if (x>=10 && x<=20){
        cout<<"\t"<<"*"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\n";
    }
    else if (x>=20 && x<=30){
        cout<<"\t"<<"\t"<<"*"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\n";
    }
    else if (x>=30 && x<=40){
        cout<<"\t"<<"\t"<<"\t"<<"*"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\n";
    }
    else if (x>= 40 && x<=50){
        cout<<"\t"<<"\t"<<"\t"<<"\t"<<"*"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\n";
    }
    else if (x>=50 && x<=60){
        cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"*"<<"\t"<<"\t"<<"\t"<<"\t"<<"\n";
    }
    else if (x>=60 && x<=70){
        cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"*"<<"\t"<<"\t"<<"\t"<<"\n";
    }
    else if (x>=70 && x<=80){
        cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"*"<<"\t"<<"\t"<<"\n";
    }
    else if (x>=80 && x<=90){
        cout<<"*"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"*"<<"\t"<<"\n";
    }
    else if (x>=90 && x<=100){
        cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"*"<<"\n";

    }

}
cout<<"====================================================================================="<< "\n";   
cout<<"0-9"<<"10-19"<<"20-29"<<"30-39"<<"40-49"<<"50-59"<<"60-69"<<"70-79"<<"80-89"<<"90-100"<<"\n";
system("PAUSE");
 }

最佳答案

你有两个问题。显然,您想要构建一个直方图,并且想要可视化该直方图。

直方图 构建直方图的一种方法要求您预先指定 bin 的数量(均匀宽度)、最小值(含)和最大值(不含)。然后你可以计算每个项目应该分配到的 bin 的索引。

这是一个(未经测试的)示例:

const int nbins = 10;
const double minval = .0, maxval = 100.;
std::vector<int> bins(nbins, 0);
for (double x; infile >> x; ) {
  if (x >= minval && x < maxval) {
    // note that integer rounding is probably towards zero, not towards -inf
    int idx = floor((x-minval)/(maxval-minval)*nbins);
    bins[idx]++;
  }
  else {
    // handle outlier
  }
}

可视化 this answer 中描述的方法似乎合适。对于大 bin 计数,您可能需要一些规范化过程,即将值缩放到 [0,10] 或类似范围内。

看看这个(未经测试的)例子:

const int chart_height = 10;
const int max_count = *std::max_element(bins.begin(), bins.end());
for (int current_height = chart_height; current_height > 0; --current_height) {
    for (int count : bins) {
        const int bar_height = (count*chart_height)/max_count;
        if (bar_height < current_height)
            std::cout << "     "; // we're still above the bar
        else if (bar_height == current_height)
            std::cout << "  _  "; // reached the top of the bar
        else // bar_height > current_height
            std::cout << " | | "; // now the rest of the bar ...
    }
    std::cout << '\n';
}

通过一点点摆弄和格式化魔法,您还可以扩展它以生成边界灵活的可视化效果,如 this :

   11 |              _______    _______                                   
      |             |       |  |       |                                  
      |             |       |  |       |                                  
      |             |       |  |       |                                  
      |             |       |  |       |   _______                        
    5 |             |       |  |       |  |       |                       
      |             |       |  |       |  |       |                       
      |             |       |  |       |  |       |              _______  
      |   _______   |       |  |       |  |       |   _______   |       | 
      |  |       |  |       |  |       |  |       |  |       |  |       | 
      +------v----------v----------v----------v----------v----------v-----
         3.7 - 4.3  4.3 - 4.9  4.9 - 5.6  5.6 - 6.2  6.2 - 6.8  6.8 - 7.4 

关于c++ - C++ 中的垂直条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9988196/

相关文章:

c++ - 在使用 push_back() 实例化对象时,如何将指针传递给对象的构造函数?

c++ - 如何从特殊容器中的TextInput获取文本[Nokia Qt Qml]

reactjs - 如何在react-chartjs-2中显示每个柱的值

javafx-2 - JavaFx 2.x : BarChart with both axes as numbers

android - 在 MPandroid 图表(条形图)中为 X 轴设置标签

c++ - clang-format:在 lambda 函数和外部 block 的大括号之前中断

c++ - 如何使用 Qt 获取系统代理?

algorithm - Bellman-Ford 算法和步骤数

graph - 为图/分子比较算法调整文本搜索

algorithm - 对 dijkstra 算法的困惑?