C++ Visual studio Write a program that will: 1. declare a Class called Stocklist
ID: 3739589 • Letter: C
Question
C++ Visual studio
Write a program that will:
1. declare a Class called Stocklist. It will encapsulate the following:
A. Name of the Company (private)
B.StockNode (private)
i.trading day
ii.Price
iii.percent change (from previous day)
iv.pointer to the next node in the list
C. Constructor (public).It will take in the name of the company as an argument.
D.appendNode(public). This function will take in a price as an argument. It will allocate space for the new node, set its price, trading day, and percent change (from the previous day).It will also append the new node to the list. The trading day should begin at 1. The percent change of the first trading day should be 0.0.
E.Output_Greatest_Percent_Change(public).This function will not take in any arguments.It will simply output:
i.The name of the company
ii.The day that had the highest percent change
iii.What the percent change was on that day
iv.The closing price of the stock on that day
G.Get_Size().This will return how many trading days exist for this Linked List.
2. The main function will:
a.Prompt the user to input the name of a company
b.Input that name
c.Create a Stocklist object using the name as an argument
d.Use a loop in order to input stock prices for that company
i.When the user enters in a new price, a new node is appended to the Stocklist object.
ii.The loop will terminate when the user enters in -1 for a price.
e.Call the Output_Greatest_Percent_Change function for the Stocklist object.Only call this function if there is at least 1 trading day
Explanation / Answer
Hi, Implemented in C++ and added all the functions except one function. Try to do it.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
using namespace std;
struct StockNode {
string trading_day;
double price;
double percentage_change;
StockNode *next;
};
class Stocklist
{
StockNode *head, *tail;
string company_name;
int length;
public:
Stocklist(string company_name)
{
this->head=NULL;
this->length = 0;
this->company_name = company_name;
}
void appendNode(int price)
{
StockNode *temp=new StockNode;
time_t now = time(0);
temp->trading_day = ctime(&now);
temp->price=price;
temp->next=NULL;
if(this->head==NULL)
{
this->head=temp;
tail = this->head;
this->head->percentage_change = 100;
}
else
{ temp->percentage_change = (temp->price * tail->percentage_change) / tail->price;
tail->next=temp;
tail=temp;
}
this->length += 1;
}
void Output_Greatest_Percent_Change() {
cout<<" Try to Implement it"<<endl;
}
int Get_Size() {
return this->length;
}
void printList() {
StockNode *temp = this->head;
while (temp != NULL) {
cout<<this->company_name<<" "<<temp->trading_day<<" "<<temp->price<<" "<<temp->percentage_change<<endl;
temp = temp->next;
}
}
};
int main() {
// adding stocklist of google and yahoo
Stocklist sl1("google");
Stocklist sl2("yahoo");
//add nodes for google
sl1.appendNode(100);
sl1.appendNode(120);
sl1.appendNode(1000);
sl1.appendNode(500);
//add nodes for yahoo
sl2.appendNode(1000);
sl2.appendNode(1200);
sl2.appendNode(10000);
sl2.appendNode(5000);
//print google stock list
sl1.printList();
//print yahoo stock list
sl2.printList();
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.