0% found this document useful (0 votes)
24 views

OOP_DSA - Copy (2)

Uploaded by

hasbirichie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

OOP_DSA - Copy (2)

Uploaded by

hasbirichie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

OOP = object oriented programming

DSA = Data structures & algorithms

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>

using namespace std;

struct User {
string name;
string password;

User() {
name = "";
password = "";
}

User(string name, string password) {


this->name = name;
this->password = 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:

bool is_empty() const {


int sum = 0;

for (int i = 0; i < size; i++) {


sum += table[i].size();
}

return sum == 0;
}

// hash table will be considered full if its 70% filled


bool is_full() const {
int percent = (int)floor(((double)entries / (double)size) * (double)100);
return percent >= 70;
}

int hash_function(string str) {


int hash = 0;

for (int i = 0; i < str.size(); i++) {


hash += str[i] * 2 ^ (i + 1);
}

return hash % size;


}

bool insert(const User& user) {


int hash = hash_function(user.name);
auto& cell = table[hash];

for (auto it = cell.begin(); it != cell.end(); it++) {


// user with the same name already exists, break and return
if (it->name == user.name) {
return false;
}
}

// user does not exist and so add him


cell.push_back(user);
entries++;

return true;
}

bool remove(string username) {


int hash = hash_function(username);
auto& cell = table[hash];

for (auto it = cell.begin(); it != cell.end(); it++) {


if (it->name == username) {
it = cell.erase(it);
return true;
}
}

return false;
}

bool retrieve(string username, User& user) {


int hash = hash_function(username);
auto& cell = table[hash];

for (auto it = cell.begin(); it != cell.end(); it++) {


// user found
if (it->name == username) {
user = *it;
return true;
}
}

return false;
}

void clear() {
// first clear all the cells in the table
for (int i = 0; i < size; i++) {
table[i].clear();
}

entries = 0; // table cleared, so no entries


}

void show_structure() {
for (int i = 0; i < size; i++) {
auto& cell = table[i];

if (cell.size() == 0) {
continue;
}
cout << "[Index: " << i << "]" << endl;

for (auto it = cell.begin(); it != cell.end(); it++) {


cout << "- User: " << it->name << endl;
cout << " Password: " << it->password << endl;
}
}
}
};

class Website {

private:
HashTable table;

public:

void signup(string username, string password) {


if (!table.insert(User(username, password))) {
cout << "User: " << username << " is already registered" << endl;
return;
}

cout << "User: " << username << " registered successfully" << endl;
}

void login(string username, string password) {


User user;

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;
}

cout << "Registered users: " << endl;


table.show_structure();
}

HashTable& get_hash_table() {
return table;
}
};

int main() {

Website site;

cout << "Operation 1 - ";


site.login("user1", ""); // operation 1

cout << "Operation 2 - ";


site.signup("user1", "user1"); // operation 2

cout << "Operation 3 - ";


site.login("user1", "user1"); // operation 3

cout << "Operation 4 - ";


site.login("user1", "check"); // operation 4

cout << "Operation 5 - ";


site.signup("user1", "hello"); // operation 5
cout << "Operation 6 - ";
site.signup("user2", "user2"); // operation 6

cout << "Operation 7 - ";


site.login("user2", "user2"); // operation 7

cout << "Operation 8 - ";


site.display(); // operation 8

_getch();
return 0;
}

OOP DESIGN PROBLEM:

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>

using namespace std;

class TollBooth {
static double money;
static int total_car;

public:
TollBooth() {}

void pay() {
total_car++;
money += 0.50;
}

void nopay() {
total_car++;
}

int getCars() const {


return total_car;
}

double getMoney() const {


return money;
}

void display() const {


cout << "Total number of cars: " << total_car << endl;
cout << "Total money collected: $" << money << endl;
}
};

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;
}
}

cout << "\nData is: " << booth.getCars() << endl;


booth.display();

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>

using namespace std;

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;
}

string getTitle() const {


return title;
}

void checkIn() {
available = true;
}

bool getStatus() const {


return available;
}

// Make displayBookInfo() a const member function


void displayBookInfo() const {
cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "ISBN: " << isbn << endl;
cout << "Genre: " << genre << endl;
cout << "Availability: " << (available ? "available" : "unavailable") <<
endl;
}

bool operator==(const LibraryBook& other) const {


return this->isbn == other.isbn;
}
};

class Borrower {
string name;
string libraryCardNumber;
vector<LibraryBook> borrowedBooks; // as a list

public:
Borrower(const string& name = "", const string& libraryCardNumber = "")
: name(name), libraryCardNumber(libraryCardNumber) {}

void borrowBook(LibraryBook& book) {


if (book.getStatus()) {
borrowedBooks.push_back(book);
// Borrowed, so mark it as checked out
book.checkOut();
} else {
cout << "Sorry, the book is not available" << endl;
}
}

void returnBook(LibraryBook& book) {


for (auto it = borrowedBooks.begin(); it != borrowedBooks.end(); ++it) {
if (*it == book) {
borrowedBooks.erase(it);
book.checkIn();
break;
}
}
}

void listBorrowedBooks() const {


cout << "Books borrowed by " << name << ":\n";
for (const auto& book : borrowedBooks) {
cout << book.getTitle() << endl;
}
}

void displayBorrowerInfo() const {


cout << "Name: " << name << endl;
cout << "Library Card Number: " << libraryCardNumber << endl;
}
};

int main() {
LibraryBook book1("The Catcher in the Rye", "J.D. Salinger", "9780316769174",
"Fiction");
LibraryBook book2("To Kill a Mockingbird", "Harper Lee", "9780061120084",
"Fiction");

Borrower borrower1("John Doe", "LIB123");


Borrower borrower2("Jane Smith", "LIB456");

cout << "Book 1" << endl;


book1.displayBookInfo();
cout << "Book 2" << endl;
book2.displayBookInfo();

cout << "Borrower 1" << endl;


borrower1.displayBorrowerInfo();
cout << "Borrower 2" << endl;
borrower2.displayBorrowerInfo();

cout << "Borrowing book 1" << endl;


borrower1.borrowBook(book1);
cout << "Borrowing book 2" << endl;
borrower2.borrowBook(book2);

cout << "Before returning" << endl;


borrower1.listBorrowedBooks();
borrower2.listBorrowedBooks();

cout << "Book1 Availability: " << (book1.getStatus() ? "available" :


"unavailable") << endl;
cout << "Book2 Availability: " << (book2.getStatus() ? "available" :
"unavailable") << endl;

borrower1.returnBook(book1);
borrower2.returnBook(book2);

cout << "After returning" << endl;


borrower1.listBorrowedBooks();
borrower2.listBorrowedBooks();

cout << "Book1 Availability: " << (book1.getStatus() ? "available" :


"unavailable") << endl;
cout << "Book2 Availability: " << (book2.getStatus() ? "available" :
"unavailable") << endl;

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());
}

void copy(node* root)


{
if (root == NULL)
return;

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;

while (temp != NULL)


{

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 inorder(node* root) {


if (root != NULL)
{
inorder(root->left);
cout << root->data << " , ";
inorder(root->right);

void deletee(int d) {
node* p = search_parent(d);
node* t = search(d);

if (t->left == NULL && t->right == NULL)


{
if (p->left == t)
{
p->left = NULL;
}
else {
p->right = NULL;
}
delete t;
}
else if ( t->left == NULL || t->right == NULL) {
if (p->left == t)
{
if (t->left == NULL)
{
p->left = t->right;
t = NULL;
}
else
{
p->left = t->left;
t = NULL;
}

}
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;

while (temp->right != NULL || temp->left != NULL) //.......


{

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;

cout << root->data << " , ";


Preorder(root->left);
Preorder(root->right);

}
void Postorder(node* root)
{

if (root != NULL) {
Postorder(root->left);
Postorder(root->right);
cout << root->data << " , ";
}

};

NOTE: I’ve done other problems too in


this domain.
Besides this I also know desktop
development in c++,java etc.

You might also like