I need help to complete this assignment below as per c+++, if it can be done dif
ID: 3867296 • Letter: I
Question
I need help to complete this assignment below as per c+++, if it can be done differently from the authors's own that will be great. Thank you
Source: C++ PROGRAMMING (Program Design Including Data Structure (7th Edition) by D.S.Malik.
Chapter 13: Overloading and Templates
Programming Exercise 18
Minimum Requirements
You must provide a Word-style document explaining what your program does showing the expected runtime output. Please advise me as to anything else that’s of importance to running it.
Write a program to help a local stock trading company automate its systems. The company invests only in the stock market. At the end of each trading day, the company would like to generate and post the listing of its stocks so that investors can see how their holdings performed that day. We assume that the company invests in, say, 10 different stocks. The desired output is to produce two listings, one sorted by stock symbol and another sorted by percent gain from highest to lowest.
The input data is provided in a file in the following format:
symbol openingPrice closingPrice todayHigh todayLow prevClose volume
For example, the sample data is:
MSMT 112.50 115.75 116.50 111.75 113.50 6723823
CBA 67.50 75.50 78.75 67.50 65.75 378233
The first line indicates that the stock symbol is MSMT, today's opening price was 112.50, the closing price was 115.75, today's high price was 116.50, today's low price was 111.75, yesterday's closing price was 113.50, and the number of shares currently being held is 6723823
The listing sorted by stock symbols must be of the following form:
Financial report
STOCK SYMBOL
…………..
OPEN
…………
CLOSE
………..
HIGH
……….
LOW
………..
PREVIOUS CLOSE
…………
PERCENT GAIN
…………
VOLUME
……………
ABC
123.45
130.95
132.00
125.00
120.50
8.67%
10000
AOLK
80.00
75.00
82.00
74.00
83.00
-9.64%
5000
CSCO
100.00
102.00
105.00
98.00
101.00
0.99%
25000
IBD
68.00
71.00
72.00
67.00
75.00
-5.33%
15000
MSET
120.00
140.00
145.00
140.00
115.00
21.74%
30920
Develop this programming exercise in two steps. In the first step (part a), design and implement a stock object. In the second step (part b), design and implement an object to maintain a list of stocks.
Part a:
(Stock Object) Design and implement the stock object. Call the class that captures the various characteristics of a stock object stockType.
The main components of a stock are the stock symbol, stock price, and number of shares. Moreover, we need to output the opening price, closing price, high price, low price, previous price, and the percent gain/loss for the day. These are also all the characteristics of a stock. Therefore, the stock object should store all this information.
Perform the following operations on each stock object:
1. Set the stock information.
2. Print the stock information.
3. Show the different prices.
4. Calculate and print the percent gain/loss.
5. Show the number of shares.
~the natural ordering of the stock list is by stock symbol. Overload the relational operators to compare two stock objects by their symbols.
~ Overload the insertion operator, < ~Because the data is stored in a file, overload the stream extraction operator, >>, for easy input.
For example, suppose infile is ifstream object and the input file was opened using the object infile. Further supppose that myStock is a stock object. Then the statement:
infile >>myStock;
read the data from the input file and stores it in the object myStock. (Note that this statement reads and stores the data in the relevant components of myStock.)
Part C:
Write a program that uses these two classes to automate the company's analysis of stock data
STOCK SYMBOL
…………..
OPEN
…………
CLOSE
………..
HIGH
……….
LOW
………..
PREVIOUS CLOSE
…………
PERCENT GAIN
…………
VOLUME
……………
ABC
123.45
130.95
132.00
125.00
120.50
8.67%
10000
AOLK
80.00
75.00
82.00
74.00
83.00
-9.64%
5000
CSCO
100.00
102.00
105.00
98.00
101.00
0.99%
25000
IBD
68.00
71.00
72.00
67.00
75.00
-5.33%
15000
MSET
120.00
140.00
145.00
140.00
115.00
21.74%
30920
Explanation / Answer
class newString
{
//overload the stream insertion and extraction operators
friend ostream& operator<< (ostream&, const newString&);
friend istream& operator>> (istream&, newString&);
public:
const newString& operator=(const newString& rightStr);
//overload the assignment operator
newString(const char *);
//constructor; conversion from the char string
newString();
//default constructor to initialize the string to null
newString(const newString& rightStr);
//copy constructor
~newString();
//destructor
char &operator[] (int);
const char &operator[](int) const;
//overload the relational operators
bool operator==(const newString& rightStr) const;
bool operator!=(const newString& rightStr) const;
bool operator<=(const newString& rightStr) const;
bool operator<(const newString& rightStr) const;
bool operator>=(const newString& rightStr) const;
bool operator>(const newString& rightStr) const;
private:
char *strPtr; //pointer to the char array
//that holds the string
int strLength; //data member to store the length
//of the string
};
//overload the assignment operator
const newString& newString::operator=(const newString &rightStr)
{
if(this != &rightStr) //avoid self-copy
{
delete [] strPtr;
strLength = rightStr.strLength;
strPtr = new char[strLength + 1];
assert(strPtr != NULL);
strcpy(strPtr, rightStr.strPtr);
}
return *this;
}
//constructor; conversion from the char string
newString::newString(const char *str)
{
strLength = strlen(str);
strPtr = new char[strLength + 1]; //allocate memory to store char string
assert(strPtr != NULL);
strcpy(strPtr, str); //copy string into strPtr
}
//default constructor to initialize the string to null
newString::newString()
{
strLength = 0;
strPtr = new char[1];
assert(strPtr != NULL);
strcpy(strPtr, "");
}
//copy constructor
newString::newString(const newString& rightStr)
{
strLength = rightStr.strLength;
strPtr = new char[strLength + 1];
assert(strPtr != NULL);
strcpy(strPtr, rightStr.strPtr);
}
//destructor
newString::~newString()
{
delete [] strPtr;
}
char& newString::operator[] (int index)
{
assert(0 <= index && index < strLength);
return strPtr[index];
}
const char& newString::operator[](int index) const
{
assert(0 <- index && index < strLength);
return strPtr[index];
}
bool newString::operator==(const newString& rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) == 0);
}
bool newString::operator!=(const newString& rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) != 0);
}
bool newString::operator<=(const newString& rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) <= 0);
}
bool newString::operator<(const newString& rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) < 0);
}
bool newString::operator>=(const newString& rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) >= 0);
}
bool newString::operator>(const newString& rightStr) const
{
return (strcmp(strPtr, rightStr.strPtr) > 0);
}
//Friend functions
ostream& operator<< (ostream &out, const newString &rightStr)
{
out << rightStr.strPtr;
return out;
}
istream& operator>> (istream &in, newString &rightStr)
{
char temp[81];
in >> setw(81) >> temp;
rightStr = temp;
return in;
}
//////////////////////////////////
#include <iostream>
#include <fstream>
#include "myString.h"
using namespace std;
class stockType
{
friend ostream& operator<< (ostream&, stockType&);
friend ifstream& operator>> (ifstream&, stockType&);
public:
void setStockInfo(newString symbol, double openPrice,
double closeProce, double high,
double Low, double prevClose,
int shares);
newString getSymbol();
double getPercentChange();
double getOpenPrice();
double getClosePrice();
double getHighPrice();
double getLowPrice();
double getPrevPrice();
int getNoOfShares();
stockType();
stockType(newString symbol, double openPrice,
double closeProce, double high,
double Low, double prevClose,
int shares);
int operator ==(stockType &other);
int operator !=(stockType &other);
int operator <=(stockType &other);
int operator >=(stockType &other);
int operator >(stockType &other);
int operator <(stockType &other);
private:
newString stockSymbol;
double todayOpenPrice;
double todayClosePrice;
double todayHigh;
double todayLow;
double yesterdayClose;
double percentChange;
int noOfShares;
};
#include <iostream>
#include <iomanip>
#include "stockListType.h"
using namespace std;
void stockListType::printBySymbol()
{
sort();
printReports();
}
void stockListType::printByGain()
{
sortByGain();
//Output file contents to screen
cout << "********* First Investor's Heaven *********" << endl;
cout << "********* Financial Report *********" << endl;
cout << "Stock" << " Today" << " Previous Percent" << endl;
cout << left << setw(6) << "Symbol" << setw(8) << " Open" << setw(8) << " Close" << setw(8) << " High";
cout << setw(8) << " Low" << setw(10) << " Close" << setw(8) << " Gain" << setw(8) << " Volume" << endl;
cout << setw(6) << "------" << setw(8) << " ------" << setw(8) << " ------" << setw(8) << " ------";
cout << setw(8) << " ------" << setw(10) << " --------" << setw(8) << " ------" << setw(8) << " ------" << endl;
//Iterate through list and print contents
int i;
for(i = 0; i < length; ++i)
cout << elements[indexByGain[i]] << endl;
//cout << "Closing Assets: $??????.??" << endl;
cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl;
cout << endl;
}
void stockListType::printReports()
{
//Output file contents to screen
cout << "********* First Investor's Heaven *********" << endl;
cout << "********* Financial Report *********" << endl;
cout << "Stock" << " Today" << " Previous Percent" << endl;
cout << left << setw(6) << "Symbol" << setw(8) << " Open" << setw(8) << " Close" << setw(8) << " High";
cout << setw(8) << " Low" << setw(10) << " Close" << setw(8) << " Gain" << setw(8) << " Volume" << endl;
cout << setw(6) << "------" << setw(8) << " ------" << setw(8) << " ------" << setw(8) << " ------";
cout << setw(8) << " ------" << setw(10) << " --------" << setw(8) << " ------" << setw(8) << " ------" << endl;
//Iterate through list and print contents
int i;
for(i = 0; i < length; ++i)
cout << elements[i] << endl;
//cout << "Closing Assets: $??????.??" << endl;
cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl;
cout << endl;
}
void stockListType::sort()
{
listType<stockType>::sort();
}
void stockListType::sortByGain()
{
int i, j;
int temp;
indexByGain = new int[length];
//Pre-populate the index array
for(int cntr = 0; cntr < length; cntr++)
{
indexByGain[cntr] = cntr;
}
//Sort by the percentage change
for (i = 0; i <length; i++)
{
for (j = i + 1; j < length; j++)
{
if (elements[indexByGain[j]].getPercentChange() > elements[indexByGain[i]].getPercentChange())
{
temp = indexByGain[i];
indexByGain[i] = indexByGain[j];
indexByGain[j] = temp;
}
}
}//end for
}
stockListType::stockListType()
{
indexByGain = new int[50];
}
stockListType::stockListType(int size)
{
indexByGain = new int[size];
}
/Program to test the class listType
#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>
#include "listType.h"
#include "myString.h"
#include "stockType.h"
#include "stockListType.h"
using namespace std;
const int noOfStocks = 5;
void getData(stockListType& list);
int main()
{
stockListType stockList(100);
cout << fixed << showpoint;
cout << setprecision(2);
getData(stockList);
stockList.sort();
stockList.printBySymbol();
cout << endl;
stockList.printByGain();
cout << " Press any key to end...";
getch();
return 0;
}
void getData(stockListType& list)
{
ifstream infile;
char fileName[50];
cout << "Enter input file name: ";
cin >> fileName;
cout << endl;
infile.open(fileName);
if (!infile)
{
cout << "Input file does not exist. Program terminates." << endl;
return;
}
list.setLength(noOfStocks);
list.getList(infile);
}
Output:
Enter input file name: stockdat.txt
********* First Investor's Heaven *********
********* Financial Report *********
Stock Today Previous Percent
Symbol Open Close High Low Close Gain Volume
------ ------ ------ ------ ------ -------- ------ ------
ABC 123.45 130.95 132.00 125.00 120.50 8.67% 10000
AOLK 80.00 75.00 82.00 74.00 83.00 -9.64% 5000
CSCO 100.00 102.00 105.00 98.00 101.00 0.99% 25000
IBD 68.00 71.00 72.00 67.00 75.00 -5.33% 15000
MSET 120.00 140.00 145.00 140.00 115.00 21.74% 30920
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
********* First Investor's Heaven *********
********* Financial Report *********
Stock Today Previous Percent
Symbol Open Close High Low Close Gain Volume
------ ------ ------ ------ ------ -------- ------ ------
MSET 120.00 140.00 145.00 140.00 115.00 21.74% 30920
ABC 123.45 130.95 132.00 125.00 120.50 8.67% 10000
CSCO 100.00 102.00 105.00 98.00 101.00 0.99% 25000
IBD 68.00 71.00 72.00 67.00 75.00 -5.33% 15000
AOLK 80.00 75.00 82.00 74.00 83.00 -9.64% 5000
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Hope this will help you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.