Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stocklist.h"
- //PRIVATE
- //add a stock object to our list and increase the total
- void StockList::add(Stock s)
- {
- list[++counter] = s;
- total_assets += (s.get_volume()*s.get_closingPrice());
- }
- //swap two positions in the list, used for sorting.
- void StockList::swap(int a, int b)
- {
- Stock tmp;
- tmp = list[a];
- list[a] = list[b];
- list[b] = tmp;
- }
- //PUBLIC
- //Constructor sets member variables
- StockList::StockList() : counter(0), total_assets(0.0F)
- {
- list = new Stock[10];
- }
- //Destructor deletes the memory allocated for the list
- StockList::~StockList()
- {
- delete [] list;
- }
- //load the data from the textfile using the overloaded >>stock operator
- bool StockList::load(const char* filename)
- {
- std::ifstream ifs;
- ifs.open(filename);
- if (ifs.is_open() == true && ifs.good())
- {
- //while not at end of file, parse a line to a stock object and add it to the list
- while (!ifs.eof())
- {
- Stock tmp;
- ifs >> tmp;
- add(tmp);
- }
- ifs.close();
- return true;
- }
- else
- {
- return false;
- }
- }
- unsigned int StockList::listlength() const
- {
- return counter;
- }
- //print our list all pretty
- void StockList::print() const
- {
- std::cout << std::left << std::setw(8) << "Symbol"
- << std::right
- << std::setw(8) << "Open"
- << std::setw(8) << "Close"
- << std::setw(8) << "High"
- << std::setw(8) << "Low"
- << std::setw(8) << "pClose"
- << std::setw(8) << "\% gain"
- << std::setw(8) << "Volume" << "\n";
- for (unsigned int i=0; i<counter; i++)
- {
- std::cout.precision(2);
- std::cout << std::left << std::setw(8) << list[i].get_symbol()
- << std::right << std::fixed
- << std::setw(8) << list[i].get_openingPrice()
- << std::setw(8) << list[i].get_closingPrice()
- << std::setw(8) << list[i].get_todayHigh()
- << std::setw(8) << list[i].get_todayLow()
- << std::setw(8) << list[i].get_prevClose()
- << std::setw(8) << list[i].today_gain()
- << std::setw(8) << list[i].get_volume() << "\n";
- }
- std::cout << "\n\nClosing assets: $" << std::setprecision(2) << std::fixed << total_assets << std::endl;
- }
- //sort using a basic bubble sort, good enough for this task
- //it basically keeps itaerating the list, checking if the current element
- //is bigger than the next and if so it swaps them, it keeps doing this until it
- //finds nothing to swap anymore.
- void StockList::sort()
- {
- bool swap_flag;
- do
- {
- swap_flag = false;
- for (unsigned int i=0; i<counter-1; i++)
- {
- //this uses the overloaded >Stock operator
- if (list[i] > list[i+1])
- {
- swap(i, i+1);
- swap_flag = true;
- }
- }
- } while (swap_flag == true);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement