OOP_DSA - Copy (2)
OOP_DSA - Copy (2)
DSA PROBLEM:
1. Create a Website login system. System should provide an interactive panel to register
the users. User can login the system if password matched. Insert passwords into the
Hash Table retrieve one user's Password structure from the Hash Table compare
retrieved user password to input password and print "Authentication failure" or
"Authentication successful". Add in a line to insert passwords into the table. Build and
Run your program.
Used HashTable
#include <iostream>
#include <conio.h>
#include <list>
struct User {
string name;
string password;
User() {
name = "";
password = "";
}
class HashTable {
private:
static const int initial_size = 11;
list<User> table[initial_size];
int size = initial_size; // or can be called as buckets
int entries = 0; // the total entries added into the hash table
public:
return sum == 0;
}
return true;
}
return false;
}
return false;
}
void clear() {
// first clear all the cells in the table
for (int i = 0; i < size; i++) {
table[i].clear();
}
void show_structure() {
for (int i = 0; i < size; i++) {
auto& cell = table[i];
if (cell.size() == 0) {
continue;
}
cout << "[Index: " << i << "]" << endl;
class Website {
private:
HashTable table;
public:
cout << "User: " << username << " registered successfully" << endl;
}
if (!table.retrieve(username, user)) {
cout << "User: " << username << " is not registered" << endl;
return;
}
if (user.password != password) {
cout << "Unable to login. Invalid password provided for User: " << username << endl;
return;
}
cout << "User: " << username << " logged in successfully" << endl;
}
void display() {
if (table.is_empty()) {
cout << "No registered users to display" << endl;
return;
}
HashTable& get_hash_table() {
return table;
}
};
int main() {
Website site;
_getch();
return 0;
}
1. Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50 cent
toll. Mostly they do, but sometimes a car goes by without paying. The tollbooth keeps
track of the number of cars that have gone by, and of the total amount of money
collected. Model this tollbooth with a class called tollBooth. The two data items are a
type unsigned int to hold the total number of cars, and a type double to hold the total
amount of money collected. A constructor initializes both of these to 0. A member
function called payingCar() increments the car total and adds 0.50 to the cash total.
Another function, called nopayCar(), increments the car total but adds nothing to the
cash total. Finally, a member function called display() displays the two totals. Make
appropriate member functions const. Include a program to test this class. This
program should allow the user to push one key to count a paying car, and another to
count a nonpaying car. Pushing the Esc key should cause the program to print out the
total cars and total cash and then exit.
#include <iostream>
#include <conio.h>
class TollBooth {
static double money;
static int total_car;
public:
TollBooth() {}
void pay() {
total_car++;
money += 0.50;
}
void nopay() {
total_car++;
}
int TollBooth::total_car = 0;
double TollBooth::money = 0;
int main() {
TollBooth booth;
int num;
while (true) {
cout << "Enter 'p' for paying and 'n' for non-paying cars. Press 'Esc' to
exit." << endl;
num = _getch();
if (num == 'p') {
booth.pay();
} else if (num == 'n') {
booth.nopay();
} else if (num == 27) { // 27 is the ASCII code for 'Esc' key
break;
}
}
return 0;
}
I pressed three paying and two non paying cars:
that is why cost is 1.5
OOP DESIGN PROBLEM:
1. Design and implement the "LibraryBook" and "Borrower" classes in a programming
language of your choice.
2. Establish a composition relationship by allowing each "Borrower" object to contain a list
of "LibraryBook" objects they have borrowed.
3. Create instances of "LibraryBook" and "Borrower" and demonstrate the composition
relationship between them.
4. Implement methods to allow borrowers to check out and return books, ensuring the
availability status is updated.
5. Provide a mechanism for borrowers to view the books they have borrowed.
6. Test the system by creating multiple borrowers and books, allowing them to interact.
#include <iostream>
#include <string>
#include <vector>
class LibraryBook {
string title;
string author;
string isbn;
string genre;
bool available;
public:
// Default + parameterized constructor
LibraryBook(const string& title = "", const string& author = "", const
string& isbn = "", const string& genre = "", bool b = true)
: title(title), author(author), isbn(isbn), genre(genre), available(b) {}
void checkOut() {
available = false;
}
void checkIn() {
available = true;
}
class Borrower {
string name;
string libraryCardNumber;
vector<LibraryBook> borrowedBooks; // as a list
public:
Borrower(const string& name = "", const string& libraryCardNumber = "")
: name(name), libraryCardNumber(libraryCardNumber) {}
int main() {
LibraryBook book1("The Catcher in the Rye", "J.D. Salinger", "9780316769174",
"Fiction");
LibraryBook book2("To Kill a Mockingbird", "Harper Lee", "9780061120084",
"Fiction");
borrower1.returnBook(book1);
borrower2.returnBook(book2);
return 0;
}
DSA PROBLEM:
BINARY SEARCH TREE:
#include <iostream>
using namespace std;
struct node
{
int data;
node* left; node* right;
node() : data(int()), left(NULL), right(NULL) {}
node(int d) : data(d), left(NULL), right(NULL) {}
};
class bst {
protected:
node* root;
public:
bst() :root(NULL) {
}
bst(bst& b) {
cout << " now deep copy" << endl;
copy(b.get_root());
}
insert(root->data);
copy(root->left);
copy(root->right);
bool is_empty() {
return root == NULL;
}
void insert(int d) {
node* new_node = new node(d);
node* temp = root;
if (is_empty())
{
root = new_node;
return;
}
while (true)
{
if (d > temp->data) {
if (temp->right == NULL)
{
temp->right = new_node;
break;
}
else
{
temp = temp->right;
}
}
else
{
if (temp->left == NULL) {
temp->left = new_node;
break;
}
else
{
temp = temp->left;
}
}
node* search(int d) {
node* temp = root;
if (d > temp->data) {
temp = temp->right;
}
else if (d < temp->data)
{
temp = temp->left;
}
else
{
break;
}
}
return temp;
}
node* search_parent(int d) {
node* temp = root;
node* prev = NULL;
while (temp != NULL)
{
if (d > temp->data) {
prev = temp;
temp = temp->right;
}
else if (d < temp->data)
{
prev = temp;
temp = temp->left;
}
else
{
break;
}
}
return prev;
}
void deletee(int d) {
node* p = search_parent(d);
node* t = search(d);
}
else
{
if (t->left == NULL)
{
p->right = t->right;
t = NULL;
}
else
{
p->right = t->left;
t = NULL;
}
}
}
else
{
node* temp = t->right;
node* prev = t;
if (temp->left != NULL)
{
prev = temp;
temp = temp->left;
}
else
{
prev = temp;//......
temp = temp->right;
}
}
cout << "copying " << temp->data << endl;
t->data = temp->data;
prev->left = NULL;
//t->right = temp->right;
delete temp; temp = NULL;
}
node* get_root() {
return root;
}
void Preorder(node* root)
{
if (root == NULL)
return;
}
void Postorder(node* root)
{
if (root != NULL) {
Postorder(root->left);
Postorder(root->right);
cout << root->data << " , ";
}
};