Skip to content

Commit

Permalink
Added recent labs
Browse files Browse the repository at this point in the history
  • Loading branch information
sheikheddy committed Apr 5, 2019
1 parent 223b752 commit 5369b3f
Show file tree
Hide file tree
Showing 33 changed files with 1,422 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Data/Lab3/12N0xb.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
*A program to print all binary numbers from One to N using queues.
Made by Sheikh Abdul Raheem Ali
* */

#include"CircularArray.cpp"

int main(){

int n;
do{
cout << "Enter a number: ";
cin >> n;
}while(n < 1);

string str = "1";
QueueType q(n);
q.Enqueue(str);

cout << "Binary numbers from 1 to " << n << " are: \n";
for(int i = 1; i <= n; ++i){
q.Dequeue(str);
cout << str << endl;
q.Enqueue(str+"0");
q.Enqueue(str+"1");
}


return 0;
}
Binary file added Data/Lab3/CMP305-Lab3-Queue.odt
Binary file not shown.
61 changes: 61 additions & 0 deletions Data/Lab3/CircularArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include"QueueType.h"


QueueType::QueueType(int max){
maxQueue = max;
front = 0;
rear = maxQueue - 1;
count = 0;
items = new ItemType[maxQueue];


} // PARAMETERIZED CONSTRUCTOR



void QueueType::MakeEmpty ( ){count = 0; front = (rear + 1) % maxQueue;}
bool QueueType::IsFull( )const{return count == maxQueue;}
bool QueueType::IsEmpty( )const{return count == 0;}
ERROR_CODE QueueType::Enqueue( ItemType item ){
if(IsFull()){return Overflow;}
rear = (rear + 1) % maxQueue;
items[rear] = item;
count++;
return Success;
}
ERROR_CODE QueueType::Dequeue( ItemType& item ){
if(IsEmpty()){return Underflow;}
item = items[front];
front = (front + 1) % maxQueue;
count--;
return Success;

}

QueueType::~QueueType(){
delete[] items;
}

QueueType& QueueType::operator=(const QueueType& q){
if(q != this){
if(maxQueue != q.maxQueue){
maxQueue = q.maxQueue;
delete[] items;
items = new ItemType[maxQueue];
}
front = q.front;
rear = q.rear;
count = q.count;
for(int i = 0; i < maxQueue; ++i){items[i] = q.items[i];}
}
return (*this);
}

QueueType::QueueType(const QueueType& q){
maxQueue = q.maxQueue;
items = new ItemType[maxQueue];
front = q.front;
rear = q.rear;
count = q.count;
for(int i = 0; i < maxQueue; ++i){items[i] = q.items[i];}
}
70 changes: 70 additions & 0 deletions Data/Lab3/MarchingIndices.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include"QueueType.h"


QueueType::QueueType(int max){
maxQueue = max;
items = new ItemType[maxQueue];
front = rear = -1;

} // PARAMETERIZED CONSTRUCTOR



void QueueType::MakeEmpty ( ){front = rear = -1;}
bool QueueType::IsFull( )const{
return((rear == maxQueue - 1)&&(front == 0));
}
bool QueueType::IsEmpty( )const{
return (front == -1);
}
ERROR_CODE QueueType::Enqueue( ItemType item ){
if(IsFull()){return Overflow;}
if(front == -1){front++;}
if(rear == maxQueue - 1){
QueueType q(maxQueue);
ItemType tmp;
while(!IsEmpty()){
Dequeue(tmp);
q.Enqueue(tmp);
}
*this = q;
}
rear = rear + 1;
items[rear] = item;
return Success;
}
ERROR_CODE QueueType::Dequeue( ItemType& item ){
if(IsEmpty()){return Underflow;}
item = items[front];
front = front + 1;
if(front == rear + 1){MakeEmpty();}
return Success;

}

QueueType::~QueueType(){
delete[] items;
}

QueueType& QueueType::operator=(const QueueType& q){
if( q != this){
if(maxQueue != q.maxQueue){
maxQueue = q.maxQueue;
delete items;
items = new ItemType[maxQueue];
}
front = q.front;
rear = q.rear;
for(int i = 0; i < maxQueue; ++i){items[i] = q.items[i];}
}
return (*this);
}

QueueType::QueueType(const QueueType& q){
maxQueue = q.maxQueue;
items = new ItemType[maxQueue];
front = q.front;
rear = q.rear;
for(int i = 0; i < maxQueue; ++i){items[i] = q.items[i];}
}

34 changes: 34 additions & 0 deletions Data/Lab3/QueueType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef QUEUETYPE_H

#define QUEUETYPE_H

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

enum ERROR_CODE{Underflow, Overflow, Success};


typedef string ItemType;
class QueueType {
public:
QueueType( int max = 10); // PARAMETERIZED CONSTRUCTOR
void MakeEmpty ( );
bool IsFull( ) const;
bool IsEmpty( ) const;
ERROR_CODE Enqueue( ItemType item );
ERROR_CODE Dequeue( ItemType& item );
// Add the definition of the safety functions

~QueueType(); //Destructor
QueueType(const QueueType& q); //Copy Constructor
QueueType& operator=(const QueueType& q); //Assigment operator
private:
int maxQueue; // Queue capacity
ItemType* items; // DYNAMIC ARRAY IMPLEMENTATION
int front;
int rear;
int count;
};

#endif
58 changes: 58 additions & 0 deletions Data/Lab3/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
*A program to test my implementation of the queue data structure
*
* */

#include"MarchingIndices.cpp"

void displayOptions();
void displayQueue(const QueueType& q);
QueueType initialize();
int main(){
QueueType q;
ItemType front;
ItemType rear;
int option = 1;
do{
switch(option){
case 1: q = initialize(); break;
case 2: cin >> rear; cout << (q.Enqueue(rear) == Overflow ? "Overflow \n": " Enqueued "+ rear +"\n"); break;
case 3: cout << (q.Dequeue(front) == Underflow ? "Underflow \n" : " Dequeued item " + front + "\n"); break;
case 4: displayQueue(q); break;
case 5: cout << (q.IsFull() ? "Queue is full \n" : "Queue is not full \n"); break;
case 6: cout << (q.IsEmpty() ? "Queue is empty \n" : "Queue is not empty \n"); break;
case 7: q.MakeEmpty(); cout << "Queue Emptied \n"; break;
case 8: break;
}
if(option != 8){
displayOptions();
cin >> option;
}
}while(option != 8);



return 0;
}


void displayOptions(){
cout << "Enter an Option: \n 1: Initialize a queue \2 2: Enqueue \n 3: Dequeue \n 4: Display Queue \n 5: Is Full? \n 6: Is Empty? \n 7: Make Empty \n 8: Exit" << endl;
}

QueueType initialize(){
int n;
cout << "Enter queue size: ";
cin >> n;
return QueueType(n);
}

void displayQueue(const QueueType& q){
QueueType temporary(q);
ItemType item;
while(!temporary.IsEmpty()){
temporary.Dequeue(item);
cout << item << " ";
}
cout << endl;
}
109 changes: 109 additions & 0 deletions Data/Lab4/Book.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include<iostream>
#include<string>
using namespace std;

const int MAX_ITEMS = 50 ;
enum RelationType { LESS, EQUAL, GREATER } ;


class Book // declares class data type
{
public : // 3 public member functions
Book();
Book(int, string,string,string,float,int);
Book(const Book & b);
~Book();
Book& operator=(const Book& b);


int sellBook(int count); //decrements the number of copies
int buyBook(int count); //increments the number of copies
RelationType ComparedTo (const Book &)const; //Compare with ISBN
void print ( ) const ; //prints book info
int getISBN(){return isbn;}
string getAuthor(){return author;}

string getTitle(){return title;}
float getPrice(){return price;}
int getNumCopies(){return numCopies;}
string getPublisher(){return publisher;}

friend ostream& operator<<(ostream& cout, const Book& b);
private :
int isbn;
string author;
string title;
float price;
int numCopies;
string publisher;
} ;

Book::~Book(){

}

Book::Book(int ISBN, string Title, string Author, string Publisher, float Price, int NumCopies){
isbn = ISBN;
author = Author;
publisher = Publisher;
title = Title;
price = Price;
numCopies = NumCopies;
}

Book::Book(){

}

Book::Book(const Book& b){

isbn = b.isbn;
author = b.author;
title = b.title;
price = b.price;
numCopies = b.numCopies;
publisher = b.publisher;


}

Book& Book::operator=(const Book& b){

isbn = b.isbn;
author = b.author;
title = b.title;
price = b.price;
numCopies = b.numCopies;
publisher = b.publisher;

return *this;
}
int Book::sellBook(int count){
if(numCopies - count < 0){
cout << "Cannot sell "<< count << " copies." << endl;
return -1;
}
numCopies -= count;
return numCopies;
}

int Book::buyBook(int count){
numCopies += count;
return numCopies;
}

RelationType Book::ComparedTo(const Book & b) const{
if(isbn < b.isbn){return LESS;}
if(isbn == b.isbn){return EQUAL;}
if(isbn > b.isbn){return GREATER;}
}

void Book::print() const{
cout << "ISBN: " << isbn << " Title: " << title << " Author: "<< author << " Publisher: " << publisher << " Price: " << price << " Number of Copies: " << numCopies;
}


ostream& operator<<(ostream& cout, const Book& b){
b.print();
return cout;
}
Binary file added Data/Lab4/CMP305-Lab4-unsortedList.docx
Binary file not shown.
Binary file added Data/Lab4/CMP305-Lab4-unsortedList.odt
Binary file not shown.
Loading

0 comments on commit 5369b3f

Please sign in to comment.