Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This application implements the buying and selling of stocks. It begins by printing a welcome message Welcome to YourTrade.com Then a main menu of choices

This application implements the buying and selling of stocks. It begins by printing a welcome message

Welcome to YourTrade.com

Then a main menu of choices for the user is output.

Reporting, buying or selling?

(0=quit, 1=report, 2=buy, 3=sell):

The program ends with a goodbye message.

Thank you for trading with YourTrade.com

Use a doubly linked list of stock_t structures. A stock_t structure looks like:

#define MAX_TICKER_LENGTH 6

typedef struct stock_t {

char ticker[MAX_TICKER_LENGTH];

date_t date; // date bought

int numShares;

double pricePerShare;

} stock_t;

A date_t structure looks like:

typedef struct date_t {

int month, day, year;

} date_t;

Create the following files in a directory named c1020axxLab2 where xx is your user ID number.

date.h: contains the above date_t structure

stock.h and stock.c: contain the stock structure and any constants and stock function prototypes. Example: a print stock function.

node.h, node.c: contain at least the node typdef and initNode function

list.h and list.c: contain the doubly linked list structure and any constants and list function prototypes. Start with the pair programming list files and add function(s) for selling (e.g., searching the list). All functions that operate on the list as a whole should go here.

main.c contains the main function and, possibly, other functions such as functions to report, buy and sell.

Makefile: a makefile to make each source file separately or the entire program using the gcc compiler (not g++) and to clean the directory (remove all object files and the executable)

Reporting: Prints all stocks owned to standard output. After printing stocks owned, ask the user which stock to report on and print all of the details of that stock. Stocks owned are stored in binary files with the stock ticker symbol as the name in all capital letters. For example, Apple stocks would be in a file called AAPL.bin and Google stocks in a file called GOOG.bin. In order to report, the program should use the following types and functions from dirent.h (directory entry header file). A single directory entry has information in it such as file name.

DIR* dirPtr: a pointer to a DIR, a directory. Used by the opendir function to open a directory for reading. This is analogous to a FILE* (file pointer) used for reading/writing from/to files that are not directories.

struct dirent* dirEntry: a pointer to a directory entry structure. The readdir function returns one of these, a directory entry, which can be used to get the name of the file that the entry pertains to, dirEntry->d_name

DIR* opendir( char* ): takes the path of a directory such as . for the current directory and opens it. It returns a DIR* or NULL if the open was unsuccessful (i.e., the directory doesnt exist). The opendir function is analogous to the fopen function used for opening files.

dirPtr =opendir( . );

struct dirent* readdir( DIR* ): takes a DIR* (a directory) and returns a structure representing the next entry in the directory. The function returns NULL if there are no more entries in the directory.

while( (dirEntry = readdir( dirPtr )) != NULL ) {

// use dirEntry->d_name and see if its a *.bin

// file. If it is, make a string out of the name

// only (without the .bin), and print it

}

int closedir( DIR* ): closes a directory pointer just like you would close a file after reading from it.

closedir( dirPtr );

Buying: Prompts the user for the ticker symbol, the number of shares to buy and the price per share. It opens a binary file for that ticker symbol in append mode:

FILE* output;

output = fopen( filename, "a" );

gets the date from the system, fills up a stock_t structure with this information and writes the structure to the binary file. Closes the binary file.

Selling: Prompts the user for ticker symbol of stock. Opens that stocks binary file for reading. If the file doesnt exist, prints a message indicating that the user doesnt own any of that stock. Otherwise, the program reads all of the stocks from the file and puts them in a queue then closes the file. The program prints out how many shares of that stock the user has and asks the user for the number of stocks to sell and the current stock price. If the user doesnt own that many shares total, output an error message and reprint the main menu above (report, buy, sell or quit). Otherwise, go through the queue of stocks removing the number of shares needed and calculating and printing the total price to buy the stocks, the total selling price, and the gains (or losses). Open the file again in write mode and writes the entire file from the updated contents of the list. Close the file. If the user sells all shares of a stock in a file, then the program has to delete the file with the remove function in stdio.h which takes a filename (char *) as a parameter.

remove( filename );

Assumptions

User input will be of the correct format. For example, if you ask for an integer you will get a valid integer although it may not be in a valid range.

Max price per share < 1000

Max ticker symbol length = 5 chars (so a C-string needs 6 characters, an extra for the null character)

Stocks are listed in the file in the order by date that they were purchased

See the binary read and write programs for examples of reading and writing stocks from/to binary files. Use sample binary files: AAPL.bin, INTC.bin and GOOG.bin to begin testing your program in a zip file that is one of the assignment files.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2019 Wurzburg Germany September 16 20 2019 Proceedings Part 2 Lnai 11907

Authors: Ulf Brefeld ,Elisa Fromont ,Andreas Hotho ,Arno Knobbe ,Marloes Maathuis ,Celine Robardet

1st Edition

3030461467, 978-3030461461

More Books

Students also viewed these Databases questions

Question

Advance warning and an explanation for the layoff.

Answered: 1 week ago