Advertisement
Guest User

stocklist.cpp

a guest
Jul 30th, 2012
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #include "stocklist.h"
  2.  
  3. //PRIVATE
  4. //add a stock object to our list and increase the total
  5. void StockList::add(Stock s)
  6. {
  7.     list[++counter] = s;
  8.     total_assets += (s.get_volume()*s.get_closingPrice());
  9. }
  10.  
  11.  
  12. //swap two positions in the list, used for sorting.
  13. void StockList::swap(int a, int b)
  14. {
  15.     Stock tmp;
  16.     tmp = list[a];
  17.     list[a] = list[b];
  18.     list[b] = tmp;
  19. }
  20.  
  21.  
  22. //PUBLIC
  23. //Constructor sets member variables
  24. StockList::StockList() : counter(0), total_assets(0.0F)
  25. {
  26.     list = new Stock[10];
  27. }
  28.  
  29.  
  30. //Destructor deletes the memory allocated for the list
  31. StockList::~StockList()
  32. {
  33.     delete [] list;
  34. }
  35.  
  36.  
  37. //load the data from the textfile using the overloaded >>stock operator
  38. bool StockList::load(const char* filename)
  39. {
  40.     std::ifstream ifs;
  41.     ifs.open(filename);
  42.  
  43.     if (ifs.is_open() == true  && ifs.good())
  44.     {
  45.         //while not at end of file, parse a line to a stock object and add it to the list
  46.         while (!ifs.eof())
  47.         {
  48.             Stock tmp;
  49.             ifs >> tmp;
  50.             add(tmp);
  51.         }
  52.        
  53.         ifs.close();
  54.         return true;
  55.     }
  56.  
  57.     else
  58.     {
  59.         return false;
  60.     }
  61.  
  62. }
  63.  
  64.  
  65. unsigned int StockList::listlength() const
  66. {
  67.     return counter;
  68. }
  69.  
  70.  
  71. //print our list all pretty
  72. void StockList::print() const
  73. {  
  74.     std::cout   << std::left << std::setw(8) << "Symbol"
  75.             << std::right
  76.             << std::setw(8) << "Open"
  77.             << std::setw(8) << "Close"
  78.             << std::setw(8) << "High"
  79.             << std::setw(8) << "Low"
  80.             << std::setw(8) << "pClose"
  81.             << std::setw(8) << "\% gain"
  82.             << std::setw(8) << "Volume" << "\n";
  83.  
  84.     for (unsigned int i=0; i<counter; i++)
  85.     {
  86.         std::cout.precision(2);
  87.         std::cout   << std::left << std::setw(8) << list[i].get_symbol()
  88.                 << std::right << std::fixed
  89.                 << std::setw(8) << list[i].get_openingPrice()
  90.                 << std::setw(8) << list[i].get_closingPrice()
  91.                 << std::setw(8) << list[i].get_todayHigh()
  92.                 << std::setw(8) << list[i].get_todayLow()
  93.                 << std::setw(8) << list[i].get_prevClose()
  94.                 << std::setw(8) << list[i].today_gain()
  95.                 << std::setw(8) << list[i].get_volume() << "\n";
  96.     }
  97.  
  98.     std::cout << "\n\nClosing assets: $" << std::setprecision(2) << std::fixed << total_assets << std::endl;
  99. }
  100.  
  101. //sort using a basic bubble sort, good enough for this task
  102. //it basically keeps itaerating the list, checking if the current element
  103. //is bigger than the next and if so it swaps them, it keeps doing this until it
  104. //finds nothing to swap anymore.
  105. void StockList::sort()
  106. {
  107.     bool swap_flag;
  108.     do
  109.     {
  110.         swap_flag = false;
  111.  
  112.         for (unsigned int i=0; i<counter-1; i++)
  113.         {
  114.             //this uses the overloaded >Stock operator
  115.             if (list[i] > list[i+1])
  116.             {
  117.                 swap(i, i+1);
  118.                 swap_flag = true;
  119.             }
  120.         }
  121.     } while (swap_flag == true);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement