Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- #include "HighPrices.h"
- using namespace std;
- void sortNums(vector<int> &);
- void sortPrices(vector<HighPrices> &, const string &);
- void readFile(vector<HighPrices> &);
- void createTxtFile(vector<HighPrices> &, const string &);
- void highestPrice(vector<HighPrices> &, vector<string> &);
- void lowestPrice(vector<HighPrices> &, vector<string> &);
- void monthAvg(vector<HighPrices> &, vector<string> &);
- void yearAvg(vector<HighPrices> &, vector<string> &);
- bool inVector(vector<int> &, const int &);
- void displayInfo(vector<string> &, vector<string> &,
- vector<string> &, vector<string> &);
- int main()
- {
- vector<HighPrices> highStockP = {};
- vector<string> yearAvgs = {};
- vector<string> monthAvgs = {};
- vector<string> highPerYear = {};
- vector<string> lowPerYear = {};
- readFile(highStockP);
- yearAvg(highStockP, yearAvgs);
- monthAvg(highStockP, monthAvgs);
- highestPrice(highStockP, highPerYear);
- lowestPrice(highStockP, lowPerYear);
- createTxtFile(highStockP, "ascen");
- createTxtFile(highStockP, "descen");
- displayInfo(highPerYear, lowPerYear, yearAvgs, monthAvgs);
- system("pause");
- return 0;
- }
- /**
- * @brief Reads a file and takes the info in, and saves it
- *
- * @param prices is an initialized vector of the HighPrices class
- * @return void
- */
- void readFile(vector<HighPrices> &prices)
- {
- ifstream inFile("StockPrices.csv");
- string dateStr = "";
- string highPri = "";
- string trashData = "";
- string yearStr = "";
- string monthStr = "";
- int year = 0;
- int month = 0;
- double high = 0.0;
- while (inFile.good())
- {
- dateStr = "";
- highPri = "";
- //All of the trashData is the stuff that is not necessary for us
- getline(inFile, dateStr, ',');
- getline(inFile, trashData, ',');
- getline(inFile, highPri, ',');
- getline(inFile, trashData, ',');
- getline(inFile, trashData, ',');
- getline(inFile, trashData, ',');
- getline(inFile, trashData, '\n');
- if (highPri != "High" && highPri != "" && dateStr != "")
- {
- //Getting the points to split the string into month and year
- high = stod(highPri);
- int pos1 = dateStr.find_first_of("/");
- int pos2 = dateStr.find_last_of("/");
- //Month is before the pos1, and Year is after pos2
- monthStr = dateStr.substr(0, pos1);
- yearStr = dateStr.substr(pos2 + 1);
- //Coverting both to int
- year = stoi(yearStr);
- month = stoi(monthStr);
- //Creating a class of HighPrices with that info
- HighPrices tempPrice(high, month, year, dateStr);
- prices.push_back(tempPrice);
- }
- }
- }
- /**
- * @brief Sorts basic ints
- *
- * @param input is an initialized vector of integers
- * @return void
- */
- void sortNums(vector<int> &input)
- {
- for (unsigned i = 0; i < input.size(); i++)
- {
- for (unsigned y = 0; y < input.size() - 1; y++)
- {
- if (input[y] > input[y + 1])
- {
- swap(input[y], input[y + 1]);
- }
- }
- }
- }
- /**
- * @brief sorts a vector of HighPrices based on their price
- *
- * @param prices is an initialized vector of High Prices
- * @param type is an initialized string variable
- * @precondition type == "ascen" or "descen"
- * @return void
- */
- void sortPrices(vector<HighPrices> &prices, const string &type)
- {
- for (unsigned i = 0; i < prices.size(); i++)
- {
- for (int y = 0; y < prices.size() - 1; y++)
- {
- if (type == "ascen")
- {
- if (prices[y].getPrice() > prices[y + 1].getPrice())
- {
- swap(prices[y], prices[y + 1]);
- }
- }
- else if (type == "descen")
- {
- if (prices[y].getPrice() < prices[y + 1].getPrice())
- {
- swap(prices[y], prices[y + 1]);
- }
- }
- }
- }
- }
- /**
- * @brief Creates a text file either ascending or descending
- *
- * @param prices is an initialized vector of HighPrices
- * @param type is an initialized string variable
- * @precondition type == "ascen" or "descen"
- * @return void
- */
- void createTxtFile(vector<HighPrices> &prices, const string &type)
- {
- vector<HighPrices> pricesCopy = prices;
- ofstream outputFile;
- if (type == "ascen")
- {
- outputFile.open("ascendingStocks.txt");
- }
- else if (type == "descen")
- {
- outputFile.open("descendingStocks.txt");
- }
- sortPrices(pricesCopy, type);
- outputFile << "Date,High\n";
- for (unsigned i = 0; i < pricesCopy.size(); i++)
- {
- outputFile << pricesCopy[i].getDate() + "," +
- to_string(pricesCopy[i].getPrice()) + "\n";
- }
- outputFile.close();
- }
- /**
- * @brief Grabs the highest price in a vector of HighPrices
- *
- * @param prices is an initialized vector of HighPrices
- * @param highs is an initialized vector of strings
- * @return void
- */
- void highestPrice(vector<HighPrices> &prices, vector<string> &highs)
- {
- vector<int> years = {};
- double tempHigh = prices[0].getPrice();
- for (int i = 0; i < prices.size(); i++)
- if (!inVector(years, prices[i].getYear()))
- years.push_back(prices[i].getYear());
- sortNums(years);
- for (unsigned i = 0; i < years.size(); i++)
- {
- for (int y = 1; y < prices.size(); y++)
- {
- if (prices[y].getPrice() > tempHigh && prices[y].getYear() == years[i])
- tempHigh = prices[y].getPrice();
- }
- highs.push_back("In " + to_string(years[i]) + " the highest " +
- " price was $" + to_string(tempHigh));
- }
- }
- /**
- * @brief Grabs the lowest price in a vector of HighPrices
- *
- * @param prices is an initialized vector of HighPrices
- * @param lows is an initialized vector of strings
- * @return void
- */
- void lowestPrice(vector<HighPrices> &prices, vector<string> &lows)
- {
- vector<int> years = {};
- double tempLow = prices[0].getPrice();
- for (int i = 0; i < prices.size(); i++)
- if (!inVector(years, prices[i].getYear()))
- years.push_back(prices[i].getYear());
- sortNums(years);
- for (unsigned i = 0; i < years.size(); i++)
- {
- for (int y = 1; y < prices.size(); y++)
- {
- if (prices[y].getPrice() < tempLow && prices[y].getYear() == years[i])
- tempLow = prices[y].getPrice();
- }
- lows.push_back("In " + to_string(years[i]) + " the lowest " +
- " price was $" + to_string(tempLow));
- }
- }
- /**
- * @brief Gets the average price across a month at a time
- *
- * @param prices is an initialized vector of HighPrices
- * @param avgs is an initialized vector of strings
- * @return void
- */
- void monthAvg(vector<HighPrices> &prices, vector<string> &avgs)
- {
- double sum = 0;
- int n = 0;
- double avg = 0;
- vector<int> years = {};
- vector<int> months = {};
- for (unsigned i = 0; i < prices.size(); i++)
- {
- if (inVector(years, prices[i].getYear()) == false)
- {
- years.push_back(prices[i].getYear());
- }
- if (inVector(months, prices[i].getMonth()) == false)
- {
- months.push_back(prices[i].getMonth());
- }
- }
- sortNums(years);
- sortNums(months);
- for (unsigned i = 0; i < years.size(); i++)
- {
- for (unsigned x = 0; x < months.size(); x++)
- {
- sum = 0;
- n = 0;
- avg = 0;
- for (unsigned y = 0; y < prices.size(); y++)
- {
- if (prices[y].getYear() == years[i] &&
- prices[y].getMonth() == months[x])
- {
- sum += prices[y].getPrice();
- n++;
- }
- }
- if (sum != 0)
- {
- avg = sum / n;
- avgs.push_back("In " + to_string(months[x]) + "/" +
- to_string(years[i]) + " the average was $"
- + to_string(avg));
- }
- }
- }
- }
- /**
- * @brief Gets the average price across a year at a time
- *
- * @param prices is an initialized vector of HighPrices
- * @param avgs is an initialized vector of strings
- * @return void
- */
- void yearAvg(vector<HighPrices> &prices, vector<string> &avgs)
- {
- double sum = 0;
- int n = 0;
- double avg = 0;
- vector<int> years = {};
- for (unsigned i = 0; i < prices.size(); i++)
- {
- if (inVector(years, prices[i].getYear()) == false)
- {
- years.push_back(prices[i].getYear());
- }
- }
- sortNums(years);
- for (unsigned i = 0; i < years.size(); i++)
- {
- sum = 0;
- n = 0;
- avg = 0;
- for (unsigned y = 0; y < prices.size(); y++)
- {
- if (prices[y].getYear() == years[i])
- {
- sum += prices[y].getPrice();
- n++;
- }
- }
- if (sum != 0)
- {
- avg = sum / n;
- avgs.push_back("In " + to_string(years[i]) +
- " the average was $" + to_string(avg));
- }
- }
- }
- /**
- * @brief Searchs in a vector for a specific int value
- *
- * @param vectorSearched is an initialized vector of ints
- * @param search is a constant int variable
- * @return result as a boolean variable
- */
- bool inVector(vector<int> &vectorSearched, const int &search)
- {
- bool result = false;
- for (unsigned i = 0; i < vectorSearched.size(); i++)
- {
- if (vectorSearched[i] == search)
- {
- result = true;
- break;
- }
- }
- return result;
- }
- /**
- * @brief Displays all currently gathered info
- *
- * @param high is a vector of strings with the highest value per year
- * @param low is a vector of strings with the lowest value per year
- * @param avgYear is a vector of strings with the average per year
- * @param avgMonth is a vector of strings with the average per month
- * @return void
- */
- void displayInfo(vector<string> &high, vector<string> &low,
- vector<string> &avgYear, vector<string> &avgMonth)
- {
- cout << "-----High Values Per Year-----" << endl << endl;
- for (unsigned i = 0; i < high.size(); i++)
- {
- cout << high[i] << endl;
- }
- cout << endl << endl;
- cout << "-----Low Values Per Year-----" << endl << endl;
- for (unsigned i = 0; i < low.size(); i++)
- {
- cout << low[i] << endl;
- }
- cout << endl << endl;
- cout << "-----Average Values Per Year-----" << endl << endl;
- for (unsigned i = 0; i < avgYear.size(); i++)
- {
- cout << avgYear[i] << endl;
- }
- cout << endl << endl;
- cout << "-----Average Values Per Month-----" << endl << endl;
- for (unsigned i = 0; i < avgMonth.size(); i++)
- {
- cout << avgMonth[i] << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement