Question

I need one file please (C++)(Stock Market) Write a program to help a local stock trading...

I need one file please

(C++)(Stock Market) 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:

********* 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

Closing Assets: $9628300.00

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

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.

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:

i. Set the stock information.

ii. Print the stock information.

iii. Show the different prices.

iv. Calculate and print the percent gain/loss.

v. Show the number of shares.

a.1. The natural ordering of the stock list is by stock symbol.

Overload the relational operators to compare two stock

objects by their symbols.

a.2. Overload the insertion operator, <<, for easy output.

a.3. Because the data is stored in a file, overload the stream

extraction operator, >>, for easy input.

For example, suppose infile is an ifstream object and the input file

was opened using the object infile. Further suppose that myStock is

a stock object. Then, the statement:

infile >> myStock;

reads 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.)

b. Now that you have designed and implemented the class stockType

to implement a stock object in a program, it is time to create a list of

stock objects.

Let us call the class to implement a list of stock objects stockListType.

The class stockListType must be derived from the class

listType, which you designed and implemented in the previous

exercise. However, the class stockListType is a very specific class,

designed to create a list of stock objects. Therefore, the class

stockListType is no longer a template.

Add and/or overwrite the operations of the class listType to

implement the necessary operations on a stock list.

994 | Chapter 13: Overloading and Templates

The following statement derives the class stockListType from the

class listType.

class stockListType: public listType<stockType>

{

member list

};

The member variables to hold the list elements, the length of the list,

and the max listSize were declared as protected in the class

listType. Therefore, these members can be directly accessed in the

class stockListType.

Because the company also requires you to produce the list ordered by

the percent gain/loss, you need to sort the stock list by this component.

However, you are not to physically sort the list by the component

percent gain/loss. Instead, you will provide a logical ordering with

respect to this component.

To do so, add a member variable, an array, to hold the indices of the

stock list ordered by the component percent gain/loss. Call this array

sortIndicesGainLoss. When printing the list ordered by the

component percent gain/loss, use the array sortIndicesGainLoss

to print the list. The elements of the array sortIndicesGainLoss

will tell which component of the stock list to print next.

c. Write a program that uses these two classes to automate the company’s

analysis of stock data.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE TO COPY:

#include <iostream>
#include <fstream>

using namespace std;

template <class T>
class listType
{
public:
bool isEmptyList() const;

bool isFullList() const;

void setLength(int len);
int showLength() const;
void search(T searchItem) const;

void insert(T newElement);

void deleteItem(T deleteElement);


void print() const;

void getList(ifstream&);


void printList() const;

listType(int listSize);


~listType();

protected:
void binarySearch(T searchItem,
int& found, int& index);

int maxSize;
int length;
T *elements;
};

template <class T>
bool listType<T>::isEmptyList() const
{
return (length == 0);
}

template <class T>
bool listType<T>::isFullList() const
{
return (length == maxsize);
}

template <class T>
void listType<T>::setLength(int len)
{
length = len;
}

template <class T>
int listType<T>::showLength() const
{
return length;
}

template <class T>
void listType<T>::insert(T newElement)
{
if(!isFullList())
{
elements[length] = newElement;
}
else
{
cout << "Cannot insert into a full list!\n";
}
}

template <class T>
void listType<T>::deleteItem(T deleteElement)
{
if(!isEmptyList())
{
T *temp = new T[length];

int counter = 0;

for(int i = 0; i < length; i++)
{
if(elements[i] != deleteElement)
{
temp[counter++] = elements[i];
}
}

delete [] elements;
elements = new T[counter];
length = 0;

for(int j = 0; j < counter; j++)
{
elements[j] = temp[j];
length++;
}
}
else
{
cout << "Cannot delete from the empty list!\n";
}
}

template <class T>
void listType<T>::print() const
{
for(int i = 0; i < length; i++)
{
cout << elements[i];
}
}

template <class T>
void listType<T>::destroyList()
{
length = 0;
}

template <class T>
void listType<T>::printList() const
{
for(int i = 0; i < length; i++)
{
cout << elements[i];
}
}

template <class T>
listType<T>::listType(int listSize)
{
maxSize = listSize;
length = 0;
elements = new T[maxSize];
}
  

template <class T>
listType<T>::listType()
{
maxSize = 50;
length = 0;
elements = new T[maxSize];
}

template <class T>
listType<T>::~listType()
{
delete[] elements;
}

template<class T>
void listType<T>::sort()
{
int i, j;
int min;
T temp;

for (i = 0; i <length; i++)
{
min = i;
for (j = i + 1; j < length; ++j)
if (elements[j] < elements[min])
min = j;
temp = elements[i];
elements[i] = elements[min];
elements[min] = temp;
}
}


template<class T>
void listType<T>::getList(ifstream& infile)
{
int i;

for (i = 0; i < length; i++)
infile >> elements[i];   
}

template<class T>
void listType<T>::search(T searchItem) const
{
int found;
int index;

for(int i = 0; i < length; i++)
{
if(elements[i] == searchItem)
{
found = 1;
index = i;
break;
}
}

if (found)
cout << "Item is in the list" << endl;
else
cout << "Item is not in the list" << endl;
}

template<class T>
void listType<T>::binarySearch(T searchItem,
int& found, int& index)
{

int first = 0;
int last = length -1;

int mid;

found = 0;

while( !found && (first <= last))
{
mid = (first + last) / 2;

if (elements[mid] == searchItem)
found = 1;
else
if (elements[mid] > searchItem)
last = mid -1;
else
first = mid + 1;
}

if(found)
{
index = mid;
}
else
{
index = -1;
}
}

STOCK TYPE:

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;
};


ostream& operator<< (ostream &out, stockType &obj)
{
out << setprecision(2) << fixed << right;
out << setw(6) << obj.stockSymbol;
out << setw(8) << obj.todayOpenPrice;
out << setw(8) << obj.todayClosePrice;
out << setw(8) << obj.todayHigh;
out << setw(8) << obj.todayLow;
out << setw(10) << obj.yesterdayClose;
out << setw(8) << obj.getPercentChange() << "%";
out << setw(8) << obj.noOfShares;
out << endl;

return out;   
}

ifstream& operator>> (ifstream &in, stockType &obj)
{
in >> obj.stockSymbol;
in >> obj.todayOpenPrice;
in >> obj.todayClosePrice;
in >> obj.todayHigh;
in >> obj.todayLow;
in >> obj.yesterdayClose;
in >> obj.noOfShares;

return in;
}

void stockType::setStockInfo(newString symbol, double openPrice,
double closePrice, double high,
double low, double prevClose,
int shares)
{
stockSymbol = symbol;
todayOpenPrice = openPrice;
todayClosePrice = closePrice;
todayHigh = high;
todayLow = low;
yesterdayClose = prevClose;
noOfShares = shares;
}

newString stockType::getSymbol()
{
return stockSymbol;
}

double stockType::getPercentChange()
{
double rslt = 0.0;

rslt = (todayClosePrice - yesterdayClose) / yesterdayClose * 100;

return rslt;
}

double stockType::getOpenPrice()
{
return todayOpenPrice;
}

double stockType::getClosePrice()
{
return todayClosePrice;
}

double stockType::getHighPrice()
{
return todayHigh;
}

double stockType::getLowPrice()
{
return todayLow;
}

double stockType::getPrevPrice()
{
return yesterdayClose;
}

int stockType::getNoOfShares()
{
return noOfShares;
}

stockType::stockType()
{
stockSymbol = "";
todayOpenPrice = 0.0;
todayClosePrice = 0.0;
todayHigh = 0.0;
todayLow = 0.0;
yesterdayClose = 0.0;
noOfShares = 0;
}

stockType::stockType(newString symbol, double openPrice,
double closePrice, double high,
double low, double prevClose,
int shares)
{
stockSymbol = symbol;
todayOpenPrice = openPrice;
todayClosePrice = closePrice;
todayHigh = high;
todayLow = low;
yesterdayClose = prevClose;
noOfShares = shares;
}

int stockType::operator ==(stockType &other)
{
return (stockSymbol == other.stockSymbol);
}

int stockType::operator !=(stockType &other)
{
return (stockSymbol != other.stockSymbol);
}

int stockType::operator <=(stockType &other)
{
return (stockSymbol <= other.stockSymbol);
}

int stockType::operator >=(stockType &other)
{
return (stockSymbol >= other.stockSymbol);
}

int stockType::operator >(stockType &other)
{
return (stockSymbol > other.stockSymbol);
}

int stockType::operator <(stockType &other)
{
return (stockSymbol < other.stockSymbol);
}

STOCK LIST TYPE:

class stockListType: public listType<stockType>
{
public:
void printBySymbol();
void printByGain();
void printReports();
void sort();
void sortByGain();

stockListType();
stockListType(int size);
private:
int *indexByGain;
};


void stockListType::printBySymbol()
{
sort();
printReports();
}

void stockListType::printByGain()
{
sortByGain();



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;


int i;
for(i = 0; i < length; ++i)
cout << elements[indexByGain[i]] << endl;

  
cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl;
cout << endl;
}

void stockListType::printReports()
{

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;

int i;
for(i = 0; i < length; ++i)
cout << elements[i] << endl;

  
cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl;
cout << endl;
}

void stockListType::sort()
{
listType<stockType>::sort();
}

void stockListType::sortByGain()
{
int i, j;
int temp;
indexByGain = new int[length];

  
for(int cntr = 0; cntr < length; cntr++)
{
indexByGain[cntr] = cntr;
}


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;
}
}
}
}

stockListType::stockListType()
{
indexByGain = new int[50];
}

stockListType::stockListType(int size)
{
indexByGain = new int[size];
}

Add a comment
Know the answer?
Add Answer to:
I need one file please (C++)(Stock Market) Write a program to help a local stock trading...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need a program that solves this 34. Write a program to process stock data. The...

    I need a program that solves this 34. Write a program to process stock data. The stock data should be read from a text file containing the following data: stock code, stock name, amount invested r.xx), shares held, and current price. Use the Internet or your local paper to gather data on at least 20 stocks. (You may use mutual funds in place of stocks.) As each stock is read, insert it into a doubly linked multilinked list The first...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Please!!! need help asap!!!! write a C++program to analyze a small subset of the data that...

    Please!!! need help asap!!!! write a C++program to analyze a small subset of the data that has been collected. See file universities.txt .Use precisely seven parallel arrays: one for name of university, one for state, one for city, one for yearly tuition, one for enrollment, one for average freshman retention, and one for the percent of students who graduate with in six years. Note that the percentage of student accepted is not stored.An output file is opened in main() and...

  • Write a program in C++ that simulates a soft drink machine. The program will need several...

    Write a program in C++ that simulates a soft drink machine. The program will need several classes: DrinkItem, DrinkMachine and Receipt. For each of the classes you must create the constructors and member functions required below. You can, optionally, add additional private member functions that can be used for doing implementation work within the class. DrinkItem class The DrinkItem class will contains the following private data members: name: Drink name (type of drink – read in from a file). Type...

  • Write a Java program in Eclipse that reads from a file, does some clean up, and...

    Write a Java program in Eclipse that reads from a file, does some clean up, and then writes the “cleaned” data to an output file. Create a class called FoodItem. This class should have the following: A field for the item’s description. A field for the item’s price. A field for the item’s expiration date. A constructor to initialize the item’s fields to specified values. Getters (Accessors) for each field. This class should implement the Comparable interface so that food...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • I need help in this please, in the c# language. Objects As Parameters The goal for...

    I need help in this please, in the c# language. Objects As Parameters The goal for this exercise is to define the Television object, which you will be using in several, subsequent exercises. For all the methods that you implement (in this course (not just this exercise, but in this course, as you go forwards)), you should remember that since method is public, anyone, anywhere can call the method. Even people whom you never thought would call this method. Therefore,...

  • Needs Help with Java programming language For this assignment, you need to write a simulation program...

    Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • I need to make a project named CStore. It is a Java starter project that only...

    I need to make a project named CStore. It is a Java starter project that only uses the console. I don't know how to even start it.. Please help me asap First I need to define 3 classes names Goods, Manage, UI. Then simply implement the Goods class, then make a function that inserts Goods object into a Goods list in the Manage class. In the UI class, the user inputs Goods that will be registered and that Goods will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT