Question

Create a program SearchNewBooks.cc that given as command line input newbooks.dat and request.dat and a method...

Create a program SearchNewBooks.cc that given as command line input newbooks.dat and request.dat and a method of search either binary or linear, creates a file of the number of books found from the request list.

program should receive two filenames as command line arguments, the new books file and request file, which the program should read in. The list of new books and list of requested books should should be stored in only two ​std::vector​ containers.
Because some code to open these two files will probably be equivalent, you are encourage to write generic functions in order to avoid code duplication.

You should write a class for representing book as a combination of both the ISNB number and type, so that each vector is an object of that class. For debugging purposes, you can overload the << operator to print positional vectors in a pretty way.

$ cat newbooks.dat

58,new

92,used

82,digital

114,used

26,new

$ cat request.dat

8,new
1,new
95,new

you will need to implement two search strategies in order to find the given requested books in the list of new arrivals: a ​linear search and a binary search​.
The user will choose between using one of these two strategies every time the program is run. If the user input is invalid, they should be asked again, as shown in the example below. The error message should be printed to std::cerr.

For the binary search, the vector of new books should be sorted by their ISNB number beforehand. For the sorting, you should use ​std::sort​. If you overload the < operator of your positional vector class, this function can easily be applied for sorting all of objects contained in a vector container:

please help me to write code as a class. please write in C++ language.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Jan_14_19_Book.h

#pragma once
#include<iostream>
#include<string>
using namespace std;

class Book
{
   int ISBN;
   string type;
public:
   Book();
   void setISBN(int is);
   void setType(string type);
   int getISBN();
   string getType();
};

=====================================

//Jan_14_19_Book.cc

#include"Jan_14_19_Book.h"
Book::Book()
{
   ISBN = 0;
   type = "";
}
void Book::setISBN(int i)
{
   ISBN = i;
}
void Book::setType(string t)
{
   type=t;
}
int Book::getISBN()
{
   return ISBN;
}
string Book::getType()
{
   return type;
}

=========================================

//SearchNewBooks.cc

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<algorithm>
#include"Jan_14_19_Book.h"

using namespace std;

ostream &operator<<(ostream &out, vector<Book> vec);
bool loadToVector(string filename, vector<Book> &vec);
bool binarySearch(vector<Book> vec, int search);
bool linearSearch(vector<Book> vec, int search);
bool compare(Book i, Book j);

int main(int argc, char **argv)
{
   //declare two vectors for new books and requested books
   vector<Book> newBooks, requestedBooks;
   string filename1, filename2;
   if (argc < 3)
   {
       cout << "Usage: ./a.out newbooks.dat request.dat" << endl;
       return -1;
   }
   filename1 = argv[1];
   filename2 = argv[2];
   int choice;
   if (loadToVector(filename1, newBooks) == false)
       return -1;
   if (loadToVector(filename2, requestedBooks) == false)
       return -1;
   cout << "New book vector: " << endl;
   cout << newBooks << endl;
   cout << "requested book vector: " << endl;
   cout << requestedBooks << endl;
   cout << "Enter 1. binary search 2. linear search " << endl;
   cin >> choice;
   switch (choice)
   {
       case 1:
           //sort the vector before calling binarySearch
           sort(newBooks.begin(), newBooks.end(), compare);
//cout <<"Sorted newBooks"<<endl;
//cout << newBooks << endl;
           for (int i = 0; i < requestedBooks.size(); i++)
           {
               if (binarySearch(newBooks, requestedBooks[i].getISBN()) == true)
               {
                   cout << "Book with ISBN " << requestedBooks[i].getISBN() << " is found " << endl;
               }
               else
               {
                   cout << "Book with ISBN " << requestedBooks[i].getISBN() << " is not found " << endl;
               }
           }
           break;
       case 2:
           for (int i = 0; i < requestedBooks.size(); i++)
           {
               if (linearSearch(newBooks, requestedBooks[i].getISBN()) == true)
               {
                   cout << "Book with ISBN " << requestedBooks[i].getISBN() << " is found " << endl;
               }
               else
               {
                   cout << "Book with ISBN " << requestedBooks[i].getISBN() << " is not found " << endl;
               }
           }
           break;
       default:
           cout << "Invalid choice " << endl;
   }
  
}

ostream &operator<<(ostream &out, vector<Book> vec)
{
   for (int i = 0; i < vec.size(); i++)
   {
       out << "ISBN: " << vec[i].getISBN() << endl;
       out << "Type: " << vec[i].getType() << endl;
   }
   return out;
}

bool loadToVector(string filename, vector<Book> &vec)
{
   ifstream in;
  
   //open filename for reading from file
   in.open(filename);
   //check if given filename is opened
   if (!in)
   {
       cout << "Not able to open the " << filename << endl;
       return false;
   }
  
   string str;
  
   while (!in.eof())
   {
       Book *b;
       getline(in, str, ',');
       b = new Book;
       b->setISBN(stoi(str));
       getline(in, str);
       b->setType(str);
       vec.push_back(*b);
   }
}
bool compare(Book i, Book j)
{
   return i.getISBN() < j.getISBN();
}

bool binarySearch(vector<Book> vec,int search)
{
   int high, low, mid;
   high = vec.size() - 1;
   low = 0;
  
  
   while (low <= high)
   {
       mid = (high + low) / 2;
       if (search == vec[mid].getISBN())
           return true;
       else if (search < vec[mid].getISBN())
       {
           high = mid - 1;
       }
       else
           low = mid + 1;
   }
   return false; //search not found
}
bool linearSearch(vector<Book> vec,int search)
{
   for (int i = 0; i < vec.size(); i++)
   {
       if (vec[i].getISBN() == search)
       {
           return true;
       }
   }
   return false;
}

================================

newbooks.dat file

58,new
92,used
82,digital
114,used
26,new

request.dat

8,new
1,new
95,new
58,new

//output

New book vector:   
ISBN: 58   
Type: new
ISBN: 92   
Type: used   
ISBN: 82   
Type: digital
ISBN: 114
Type: used   
ISBN: 26   
Type: new

requested book vector:   
ISBN: 8
Type: new
ISBN: 1
Type: new
ISBN: 95   
Type: new
ISBN: 58   
Type: new

Enter 1. binary search 2. linear search
1
Book with ISBN 8 is not found
Book with ISBN 1 is not found
Book with ISBN 95 is not found   
Book with ISBN 58 is found

//output2

New book vector:                                                                                                                   

ISBN: 58                                                                                                                           

Type: new                                                                                                                          

ISBN: 92                                                                                                                           

Type: used                                                                                                                         

ISBN: 82                                                                                                                           

Type: digital                                                                                                                      

ISBN: 114                                                                                                                          

Type: used                                                                                                                         

ISBN: 26                                                                                                                           

Type: new                                                                                                                          

                                                                                                                                   

requested book vector:                                                                                                             

ISBN: 8                                                                                                                            

Type: new                                                                                                                          

ISBN: 1                                                                                                                            

Type: new                                                                                                                          

ISBN: 95                                                                                                                           

Type: new                                                                                                                          

ISBN: 58                                                                                                                           

Type: new                                                                                                                          

                                                                                                                                   

Enter 1. binary search 2. linear search                                                                                            

2                                                                                                                                  

Book with ISBN 8 is not found                                                                                                      

Book with ISBN 1 is not found                                                                                                      

Book with ISBN 95 is not found                                                                                                     

Book with ISBN 58 is found    

Add a comment
Know the answer?
Add Answer to:
Create a program SearchNewBooks.cc that given as command line input newbooks.dat and request.dat and a method...
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
  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Using network sockets, write a C program called client that receives three command-line arguments in the...

    Using network sockets, write a C program called client that receives three command-line arguments in the form: client host port file and sends a request to a web server. The command-line arguments are hostRepresents the web server to connect to port Represents the port number where a request is sent. Normally an HTTP request is sent over port 80, but this format allows for custom ports file Represents the file requested from the web server Your program should create a...

  • Java program Program: Grade Stats In this program you will create a utility to calculate and...

    Java program Program: Grade Stats In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students Things you will learn Robustly parsing simple text files Defining your own objects and using them in a program Handling multiple...

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • get names of input and output files from command line (NOT from user input)

     2.12 LAB 2.3: File I/O - CSV update This program shouldget names of input and output files from command line (NOT from user input)read in integers from a csv (comma-separated values) file into a vectorcompute the integer average of all of the valuesconvert each value in the vector to the difference between the original value and the averagewrite the new values into a csv file

  • FOR JAVA Write a program that takes two command line arguments: an input file and an...

    FOR JAVA Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message. Notes: This program can be written in a single main method Remember that...

  • All commands must be in the command line. The expense data file is separate. Programming Project...

    All commands must be in the command line. The expense data file is separate. Programming Project #2: Manage Spending Using Commands Objectives: • Understand how to create and manipulate list of objects using array or vector class • Handle input errors and invalid values • Design and create a well-structure program using C++ basic programming constructs and classes. Description: Each “expense” contains 2 values: the spending amount (double) and its description (string) Here is an example of the “expense” data...

  • C Language program problem. Suppose that I want to create a program by using command line....

    C Language program problem. Suppose that I want to create a program by using command line. User will type a name of file such as "test.data" to access the content of the file. Program should scan whole file and create a list with 30 position . You should show the two situation, one is for when scanned space, the other one is when it scanned new line. Then the other task is printing them out. You see there are spaces...

  • C ++ Implement cat command The purpose of this assignment is to provide practice using the...

    C ++ Implement cat command The purpose of this assignment is to provide practice using the system calls we discussed for working with files on a UNIX system. You will be writing a basic implementation of the cat command using C++. Description As you should recall, the cat command takes a list of files as command line arguments. It then opens each file in turn, writing each file’s entire contents to standard output in the order they were supplied. You...

  • Java!!! Write a program that takes a file name as its command line argument. It assumes...

    Java!!! Write a program that takes a file name as its command line argument. It assumes the file is a binary data file and checks the first 4 bytes of the file to see whether they contain the integer −889275714. It outputs “yes” or “no” or an error message if there was an IOException generated. (Trivia question: Why test for that particular value? Hint: Try it on several .class files.)

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
ADVERTISEMENT