Question

In: Computer Science

#include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10...

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);

string rtrim(const string &);



#define BUFFLEN 10

/*

    This code closely mimics the Python hash table we created in class this

    week. Each "person" is defined by their name, zipcode, and their pet's name.

    Persons are hashed by their zipcode.

*/


//----------------------------------------------------------------------------

/* function declarations

------------------------*/

int computeKey(int);

void add_to_buffer(string,int,string);

void find_in_buffer(int);

//----------------------------------------------------------------------------

/* define a "person"

--------------------*/

class person{

    public:

    // variables that define a person

    string name;

    int zipcode;

    string petsname;

    person *next;

    // constructor

    person(string nm, int zc, string pn, person *n=NULL){

        name = nm;

        zipcode = zc;

        petsname = pn;

        next = n;

    }

    // print a person

    void print(){

        cout << name << " lives in " << zipcode << " with pet " << petsname << endl;

    }

};

//----------------------------------------------------------------------------

/* define a Linked List

-------------------------*/

class LL{

    public:

    // just the one variable

    person *head;

    // constructor

    LL(){head=NULL;}

    

    // create a new person and push them onto the List

    void push(string nm, int zc, string pn){

        person *p = new person(nm,zc,pn,head);

        head = p;

    }

    // find a person in the list

    // inputs provided: the person's zipcode

    // return: a pointer to either the person or to NULL (if the person isn't found)

    person *find(int zc){

       // --------------------

        // YOUR CODE GOES HERE

        // --------------------

    }

};


//----------------------------------------------------------------------------

/* Define the buffer, which is an array of Linked Lists

-------------------------------------------------------*/

LL buffer[BUFFLEN];


//----------------------------------------------------------------------------

/* Function definitions

-------------------------------------------------------*/

void add_to_buffer(string nm, int zc, string pn){

    int key = computeKey(zc);

    buffer[key].push(nm,zc,pn);

}

int computeKey(int val){

    return val%BUFFLEN;

}

void find_in_buffer(int zc){

    int key = computeKey(zc);

    person *p = buffer[key].find(zc);

    if (p!=NULL)

        p->print();

    else

        cout << zc << " not found" << endl;

}


void test_buffer(int zip) {

    add_to_buffer("alice",19122,"hoot");

    add_to_buffer("bob",12193,"rover");

    add_to_buffer("charlie",27707,"lady");

    add_to_buffer("dan",90210,"bubbles");

    add_to_buffer("elise",20904,"marbles");

    add_to_buffer("fran",86753,"moose");

    

    find_in_buffer(zip);

}

int main()

{

    string zip_temp;

    getline(cin, zip_temp);

    int zip = stoi(ltrim(rtrim(zip_temp)));

    test_buffer(zip);

    return 0;

}

string ltrim(const string &str) {

    string s(str);

    s.erase(

        s.begin(),

        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))

    );

    return s;

}

string rtrim(const string &str) {

    string s(str);

    s.erase(

        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),

        s.end()

    );

    return s;

}

Solutions

Expert Solution

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);

string rtrim(const string &);

#define BUFFLEN 10

/*

This code closely mimics the Python hash table we created in class this

week. Each "person" is defined by their name, zipcode, and their pet's name.

Persons are hashed by their zipcode.

*/


//----------------------------------------------------------------------------

/* function declarations

------------------------*/

int computeKey(int);

void add_to_buffer(string,int,string);

void find_in_buffer(int);

//----------------------------------------------------------------------------

/* define a "person"

--------------------*/

class person{

public:

// variables that define a person

string name;

int zipcode;

string petsname;

person *next;

// constructor

person(string nm, int zc, string pn, person *n=NULL){

name = nm;

zipcode = zc;

petsname = pn;

next = n;

}

// print a person

void print(){

cout << name << " lives in " << zipcode << " with pet " << petsname << endl;

}

};

//----------------------------------------------------------------------------

/* define a Linked List

-------------------------*/

class LL{

public:

// just the one variable

person *head;

// constructor

LL(){head=NULL;}

  

// create a new person and push them onto the List

void push(string nm, int zc, string pn){

person *p = new person(nm,zc,pn,head);

head = p;

}

// find a person in the list

// inputs provided: the person's zipcode

// return: a pointer to either the person or to NULL (if the person isn't found)

person *find(int zc){

// --------------------

// YOUR CODE GOES HERE
person *p = head; // get address of first pointer
       while(p!=NULL){ // run for all list
           if(p->zipcode == zc){ // check if code matches
               return p;   // return pointer
           }
           p = p->next; // move pointer to next
       }
       return NULL; // if not found return null
       // --------------------

}

};


//----------------------------------------------------------------------------

/* Define the buffer, which is an array of Linked Lists

-------------------------------------------------------*/

LL buffer[BUFFLEN];


//----------------------------------------------------------------------------

/* Function definitions

-------------------------------------------------------*/

void add_to_buffer(string nm, int zc, string pn){

int key = computeKey(zc);

buffer[key].push(nm,zc,pn);

}

int computeKey(int val){

return val%BUFFLEN;

}

void find_in_buffer(int zc){

int key = computeKey(zc);
person *p = buffer[key].find(zc);

if (p!=NULL)

p->print();

else

cout << zc << " not found" << endl;

}

void test_buffer(int zip) {

add_to_buffer("alice",19122,"hoot");

add_to_buffer("bob",12193,"rover");

add_to_buffer("charlie",27707,"lady");

add_to_buffer("dan",90210,"bubbles");

add_to_buffer("elise",20904,"marbles");

add_to_buffer("fran",86753,"moose");


find_in_buffer(zip);

}

int main()

{

string zip_temp;

getline(cin, zip_temp);

int zip = stoi(ltrim(rtrim(zip_temp)));

test_buffer(zip);

return 0;

}

string ltrim(const string &str) {

string s(str);

s.erase(

s.begin(),

find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))

);

return s;

}

string rtrim(const string &str) {

string s(str);

s.erase(

find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),

s.end()

);

return s;

}

/* OUTPUT */


Related Solutions

#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
I want flowchart process for this code c++ _____________________ #include<bits/stdc++.h> using namespace std; int main() {...
I want flowchart process for this code c++ _____________________ #include<bits/stdc++.h> using namespace std; int main() { char repeat = 'Y'; for (;repeat == 'Y';){ char empname[222]; float basicSalary, h_r_a, DearnessAllow, tax, netSalary; int e_id; cout<<"\nEmployee Name :"; cin>>empname; cout<<"\nEmployee Id :"; cin>>e_id; cout << "Enter Basic Salary : "; cin >> basicSalary; DearnessAllow = 0.30 * basicSalary; h_r_a= 800; switch (1) { case 1: if (basicSalary <= 2,50,000) tax = 0; case 2: if (basicSalary > 250000 && basicSalary <=...
code from assignment 1 #include #include using namespace std; const int nameLength = 20; const int...
code from assignment 1 #include #include using namespace std; const int nameLength = 20; const int stateLength = 6; const int cityLength = 20; const int streetLength = 30; int custNomber = -1; // Structure struct Customers { long customerNumber; char name[nameLength]; char state[stateLength]; char city[cityLength]; char streetAddress1[streetLength]; char streetAddress2[streetLength]; char isDeleted = 'N'; char newLine = '\n'; int zipCode; }; int main() { ofstream file; file.open("Customers.dat", fstream::binary | fstream::out); char go; long entries = 0; struct Customers data; do...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state,...
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state, const std::string& zip) : StreetNumber(street), CityName(city), StateName(state), ZipCode(zip) {} std::string GetStreetNumber() const { return StreetNumber; } void SetStreetNumber(const std::string& street) { StreetNumber = street; } std::string GetCity() const { return CityName; } void SetCity(const std::string& city) { CityName = city; } std::string GetState() const { return StateName; } void SetState(const std::string& state) { StateName = state; } std::string GetZipCode() const { return ZipCode; }...
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput);...
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput); // Declaring base int N = 30; if (userInput.length() > 10) { cout << 0 << endl; } else { int finalTotal = 0; //Iterates through userInput for(int i = 0; i < 10; i++){ char convertedInput = userInput[i]; // ASCII decimal value of each character int asciiDec = int(convertedInput); //Casts char value from input to int value stringstream chr; chr << convertedInput; int...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact { public: Contact(string init_name = "", string init_phone = "000-000-0000"); void setName(string name); void setPhone(string phone); string getName()const; string getPhone()const; friend ostream& operator << (ostream& os, const Contact& c); friend bool operator == (const Contact& c1, const Contact& c2); friend bool operator != (const Contact& c1, const Contact& c2); private: string name, phone; }; Contact::Contact(string init_name, string init_phone) { name = init_name; phone = init_phone;...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const Point& p2) { return this->x == p2.x and this->y == p2.y; } bool operator!=(const Point& p2) { return this->x != p2.x or this->y != p2.y; } friend ostream &operator<<( ostream &out, const Point &P ) { out << "(" << P.x << ", " << P.y << ")"; return out; } friend istream &operator>>( istream &in, Point &P ) { char d1, d2, d3;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT