Question
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 logical list should be ordered on the stock code. The second logical list should be ordered on the gain or loss for the stock. Gain or loss is calculated as the current value of the stock (shares held times current price) minus the amount invested. Include at least one loss in your test data. After building the lists, display a menu that allows the user to display each logical list forward or backward (a total of four options). Each display should contain an appropriate heading and column captions. Run your program and submit a list of your input data and a printout of each display option
0 0
Add a comment Improve this question Transcribed image text
Answer #1

This problem is simple.

Algorithm i used:

1.read data from file

2.create node

3.Add to double linklist

4. repeat untill all data has been read

5. display 4 option

6. sort double link list based on option selected

7. display list

8. exit

i use quicksort for sort the list based on profit or stock_code

final code in c++:

/*
stock.cpp
Created: 11-nov-2018
Author: Gurdeep
*/
#include<iostream>
#include<fstream>
#include<iomanip>
#include<stdlib.h>
#include<string>
using namespace std;
struct Node{
   int stock_code;
   string stock_name;
   int amount_invested;
   int shares_held;
   int current_price;
   int profit;
   struct Node *prev;
   struct Node *next;
};

struct Node* head=NULL;
void swap ( int* a, int* b )
{   int t = *a;      *a = *b;       *b = t;   }
struct Node *lastNode(Node *root)
{
    while (root && root->next)
        root = root->next;
    return root;
}
void push(struct Node** head_ref, int code,string name,int invest,int shares,int cur_price)
{
    struct Node* new_node = new Node;     /* allocate node */
    new_node->stock_code = code;
    new_node->stock_name = name;
    new_node->amount_invested=invest;
    new_node->shares_held=shares;
    new_node->current_price=cur_price;
    new_node->profit=shares*cur_price-invest;

    /* since we are adding at the begining, prev is always NULL */
    new_node->prev = NULL;

    /* link the old list off the new node */
    new_node->next = (*head_ref);

    /* change prev of head node to new node */
    if ((*head_ref) != NULL) (*head_ref)->prev = new_node ;

    /* move the head to point to the new node */
    (*head_ref)    = new_node;
}
void printList(struct Node *head)
{
    while (head)
    {
        cout <<"(stock_code= "<<head->stock_code <<", stock_name= "<<head->stock_name<<", profit= "<<head->profit<<")"<< "--> ";
        head = head->next;
    }
    cout << endl;
}

Node* partition(Node *l, Node *h,int c)
{
    if(c==1){
    // set pivot as h element
    int x = h->stock_code;

    // similar to i = l-1 for array implementation
    Node *i = l->prev;

    // Similar to "for (int j = l; j <= h- 1; j++)"
    for (Node *j = l; j != h; j = j->next)
    {
        if (j->stock_code <= x)
        {
            // Similar to i++ for array
            i = (i == NULL)? l : i->next;

            swap(&(i->stock_code), &(j->stock_code));
        }
    }
    i = (i == NULL)? l : i->next; // Similar to i++
    swap(&(i->stock_code), &(h->stock_code));
    return i;}
else{
// set pivot as h element
    int x = h->profit;

    // similar to i = l-1 for array implementation
    Node *i = l->prev;

    // Similar to "for (int j = l; j <= h- 1; j++)"
    for (Node *j = l; j != h; j = j->next)
    {
        if (j->profit <= x)
        {
            // Similar to i++ for array
            i = (i == NULL)? l : i->next;

            swap(&(i->profit), &(j->profit));
        }
    }
    i = (i == NULL)? l : i->next; // Similar to i++
    swap(&(i->profit), &(h->profit));
    return i;
}
}
void _quickSort(struct Node* l, struct Node *h,int c)
{
    if (h != NULL && l != h && l != h->next)
    {
        struct Node *p = partition(l, h,c);
        _quickSort(l, p->prev,c);
        _quickSort(p->next, h,c);
    }
}
void quickSort(struct Node *head,int c)
{
    // Find last node
    struct Node *h = lastNode(head);

    // Call the recursive QuickSort
    _quickSort(head, h,c);
}
void reverse(struct Node **head_ref)
{
     struct Node *temp = NULL;
     struct Node *current = *head_ref;
     
     /* swap next and prev for all nodes of
       doubly linked list */
     while (current != NULL)
     {
       temp = current->prev;
       current->prev = current->next;
       current->next = temp;             
       current = current->prev;
     }     
     
     /* Before changing head, check for the cases like empty
        list and list with only one node */
     if(temp != NULL )
        *head_ref = temp->prev;
}    
int main(){
    int code,invest,shares,cur_price;
    string name;
    ifstream infile;
    int choice;
    struct Node *a=NULL;
    struct Node *b=NULL;
    infile.open("data.txt");
    if(!infile){
    cout<<"file not open\n";
    exit(1);
}
    while(infile>>code){
      infile>>name;
      infile>>invest;
      infile>>shares;
      infile>>cur_price;
      push(&a,code,name,invest,shares,cur_price);
      push(&b,code,name,invest,shares,cur_price);
}
    infile.close();
    cout<<"enter 1 to print list orderby stock_code in forward\n";
    cout<<"enter 2 to print list orderby stock_code in backward\n";
    cout<<"enter 3 to print list orderby profit in forward\n";
    cout<<"enter 4 to print list orderby profit in backward\n";
    cin>>choice;
    switch(choice){
      case 1:
       quickSort(a,1);
       printList(a);
       break;
      case 2:
       quickSort(a,1);
       reverse(&a);
       printList(a);
       break;
      case 3:
       quickSort(b,2);
       printList(b);
       break;
      case 4:
       quickSort(b,2);
       reverse(&b);
       printList(b);
       break;
      default:
       cout<<"invalid option \n";
       break;
       }
     return 0;
}


data.txt (stock_code,stock_name,amount_invested,shares_held,current_price) :

1
facebook
1000
500
3
2
cbm
2000
300
10
3
google
6000
1000
3
4
microsoft
1000
500
1

run using:

$g++ stock.cpp

$./a.out

output:

enter 1 to print list orderby stock_code in forward
enter 2 to print list orderby stock_code in backward
enter 3 to print list orderby profit in forward
enter 4 to print list orderby profit in backward
3
(stock_code= 4, stock_name= microsoft, profit= -3000)--> (stock_code= 3, stock_name= google, profit= -500)--> (stock_code= 2, stock_name= cbm, profit= 500)--> (stock_code= 1, stock_name= facebook, profit= 1000)

Add a comment
Know the answer?
Add Answer to:
I need a program that solves this 34. Write a program to process stock data. The...
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 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...

  • Requires Python to answer A client wishes to keep track of his investment in shares. Write...

    Requires Python to answer A client wishes to keep track of his investment in shares. Write a program to help him manage his stock portfolio. You are to record both the shares that he is holding as well as shares that he has sold. For shares be is curreatly holding, record the following data: .a 3-character share code, share name, last purchase date, volume currently held and average purchase price (refer to description under part cii) option 2) For shares...

  • CSC151 Stock Portfolio GUI Project Goal You are to write a GUI program that will allow...

    CSC151 Stock Portfolio GUI Project Goal You are to write a GUI program that will allow a user to buy, sell and view stocks in a stock portfolio. This document will describe the minimum expected functions for a grade of 90. Your mission is to “go and do better.” You’ll find a list of enhancement options at the end of this document. Objectives By the end of this project, the student will be able to • write a GUI program...

  • I need help with using the infile and outfile operations in C++... Here's my assignment requirements:...

    I need help with using the infile and outfile operations in C++... Here's my assignment requirements: Assignment: Cindy uses the services of a brokerage firm to buy and sell stocks. The firm charges 1.5% service charges on the total amount for each transaction, buy or sell. When Cindy sells stocks, she would like to know if she gained or lost on a particular investment. Write a program that allows Cindy to read input from a file called Stock.txt: 1. The...

  • Problem: Design and write a C language program that can be used to calculate Voltage, Current...

    Problem: Design and write a C language program that can be used to calculate Voltage, Current and Total Resistance for a Series or Parallel or a Series Parallel circuit. The program should display the main menu that contains the three types of circuit calculations that are available and then the user will be prompted to select a circuit first. After the circuit has been selected the program should then display another menu (i.e., a submenu) requesting the necessary data for...

  • Write a program in Matlab that solves linear systems of equations using Gauss elimination with pa...

    Write a program in Matlab that solves linear systems of equations using Gauss elimination with partial pivoting. Make sure that you use variables that are explicit, and make sure to include comment lines (each subroutine should have at least a sentence stating what it does). Make sure that your program checks for valid inputs in matrix and vectors dimensionality. • Using your code, solve the systems of equations in problems 9.11, 9.12, and 9.13 9.11 9.12 9.13 2x1-6x2-X3 =-38 We...

  • (PYTHON) Stock Transaction Program Last month Joe purchased some stock from StockTrade.   1. Write a function(s)...

    (PYTHON) Stock Transaction Program Last month Joe purchased some stock from StockTrade.   1. Write a function(s) to allow the user to input the followings: ·The name of the stock ·Number of shares Joe bought · Stock purchase price · Stock selling price ·Broker commission 2. Write function(s) to calculate: and: · The amount of money Joe paid for the stock (number of shares bought * purchase price) · The amount of commission Joe paid his broker when he bought the...

  • In C++, Step 1: Implement the Student Class and write a simple main() driver program to...

    In C++, Step 1: Implement the Student Class and write a simple main() driver program to instantiate several objects of this class, populate each object, and display the information for each object. The defintion of the student class should be written in an individual header (i.e. student.h) file and the implementation of the methods of the student class should be written in a corresponding source (i.e. student.cpp) file. Student class - The name of the class should be Student. -...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Please write comments so the user will understand the program. The output for the calculation is provided below thw assignment. For this assignment, you will take on the role of a member of an...

    Please write comments so the user will understand the program. The output for the calculation is provided below thw assignment. For this assignment, you will take on the role of a member of an IT department. Your chief technology officer (CTO) has tasked your department with the replacement of IT equipment. Your manager does not want to buy a piece of equipment based on the brand name alone. Rather, your manager wants to know the return on investment for each...

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
Active Questions
ADVERTISEMENT