Rodex_

Hashtable_Algorithm

Apr 3rd, 2017
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 24.97 KB | None | 0 0
  1. =================================================== STOCK.H ========================================================
  2.  
  3. #ifndef HASHTABLE_STOCK_H
  4. #define HASHTABLE_STOCK_H
  5.  
  6. #include <iostream>
  7. #include <vector>
  8.  
  9. /**
  10.  * Represents price data of a stock for a single day
  11.  */
  12. class PriceData {
  13.     std::string date;
  14.     double open, high, low, close, adj_close;
  15.     int volume;
  16. public:
  17.     /**
  18.      * Creates price data for a single day
  19.      * @param date Date of given price data
  20.      * @param open Open value of given day
  21.      * @param high High value of given day
  22.      * @param low Low value of given day
  23.      * @param close Close value of given day
  24.      * @param volume Volume of given day
  25.      * @param adj_close Adjusted close value of given day
  26.      */
  27.     PriceData(std::string date, double open, double high, double low, double close, int volume, double adj_close);
  28.     /**
  29.      * Returns the date of this PriceData
  30.      * @return a string containing the date
  31.      */
  32.     std::string getDate();
  33.     /**
  34.      * Returns the open value of this PriceData
  35.      * @return a double containing the open value
  36.      */
  37.     double getOpen();
  38.     /**
  39.      * Returns the high value of this PriceData
  40.      * @return a double containing the high value
  41.      */
  42.     double getHigh();
  43.     /**
  44.      * Returns the low value of this PriceData
  45.      * @return a double containing the low value
  46.      */
  47.     double getLow();
  48.     /**
  49.      * Returns the close value of this PriceData
  50.      * @return a double containing the close value
  51.      */
  52.     double getClose();
  53.     /**
  54.      * Returns the volume value of this PriceData
  55.      * @return an int containing the volume value
  56.      */
  57.     int getVolume();
  58.     /**
  59.      * Returns the adjusted close value of this PriceData
  60.      * @return a double containing the adjusted close value
  61.      */
  62.     double getAdjClose();
  63.  
  64. };
  65.  
  66. /**
  67.  * Represents a stock with a name, member code, security identification number and price data of last 30 days
  68.  */
  69. class Stock {
  70.     std::string name;
  71.     std::string member_code;
  72.     // Security Identification Number
  73.     int sin;
  74.     PriceData* price_data[30];
  75. public:
  76.     /**
  77.      * Creates an empty stock
  78.      */
  79.      Stock();
  80.     /**
  81.      * Creates a new stock
  82.      * @param name Stock name
  83.      * @param member_code Short name of stock
  84.      * @param sin Security Identification Number
  85.      */
  86.     Stock(std::string name, std::string member_code, int sin);
  87.     /**
  88.      * Import price data of last 30 days from a csv file
  89.      * @param filepath path to csv file (including filename + ending)
  90.      */
  91.     void import_price_data(std::string filepath);
  92.     /**
  93.      * Returns the name of the stock
  94.      * @return a string containing the name
  95.      */
  96.     std::string getName();
  97.     /**
  98.      * Returns the member code of the stock
  99.      * @return a string containing the member code
  100.      */
  101.     std::string getMemberCode();
  102.     /**
  103.      * Returns the security identification number of the stock
  104.      * @return an int containing the sin
  105.      */
  106.     int getSIN();
  107.     /**
  108.      * Returns the PriceData Array of the stock
  109.      * @return an array of PriceData Pointer of length 30
  110.      */
  111.     PriceData** getPriceData();
  112.     /**
  113.      * Sets the PriceData Array
  114.      * @param data an array of PriceData Pointer of length 30
  115.      */
  116.     void setPriceData(PriceData** data);
  117. };
  118.  
  119. #endif //HASHTABLE_STOCK_H
  120.  
  121.  
  122. =================================================== STOCK.CPP ======================================================
  123.  
  124. #include "stock.h"
  125. #include <fstream>
  126. #include <sstream>
  127. #include <cstring>
  128.  
  129. PriceData::PriceData(std::string date, double open, double high, double low, double close, int volume, double adj_close)
  130.         : date(date), open(open), high(high), low(low), close(close), volume(volume), adj_close(adj_close) {
  131. }
  132.  
  133. std::string PriceData::getDate() {
  134.     return date;
  135. }
  136.  
  137. double PriceData::getOpen() {
  138.     return open;
  139. }
  140.  
  141. double PriceData::getHigh() {
  142.     return high;
  143. }
  144.  
  145. double PriceData::getLow() {
  146.     return low;
  147. }
  148.  
  149. double PriceData::getClose() {
  150.     return close;
  151. }
  152.  
  153. int PriceData::getVolume() {
  154.     return volume;
  155. }
  156.  
  157. double PriceData::getAdjClose() {
  158.     return adj_close;
  159. }
  160.  
  161.  
  162. Stock::Stock(std::string name, std::string member_code, int sin) : name(name), member_code(member_code), sin(sin) {
  163.     for (int i = 0; i < 30; i++) {
  164.         price_data[i] = nullptr;
  165.     }
  166. }
  167.  
  168. Stock::Stock() {
  169.     name = nullptr;
  170.     member_code = nullptr;
  171.     sin = -1;
  172. }
  173.  
  174. void Stock::import_price_data(std::string filepath) {
  175.     std::ifstream file(filepath, std::ios::in);
  176.     std::string line;
  177.     int line_numbers = 0;
  178.     for (; std::getline(file, line); line_numbers++);
  179.  
  180.     std::string date, open, high, low, close, volume, adj_close;
  181.     double open_d, high_d, low_d, close_d, adj_close_d;
  182.     int volume_d;
  183.     std::ifstream datei;
  184.     //open file with correct Filepath of Input
  185.     datei.open(filepath, std::ios::in);
  186.     //read first line without values to get to second line
  187.     getline(datei, date, '\n');
  188.     for (int i = 0; i < 30 && i < line_numbers-1; ++i) {
  189.         //read the line of the file and get the values into the correct variables
  190.         getline(datei, date, ',');
  191.         getline(datei, open, ',');
  192.         getline(datei, high, ',');
  193.         getline(datei, low, ',');
  194.         getline(datei, close, ',');
  195.         getline(datei, volume, ',');
  196.         getline(datei, adj_close, '\n');
  197.         //prepare stringstreams to get the values from string into double
  198.         std::stringstream open_s(open);
  199.         std::stringstream high_s(high);
  200.         std::stringstream low_s(low);
  201.         std::stringstream close_s(close);
  202.         std::stringstream volume_s(volume);
  203.         std::stringstream adj_close_s(adj_close);
  204.         //convert stringstreams in doubles
  205.         open_s >> open_d;
  206.         high_s >> high_d;
  207.         low_s >> low_d;
  208.         close_s >> close_d;
  209.         adj_close_s >> adj_close_d;
  210.         volume_s >> volume_d;
  211.         if(!open_s.fail() && !high_s.fail() && !low_s.fail() && !close_s.fail() && !adj_close_s.fail() && !volume_s.fail()) { //erstelle neues Objekt und übergebe die daten
  212.             if(price_data[i] != nullptr) {
  213.                 delete price_data[i];
  214.             }
  215.             price_data[i] = new PriceData(date, open_d, high_d, low_d, close_d, volume_d, adj_close_d);
  216.         }
  217.     }
  218. }
  219.  
  220. std::string Stock::getName() {
  221.     return name;
  222. }
  223.  
  224. std::string Stock::getMemberCode() {
  225.     return member_code;
  226. }
  227.  
  228. int Stock::getSIN() {
  229.     return sin;
  230. }
  231.  
  232. PriceData** Stock::getPriceData() {
  233.     return price_data;
  234. }
  235.  
  236. void Stock::setPriceData(PriceData **data) {
  237.     for(int i = 0; i < 30; i++){
  238.         price_data[i] = data[i];
  239.     }
  240. }
  241.  
  242. =================================================== HASHTABLE.H ====================================================
  243.  
  244. #ifndef HASHTABLE_HASHTABLE_H
  245. #define HASHTABLE_HASHTABLE_H
  246.  
  247. #include "stock.h"
  248.  
  249. /**
  250.  * Represents a HashTable for saving stock information
  251.  */
  252. class HashTable {
  253.     const static int array_size = 1999;
  254.     // Data format to store stocks in
  255.     Stock* stocks[array_size];
  256.  
  257. public:
  258.     const static int HASH_BY_NAME = 0;
  259.     const static int HASH_BY_CODE = 1;
  260.     /**
  261.      * Creates a new HashTable
  262.      */
  263.     HashTable();
  264.     /**
  265.      * Hash function, which creates an integer value from a string
  266.      * @param str string to be hashed
  267.      * @return an integer value representing a hashed value of the given string
  268.      */
  269.     int hash(std::string str);
  270.     /**
  271.      * Puts a stock into the HashTable
  272.      * @param stock Stock to be inserted
  273.      * @param hash_by HASH_BY_NAME or HASH_BY_CODE
  274.      */
  275.     void add(Stock* stock, int hash_by);
  276.     /**
  277.      * Removes a stock from the HashTable
  278.      * @param name Name of stock to be removed
  279.      * @param hash_by HASH_BY_NAME or HASH_BY_CODE
  280.      */
  281.     void remove(std::string name, int hash_by);
  282.     /**
  283.      * Searches a stock in the HashTable and returns it if found
  284.      * @param name Name of wanted stock
  285.      * @param hash_by HASH_BY_NAME or HASH_BY_CODE
  286.      * @return Wanted stock, if found. Else it will return null
  287.      */
  288.     Stock* search(std::string name, int hash_by);
  289.     /**
  290.      * Saves the complete HashTable into a file
  291.      * @param filepath the path (+filename) where it should be saved
  292.      */
  293.     void saveToFile(std::string filepath);
  294.     /**
  295.      * Reads a complete HashTable from a file
  296.      * @param filepath the path (+filename) from where it should be read
  297.      */
  298.     void readFromFile(std::string filepath);
  299.  
  300. };
  301.  
  302. #endif //HASHTABLE_HASHTABLE_H
  303.  
  304.  
  305.  
  306. =================================================== HASHTABLE.CPP ==================================================
  307.  
  308. #include <string.h>
  309. #include <math.h>
  310. #include <fstream>
  311. #include <sstream>
  312. #include "hashtable.h"
  313.  
  314. HashTable::HashTable() {
  315.     for(int i = 0; i < array_size; i++){
  316.         stocks[i] = nullptr;
  317.     }
  318. }
  319.  
  320. int HashTable::hash(std::string str) {
  321.     int intLength = str.length() / 4;
  322.     long sum = 0;
  323.     for (int j = 0; j < intLength; j++) {
  324.         char c[5];
  325.         std::string tmp = str.substr(j * 4, (j * 4) + 4);
  326.         strcpy(c, tmp.c_str());
  327.         long mult = 1;
  328.         for (int k = 0; k < 5; k++) {
  329.             sum += c[k] * mult;
  330.             mult *= 256;
  331.         }
  332.     }
  333.  
  334.     int lengthMax = str.length() % 4;
  335.     char c[lengthMax];
  336.     std::string tmp = str.substr(intLength * 4);
  337.     strcpy(c, tmp.c_str());
  338.     long mult = 1;
  339.     for (int k = 0; k < lengthMax; k++) {
  340.         sum += c[k] * mult;
  341.         mult *= 256;
  342.     }
  343.  
  344.     return(sum % array_size);
  345. }
  346.  
  347. void HashTable::add(Stock* stock, int hash_by) {
  348.     int hashed = 0;
  349.     if(hash_by == HASH_BY_NAME) {
  350.         hashed = hash(stock->getName());
  351.     }else{
  352.         hashed = hash(stock->getMemberCode());
  353.     }
  354.     //look in the list if the hashed position for stock is empty
  355.     if(stocks[hashed] == nullptr){
  356.         stocks[hashed] = stock;
  357.     }else{
  358.         //if not empty start the search algorithm for empty position
  359.         int plus = 1;
  360.         int next_pos = 0;
  361.         for(int i = 1; i <= array_size;){
  362.             int x = (int)pow(i,2);
  363.             //function to take the numbers of square always negative and positive
  364.             if(!plus){
  365.                 x = (-1) * x;
  366.                 i++;
  367.             }
  368.             plus = (plus+1)%2;
  369.             //calculation to get the correct value always as a positive integer
  370.             next_pos = ((hashed+x)% array_size + array_size) % array_size ;
  371.             if(stocks[next_pos] == nullptr){
  372.                 stocks[next_pos] = stock;
  373.                 break;
  374.             }
  375.         }
  376.     }
  377. }
  378.  
  379. void HashTable::remove(std::string name, int hash_by) {
  380.     int hashed = hash(name);
  381.     //look in the list if correct stock from the input is on the hashed position
  382.     if((hash_by == HASH_BY_NAME && stocks[hashed] != nullptr && stocks[hashed]->getName() == name) ||
  383.             (hash_by == HASH_BY_CODE && stocks[hashed] != nullptr && stocks[hashed]->getMemberCode() == name)){
  384.         delete stocks[hashed];
  385.         stocks[hashed] = nullptr;
  386.     }else{
  387.         int plus = 1;
  388.         int next_pos = 0;
  389.         for(int i = 1; i <= array_size;){
  390.             int x = (int)pow(i,2);
  391.             //function to take the numbers of square always negative and positive
  392.             if(!plus){
  393.                 x = (-1) * x;
  394.                 i++;
  395.             }
  396.             plus = (plus+1)%2;
  397.             //calculation to get the correct value always as a positive integer
  398.             next_pos = ((hashed+x) % array_size + array_size) % array_size ;
  399.             if((hash_by == HASH_BY_NAME && stocks[next_pos] != nullptr && stocks[next_pos]->getName() == name) ||
  400.                     (hash_by == HASH_BY_CODE && stocks[next_pos] != nullptr && stocks[next_pos]->getMemberCode() == name)){
  401.                 delete stocks[next_pos];
  402.                 stocks[next_pos] = nullptr;
  403.                 break;
  404.             }
  405.         }
  406.     }
  407. }
  408.  
  409. Stock* HashTable::search(std::string name, int hash_by) {
  410.     int hashed = hash(name);
  411.     //look in the list if correct stock from the input is on the hashed position
  412.     if((hash_by == HASH_BY_NAME && stocks[hashed] != nullptr && stocks[hashed]->getName() == name) ||
  413.        (hash_by == HASH_BY_CODE && stocks[hashed] != nullptr && stocks[hashed]->getMemberCode() == name)){
  414.         return stocks[hashed];
  415.     }else{
  416.         //if not on the hashed position start with the search algorithm
  417.         int plus = 1;
  418.         int next_pos = 0;
  419.         for(int i = 1; i <= array_size;){
  420.             int x = (int)pow(i,2);
  421.             //function to take the numbers of square always negative and positive
  422.             if(!plus){
  423.                 x = (-1) * x;
  424.                 i++;
  425.             }
  426.             plus = (plus+1)%2;
  427.             //calculation to get the correct value always as a positive integer
  428.             next_pos = ((hashed+x) % array_size + array_size) % array_size ;
  429.             if(hash_by == HASH_BY_NAME && stocks[next_pos] != nullptr && stocks[next_pos]->getName() == name ||
  430.                hash_by == HASH_BY_CODE && stocks[next_pos] != nullptr && stocks[next_pos]->getMemberCode() == name){
  431.                 return stocks[next_pos];
  432.             }
  433.         }
  434.     }
  435.     return nullptr;
  436. }
  437.  
  438. void HashTable::saveToFile(std::string filepath) {
  439.     int count_pricedata;
  440.     std::ofstream file;
  441.     file.open(filepath, std::ios::out);
  442.  
  443.     for(int i = 0; i < array_size; i++){
  444.         if(stocks[i] != nullptr){
  445.             // Write stock information
  446.             file << i << " " << stocks[i]->getName() << " " << stocks[i]->getMemberCode() << " " << stocks[i]->getSIN();
  447.             count_pricedata = 0;
  448.             for(int j = 0; j < 30; j++){
  449.                 if(stocks[i]->getPriceData()[j] != nullptr){
  450.                     count_pricedata++;
  451.                 }
  452.             }
  453.             file << " " << count_pricedata << "\n";
  454.             // Write price data
  455.             for(int j = 0; j < 30; j++){
  456.                 if(stocks[i]->getPriceData()[j] != nullptr){
  457.                     file << j << " " << stocks[i]->getPriceData()[j]->getDate();
  458.                     file << " " << stocks[i]->getPriceData()[j]->getOpen();
  459.                     file << " " << stocks[i]->getPriceData()[j]->getHigh();
  460.                     file << " " << stocks[i]->getPriceData()[j]->getLow();
  461.                     file << " " << stocks[i]->getPriceData()[j]->getClose();
  462.                     file << " " << stocks[i]->getPriceData()[j]->getVolume();
  463.                     file << " " << stocks[i]->getPriceData()[j]->getAdjClose() << "\n";
  464.                 }
  465.             }
  466.         }
  467.     }
  468.  
  469.     // End of File
  470.     file << "eof" << " \n";
  471.  
  472.     file.close();
  473. }
  474.  
  475. void HashTable::readFromFile(std::string filepath) {
  476.     std::string in, name, membercode, sin_s, date;
  477.     int count_pricedata, sin, index, volume, pdindex;
  478.     double open, high, low, close, adjclose;
  479.     std::ifstream file;
  480.     file.open(filepath, std::ios::in);
  481.  
  482.     for(int i = 0; i < array_size; i++){
  483.         getline(file, in, ' ');
  484.         // If not end of file
  485.         if(in != "eof"){
  486.             // Read stock information
  487.             getline(file, name, ' ');
  488.             getline(file, membercode, ' ');
  489.             getline(file, sin_s, ' ');
  490.             std::stringstream sin_stream(sin_s);
  491.             sin_stream >> sin;
  492.             std::stringstream index_stream(in);
  493.             index_stream >> index;
  494.             // Insert stock into hashtable
  495.             Stock* stock = new Stock(name, membercode, sin);
  496.             stocks[index] = stock;
  497.  
  498.             // Read price data
  499.             getline(file, in, '\n');
  500.             std::stringstream pricedata_stream(in);
  501.             pricedata_stream >> count_pricedata;
  502.  
  503.             PriceData* data[30];
  504.             for(int j = 0; j < 30; j++){
  505.                 data[j] = nullptr;
  506.             }
  507.             for(int j = 0; j < count_pricedata; j++){
  508.                 getline(file, in, ' ');
  509.                 {
  510.                     std::stringstream in_stream(in);
  511.                     in_stream >> pdindex;
  512.                     getline(file, date, ' ');
  513.                 }
  514.                 getline(file, in, ' ');
  515.                 {
  516.                     std::stringstream in_stream(in);
  517.                     in_stream >> open;
  518.                 }
  519.                 getline(file, in, ' ');
  520.                 {
  521.                     std::stringstream in_stream(in);
  522.                     in_stream >> high;
  523.                 }
  524.                 getline(file, in, ' ');
  525.                 {
  526.                     std::stringstream in_stream(in);
  527.                     in_stream >> low;
  528.                 }
  529.                 getline(file, in, ' ');
  530.                 {
  531.                     std::stringstream in_stream(in);
  532.                     in_stream >> close;
  533.                 }
  534.                 getline(file, in, ' ');
  535.                 {
  536.                     std::stringstream in_stream(in);
  537.                     in_stream >> volume;
  538.                 }
  539.                 getline(file, in, '\n');
  540.                 {
  541.                     std::stringstream in_stream(in);
  542.                     in_stream >> adjclose;
  543.                 }
  544.  
  545.                 PriceData* priceData = new PriceData(date, open, high, low, close, volume, adjclose);
  546.                 data[pdindex] = priceData;
  547.             }
  548.             stocks[index]->setPriceData(data);
  549.         }else{
  550.             break;
  551.         }
  552.     }
  553.  
  554.     file.close();
  555. }
  556.  
  557.  
  558.  
  559. =================================================== MAIN.CPP ========================================================
  560.  
  561. #include <iostream>
  562. #include <sstream>
  563. #include <array>
  564. #include <iomanip>
  565. #include "hashtable.h"
  566.  
  567.  
  568. Stock* searchStock(std::string name, HashTable* table_names, HashTable* table_codes){
  569.     Stock *s = nullptr;
  570.     if(name.length() > 4) {
  571.         s = table_names->search(name, HashTable::HASH_BY_NAME);
  572.     }else{
  573.         s = table_codes->search(name, HashTable::HASH_BY_CODE);
  574.     }
  575.     if(s == nullptr){
  576.         if(name.length() > 4) {
  577.             s = table_codes->search(name, HashTable::HASH_BY_CODE);
  578.         }else{
  579.             s = table_names->search(name, HashTable::HASH_BY_NAME);
  580.         }
  581.     }
  582.     return s;
  583. }
  584.  
  585. void printMenuInfo(){
  586.     std::cout << "\n\n";
  587.     std::cout << "==============================" << std::endl;
  588.     std::cout << "1 ... Add stock" << std::endl;
  589.     std::cout << "2 ... Search stock" << std::endl;
  590.     std::cout << "3 ... Delete stock" << std::endl;
  591.     std::cout << "4 ... Import price data from file" << std::endl;
  592.     std::cout << "5 ... Plot price data of a stock" << std::endl;
  593.     std::cout << "6 ... Save hashtable to file" << std::endl;
  594.     std::cout << "7 ... Load hashtable from file" << std::endl;
  595.     std::cout << "8 ... Quit program" << std::endl;
  596.     std::cout << "------------------------------" << std::endl;
  597. }
  598.  
  599. int main() {
  600.  
  601.     std::string in = "";
  602.     HashTable *table_names = new HashTable();
  603.     HashTable *table_codes = new HashTable();
  604.  
  605.     // QUIT if input is 8
  606.     while(in.compare("8") != 0){
  607.         printMenuInfo();
  608.         std::cin >> in;
  609.         if(in.compare("1") == 0){
  610.             // ADD
  611.             std::string name, member_code, sin_s;
  612.             int sin;
  613.             std::cout << "Name: ";
  614.             std::cin >> name;
  615.             std::cout << "Member Code: ";
  616.             std::cin >> member_code;
  617.             int fail = 0;
  618.             do {
  619.                 std::cout << "Security Identification Number: ";
  620.                 std::cin >> sin_s;
  621.                 std::stringstream s(sin_s);
  622.                 s >> sin;
  623.                 if(s.fail()){
  624.                     fail = 1;
  625.                 }else{
  626.                     fail = 0;
  627.                 }
  628.             }while(fail);
  629.             Stock *stock = new Stock(name, member_code, sin);
  630.             table_names->add(stock, HashTable::HASH_BY_NAME);
  631.             table_codes->add(stock, HashTable::HASH_BY_CODE);
  632.         }else if(in.compare("3") == 0){
  633.             // DELETE
  634.             std::string name;
  635.             std::cout << "Name/Member Code: ";
  636.             std::cin >> name;
  637.             Stock *s = searchStock(name, table_names, table_codes);
  638.             if(s != nullptr) {
  639.                 table_names->remove(s->getName(), HashTable::HASH_BY_NAME);
  640.                 table_codes->remove(s->getMemberCode(), HashTable::HASH_BY_CODE);
  641.                 std::cout << name << " successfully removed!" << std::endl;
  642.             }else{
  643.                 std::cout << "Could not delete stock! (Not found)";
  644.             }
  645.         }else if(in.compare("4") == 0){
  646.             // IMPORT
  647.             std::string name;
  648.             std::cout << "Name/Member Code: ";
  649.             std::cin >> name;
  650.             Stock* s = searchStock(name, table_names, table_codes);
  651.             if(s != nullptr) {
  652.                 std::string filepath;
  653.                 std::cout << "Filepath: ";
  654.                 std::cin >> filepath;
  655.                 s->import_price_data(filepath);
  656.                 std::cout << "Import successful!" << std::endl;
  657.             }else{
  658.                 std::cout << "Could not find stock!"  << std::endl;
  659.             }
  660.  
  661.         }else if(in.compare("2") == 0){
  662.             // SEARCH
  663.             std::string name;
  664.             std::cout << "Name/Member Code: ";
  665.             std::cin >> name;
  666.             Stock* s = searchStock(name, table_names, table_codes);
  667.             if(s != nullptr) {
  668.                 std::cout << "NAME: " << s->getName() << std::endl;
  669.                 std::cout << "MEMBER CODE: " << s->getMemberCode() << std::endl;
  670.                 std::cout << "SIN: " << s->getSIN() << std::endl;
  671.                 PriceData* data = s->getPriceData()[0];
  672.                 if(data != nullptr) {
  673.                     std::cout << "High: " << data->getHigh() << std::endl;
  674.                 }else{
  675.                     std::cout << "No price data found" << std::endl;
  676.                 }
  677.             }else{
  678.                 std::cout << "Could not find stock!";
  679.             }
  680.         }else if(in.compare("5") == 0){
  681.             // PLOT
  682.             std::string name;
  683.             std::cout << "Name/Member Code: ";
  684.             std::cin >> name;
  685.             Stock* s = searchStock(name, table_names, table_codes);
  686.             //check if the stock from input is available
  687.             if(s != nullptr) {
  688.                 //define the size of the Plotting window
  689.                 constexpr size_t entries = 30;
  690.                 constexpr size_t steps   = 25;
  691.                 //the array/vector to put the values of the stock in it
  692.                 std::array<int, entries> arr;
  693.                 double max = 0;
  694.                 PriceData* data = nullptr;
  695.                 //get the max value of the chosen stock
  696.                 for (int i = 0; i < entries; ++i) {
  697.                     data = s->getPriceData()[i];
  698.                     if (data != nullptr)
  699.                         max = std::max(max, data->getHigh());
  700.                 }
  701.                 //write every value of the last 30 days in the correct position of the array/vector
  702.                 auto adjust = max / steps;
  703.                 for (int i = 0; i < entries; ++i) {
  704.                     data = s->getPriceData()[i];
  705.                     arr[i] = (data != nullptr) ? data->getHigh() / adjust : 0;
  706.                 }
  707.                 // plot the graph of the stock
  708.                 std::cout << "\n" << std::endl; // clear new line...
  709.                 for (int j = steps; j > 0; j--) {
  710.                     for (int i = 0; i < entries; ++i)
  711.                         if (arr[i] >= j)
  712.                             std::cout << "#";
  713.                         else
  714.                             std::cout << " ";
  715.                     std::cout << " " << j*adjust << std::endl;
  716.                 }
  717.  
  718.                 //function to add the correct numbers of day in under my plotted graph
  719.                 auto plot_numbers = [&](const std::vector<int>& in) {
  720.                     for (auto& i : in) std::cout << i;
  721.                     std::cout << std::endl;
  722.                 };
  723.                 plot_numbers(std::vector <int>{0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3});
  724.                 plot_numbers(std::vector <int>{1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0});
  725.  
  726.  
  727.  
  728.             }else{
  729.                 std::cout << "Could not find stock!";
  730.             }
  731.         }else if(in.compare("6") == 0){
  732.             // SAVE
  733.             std::cout << "Saving to file..." << std::endl;
  734.             table_names->saveToFile("hashtable_names.txt");
  735.             table_codes->saveToFile("hashtable_codes.txt");
  736.             std::cout << "Saved!" << std::endl;
  737.         }else if(in.compare("7") == 0){
  738.             // LOAD
  739.             std::cout << "Reading from file..." << std::endl;
  740.             table_names->readFromFile("hashtable_names.txt");
  741.             table_codes->readFromFile("hashtable_codes.txt");
  742.             std::cout << "Finished!" << std::endl;
  743.         }
  744.     }
  745.  
  746.     std::cout << "Exiting program..." << std::endl;
  747.     std::cout << "Good Bye!" << std::endl;
  748.  
  749.     return 0;
  750. }
Advertisement
Add Comment
Please, Sign In to add comment