Sunday, 29 September 2013

sorting using vectors c++

sorting using vectors c++

Hello everyone i'm writing a program of a stock market where i read from a
file and sort with symbols and percent gain/loss. I have completed sorting
with symbols but having trouble establishing the percent gain loss. First
i designed and implemented the stock object. call the class stockType.
main components of a stock are the stock symbol, stock price and number of
shares. Second we have to create a list of stock objects. call the class
to implement a list of stock objects stockListType. To store the list of
stocks, i declared a vector and called the component type of this vector
stockType.
Because the company requires me to produce the list ordered by percent
gain/loss, i need to sort the stock list by this component. However, i'm
not to physically sort the list by component percent gain/loss; instead i
should provide a logic ordering with respect to this component. To do so i
added a data member, a vector to hold the indices of the stock list
ordered by the component percent gain/loss and i called this array
indexByGain. I am going to use the array indexByGain to print the list.
The elements of the array indexByGain will tell which component of the
stock list to print next. I have trouble going about how to implement the
function sortStockGain(). i need assistance and this would be of great
help as i don't know how to start. below is my entire code and the txt
file. I have sort symbols working but dont know how to implement the sort
by gain if one could assist me this would be of great help. By the way
this is my first programming class.
Below is my entire code:
#ifndef STOCKTYPE_H
#define STOCKTYPE_H
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class stockType
{
public:
stockType(string symbol="", double openPrice=0.0, double closePrice=0.0,
double highPrice=0.0,
double lowPrevPrice=0.0, double closePrevPrice=0.0, int shares=0);
friend istream& operator>>(istream& ins, stockType& stock);
friend ostream& operator<<(ostream& outs, const stockType& stock);
//sets the variables
void setStockInfo(string syms, double open, double close, double high,
double lowPrev, double closePrev, int no_of_shares);
//calculates the gain
void calculateGain(double closeP, double prevP);
bool operator==(const stockType& stock1) const;
bool operator!=(const stockType& stock1) const;
bool operator<=(const stockType& stock1) const;
bool operator<(const stockType& stock1) const;
bool operator>=(const stockType& stock1) const;
bool operator>(const stockType& stock1) const;
//member functions
string getSymbols()const {return symbols;}
double getOpenPrice()const {return open_price;}
double getClosePrice()const {return close_price;}
double getHighPrice()const {return high_price;}
double getLowPrevPrice()const {return low_prev_price;}
double getClosePrevPrice()const {return close_prev_price;}
double getShares()const {return no_shares;}
double getGain()const {return gain;}
private:
string symbols;
double open_price, close_price, high_price, low_prev_price,
close_prev_price, gain;
int no_shares;
};
#include "stockType.h"
stockType::stockType(string symbol, double openPrice, double
closePrice, double highPrice,
double lowPrevPrice, double closePrevPrice, int shares)
{
setStockInfo(symbol, openPrice, closePrice, highPrice, lowPrevPrice,
closePrevPrice, shares);
}
void stockType::setStockInfo(string syms, double open, double close,
double high,
double lowPrev, double closePrev, int no_of_shares)
{
symbols = syms;
open_price = open;
close_price = close;
high_price = high;
low_prev_price = lowPrev;
close_prev_price = closePrev;
no_shares = no_of_shares;
}
istream& operator>>(istream& ins, stockType& stock)
{
ins>>stock.symbols;
ins>>stock.open_price;
ins>>stock.close_price;
ins>>stock.high_price;
ins>>stock.low_prev_price;
ins>>stock.close_prev_price;
ins>>stock.no_shares;
stock.calculateGain(stock.close_price, stock.close_prev_price);
return ins;
}
ostream& operator<<(ostream& outs, const stockType& stock)
{
outs<<stock.getSymbols()
<<fixed<<showpoint<<setprecision(2)
<<setw(10)<<stock.getOpenPrice()<<setw(10)
<<stock.getClosePrice()<<setw(10)
<<stock.getHighPrice()<<setw(10)
<<stock.getLowPrevPrice()<<setw(11)
<<stock.getClosePrevPrice()
<<setw(10)<<stock.getGain()<<"%"<<setw(13)
<<stock.getShares()<<endl<<endl;
return outs;
}
void stockType::calculateGain(double closeP, double prevP)
{
gain = ((closeP - prevP)/(prevP)*100);
}
bool stockType::operator==(const stockType& stock1) const
{
return (symbols==stock1.symbols);
}
bool stockType::operator!=(const stockType& stock1) const
{
return (symbols!=stock1.symbols);
}
bool stockType::operator>=(const stockType& stock1) const
{
return (symbols>=stock1.symbols);
}
bool stockType::operator>(const stockType& stock1) const
{
return (symbols>stock1.symbols);
}
bool stockType::operator<=(const stockType& stock1) const
{
return (symbols<=stock1.symbols);
}
bool stockType::operator<(const stockType& stock1) const
{
return (symbols<stock1.symbols);
}
#ifndef stockListType_H
#define stockListType_H
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "stockType.h"
using namespace std;
class stockListType
{
public:
stockListType()
{
}
void insert(const stockType& item);
void sortStockSymbols();
void sortStockGain();
void printStockSymbols();
void printStockGain();
private:
vector<int> indexByGain;
vector<stockType> list;
};

No comments:

Post a Comment