Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. #include "HighPrices.h"
  6.  
  7. using namespace std;
  8.  
  9. void sortNums(vector<int> &);
  10. void sortPrices(vector<HighPrices> &, const string &);
  11. void readFile(vector<HighPrices> &);
  12. void createTxtFile(vector<HighPrices> &, const string &);
  13. void highestPrice(vector<HighPrices> &, vector<string> &);
  14. void lowestPrice(vector<HighPrices> &, vector<string> &);
  15. void monthAvg(vector<HighPrices> &, vector<string> &);
  16. void yearAvg(vector<HighPrices> &, vector<string> &);
  17. bool inVector(vector<int> &, const int &);
  18. void displayInfo(vector<string> &, vector<string> &,
  19.     vector<string> &, vector<string> &);
  20.  
  21. int main()
  22. {
  23.     vector<HighPrices> highStockP = {};
  24.     vector<string> yearAvgs = {};
  25.     vector<string> monthAvgs = {};
  26.     vector<string> highPerYear = {};
  27.     vector<string> lowPerYear = {};
  28.  
  29.     readFile(highStockP);
  30.     yearAvg(highStockP, yearAvgs);
  31.     monthAvg(highStockP, monthAvgs);
  32.     highestPrice(highStockP, highPerYear);
  33.     lowestPrice(highStockP, lowPerYear);
  34.     createTxtFile(highStockP, "ascen");
  35.     createTxtFile(highStockP, "descen");
  36.     displayInfo(highPerYear, lowPerYear, yearAvgs, monthAvgs);
  37.  
  38.     system("pause");
  39.     return 0;
  40. }
  41.  
  42. /**
  43. * @brief Reads a file and takes the info in, and saves it
  44. *
  45. * @param prices is an initialized vector of the HighPrices class
  46. * @return void
  47. */
  48. void readFile(vector<HighPrices> &prices)
  49. {
  50.     ifstream inFile("StockPrices.csv");
  51.     string dateStr = "";
  52.     string highPri = "";
  53.     string trashData = "";
  54.     string yearStr = "";
  55.     string monthStr = "";
  56.     int year = 0;
  57.     int month = 0;
  58.     double high = 0.0;
  59.  
  60.     while (inFile.good())
  61.     {
  62.         dateStr = "";
  63.         highPri = "";
  64.         //All of the trashData is the stuff that is not necessary for us
  65.         getline(inFile, dateStr, ',');
  66.         getline(inFile, trashData, ',');
  67.         getline(inFile, highPri, ',');
  68.         getline(inFile, trashData, ',');
  69.         getline(inFile, trashData, ',');
  70.         getline(inFile, trashData, ',');
  71.         getline(inFile, trashData, '\n');
  72.         if (highPri != "High" && highPri != "" && dateStr != "")
  73.         {
  74.             //Getting the points to split the string into month and year
  75.             high = stod(highPri);
  76.             int pos1 = dateStr.find_first_of("/");
  77.             int pos2 = dateStr.find_last_of("/");
  78.             //Month is before the pos1, and Year is after pos2
  79.             monthStr = dateStr.substr(0, pos1);
  80.             yearStr = dateStr.substr(pos2 + 1);
  81.             //Coverting both to int
  82.             year = stoi(yearStr);
  83.             month = stoi(monthStr);
  84.             //Creating a class of HighPrices with that info
  85.             HighPrices tempPrice(high, month, year, dateStr);
  86.             prices.push_back(tempPrice);
  87.         }
  88.     }
  89. }
  90.  
  91. /**
  92. * @brief Sorts basic ints
  93. *
  94. * @param input is an initialized vector of integers
  95. * @return void
  96. */
  97. void sortNums(vector<int> &input)
  98. {
  99.     for (unsigned i = 0; i < input.size(); i++)
  100.     {
  101.         for (unsigned y = 0; y < input.size() - 1; y++)
  102.         {
  103.             if (input[y] > input[y + 1])
  104.             {
  105.                 swap(input[y], input[y + 1]);
  106.             }
  107.         }
  108.     }
  109. }
  110.  
  111. /**
  112. * @brief sorts a vector of HighPrices based on their price
  113. *
  114. * @param prices is an initialized vector of High Prices
  115. * @param type is an initialized string variable
  116. * @precondition type == "ascen" or "descen"
  117. * @return void
  118. */
  119. void sortPrices(vector<HighPrices> &prices, const string &type)
  120. {
  121.     for (unsigned i = 0; i < prices.size(); i++)
  122.     {
  123.         for (int y = 0; y < prices.size() - 1; y++)
  124.         {
  125.             if (type == "ascen")
  126.             {
  127.                 if (prices[y].getPrice() > prices[y + 1].getPrice())
  128.                 {
  129.                     swap(prices[y], prices[y + 1]);
  130.                 }
  131.             }
  132.             else if (type == "descen")
  133.             {
  134.                 if (prices[y].getPrice() < prices[y + 1].getPrice())
  135.                 {
  136.                     swap(prices[y], prices[y + 1]);
  137.                 }
  138.             }
  139.         }
  140.     }
  141. }
  142.  
  143. /**
  144. * @brief Creates a text file either ascending or descending
  145. *
  146. * @param prices is an initialized vector of HighPrices
  147. * @param type is an initialized string variable
  148. * @precondition type == "ascen" or "descen"
  149. * @return void
  150. */
  151. void createTxtFile(vector<HighPrices> &prices, const string &type)
  152. {
  153.     vector<HighPrices> pricesCopy = prices;
  154.     ofstream outputFile;
  155.    
  156.    
  157.     if (type == "ascen")
  158.     {
  159.         outputFile.open("ascendingStocks.txt");
  160.     }
  161.     else if (type == "descen")
  162.     {
  163.         outputFile.open("descendingStocks.txt");
  164.     }
  165.    
  166.     sortPrices(pricesCopy, type);
  167.  
  168.     outputFile << "Date,High\n";
  169.    
  170.     for (unsigned i = 0; i < pricesCopy.size(); i++)
  171.     {
  172.         outputFile << pricesCopy[i].getDate() + "," +
  173.             to_string(pricesCopy[i].getPrice()) + "\n";
  174.     }
  175.     outputFile.close();
  176. }
  177.  
  178. /**
  179. * @brief Grabs the highest price in a vector of HighPrices
  180. *
  181. * @param prices is an initialized vector of HighPrices
  182. * @param highs is an initialized vector of strings
  183. * @return void
  184. */
  185. void highestPrice(vector<HighPrices> &prices, vector<string> &highs)
  186. {
  187.     vector<int> years = {};
  188.     double tempHigh = prices[0].getPrice();
  189.    
  190.     for (int i = 0; i < prices.size(); i++)
  191.         if (!inVector(years, prices[i].getYear()))
  192.             years.push_back(prices[i].getYear());
  193.    
  194.     sortNums(years);
  195.    
  196.     for (unsigned i = 0; i < years.size(); i++)
  197.     {
  198.         for (int y = 1; y < prices.size(); y++)
  199.         {
  200.             if (prices[y].getPrice() > tempHigh && prices[y].getYear() == years[i])
  201.                 tempHigh = prices[y].getPrice();
  202.         }
  203.         highs.push_back("In " + to_string(years[i]) + " the highest " +
  204.             " price was $" + to_string(tempHigh));
  205.     }
  206. }
  207.  
  208. /**
  209. * @brief Grabs the lowest price in a vector of HighPrices
  210. *
  211. * @param prices is an initialized vector of HighPrices
  212. * @param lows is an initialized vector of strings
  213. * @return void
  214. */
  215. void lowestPrice(vector<HighPrices> &prices, vector<string> &lows)
  216. {
  217.     vector<int> years = {};
  218.     double tempLow = prices[0].getPrice();
  219.    
  220.     for (int i = 0; i < prices.size(); i++)
  221.         if (!inVector(years, prices[i].getYear()))
  222.             years.push_back(prices[i].getYear());
  223.    
  224.     sortNums(years);
  225.    
  226.     for (unsigned i = 0; i < years.size(); i++)
  227.     {
  228.         for (int y = 1; y < prices.size(); y++)
  229.         {
  230.             if (prices[y].getPrice() < tempLow && prices[y].getYear() == years[i])
  231.                 tempLow = prices[y].getPrice();
  232.         }
  233.         lows.push_back("In " + to_string(years[i]) + " the lowest " +
  234.             " price was $" + to_string(tempLow));
  235.     }
  236. }
  237.  
  238. /**
  239. * @brief Gets the average price across a month at a time
  240. *
  241. * @param prices is an initialized vector of HighPrices
  242. * @param avgs is an initialized vector of strings
  243. * @return void
  244. */
  245. void monthAvg(vector<HighPrices> &prices, vector<string> &avgs)
  246. {
  247.     double sum = 0;
  248.     int n = 0;
  249.     double avg = 0;
  250.     vector<int> years = {};
  251.     vector<int> months = {};
  252.    
  253.    
  254.     for (unsigned i = 0; i < prices.size(); i++)
  255.     {
  256.         if (inVector(years, prices[i].getYear()) == false)
  257.         {
  258.             years.push_back(prices[i].getYear());
  259.         }
  260.         if (inVector(months, prices[i].getMonth()) == false)
  261.         {
  262.             months.push_back(prices[i].getMonth());
  263.         }
  264.     }
  265.    
  266.     sortNums(years);
  267.     sortNums(months);
  268.    
  269.     for (unsigned i = 0; i < years.size(); i++)
  270.     {
  271.         for (unsigned x = 0; x < months.size(); x++)
  272.         {
  273.             sum = 0;
  274.             n = 0;
  275.             avg = 0;
  276.  
  277.             for (unsigned y = 0; y < prices.size(); y++)
  278.             {
  279.                 if (prices[y].getYear() == years[i] &&
  280.                     prices[y].getMonth() == months[x])
  281.                 {
  282.                     sum += prices[y].getPrice();
  283.                     n++;
  284.                 }
  285.             }
  286.             if (sum != 0)
  287.             {
  288.                 avg = sum / n;
  289.                 avgs.push_back("In " + to_string(months[x]) + "/" +
  290.                     to_string(years[i]) + " the average was $"
  291.                     + to_string(avg));
  292.             }
  293.         }
  294.     }
  295. }
  296.  
  297. /**
  298. * @brief Gets the average price across a year at a time
  299. *
  300. * @param prices is an initialized vector of HighPrices
  301. * @param avgs is an initialized vector of strings
  302. * @return void
  303. */
  304. void yearAvg(vector<HighPrices> &prices, vector<string> &avgs)
  305. {
  306.     double sum = 0;
  307.     int n = 0;
  308.     double avg = 0;
  309.     vector<int> years = {};
  310.    
  311.     for (unsigned i = 0; i < prices.size(); i++)
  312.     {
  313.         if (inVector(years, prices[i].getYear()) == false)
  314.         {
  315.             years.push_back(prices[i].getYear());
  316.         }
  317.     }
  318.    
  319.     sortNums(years);
  320.    
  321.     for (unsigned i = 0; i < years.size(); i++)
  322.     {
  323.         sum = 0;
  324.         n = 0;
  325.         avg = 0;
  326.        
  327.         for (unsigned y = 0; y < prices.size(); y++)
  328.         {
  329.             if (prices[y].getYear() == years[i])
  330.             {
  331.                 sum += prices[y].getPrice();
  332.                 n++;
  333.             }
  334.         }
  335.         if (sum != 0)
  336.         {
  337.             avg = sum / n;
  338.             avgs.push_back("In " + to_string(years[i]) +
  339.                 " the average was $" + to_string(avg));
  340.         }
  341.     }
  342. }
  343.  
  344. /**
  345. * @brief Searchs in a vector for a specific int value
  346. *
  347. * @param vectorSearched is an initialized vector of ints
  348. * @param search is a constant int variable
  349. * @return result as a boolean variable
  350. */
  351. bool inVector(vector<int> &vectorSearched, const int &search)
  352. {
  353.     bool result = false;
  354.    
  355.     for (unsigned i = 0; i < vectorSearched.size(); i++)
  356.     {
  357.         if (vectorSearched[i] == search)
  358.         {
  359.             result = true;
  360.             break;
  361.         }
  362.     }
  363.    
  364.     return result;
  365. }
  366.  
  367. /**
  368. * @brief Displays all currently gathered info
  369. *
  370. * @param high is a vector of strings with the highest value per year
  371. * @param low is a vector of strings with the lowest value per year
  372. * @param avgYear is a vector of strings with the average per year
  373. * @param avgMonth is a vector of strings with the average per month
  374. * @return void
  375. */
  376. void displayInfo(vector<string> &high, vector<string> &low,
  377.     vector<string> &avgYear, vector<string> &avgMonth)
  378. {  
  379.     cout << "-----High Values Per Year-----" << endl << endl;
  380.     for (unsigned i = 0; i < high.size(); i++)
  381.     {
  382.         cout << high[i] << endl;
  383.     }
  384.    
  385.     cout << endl << endl;
  386.    
  387.     cout << "-----Low Values Per Year-----" << endl << endl;
  388.     for (unsigned i = 0; i < low.size(); i++)
  389.     {
  390.         cout << low[i] << endl;
  391.     }
  392.    
  393.     cout << endl << endl;
  394.    
  395.     cout << "-----Average Values Per Year-----" << endl << endl;
  396.     for (unsigned i = 0; i < avgYear.size(); i++)
  397.     {
  398.         cout << avgYear[i] << endl;
  399.     }
  400.    
  401.     cout << endl << endl;
  402.    
  403.     cout << "-----Average Values Per Month-----" << endl << endl;
  404.     for (unsigned i = 0; i < avgMonth.size(); i++)
  405.     {
  406.         cout << avgMonth[i] << endl;
  407.     }
  408. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement