#include
#include
#include
#include
#include
#include // NOLINT (build/c++11)
#include
class Clock {
private:
std::chrono::high_resolution_clock::time_point start;
public:
void Reset() {
start = std::chrono::high_resolution_clock::now();
}
double CurrentTime() {
auto end = std::chrono::high_resolution_clock::now();
double elapsed_us = std::chrono::duration std::micro>(end -
start).count();
return elapsed_us;
}
};
class books{
private:
std::string type;
int ISBN;
public:
void setIsbn(int x) {
ISBN = x;
}
void setType(std::string y) {
type = y;
}
int putIsbn() {
return ISBN;
}
std::string putType() {
return type;
}
};
std::ostream &operator <<(std::ostream &out,
std::vector &myvector) {
for (int i = 0; i < myvector.size(); i++) {
out << myvector[i].putIsbn() << std::endl;
out << myvector[i].putType() << std::endl;
}
return out;
}
int setVectorFile(std::string file, std::vector *myvector) {
std::ifstream myfile;
myfile.open(file);
if (!myfile) {
std::cout << "Error: cannot open file " << file
<< std::endl;
return -1;
}
std::string character;
while (!myfile.eof()) {
books* pointer;
getline(myfile, character, ',');
pointer = new books;
int x;
std::stringstream convertToInteger(character);
convertToInteger >> x;
pointer -> setIsbn(x);
getline(myfile, character);
pointer ->setType(character);
myvector->push_back(*pointer);
}
return true;
}
int algorithmSort(books x, books y) {
return x.putIsbn() < y.putIsbn();
}
bool binarySearch(std::vector myvector, int search, std::string
type) {
int mid;
int low = 0;
int high = myvector.size() - 1;
while (low <= high) {
mid = (high + low) / 2;
int midValue = myvector[mid].putIsbn();
std::string midType = myvector[mid].putType();
if (search == midValue && type == midType) {
return midValue;
} else if (search > midValue) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return false;
}
bool linearSearch(std::vectormyvector, int search, std:: string
type) {
for (int i = 0; i < myvector.size(); i++) {
if (search == myvector[i].putIsbn() && type ==
myvector[i].putType()) {
return true;
}
}
return false;
}
int main(int argc, char **argv) {
bool found = true;
std::string myfile1, myfile2;
std::vector NewBooks;
std::vector RequestdBooks;
Clock ct;
if (argc != 4 || argv[1] == NULL || argv[2] == NULL || argv[3]
== NULL) {
std::cout << "Usage: ./SearchNewBooks ";
std::cout << " " << std::endl;
return -1;
}
myfile1 = argv[1];
myfile2 = argv[2];
char character;
if (setVectorFile(myfile1, &NewBooks) == -1) {
return -1;
}
if (setVectorFile(myfile2, &RequestdBooks) == -1) {
return -1;
}
std::cout << "Choice of search method ([l]inear, [b]inary)?"
<< std::endl;
std::cin >> character;
std::ofstream out;
out.open(argv[3]);
if (!out) {
return -1;
}
int count = 0;
while (std::cin) {
ct.Reset();
switch (character) {
case 'l':
for (int i = 0; i < RequestdBooks.size(); i++) {
if (linearSearch(NewBooks, RequestdBooks[i].putIsbn(),
RequestdBooks[i].putType()) == found) {
count++;
}
}
out << count << std::endl;
std::cout << "CPU time: " << ct.CurrentTime()
<< " microseconds" << std::endl;
exit(0);
break;
case 'b' :
sort(NewBooks.begin(), NewBooks.end(), algorithmSort);
for (int i = 0; i < RequestdBooks.size(); i++) {
if (binarySearch(NewBooks, RequestdBooks[i].putIsbn(),
RequestdBooks[i].putType()) == found) {
count++;
}
}
out << count << std::endl;
std::cout << "CPU time: " << ct.CurrentTime()
<< " microseconds" << std::endl;
exit(0);
break;
default:
std::cout << "Invalid choice" << std::endl;
std::cout << "Choice of search method ([l]inear,
[b]inary)?"
<< std::endl;
std::cin >> character;
}
}
}
Here is my code for my project everything is correct except binary search is not correct. I want to find book type and isbn has to be matched.
here is my input file:
newbooks.dat
2918,digital
2918,used
2918,new
2821,used
request.dat
2918,digital
2918,used
2918,new
Using linear search I found all 3 book in the found.dat but binary search does not give me all 3 books. It gave me 2 only. Please help me to fix my binary search. I do not know what to do. NEED URGENT! Thanks
#include <iostream>
#include <vector>
#include <fstream>
#include <time.h>
#include <chrono>
#include <sstream>
#include <algorithm>
class Clock {
private:
std::chrono::high_resolution_clock::time_point start;
public:
void Reset() {
start = std::chrono::high_resolution_clock::now();
}
double CurrentTime() {
auto end = std::chrono::high_resolution_clock::now();
double elapsed_us = std::chrono::duration std::micro>(end -
start).count();
return elapsed_us;
}
};
class books{
private:
std::string type;
int ISBN;
public:
void setIsbn(int x) {
ISBN = x;
}
void setType(std::string y) {
type = y;
}
int putIsbn() {
return ISBN;
}
std::string putType() {
return type;
}
};
std::ostream &operator <<(std::ostream &out,
std::vector &myvector) {
for (int i = 0; i < myvector.size(); i++) {
out << myvector[i].putIsbn() << std::endl;
out << myvector[i].putType() << std::endl;
}
return out;
}
int setVectorFile(std::string file, std::vector *myvector)
{
std::ifstream myfile;
myfile.open(file);
if (!myfile) {
std::cout << "Error: cannot open file " << file
<< std::endl;
return -1;
}
std::string character;
while (!myfile.eof()) {
books* pointer;
getline(myfile, character, ',');
pointer = new books;
int x;
std::stringstream convertToInteger(character);
convertToInteger >> x;
pointer -> setIsbn(x);
getline(myfile, character);
pointer ->setType(character);
myvector->push_back(*pointer);
}
return true;
}
int algorithmSort(books x, books y) {
return x.putIsbn() < y.putIsbn();
}
bool binarySearch(std::vector myvector, int search, std::string
type) {
int mid;
int low = 0;
int high = myvector.size();
while (low <= high) {
mid = (high + low) / 2;
int midValue = myvector[mid].putIsbn();
std::string midType = myvector[mid].putType();
if (search == midValue && type == midType) {
return midValue;
} else if (search > midValue) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return false;
}
bool linearSearch(std::vectormyvector, int search, std:: string
type) {
for (int i = 0; i < myvector.size(); i++) {
if (search == myvector[i].putIsbn() && type ==
myvector[i].putType()) {
return true;
}
}
return false;
}
int main(int argc, char **argv) {
bool found = true;
std::string myfile1, myfile2;
std::vector NewBooks;
std::vector RequestdBooks;
Clock ct;
if (argc != 4 || argv[1] == NULL || argv[2] == NULL || argv[3]
== NULL) {
std::cout << "Usage: ./SearchNewBooks ";
std::cout << " " << std::endl;
return -1;
}
myfile1 = argv[1];
myfile2 = argv[2];
char character;
if (setVectorFile(myfile1, &NewBooks) == -1) {
return -1;
}
if (setVectorFile(myfile2, &RequestdBooks) == -1) {
return -1;
}
std::cout << "Choice of search method ([l]inear, [b]inary)?"
<< std::endl;
std::cin >> character;
std::ofstream out;
out.open(argv[3]);
if (!out) {
return -1;
}
int count = 0;
while (std::cin) {
ct.Reset();
switch (character) {
case 'l':
for (int i = 0; i < RequestdBooks.size(); i++) {
if (linearSearch(NewBooks, RequestdBooks[i].putIsbn(),
RequestdBooks[i].putType()) == found) {
count++;
}
}
out << count << std::endl;
std::cout << "CPU time: " << ct.CurrentTime() <<
" microseconds" << std::endl;
exit(0);
break;
case 'b' :
sort(NewBooks.begin(), NewBooks.end(), algorithmSort);
for (int i = 0; i < RequestdBooks.size(); i++) {
if (binarySearch(NewBooks, RequestdBooks[i].putIsbn(),
RequestdBooks[i].putType()) == found) {
count++;
}
}
out << count << std::endl;
std::cout << "CPU time: " << ct.CurrentTime() <<
" microseconds" << std::endl;
exit(0);
break;
default:
std::cout << "Invalid choice" << std::endl;
std::cout << "Choice of search method ([l]inear, [b]inary)?"
<< std::endl;
std::cin >> character;
}
}
}
#include #include #include #include #include #include // NOLINT (build/c++11) #include class Clock { private: std::chrono::high_resolution_clock::time_point start;...
#include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock { private: std::chrono::high_resolution_clock::time_point start; public: void Reset() { start = std::chrono::high_resolution_clock::now(); } double CurrentTime() { auto end = std::chrono::high_resolution_clock::now(); double elapsed_us = std::chrono::duration std::micro>(end - start).count(); return elapsed_us; } }; class books{ private: std::string type; int ISBN; public: void setIsbn(int x) { ISBN = x; } void setType(std::string y) { type = y; } int putIsbn() { return ISBN; } std::string putType() { return...
#include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...
fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str; while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title); getline(inFile, books[size].Author); getline(inFile, books[size].publisher); getline(inFile,...
***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...
PLEASE HELP WITH THE FIX ME'S #include #include #include #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() { amount = 0.0; } }; //============================================================================ // Linked-List class definition //============================================================================ /** * Define a class containing data members and methods to *...
graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <algorithm> using namespace std; //User Libraries //Global Constants, no Global Variables are allowed //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc... //Function Prototypes //Execution Begins Here! int main(int argc, char** argv) { int n, i, arr[50], search, first, last, middle,count=0,count_in,tot; clock_t start, end; float duration; cout<<"Enter total number of elements :"; cin>>n; cout<<"Enter numbers"; for (i=0; i<n;i++) cin>>arr[i]; cout<<"Enter a...
employee.h ---------- #include <stdio.h> #include <iostream> #include <fstream> class Employee { private: int employeeNum; std::string name; std::string address; std::string phoneNum; double hrWage, hrWorked; public: Employee(int en, std::string n, std::string a, std::string pn, double hw, double hwo); std::string getName(); void setName(std::string n); int getENum(); std::string getAdd(); void setAdd(std::string a); std::string getPhone(); void setPhone(std::string p); double getWage(); void setWage(double w); double getHours(); void setHours(double h); double calcPay(double a, double b); static Employee read(std::ifstream& in); void write(std::ofstream& out); }; employee.cpp ---------- //employee.cpp #include...
In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits> #include <iostream> #include <string> // atoi #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ const unsigned int DEFAULT_SIZE = 179; // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() {...
#include <iostream> #include <cstddef> using std::cout; using std::endl; class Node { int value; public: Node* left; // left child Node* right; // right child Node* p; // parent Node(int data) { value = data; left = NULL; right = NULL; p = NULL; } ~Node() { } int d() { return value; } void print() { std::cout << value << std::endl; } }; int main(int argc, const char * argv[]) { } function insert(Node *insert_node, Node *tree_root){ //Your code here...
Use C++ #include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstring> using namespace std; /I Copy n characters from the source to the destination. 3 void mystrncpy( ???) 25 26 27 28 29 11- 30 Find the first occurrance of char acter c within a string. 32 ??? mystrchr???) 34 35 36 37 38 39 / Find the last occurrance of character c within a string. 40 II 41 ??? mystrrchr ???) 42 43 45 int main() char userInput[ 81]; char...