33 unsigned int exponent;
34 ss << fixed << setprecision(2) << price;
35 string pricestring = ss.str();
36 if (price >= 100000) {
37 exponent = pricestring.length() - 3;
38 pricestring = string(1, pricestring[0]) +
"." + string(1, pricestring[1]) +
"e";
42 pricestring += to_string(exponent);
44 while (pricestring.size() < 6) {
45 pricestring =
" " + pricestring;
68 const vector<string> & specifiedColorCoordinates,
const int width,
72 assert(width <=
static_cast<int>(screenElements.size()) &&
73 "Width exceeds screenElements width");
74 assert(height <=
static_cast<int>(screenElements[0].size()) &&
75 "Height exceeds screenElements height");
77 for (
int heightIndex = 0; heightIndex < height; heightIndex++) {
78 for (
int widthIndex = 0; widthIndex < width; widthIndex++) {
79 colorIndex = widthIndex - 9;
81 colorIndex = width - 10;
84 if (specifiedColorCoordinates[colorIndex] ==
textGreen) {
87 else if (specifiedColorCoordinates[colorIndex] ==
textRed) {
90 cout << colorCode << screenElements[widthIndex][heightIndex] <<
textDefault;
99 const string & player,
int stocknum,
string & stockname,
unsigned int width) {
101 if (stocknum != -1) {
110 vector<float> stockpricehistory;
111 fin.open(filename.c_str());
112 if (stocknum != -1) {
113 fin.ignore(256,
'\n');
114 getline(fin, stockname);
117 stockpricehistory.emplace_back(x);
123 while (fin >> loadedPrice) {
124 stockpricehistory.emplace_back(loadedPrice);
127 if (stockpricehistory.size() > (width - 9)) {
128 stockpricehistory.erase(
129 stockpricehistory.begin(), stockpricehistory.end() - (width - 9));
131 stockpricehistory.shrink_to_fit();
132 return stockpricehistory;
139 vector<float> stockpricehistory =
graphinput(player, stocknum, stockname, width);
141 vector<string> color(width - 9,
textWhite);
143 if (stockpricehistory.size() <= 1) {
144 cout <<
"Why do you want to plot graph if there is only one data point?"
148 highest_price = *max_element(stockpricehistory.begin(), stockpricehistory.end());
149 lowest_price = *min_element(stockpricehistory.begin(), stockpricehistory.end());
150 float interval = (highest_price - lowest_price) / height;
151 vector<vector<string>> graph(width, vector<string>(height,
" "));
165 for (
int i = 0; i < 6; i++) {
166 graph[i][0] = maxstring[i];
167 graph[i][height - 1] = minstring[i];
171 for (
unsigned int i = 0; i < stockpricehistory.size() - 1; i++) {
175 start = (height - 1) - (stockpricehistory[i] - lowest_price) / interval;
176 end = (height - 1) - (stockpricehistory[i + 1] - lowest_price) / interval;
184 if (start >= height) {
192 graph[i + 9][start] =
"■";
193 if (stockpricehistory[i] > stockpricehistory[i + 1]) {
196 else if (stockpricehistory[i] < stockpricehistory[i + 1]) {
201 for (
int j = end; j <= start; j++) {
202 graph[i + 9][j] =
"■";
206 else if (start < end) {
207 for (
int j = start; j <= end; j++) {
208 graph[i + 9][j] =
"■";
215 for (
int i = 0; i < height; i++) {
218 for (
int i = 9; i < width; i++) {
219 graph[i][height - 1] =
"━";
221 graph[8][height - 1] =
"┗";
void graph_plotting(const string &player, int stocknum, int width, int height)
Plot the graph of the stock price history to std::cout.