In C Write a program to automate a stock trading company At
In C++:
Write a program to automate a stock trading company. At the end of each trading day, the company would like to generate and post the listing of its stocks. The desired output is to produce a sorted list by stock symbol.
Create an input data file in the following format, seven data separated by a single space, don’t enter the heading:
Create a class, stockListType, to implement a vector of stock objects.
The class stockListType is a template, designed to create a list of stock objects.
Design and implement a class called stockType.
the data members are symbol, openPrice, closePrice, highPrice, lowPrice, prevPrice, percentGainOrLoss, and numOfShares.
the functions:
set the stock information.
print the stock information,
show the different prices.
calculate and print the percent of gain/loss.
The formula is (TodayClose – YesterdayClose) / YesterdayClose * 100.
overload the relational operators to compare two stock objects by their symbols
overload the insert \'<<\' and extraction \'>>\' operators, << , for easy input/output.
Add and/or overwrite the operations of the class to implement the necessary operations on a stock.
Write a program that uses these two classes to analyze the data and produce the following output:
Please upload the following:
The class .cpp file
The main program
The class .h file
Output File
| symbol | openingPrice | closingPrice | todayHigh | todayLow | prevClose | volume |
| MSET | 120 | 140 | 145 | 140 | 115 | 30920 |
| AOLK | 80 | 75 | 82 | 74 | 83 | 5000 |
| ABC | 123.45 | 130.95 | 132 | 125 | 120.50 | 10000 |
| IBD | 68 | 71 | 72 | 67 | 75 | 15000 |
| CSCO | 100 | 102 | 105 | 98 | 101 | 25000 |
Solution
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cassert>
using namespace std;
template <class elemType>
class listType
{
public:
//here some functions are used those can be declared as public
bool isEmpty() const;
bool isFull() const;
int getLength() const;
int getMaximumSize() const;
void sort();
void print() const;
void insertAt(const elemType& item, int position);
listType(int listSize = 50);
~listType();
protected:
int maximumSize;
int length;
elemType *list;
};
template <class elemType>
bool listType<elemType>::isEmpty() const //boolean function to check whether array is empty or not
{
return (length == 0); // return the length is zero if the array is empty
}
template <class elemType>
bool listType<elemType>::isFull() const //boolean function to check whether the array is full or not
{
return (length == maximumSize); //return the length size
}
template <class elemType>
int listType<elemType>::getLength() const // create function for length
{
return length; //then return the length
}
template <class elemType>
int listType<elemType>::getMaximumSize() const //create function to get maximum size
{
return maximumSize;// then return the maximum size value
}
template <class elemType>
listType<elemType>::listType(int listSize) //here we are using the default constructor
{
maximumSize = listSize;
length = 0;
list = new elemType[maximumSize];
}
template <class elemType>
listType<elemType>::~listType()
{
delete [] list;//delete list
}
template <class elemType>
void listType<elemType>::sort() // use sorting to be in order of array
{
int a, b;
int minimum;
elemType temp;
for(a = 0; a < length; a++)
{
minimum = a;
for(b = a; b < length; ++b)
if (list[b] < list[minimum])
minimum = b;
temp = list[a];
list[a] = list[minimum];
list[minimum] = temp;
}
}
template <class elemType>
void listType<elemType>::print() const // print the values
{
int a;
for(a = 0; a < length; ++a) //pre-incrementor function used here
cout << list[a];
cout << endl;
}
template <class elemType>
void listType<elemType>::insertAt(const elemType& item, int position) // insert the item using insertAt function
{
assert(position >= 0 && position < maximumSize); //assert function for getting maximum size
list[position] = item;
length++;
}
class Stock
{
private: //create stock values are private
string stock_symbol; //declare stock_symbol is a string
double open_price;// opening price should be double
double close_price;// closing price also be double
double today_high; // create today_high variable for stock as double
double today_low; //create today_low variable for stock as double
int number_of_shares;// number of shares should be a integer
double previous_closing_price; // previously closing price
static double total_assets;// total assets are static and declare the variable as double
double percent_gain_loss;//pecent_gain_loss is used for knowing percentage of gain and loss of the shares
public:
void setStock(string stock_symbol,double open_price,double close_price,
double today_high, double today_low, int number_of_shares, double previous_closing_price)
{
this->stock_symbol = stock_symbol;
this->open_price = open_price;
this->close_price = close_price;
this->today_high = today_high;
this->today_low = today_low;
this->number_of_shares = number_of_shares;
this->previous_closing_price = previous_closing_price;
}
void Print()//void print function used for print the values
{
cout <<\"Stock name is \" << stock_symbol << endl;
}
void show_different_prices()//funtion for different prices
{
cout <<\"today\'s opening price was \"<<open_price <<\", the closing was \"<<close_price <<\", today\'s high was \" <<
today_high << \", today\'s low was \" <<today_low << \" Previous closing price is \" << previous_closing_price <<endl;
}
void calculate_gain_loss()//gain and loss calculating function
{
percent_gain_loss = (close_price-previous_closing_price)/previous_closing_price*100;
}
void show_number_of_shares() //print number of shares are there
{
cout <<\"Number of share is \" << number_of_shares << endl;
}
bool operator> (Stock &cStock)//overloading
{
return (this->stock_symbol.compare(cStock.stock_symbol)>0);
}
bool operator>= (Stock &cStock)
{
return (this->stock_symbol.compare(cStock.stock_symbol)>=0);
}
bool operator< (Stock &cStock)
{
return (this->stock_symbol.compare(cStock.stock_symbol)<0);
}
bool operator<= (Stock &cStock)
{
return (this->stock_symbol.compare(cStock.stock_symbol)<=0);
}
bool operator==(Stock &cStock)
{
return (this->stock_symbol.compare(cStock.stock_symbol)==0);
}
friend ostream& operator<< (ostream &out, Stock &cStock);// purpose of overloading is that we can output in very convenient way
friend istream& operator>> (istream &in, Stock &cStock);//read the input file and then store it in myStock object
static double getTotalAssets() //function of total assets
{
return total_assets;//return the asset value
}
};
istream& operator>>(istream &in, Stock &cStock) //use this because of overloading
{ //read all values
in >> cStock.stock_symbol;
in >> cStock.open_price;
in >> cStock.close_price;
in >> cStock.today_high;
in >> cStock.today_low;
in >> cStock.previous_closing_price;
in >> cStock.number_of_shares;
// cout << cStock.total_assets << endl;
cStock.total_assets+=cStock.number_of_shares*cStock.close_price;
return in;
}
ostream& operator<< (ostream &out, Stock &cStock)
{
cStock.calculate_gain_loss(); //calculate the profit and loss of the stocks
out << setw(8) << left << cStock.stock_symbol <<right << fixed << setprecision(2) << setw(8) <<
cStock.open_price<< fixed << setprecision(2) << setw(8) <<
cStock.close_price<< fixed <<setprecision(2) << setw(8) <<
cStock.today_high<< fixed << setprecision(2) << setw(8) <<
cStock.today_low<< fixed <<setprecision(2) << setw(8) <<
cStock.previous_closing_price<< fixed << setprecision(2) << setw(8) <<
cStock.percent_gain_loss<< fixed << setprecision(2) << setw(12) <<
cStock.number_of_shares<< endl;
return out;
}
double Stock::total_assets = 0.0;
int main()
{
ifstream infile(\"StockData.txt\");
listType<Stock> stock_array(50);
Stock local_stock;
int count = 0;
if(!infile)
{
cout <<\"Unable to open file StockData.txt. so Exiting from program \";
return 0;
}
while(!infile.eof())
{
infile >> local_stock;
stock_array.insertAt(local_stock,count++);
}
stock_array.sort();
cout <<\"Stock Today Previous Percent \" << endl;
cout <<\"Symbol Open Close High Low Close Gain Volume\" << endl;
cout <<\"------------------------------------------------------------------\"<< endl;
stock_array.print();
cout <<\"Closing Assets: $\"<<fixed << setprecision(2) << Stock::getTotalAssets()<< endl;
system(\"pause\");//pause
return 0;
}




