This C++ project is to produce a formatted readable report to an output file rep
ID: 3857966 • Letter: T
Question
This C++ project is to produce a formatted readable report to an output file report.txt that lists all of the books and groups them by market. Only title, ISBN, author, quantity, price, and inventory value need be listed for each book. For each market, include the name of the market, the total number of inventory records (the same title may appear more than once) listed for that market, the total quantity of books for that market, the total market value of all of the books for that market, the average book price for that market, and the market’s name, company name, and address information from the markets.txt file. At the very end of the report, provide the total number of inventory records for all markets, the total quantity of books for all markets, the total market value of all of the books for all markets, and the average book price for all markets.
Design Requirements
1. Reading the books.txt must be done by a function readBooks. The books.txt data must be stored in an array of book structs. The book struct is to contain the data elements per the header line field names from the books.txt file
2. Reading the markets.txt must be done by a function readMarkets. The markets.txt data must be stored in an array of market structs. The market struct is to contain the data elements per the header line field names from the markets.txt file
3. If the market of a book is not found in the markets file, display the value “UNKNOWN” in the company-name and null values in all of the other market report fields.
The first attached text file (books.txt) contains a header line and 100 lines of data. Each of the 100 lines is an inventory record and contains data associated with a single book title listed on an online internet market. The file is tab-delimited. This is a very common format used to exchange text data between systems:
Small sample of books.txt and exact format
item-name listing-id seller-sku price quantity open-date item-note item-condition product-id market
"The Voices of Marrakesh [LARGE PRINT] by Canetti, Eilas" 0327R058051 14-501099 299.38 2 2004-03-27 10:33:21 PST 11 1852900040 amazon
Towards Prolongation of the Healthy Life Span: Practical Approaches to... 0327R604577 14-311708 216.65 1 2004-03-27 10:30:51 PST 11 157331109X alibris
Career Information Center by 0327J610011 17-503207 205.6 2 2004-03-27 11:14:31 PST 11 28974522 half
"Heads of State and Government [Hardcover] by Da Graca, John" 0624R310573 74-500122 164.13 1 2004-06-24 06:40:18 PST Bamm! This Is A Great Book! 11 1561592692 ebay
"One Nation, Indivisible: The Civil Rights Challenge for the 1990s [Paperback..." 0327H506733 11-308585 139.49 1 2004-03-27 10:26:56 PST 11 962286508 abebooks
"College Basketball's National Championships [Hardcover] by Brenner, Morgan G." 0419T782185 BB-300183 138.52 1 2004-04-19 08:18:12 PST Same Day Shipping! 100% Customer Satisfaction! Some shelf wear 1 081083474X amazon
The second attached text file (markets.txt) also contains a header line and a line for each of the markets that appears for the books in the books.txt file. It is also tab-delimited and of the same basic format as the books.txt file.
market name street-address city state country
amazon Amazon Corporation 345 Bezo Ave Sioux City IA USA
alibris Alibris LLC 5332 Nottingham St London Coventry Great Britian
half Half Corporation 87 North Cresent Blvd Los Angeles CA USA
ebay ebay Corporation 33 Whitman Dr Meg IL USA
abebooks AbeBooks 345 Homebase Ct Marvin TX USA
If you need more book file let me know. I have the entire file. There are like 100 lines of books.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node
{
char data;
struct node* left;
struct node* right;
};
/* Prototypes for utility functions */
int search(char arr[], int strt, int end, char value);
struct node* newNode(char data);
/* Recursive function to construct binary of size len from
Inorder traversal in[] and Preorder traversal pre[]. Initial values
of inStrt and inEnd should be 0 and len -1. The function doesn't
do any error checking for cases where inorder and preorder
do not form a tree */
struct node* buildTree(char in[], char pre[], int inStrt, int inEnd)
{
static int preIndex = 0;
if(inStrt > inEnd)
return NULL;
/* Pick current node from Preorder traversal using preIndex
and increment preIndex */
struct node *tNode = newNode(pre[preIndex++]);
/* If this node has no children then return */
if(inStrt == inEnd)
return tNode;
/* Else find the index of this node in Inorder traversal */
int inIndex = search(in, inStrt, inEnd, tNode->data);
/* Using index in Inorder traversal, construct left and
right subtress */
tNode->left = buildTree(in, pre, inStrt, inIndex-1);
tNode->right = buildTree(in, pre, inIndex+1, inEnd);
return tNode;
}
/* UTILITY FUNCTIONS */
/* Function to find index of value in arr[start...end]
The function assumes that value is present in in[] */
int search(char arr[], int strt, int end, char value)
{
int i;
for(i = strt; i <= end; i++)
{
if(arr[i] == value)
return i;
}
}
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct node* newNode(char data)
{
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
/* This funtcion is here just to test buildTree() */
void printInorder(struct node* node)
{
if (node == NULL)
return;
/* first recur on left child */
printInorder(node->left);
/* then print the data of node */
printf("%c ", node->data);
/* now recur on right child */
printInorder(node->right);
}
/* Driver program to test above functions */
int main()
{
char in[] = {'D', 'B', 'E', 'A', 'F', 'C'};
char pre[] = {'A', 'B', 'D', 'E', 'C', 'F'};
int len = sizeof(in)/sizeof(in[0]);
struct node *root = buildTree(in, pre, 0, len - 1);
/* Let us test the built tree by printing Insorder traversal */
printf("Inorder traversal of the constructed tree is ");
printInorder(root);
getchar();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.