DSA Project

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 11

/*

group three members

Name ID

1.Faris Hassen 1307544


2.Henok mamo 1307262
3.Bereket abeje 1307587
4.Melaku merikibe 1308650
5.Selemon gebre 1307757
6.Mekdes tekalign 1308686
7.Marta abera 1308727
8.Hebtamu tesema 1306272

*\

// number 1

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
struct Town {
string name;
double distance;
double longitude;
double latitude;
Town* next;
}* head = nullptr;
void insertTown() {
Town* newTown = new Town;
cout<<"ENTER NAME OF TOWN: ";
cin.ignore();
getline(cin,newTown->name);
cout<<"ENTER DISTANCE: ";
cin>>newTown->distance;
cout<<"ENTER LONGITUIDE: ";
cin>>newTown->longitude;
cout<<"ENTER LATITUDE: ";
cin>>newTown->latitude;
newTown->next = nullptr;
if (head == nullptr) {
head = newTown;
} else {
Town* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newTown;
}
cout<<"_______________________________________________\n";
cout<<"town '"<<newTown->name<<"' inserted successfully !\n";
cout<<"_______________________________________________\n\n";
}
void deleteTown() {
string name;
Town* prev = nullptr;
Town* curr = head;
if(head==NULL)
cout<<"Empty List\n";
else{
cout<<"----------------------------------------\n";
cout<<"Enter the Name of the Town to Delete: ";
cin.ignore();
getline(cin,name);
cout<<"\n";
while (curr != nullptr) {
if (curr->name == name) {
if (prev == nullptr) {
head = curr->next;
} else {
prev->next = curr->next;
}
delete curr;
cout<<"______________________________________\n";
cout<< "Town '" << name << "' deleted.\n";
cout<<"______________________________________\n\n";
return;
}
prev = curr;
curr = curr->next;
}
cout<<"______________________________________\n\n";
cout<<"Town '"<<name<<"' Not found\n";
cout<<"______________________________________\n\n";
}
}
void searchTown() {
string name;
if(head==NULL)
cout<<"empty list\n";
else{
cout<<"______________________________________\n";
cout << "Enter the name of the Town to Search: ";
cin >> name;
cout<<"\n\n";
Town* curr = head;
while (curr != nullptr) {
if (curr->name == name) {
cout<<"Town Name: "<<curr->name<<endl;
cout<<"Distance: "<<curr->distance<<endl;
cout<<"Longitude: "<<curr->longitude<<endl;
cout<<"Latitude: "<<curr->latitude<<endl;
return;
}
curr = curr->next;
}
cout<<"______________________________________\n\n";
cout << "Town '" << name << "' not found.\n";
cout<<"______________________________________\n\n";
}
}
void displayTowns() {
Town* curr = head;
if(head==NULL)
cout<<"empty list\n";
else{
cout<<"_______________________________________________________________________\n\
n";
while (curr != nullptr) {
cout<<"NAME OF TOWN: "<<curr->name<<"\n";
cout<<" DISTANCE : "<<curr->distance<<"\n";
cout<<" LONGITUIDE : "<<curr->longitude<<"\n";
cout<<" LATITUDE : "<<curr->latitude<<"\n\n\n";
curr = curr->next;
}
cout<<"_______________________________________________________________________\n\
n";
}
}
double calculateDistance(Town* start, Town* destination) {
double lonDiff = start->longitude - destination->longitude;
double latDiff = start->latitude - destination->latitude;
return sqrt(lonDiff * lonDiff + latDiff * latDiff);
}
void findDistance() {
if (head == nullptr) {
cout << "NO Towns Record\n";
} else {
string startTown, destinationTown;
cout << "Enter the starting town: ";
getline(cin>>ws, startTown);
cout << "Enter the destination town: ";
getline(cin>>ws, destinationTown);
Town* start = nullptr;
Town* destination = nullptr;

bool validTowns = true;


Town* curr = head;
while (curr != nullptr) {
if (curr->name == startTown) {
start = curr;
} else if (curr->name == destinationTown) {
destination = curr;
}
curr = curr->next;
}
if (start != nullptr && destination != nullptr) {
double distance = calculateDistance(start, destination);
cout<<"\n\n";
cout<<"Distance between '"<<startTown<<"' and '"<<destinationTown<<"' is "
<<distance<<" KMs\n\n";
} else {
validTowns = false;
}

if (!validTowns) {
cout<<"\n";
cout << "Invalid town name(s)." << endl;
}
}
}
int main() {
int choice;
cout<<"\n";
while (true) {
cout << "___________________________________________\n\n";
cout<<"PLEASE SELECT FROM THE FOLLOWNGS OPTIONS\n\n";
cout << "1. INSERT TOWN\n";
cout << "2. DELETE TOWN\n";
cout << "3. SEARCH TOWN\n";
cout << "4. DISPLAY ALL TOWNS\n";
cout << "5. FIND DISTANCE BETWEEN TOW TOWNS\n";
cout << "6. EXIT" << endl;
cout << "___________________________________________\n";
cin >> choice;
cout<<"\n\n";
switch (choice) {
case 1:
insertTown();
break;
case 2:
deleteTown();
break;
case 3:
searchTown();
break;
case 4:
displayTowns();
break;
case 5:
findDistance();
break;
case 6:
exit(0);
default:
cout << "Invalid choice. Please try again.\n";
}
}
return 0;
}

/*..............................................................
.............................................................
............................................................

*\
// number 2

#include <iostream>
#include <cstring>
using namespace std;
struct car {
string platenumber;
string type;
string ownername;
string arrivaltime;
car* next;
};
car* Exit = NULL;
car* entrance = NULL;
int size = 0;
bool is_empity(){
if(Exit==NULL && entrance==NULL)
return true;
else
return false;
}
void ENQUEUE_NEW_CAR() {
car* newcar = new car;
cout<<"ENTER PLATE NUMBER \n";
cin>>newcar->platenumber;
cout<<"ENTER CAR TYPE \n";
cin>>newcar->type;
cout<<"ENTER OWNER NAME \n";
cin.ignore();
getline(cin,newcar->ownername);
cout << "ARRIVAL TIME\n";
cin >> newcar->arrivaltime;
cout<<"\n\n";
newcar->next = NULL;
if (is_empity()==true) {
entrance = newcar;
Exit = newcar;
size++;
} else {
entrance->next = newcar;
entrance = newcar;
size++;
}
cout<<"........................................................."<<endl;
cout<<" Car Plate Number With '"<<newcar->platenumber<<"' Enqueued
Successfully !"<<endl;
cout<<"........................................................."<<endl;
}

void DEQUEUE_CAR() {
if (is_empity()==true) {
cout << "the queue is empty" << endl;
} else {
car* temp = Exit;
if (Exit == entrance) {
Exit = NULL;
entrance = NULL;
} else {
Exit = Exit->next;
}
size--;
cout<<"................................."<<endl;
cout << "Removed car '"<< temp->platenumber <<"'"<<endl;
cout<<"................................."<<endl;
delete temp;
}
}
void restructure() {
string plate;
if (is_empity()) {
cout << "the queue is empty" << endl;
}
else
{
cout<<"Enter license plate number: ";
cin>>plate;
car* curr = Exit;
car* prev = NULL;
while (curr != NULL && curr->platenumber != plate) {
prev = curr;
curr = curr->next;
}
if (curr == NULL) {
cout<<"..............................."<<"\n";
cout << "car not found in queue"<<endl;
cout<<"..............................."<<"\n";
} else if (prev == NULL) {
cout<<"car is already at the front of the queue"<<endl;
} else {
prev->next = curr->next;
curr->next = Exit;
Exit = curr;
cout<<"------------------------------------------------"<<endl;
cout<<" CAR WITH PLATE NUMBER '"<<Exit->platenumber<<"' FOUND AT THE
FIRST POSITION"<<endl;
cout<<"------------------------------------------------"<<endl;
}
}
}
void peek_CAR() {
if (is_empity()==true) {
cout << "queue is empty" << endl;
} else {
cout <<" NEXT CAR"<< endl;
cout<<"................................."<<endl;
cout <<" ARRIVAL TIME : "<<Exit->platenumber<<endl;
cout <<" Car Type : "<< Exit->type<<endl;
cout <<" Owner Name : "<< Exit->ownername<<endl;
cout <<" ARRIVAL TIME : "<<Exit->arrivaltime<<endl;
cout<<"................................."<<endl;
}
}
int getsize() {
return size;
}
void display_all_cars() {
if (is_empity()) {
cout<<"___________________________________________"<<endl;
cout << "queue is empty" << endl;
cout<<"___________________________________________"<<endl;
} else {
car* curr = Exit;
int x=1;
cout<<"____________________________________________________________________________
_____________________________"<<endl;
cout<<"QUEUE"<<"\t\t"<<"PLATE_NUMBER"<<"\t\t"<<"CAR_TYPE"<<"\t\t"<<"OWNER_NAME"<<"\
t\t"<<"ARRIVAL_TIME "<<endl;
cout<<"____________________________________________________________________________
_____________________________"<<"\n\n";
while (curr != NULL) {
cout <<x<<".\t\t"<<curr->platenumber<<"\t\t\t";
cout<<curr->type<<"\t\t\t";
cout<<curr->ownername<<"\t\t\t";
cout<< curr->arrivaltime <<endl;
curr = curr->next;
x++;
}
cout<<"____________________________________________________________________________
_____________________________"<<endl;
}
cout<<endl;
}
int main() {
int c = 0;
do {
cout<<"___________________________________________________"<<"\n\n";
cout<<" PLEASE SELECT THE OPTIONS"<<"\n\n";
cout<<" 1. TO ENQUEUE NEW_CAR"<<endl;
cout<<" 2. TO DISPLAY ALL CAR FROM THE QUEUE"<<endl;
cout<<" 3. TO DEQUEUE_CAR" << endl;
cout<<" 4. TO DISPLAY NEXT ELEMENT TO BE PROCESSED"<<endl;
cout<<" 5. TO GET NUMBER OF CAR FROM THE QUEUE"<<endl;
cout<<" 6. TO RESTRUCTURE THE CAR"<<endl;
cout<<" 0. To Exit" << endl;
cout<<"___________________________________________________"<<"\n\n";
cin >> c;
cout<<endl;
if (c == 1)
ENQUEUE_NEW_CAR();
else if (c == 2){
display_all_cars();
}
else if (c == 3)
DEQUEUE_CAR();
else if (c == 4)
peek_CAR();
else if(c==5){
cout<<"...................................................."<<endl;
cout << "THE NUMBER OF CAR IN THE QUEUE = " << getsize() << endl;
cout<<"...................................................."<<endl;
}
else if (c == 6) {
restructure();
}
} while (c != 0);
return 0;
}

/*

.............................................................................
.............................................................................
.............................................................................

*\

// number 3

#include <iostream>
#include <string>
using namespace std;
struct Student {
string id;
string name;
string fatherName;
string sex;
double cgpa;
Student* next;
};
Student* head = nullptr;
void addStudent(){
Student* newStudent = new Student;
cout << "ENTER STUDENT ID : ";
cin>>newStudent->id;
cout<< "ENTER STUDENT NAME : ";
cin>>newStudent->name;
cout<< "ENTER FATHER'S NAME : ";
cin>>newStudent->fatherName;
cout << "ENTER SEX : ";
cin>>newStudent->sex;
cout << "ENTER CGPA: ";
cin>>newStudent->cgpa;
newStudent->next = nullptr;
if (head == nullptr) {
head = newStudent;
} else {
Student *temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next = newStudent;
}
cout<<"\n";
cout << "-----------------------------------------------------" << endl;
cout << "Student "<<"'"<<newStudent->name<<"'"<< " Risgistrated successfully.\n";
cout << "-----------------------------------------------------" << endl;
}
void deleteStudent() {
if (head == nullptr) {
cout << "No students in the list.\n";
return;
}
bool deleted = false;
cout << "..............Academically dismissed student
list.............."<<endl;;
while (head != nullptr && head->cgpa < 1.75) {
Student* temp = head;
head = head->next;
cout<<"name : "<<temp->name<<endl;
cout<<"father's : "<<temp->fatherName<<endl;
cout<<"sex : "<<temp->sex<<endl;
cout<<"CGPA : "<<temp->cgpa<<endl;
cout<<endl;
delete temp;
deleted = true;
}
Student* prev = head;
Student* curr = head->next;
while (curr != nullptr) {
if (curr->cgpa < 1.75) {
prev->next = curr->next;
cout<<"name : "<<curr->name<<endl;
cout<<"father's : "<<curr->fatherName<<endl;
cout<<"sex : "<<curr->sex<<endl;
cout<<"CGPA : "<<curr->cgpa<<endl;
cout<<endl;
delete curr;
cout << "Academically dismissed student deleted.\n";
deleted = true;
curr = prev->next;
} else {
prev = curr;
curr = curr->next;
}
}
if (!deleted) {
cout << "No academically dismissed students found.\n";
}
}
void searchStudent() {
string name;
cout<<"Enter the name of the student to search:";
cin.ignore();
getline(cin, name);
cout<<"\n";
Student* curr = head;

bool found = false;


while (curr != nullptr) {
if (curr->name == name) {
cout<<"Student '"<<name<<"' found.\n\n";
cout<<"ID : " << curr->id << endl;
cout<<"Name : " << curr->name << endl;
cout<<"Fname : " << curr->fatherName << endl;
cout<<"Sex : " << curr->sex << endl;
cout<<"CGPA : " << curr->cgpa << endl;
found = true;
break;
}
curr = curr->next;
}
if (!found) {
cout<<"-------------------------------------------"<<endl;
cout << "Student with name '" << name << "' not found.\n";
cout<<"-------------------------------------------"<<endl;
}
}
void displayStudents() {
if (head == nullptr) {
cout<<"-------------------------------------------"<<endl;
cout << "No students in the list.\n";
cout<<"-------------------------------------------"<<endl;
return;
}
Student* curr = head;
int x=1;
cout<<" ALL REGISTERATED STUDENTS"<<endl;
cout<<"----------------------------------------------------------------------------
-------"<<endl;
cout<<" ID\t\tNAME\t\tFNAME\t\tSEX\t\tCGPA\t\t"<<endl;
cout<<"----------------------------------------------------------------------------
-------"<<endl;
while (curr != nullptr) {
cout<<x<< ". " << curr->id<<"\t\t ";
cout<<curr->name<<"\t\t";
cout<<curr->fatherName<<"\t\t";
cout<<curr->sex<<"\t\t ";
cout<<curr->cgpa<<endl;
cout<< endl;
curr = curr->next;
x++;
}

cout<<"----------------------------------------------------------------------------
-------"<<endl;
}
void swapStudents(Student* a, Student* b) {
string id = a->id;
string name = a->name;
string fatherName = a->fatherName;
string sex = a->sex;
double cgpa = a->cgpa;
a->id = b->id;
a->name = b->name;
a->fatherName = b->fatherName;
a->sex = b->sex;
a->cgpa = b->cgpa;
b->id = id;
b->name = name;
b->fatherName = fatherName;
b->sex = sex;
b->cgpa = cgpa;
}
void sortStudents() {
if (head == nullptr || head->next == nullptr) {
return;
}
bool swapped;
Student* curr;
Student* last = nullptr;
do {
swapped = false;
curr = head;
while (curr->next != last) {
if (curr->name > curr->next->name) {
swapStudents(curr, curr->next);
swapped = true;
}
curr = curr->next;
}
last = curr;
} while (swapped);
}
int main() {
int choice;
while (true) {
cout << "______________________________________________\n\n";
cout<<" PLEASE SELECT ONE OF THE FOLLOWNG OPTIONS"<<"\n\n";
cout<<" 1. ADD NEW STUDENTS\n";
cout<<" 2. DELETE ACADAMICALLY DISMISSED STUDENTS\n";
cout<<" 3. SEARCH STUDENT\n";
cout<<" 4. VIEW ALL STUDENTS\n";
cout<<" 5. SORT STUDENT BY NAME\n";
cout<<" 6. EXIT\n";
cout<<"______________________________________________" << endl;
cin>> choice;
cout<<endl;
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
searchStudent();
break;
case 4:
displayStudents();
break;
case 5:
sortStudents();
cout << "Students sorted by name.\n";
break;
case 6:
exit(0);
default:
cout << "Invalid choice. Please try again.\n";
}
}
return 0;
}

You might also like