Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
How to write reporting function on this question
This program implements the buying and selling of stocks. It starts by printing a welcome message
Welcome to mystocks.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
mystocks.com
Use doubly linked list of stock_t structures. 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;
date_t structure looks like:
typedef struct date_t {
int month, day, year;
} date_t;
Create the following files in a directory:
date.h: contains the above date_t structure
• stock.h and stock.c: contain the stock structure and any constants and stock function prototypes. Eg: 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 (eg: 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, Microsoft stocks would be in a file called MSFT.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.
o 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.
o 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
o 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 doesn’t exist). The opendir function is analogous to the fopen function used for opening files.
dirPtr =opendir( “.” );
o 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 it’s a *.bin”
// file. If it is, make a string out of the name
// only (without the “.bin”), and print it
}
o 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 stock’s binary file for reading. If the file doesn’t exist, prints a message indicating that the user doesn’t 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 doesn’t 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 );
Expert Solution
arrow_forward
Step 1
To write the reporting function, you can follow these steps:
1.Open the current directory using the opendir
function from dirent.h
.
DIR* dirPtr = opendir(".");
2. Check if the directory was opened successfully, and print an error message and return if not.
if (dirPtr == NULL) {
printf("Error opening directory\n");
return;
}
printf("Error opening directory\n");
return;
}
3.Loop through the directory entries using the
readdir
function, and check if each entry is a .bin
file by checking the extension using the strstr
function from string.h
.struct dirent* dirEntry;
while ((dirEntry = readdir(dirPtr)) != NULL) {
char* filename = dirEntry->d_name;
if (strstr(filename, ".bin") != NULL) {
// This is a .bin file, print its name
printf("%s\n", filename);
}
}
while ((dirEntry = readdir(dirPtr)) != NULL) {
char* filename = dirEntry->d_name;
if (strstr(filename, ".bin") != NULL) {
// This is a .bin file, print its name
printf("%s\n", filename);
}
}
4.Close the directory using the
closedir
function.
closedir(dirPtr);
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- reverse_number_in_list(number_list:list)-> list This function will be given a list of numbers your job is to reverse all the numbers in the list and return a list with the reversed numbers. If a number ends with 0 you need to remove all the trailing zeros before reversing the number. An example of reversing numbers with trailing zeros: 10 -> 1, 590 -> 95. None of the numbers in the number_list will be less than 1. Example: number_list = [13, 45, 690, 57] output = [31, 54, 96, 75]arrow_forwardAssume that the following statement appears in a program: my_string = "cookies>milk>fudge>cake›ice cream" Write a statement that converts this string into the list ["cookies", "milk", "fudge", "cake", "ice cream" and displays them. IN Parrow_forward#include #include #include "Product.h" using namespace std; int main() { vector productList; Product currProduct; int currPrice; string currName; unsigned int i; Product resultProduct; cin>> currPrice; while (currPrice > 0) { } cin>> currPrice; main.cpp cin>> currName; currProduct.SetPriceAndName (currPrice, currName); productList.push_back(currProduct); resultProduct = productList.at (0); for (i = 0; i < productList.size(); ++i) { Type the program's output Product.h 1 CSE Scanned Product.cpp if (productList.at (i).GetPrice () < resultProduct.GetPrice ()) { resultProduct = productList.at(i); } AM cout << "$" << resultProduct.GetPrice() << " " << resultProduct. GetName() << endl; return 0; Input 10 Cheese 6 Foil 7 Socks -1 Outputarrow_forward
- products := [5] string {"bread", "milk", "eggs", "butter", "sugar"} price := [5] float64{1.29, 3.75, 3.33, 2.97, 5.28} use a "counting loop" to print the products with their prices example: bread: $1.29 milk: $3.75 eggs: $3.33 butter: $2.97 sugar: $5.28arrow_forwardIn Python, grades_dict = {'Wally': [87, 96, 70], 'Eva': [100, 87, 90], 'Sam': [94, 77, 90], 'Katie': [100, 81, 82], 'Bob': [83, 65, 85]} plot 5 box plots one for each student within a single graph. Note: Rename the x-axis data to students’ names using: plt.xticks([1, 2, 3, 4 ,5], [’Student Name 1’, ’Student Name 2’, ’Student Name 3’, ’Student Name 4’, ’Student Name 5’]), see Figure in the next page. The values in these two arguments should be retrieved automatically and should work for any number of students not just 5'''arrow_forwardData structures return_growing_num_list(max:int) -> list This function will be given a single number, it should return a list of strings of numbers. Each string in the list will only contain a single number repeated an arbitrary amount of times. The number each string will contain will be equal to the current string's index+1. The number in the string should be repeated the same number of times as the string's index+1. Each number in the string should be separated by a space. This list should stop when its size equals the max number specified. Example: max = 3 output = ['1', '2 2', '3 3 3'] max = 4 output = ['1', '2 2', '3 3 3', '4 4 4 4']arrow_forward
- In Python pleasearrow_forwardDice_Game.cpp #include <iostream>#include "Die.h" using namespace std; // a struct for game variablesstruct GameState { int turn = 1; int score = 0; int score_this_turn = 0; bool turn_over = false; bool game_over = false; Die die;}; // declare functionsvoid display_rules();void play_game(GameState&);void take_turn(GameState&);void roll_die(GameState&);void hold_turn(GameState&); int main() { display_rules(); GameState game; play_game(game);} // define functionsvoid display_rules() { cout << "Dice Game Rules:\n" << "\n" << "* See how many turns it takes you to get to 20.\n" << "* Turn ends when you hold or roll a 1.\n" << "* If you roll a 1, you lose all points for the turn.\n" << "* If you hold, you save all points for the turn.\n\n";} void play_game(GameState& game) { while (!game.game_over) { take_turn(game); } cout << "Game…arrow_forwardQuestion 39 Which data structure is required to check whether an expression contains balanced parenthesis ? O Liked list Array Stack O Queuearrow_forward
- 6. sum_highest_five This function takes a list of numbers, finds the five largest numbers in the list, and returns their sum. If the list of numbers contains fewer than five elements, raise a ValueError with whatever error message you like. Sample calls should look like: >>> sum_highest_five([10, 10, 10, 10, 10, 5, -17, 2, 3.1])50>>> sum_highest_five([5])Traceback (most recent call last): File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode exec(code, self.locals) File "<pyshell#44>", line 1, in <module> File "/homework/final.py", line 102, in sum_highest_five raise ValueError("need at least 5 numbers")ValueError: need at least 5 numbersarrow_forwardAfter a tuple is created, its items can be changed. True False A tuple's items can be access by square brackets, similar to lists. True Falsearrow_forwardEach numeric position in a list is called a(n) ____. Question 6 options: index pointer reference iteratorarrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education