使用循环创建条形图
Creating a bar chart using loops
我的分配是使用输入文件创建条形图。它应该看起来像这样:
1930:***
1950: ******
1970 *****
etc
我已经写了一切,但它一直显示出这样的东西:
1930
1950
1970
: ***
:******
:****
我似乎无法让它们正确显示。到目前为止,我的代码是我的代码:
#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
//variables
int inputNum;
int year;
ifstream inputFile;
inputFile.open("People.txt");
if (!inputFile) // file did not open
{
cout << "Input file did not open" << endl;
return 10;
}
for (int year = 1910; year <= 2010; year += 20)
cout << year << endl;
while (inputFile >> inputNum)
{
for (int counter = 0; counter < (inputNum / 1000); counter++)
{
cout << "*";
}
cout << endl;
}
return 0;
}
此行:
cout << year << endl;
最后不需要endl
。将其删除,这样您才有:
cout << year;
这可以用std::map
#include <iostream>
#include <fstream>
#include <string>
#include <map>
int main() {
std::fstream input_file("test.txt");
std::map<int, int> years;
std::string temp;
while(input_file >> temp){
//add if it doesnt exist, otherwise, increment it
(years.find(std::stoi(temp)) == years.end()) ? years[std::stoi(temp)] = 1 : years[std::stoi(temp)]++;
}
for(auto i = years.begin(); i != years.end(); i++){
std::cout << i->first << ": ";
for(int j = 0; j < i->second; j++){
std::cout << "*";
}
std::cout << "n";
}
return 0;
}
示例txt文件:
1930
1930
1930
1950
1950
1950
输出:
1930 ***
1950 ***
这将需要文件中的任何一年,如果您只想要某些年份,就可以轻松地放置额外的语句来检查
相关文章:
- Qt 图表条形图调整大小崩溃
- 在c++中从包含新操作的python中创建的pb图创建图
- 通过显示一行星号来创建条形图,该条形图将读取数据的输入文件
- QT 条形图/QT 商业 5.6 的图形
- 使用星号在条形图中输出负值,以表示三个值的范围
- 使用 OpenGL 使用 C++ 以图形方式表示堆积条形图
- 如何在C++中创建 ASCII 艺术条形图
- 使用循环创建条形图
- 使用 char*array 存储条形图的条形图,以便我可以动态制作它并作为字符串返回
- 创建.svg条形图
- Qt.的浮动条形图
- 输出图表/条形图的棒球平均值
- 五家商店的销售条形图
- C++ :使用 * 显示数组数据的条形图
- 销售条形图
- 在QCustomPlot中,如何实时绘制相同数据的折线图和条形图
- 如何为矢量<类型*>条形图*赋值*
- 显示条形图(和其他时髦的图形)
- 通过与输入相同的boost图创建无向图
- 优化网络图创建