C++ C++;星图
我最近创建了一个由星号组成的图形,我需要在星号旁边显示文本,如下所示:C++ C++;星图,c++,vector,graph,C++,Vector,Graph,我最近创建了一个由星号组成的图形,我需要在星号旁边显示文本,如下所示: SALES BAR CHART (each * equals £100) Store 1: ********** Store 2: ***** Store 3: ************ Store 4: ****** Store 5: ** 这是我到目前为止写的代码,我想知道我是否可以对如何显示文本提供一些指导。非常感谢 #pragma once //stops duplicate library #include&
SALES BAR CHART
(each * equals £100)
Store 1: **********
Store 2: *****
Store 3: ************
Store 4: ******
Store 5: **
这是我到目前为止写的代码,我想知道我是否可以对如何显示文本提供一些指导。非常感谢
#pragma once //stops duplicate library
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
class SalesData
{
private:
ifstream inputfile;
ofstream outputfile;
vector<int> salesrecord;
public:
void loadDataFromFile(string filename);
void saveBarChartToFile(string filename);
};
void SalesData::loadDataFromFile(string filename)
{
int number;
ifstream sales;
sales.open("Sales.txt", ios::in);
while (sales >> number)
{
salesrecord.push_back(number);
}
cout << "opening file." << endl;
sales.close();
}
void SalesData::saveBarChartToFile(string filename)
{
ofstream graph;
graph.open("Graph.txt", ios::out);
string stars;
graph << "SALES BAR CHART" << endl;
graph << " (each * equals £100)" << endl;
for (int i = 0; i < salesrecord.size(); i++)
{
stars = "";
for (int starcount = 1; starcount <= (salesrecord[i] / 100); starcount++)
{
stars += "*";
}
graph << stars << endl;
}
graph.close();
}
int main()
{
SalesData Mydata;
Mydata.loadDataFromFile("Sales.txt");
Mydata.saveBarChartToFile("Graph.txt");
return 0;
}
#pragma once//停止复制库
#包括
#包括
#包括
#包括
使用名称空间std;
类销售数据
{
私人:
ifstream输入文件;
流输出文件;
媒介销售记录;
公众:
void loadDataFromFile(字符串文件名);
作废SaveBarChartFile(字符串文件名);
};
void SalesData::loadDataFromFile(字符串文件名)
{
整数;
ifstream销售;
打开(“sales.txt”,ios::in);
while(销售>>数量)
{
salesrecord.push_back(数字);
}
你可以改变吗
graph << stars << endl;
graph首先,生成星星的整个循环可以在一行中完成,使用需要重复一个整数和一个字符的
其次,您只需在打印星星之前打印文本:
for (int i = 0; i < salesrecord.size(); i++)
{
std::string stars(salesrecord[i] / 100, '*');
graph << "Store " << i + 1 << ": " << stars << endl;
}
for(int i=0;ifor (int i = 0; i < salesrecord.size(); i++)
{
std::string stars(salesrecord[i] / 100, '*');
graph << "Store " << i + 1 << ": " << stars << endl;
}