Advertisement
Guest User

stock.cpp

a guest
Jul 30th, 2012
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include "stock.h"
  2.  
  3. //Accessors for private data
  4. std::string Stock::get_symbol() const { return symbol;}
  5. float Stock::get_openingPrice() const { return openingPrice; }
  6. float Stock::get_closingPrice() const { return closingPrice; }
  7. float Stock::get_todayHigh() const { return todayHigh; }
  8. float Stock::get_todayLow() const { return todayLow; }
  9. float Stock::get_prevClose() const { return prevClose; }
  10. unsigned int Stock::get_volume() const { return volume; }
  11.  
  12.  
  13. //calculate todays gain in %
  14. float Stock::today_gain() const
  15. {
  16.     return (closingPrice-prevClose)/prevClose*100;
  17. }
  18.  
  19.  
  20. //overload >Stock operator so it checks if the stock symbol
  21. //is alpabetically larger than another.
  22. //symbol[] here is the > left operand, r_symbol the right
  23. bool Stock::operator >(Stock& right) const
  24. {
  25.     std::string r_symbol = right.get_symbol();
  26.  
  27.     //prepare to loop aal characters if needed
  28.     for (int i=0; i < (MAX_SYMBOL_SIZE-1); i++)
  29.     {
  30.         //if first letter is smaller, exit function and return true
  31.         if (symbol[i] > r_symbol[i])
  32.         {
  33.             return true;
  34.         }
  35.  
  36.         //otherwise
  37.         else if (symbol[i] == r_symbol[i])
  38.         {
  39.             //check the length of both symbols
  40.             //if we're on last characters and
  41.             //one is shorter than the other, exit function
  42.             if ( symbol.length() < (i+2))
  43.             {
  44.                 return false;
  45.             }
  46.  
  47.             else if (r_symbol.length() < (i+2))
  48.             {
  49.                 return true;
  50.             }
  51.            
  52.             else
  53.             {
  54.                 //still characters left, so continue the for loop
  55.                 continue;
  56.             }
  57.         }
  58.        
  59.         //if the first symbol was already smaller
  60.         else
  61.         {
  62.             return false;
  63.         }
  64.     }
  65.  
  66.     return false;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement