Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- //
- //
- //
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <cstring>
- #include <cmath>
- using namespace std;
- const int NOT_FOUND = -1;
- const int MAX_LINE_LENGTH = 120;
- const int MAX_STOCKS = 6;
- const float TRANSACTION_FEE = 5.0;
- class Stock
- {
- private:
- string name; // Name of the stock
- float price; // Price of the stock
- int shares; // Number of shares: neg means sold, pos means bought
- public:
- //-------------------------------------------------------------------
- // This method reads the name, price, and number of shares for this
- // stock from the standard input. It assumes the data are
- // separated by white space and in the correct format. It also
- // assumes name is a contiguous sequence of non-white characters.
- // A positive number for shares means the stock was bought, a
- // negative number means the stock was sold.
- // params: (none)
- //-------------------------------------------------------------------
- Stock()
- {
- name = "";
- price = 0.0;
- shares = 0;
- }
- void Read()
- {
- cin >> name >> price >> shares;
- }
- //-------------------------------------------------------------------
- // This method returns the value of this stock: shares times price.
- // A positive number means the stock was bought, a negative number
- // means the stock was sold.
- // params: (none)
- //-------------------------------------------------------------------
- float Value() const
- {
- return (shares * price);
- }
- //-------------------------------------------------------------------
- // This method prints the data for this stock in the format:
- // name (width of 10 characters)
- // shares (width of 6 characters)
- // price (width of 8 characters with 2 decimal places)
- // value (width of 10 characters with 2 decimal places)
- // followed by a newline.
- // params: (none)
- //-------------------------------------------------------------------
- void Print() const
- {
- cout << fixed << setw ( 10 ) << name;
- cout << fixed << setw ( 6 ) << shares;
- cout << fixed << setw ( 8 ) << setprecision(2) << price;
- cout << fixed << setw ( 10 ) << setprecision(2) << Value();
- cout << endl;
- }
- //-------------------------------------------------------------------
- // This method compares the name of this stock to the name of
- // compareStock. It returns a negative number if the name of this
- // stock is lexicographically (dictionary order) less than the name
- // of compareStock, a positive number if it is greater, and 0 if
- // they are equal.
- // params: (in)
- //-------------------------------------------------------------------
- int CompareByName( const Stock & compareStock ) const
- {
- int ( name.compare((compareStock.name)));
- }
- //-------------------------------------------------------------------
- // This method updates this stock with the data from updStock and
- // returns any resulting profit.
- //
- // If the number of shares for this stock and updStock have the same
- // sign (both positive or both negative), then there is no profit
- // (just buying or selling more) and the price of this stock
- // is updated to be the weighted average of the prices.
- //
- // Otherwise, profit is the minimum of the absolute values of the
- // shares times the difference between the prices of updStock and
- // this stock. If the number of shares of this stock is negative,
- // the sign of the profit is reversed. The price of this stock is
- // updated to be the price of whichever of this stock or updStock
- // has the larger absolute value of shares.
- //
- // In all cases, the number of shares for this stock is updated by
- // adding in the number of shares for updStock.
- //
- // Note that a negative value for the profit indicates a loss.
- // params: (in)
- //-------------------------------------------------------------------
- float Transaction( const Stock & updStock )
- {
- float profit =0; // your profit can be either negative or positive
- if ( shares < 0 && updStock.shares < 0 || shares > 0 && updStock.shares > 0 )
- {
- price = (shares * price + updStock.shares * updStock.price) /
- (shares + updStock.shares );
- }
- else if ( shares < 0 && updStock.shares > 0 || shares > 0 && updStock.shares < 0 )
- {
- if( abs( shares ) > abs(updStock.shares) )
- {
- price = (shares * price - updStock.shares * updStock.price) /
- (shares - updStock.shares );
- }
- else if( abs( shares ) <= abs( updStock.shares) )
- {
- price = ( updStock.shares * updStock.price - shares * price) /
- (shares - updStock.shares );
- }
- }
- }
- }; // Stock
- class Portfolio
- {
- private:
- float profit;
- int numStocks;
- Stock stocks[MAX_STOCKS];
- // Finish the bodies of the private methods.
- // Remove the comments and replace them
- // with good what comments and do NOT
- // reference how they will be used.
- // That is for your benefit.
- // Make sure to remove this comment and
- // all other such commentary.
- // Returns the index in stocks where stock with
- // the same name as findStock.name is found;
- // returns NOT_FOUND otherwise. But you cant
- // access findStock.name! So
- // what Stock method do you have to use?
- int Find( const Stock & findStock ) const
- {
- for ( int i = 0; i < MAX_STOCKS; i++)
- {
- if (stocks[i].CompareByName(findStock))
- return i;
- }
- return NOT_FOUND;
- }
- // Remove item from array at the specified index.
- // Shoves the ones below it up one.
- // You'll use this to delete a stock
- void Remove( int index )
- {
- for (int i = index; i < MAX_STOCKS-1; i++)
- {
- stocks[i] = stocks[i + 1];
- }
- }
- public:
- // Make a default (no parameters) constructor.
- // What should it set the data to?
- Portfolio()
- {
- profit = 0.0;
- numStocks = 0;
- stocks[MAX_STOCKS];
- }
- // Here is a good method to have. It cant print anything,
- // but it can do a bunch of heavy lifting for the T command.
- // It will return false if updStock isnt in the list and
- // the list is full. Otherwise, it will behave as indicated
- // by the T command above and return true. It will make
- // use of the Transaction method in Stock as well as the
- // private Find and Remove methods. It will also update
- // the profit. Be sure to charge the $5 for each Transaction.
- // All this work and it can easily be written in under 20
- // lines!
- bool Transaction(const Stock & updStock)
- {
- if ((Find(updStock) == NOT_FOUND) &&
- (numStocks == MAX_STOCKS))
- return false;
- if ((Find(updStock) == NOT_FOUND) &&
- (numStocks < MAX_STOCKS))
- {
- stocks[numStocks] = updStock;
- numStocks++;
- profit -= TRANSACTION_FEE;
- }
- else
- {
- profit = profit - TRANSACTION_FEE +
- stocks[Find(updStock)].Transaction(updStock);
- }
- if (stocks[Find(updStock)].Value() == 0)
- {
- Remove(Find(updStock));
- return true;
- }
- }
- int Print()
- {
- }
- // Make other good methods (such as Print)
- // subject to the rules above.
- }; // Portfolio
- int main()
- {
- Stock stock;
- Portfolio portfolio;
- char command;
- cin >> command;
- while (cin)
- {
- switch (command)
- {
- case 'T':
- stock.Read();break;
- case 'D':
- stock.Print();break;
- case 'V':
- {
- cin >> command;
- if ( command == 'P' )
- {
- stock.Print();
- break;
- }
- else
- cout <<"Not a valid value option.";
- }
- case 'Q':
- return 0;
- default:
- cout << "Bad command." << endl;break;
- }
- cin >> command;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement