Вы находитесь на странице: 1из 14

#include<iostream>

#include<cctype>
#include<stack>
#include <queue>
#include <string>
using namespace std;
int array[10];
void merge(int, int, int);
void mergesort(int, int);
void recursiveFunction(int);
typedef short SmallNumber;
typedef unsigned int Positive;
typedef double* PDouble;
typedef string FiveStrings[5];
typedef struct telephone
{
char *name;
int number;
}TELEPHONE;
struct Person
{
char name[50];
int age;
float salary;
};
enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
//classes for 7
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};

//linkedlist functions
struct Node {
int data;
Node* next;
};
// only for the 1st Node
void initNode(struct Node *head,int n){
head->data = n;
head->next =NULL;
}
// apending
void addNode(struct Node *head, int n) {
Node *newNode = new Node;
newNode->data = n;
newNode->next = NULL;
Node *cur = head;
while(cur) {
if(cur->next == NULL) {
cur->next = newNode;
return;
}
cur = cur->next;
}
}
void insertFront(struct Node **head, int n) {
Node *newNode = new Node;
newNode->data = n;
newNode->next = *head;
*head = newNode;
}
struct Node *searchNode(struct Node *head, int n) {
Node *cur = head;
while(cur) {
if(cur->data == n) return cur;
cur = cur->next;
}
cout << "No Node " << n << " in list.\n";
}
bool deleteNode(struct Node **head, Node *ptrDel) {
Node *cur = *head;
if(ptrDel == *head) {
*head = cur->next;
delete ptrDel;
return true;
}
while(cur) {
if(cur->next == ptrDel) {
cur->next = ptrDel->next;
delete ptrDel;
return true;
}
cur = cur->next;
}
return false;
}
/* reverse the list */
struct Node* reverse(struct Node** head)
{
Node *parent = *head;
Node *me = parent->next;
Node *child = me->next;
/* make parent as tail */
parent->next = NULL;
while(child) {
me->next = parent;
parent = me;
me = child;
child = child->next;
}
me->next = parent;
*head = me;
return *head;
}
/* Creating a copy of a linked list */
void copyLinkedList(struct Node *node, struct Node **pNew)
{
if(node != NULL) {
*pNew = new Node;
(*pNew)->data = node->data;
(*pNew)->next = NULL;
copyLinkedList(node->next, &((*pNew)->next));
}
}
/* Compare two linked list */
/* return value: same(1), different(0) */
int compareLinkedList(struct Node *node1, struct Node *node2)
{
static int flag;
/* both lists are NULL */
if(node1 == NULL && node2 == NULL) {
flag = 1;
}
else {
if(node1 == NULL || node2 == NULL)
flag = 0;
else if(node1->data != node2->data)
flag = 0;
else
compareLinkedList(node1->next, node2->next);
}
return flag;
}
void deleteLinkedList(struct Node **node)
{
struct Node *tmpNode;
while(*node) {
tmpNode = *node;
*node = tmpNode->next;
delete tmpNode;
}
}
void display(struct Node *head) {
Node *list = head;
while(list) {
cout << list->data << " ";
list = list->next;
}
cout << endl;
cout << endl;
}

int main(void)
{
while(true)
{
int mch = 0;
cout<<" PROJECT MAIN MENU"<<endl;
cout<<"\n A. SORTS"<<endl;
cout<<" 1. Bubble Sort"<<endl;
cout<<" 2. Selection Sort"<<endl;
cout<<" 3. Insertion Sort"<<endl;
cout<<" 4. Merge Sort"<<endl;
cout<<" B. SEARCHING ALGORITHM"<<endl;
cout<<" 5. Sequential Search"<<endl;
cout<<" 6. Binary Search"<<endl;
cout<<" C. OTHERS"<<endl;
cout<<" 7. Function (Fibonacci Sequence)"<<endl;
cout<<" 8. Class (Rectangle Shape)"<<endl;
cout<<" 9. Stacks"<<endl;
cout<<" 10. Queues"<<endl;
cout<<" 11. Pointer"<<endl;
cout<<" 12. Enum"<<endl;
cout<<" 13. TypeDef"<<endl;
cout<<" 14. Union"<<endl;
cout<<" 15. Struct"<<endl;
cout<<" 16. Linked List"<<endl;
cout<<" 17. EXIT"<<endl<<endl;
cout<<" MENU CHOICE [1-10]: ";
cin>>mch;
if(mch == 1)
{
system("CLS");
int a[50],n = 5,i,j,temp;
cout<<" BUBBLE SORT \n\n";
for(i=0;i<n;++i)
{
cout<<" Enter the array elements: ["<<i<<"]: ";
cin>>a[i];
}

for(i=1;i<n;++i)
{
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
cout<<" Array after bubble sort: ";
for(i=0;i<n;++i)
cout<<" "<<a[i];
cout<<endl<<endl;
}
else if(mch == 2)
{
int a[50],n = 10,i,j,temp, compare=0, swap=0, large;
system("CLS");
cout<<" SELECTION SORT ALGORITHM (2) "<<endl<<endl;
for(i=0;i<n;++i){
cout<<" Enter the index for("<<i<<"): ";
cin>>a[i];
}

for(i=1;i<n;++i)
{
large = 0;
for(j=0;j<(n-i);++j){
compare++;
if(a[j]>a[large])
{
large = j;
temp=a[large];
a[large]=a[i];
a[i]=temp;
swap++;
}
}
}
cout<<"\n SWAPS: "<<swap;
cout<<"\n COMPARES: "<<compare;
cout<<"\n Array after selection sort: ";
for(i=0;i<n;++i)
cout<<" "<<a[i];
cout<<endl;
}
else if(mch == 3)
{
int arrayko[10];
int i, j, temp, sizeko=10, Nswap=0, Ncompare=0;
system("CLS");
cout<<" INSERTION SORT \n\n";
for(i=0; i<10;i++)
{
cout <<" Enter Value at Index (" <<i<< "): ";
cin>>arrayko[i];
}
for(i=0; i<10; i++)
{
j=i;
while(j>0 && arrayko[j]>arrayko[j-1])
{
temp=arrayko[j];
arrayko[j]=arrayko[j-1];
arrayko[j-1]=temp;
j--;
Ncompare++;
}
Nswap++;
}
cout<<"\n\n INSERTION SORT DESCENDING OUTPUT: \n";
for(i=0; i<10; i++)
{
cout<<" "<<arrayko[i]<<" ";
}
cout<<endl<<endl;
}
else if(mch == 4)
{
int num,i;
system("CLS");
cout<<"MERGE SORT\n\n";
for (i=0; i<10; i++){
cout <<"ENTER VALUE AT INDEX (" <<i<<"): ";
cin>>array[i];
}
mergesort(0,9);
cout<<"MERGE SORT DESCENDING OUTPUT: ";
for (i=9; i>=0; i-=1) {
cout<<array[i]<<" ";
}
cout<<endl;
}
else if(mch == 5)
{
int arr1[10];
int req;
int location=-10, compare=0, w;
system("CLS");
cout<<" SEQUENTIAL SEARCH ALGORITHM "<<endl<<endl;
for(int i=0; i<10; i++)
{
cout<<" Enter value at index ("<<i<<"): ";
cin>>arr1[i];
}
cout<<endl;
cout<<" Enter the number you want to found: ";
cin>>req;
cout<<endl;
for(w=0;w<10;w++)
{
if(arr1[w] == req)
location=w;
}
if(location !=-10)
{
cout<<" Input "<<req<<" is found at INDEX "<<location;
cout<<endl<<" Number of times compared: "<<location+1;
cout<<endl;
}
else
cout<<"Number is not found ";
}
else if(mch == 6)
{
int arr[100], beg, mid, end, i, n=10, num, compare = 0;
system("CLS");
cout<<" BINARY SEARCH ALGORITHM"<<endl<<endl;
for(i=0; i<n;i++)
{
cout<<" Enter value at index ("<<i<<"): ";
cin>> arr[i];
}
cout<<"\n\n Middle elemet: " << arr[4];
beg = 0;
end = n - 1;
cout<<"\n Enter the value to be searched in array: ";
cin>> num;

while(beg<=end){
mid = (beg+end)/2;
compare++;
if(arr[mid] == num){
cout<<"\n Input "<<num<<" is found at INDEX "<<mid;
cout<<endl<<" Number of times compared: "<<compare;
exit(0);
}
else if( num > arr[mid]){
beg = mid + 1;
}
else if(num < arr[mid]){
end = mid - 1;
}
else
{
cout<<" Number does not found.";
}
}
}
else if(mch == 7)
{
int num;
system("CLS");
cout<<" Fibonacci Sequence using FUNCTIONS"<<endl<<endl;
cout<<" How many times will loop: ";
cin>>num;
num = num - 1;
recursiveFunction(num);
}
else if(mch == 8)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
system("CLS");
cout<<" Rectangle Class "<<endl<<endl;
cout << " Total area: " << Rect.getArea() << endl;
return 0;
}
else if(mch == 9)
{
char choice, ans, quit = 'N';
int value;
stack<int> myStack;
do {
system("cls");
cout << "[A] PUSH" << endl;
cout << "[B] POP" << endl;
cout << "[C] FRONT" << endl;
cout << "[D] BACK" << endl;
cout << "[E] SIZE" << endl;
cout << "[F] QUIT" << endl;
cout << "Enter your choice: " << endl;
cin >> choice;
choice = toupper(choice);
if (choice == 'A') {
do {
cout << "Enter data to be pushed into the stack:
";
cin >> value;
myStack.push(value);
cout << "Data pushed into the stack is " << mySt
ack.top() << endl;
cout << "Do you want to try again [Y/N]: ";
cin >> ans;
ans = toupper(ans);
}while(ans == 'Y');
}
else if (choice == 'B') {
do {
cout << "Do you want to pop data from the stack
[Y/N]: ";
cin >> ans;
ans = toupper(ans);
if(ans == 'Y') {
if(myStack.empty() == true) {
cout << " Stack is Empty!" << endl;
}
else {
myStack.pop();
cout << "Character was popped!" << endl;
}
}
cout << " Do you want to try again [Y/N]: ";
cin >> ans;
ans = toupper(ans);
}while(ans == 'Y');
}
else if(choice == 'C') {
if(myStack.empty()) {
cout << "Top Most Element is " << myStack.top()
<< endl;
}
else {
cout << "Stack is Empty!" << endl;
}
}
else if (choice == 'D') {
cout << "Size of Stack is " << myStack.size() << endl;
}
else if (choice == 'E') {
cout << "Do you really want to quit [Y/N]: ";
cin >> ans;
ans = toupper(ans);
if (ans == 'Y') {
quit = 'Y';
}
else {
quit = 'N';
}
}
else {
cout << "Invalid Choice!" << endl;
}
}
while (quit == 'N');
}
else if(mch == 10)
{
char choice , ans, quit = 'N', value;
queue <char> myQueue;
do {
system ("cls");
cout << "QUEUES MAIN MENU" << endl;
cout << "[A] PUSH" << endl;
cout << "[B] POP" << endl;
cout << "[C] FRONT" << endl;
cout << "[D] BACK" << endl;
cout << "[E] SIZE" << endl;
cout << "[F] QUIT" << endl;
cout << "Enter your choice: ";
cin >> choice;
choice = toupper(choice);
if (choice == 'A') {
do {
cout << "Enter a character to push into stack: "
;
cin >> value;
myQueue.push(value);
cout << "Character " << value << " pushed into q
ueue!" << endl;
cout << "Do you want try again [Y/N]: ";
cin >> ans;
ans = toupper(ans);
} while (ans == 'Y'&& ans == toupper(ans));
}
else if (choice == 'B') {
do {
cout << "Do you want to pop data out of
the queue [Y/N]?" << endl;
cin >> ans;
ans= toupper(ans);
if (ans == 'Y') {
if (myQueue.empty() == true) {
cout << " " << endl;
}
else {
myQueue.pop();
cout << "Character " << value <
< " was popped" << endl;
}
}
cout << "Do you want to do it again [Y/N]: ";
cin >> ans;
ans = toupper(ans);
}
while (ans == 'Y');
}
else if (choice == 'C') {
system ("cls");
cout << "Front most element is " << myQueue.front() << e
ndl;
system ("pause");
}
else if (choice == 'D') {
system ("cls");
cout << "Data at the back is " << myQueue.back() << endl
;
system ("pause");
}
else if (choice == 'E') {
cout << "Size of the queue is " << myQueue.size() << end
l;
system ("pause");
}
else if (choice == 'F') {
cout << "Do you really want to quit [Y/N]: ";
cin >> ans;
ans = toupper(ans);
if (ans == 'Y') {
quit = 'Y';
}
else {
quit = 'N';
}
}
}
while (quit == 'N');
}
else if(mch == 11)
{
int firstvalue, secondvalue;
int * mypointer;
system("CLS");
cout<<" POINTER"<<endl<<endl;
mypointer = &firstvalue;
*mypointer = 10;
mypointer = &secondvalue;
*mypointer = 20;
cout << " First value is " << firstvalue << '\n';
cout << " Second value is " << secondvalue << '\n';
system("pause");
}
else if(mch == 12)
{
system("CLS");
cout<<" ENUM ( COUNT OF DAY )"<<endl<<endl;
week today;
today = Tuesday;
cout << "Day " << today+1;
}
else if(mch == 13)
{
system("CLS");
cout<<" TYPEDEF PROGRAM "<<endl<<endl;
SmallNumber temperature = -248;
Positive height = 1048;
PDouble distance = new double(390.82);
cout << " Temperature: " << temperature << endl;
cout << " Height: " << height << endl;
cout << " Distance: " << *distance << endl;
cout << endl;
}
else if(mch == 14)
{
Person p1;
system("CLS");
cout<<" UNION PROGRAM "<<endl<<endl;
cout << "Enter Full name: ";
cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;
}
else if(mch == 15)
{
Person p1;
system("CLS");
cout<<" STRUCT PROGRAM "<<endl<<endl;
cout << "Enter Full name: ";
cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;
}
else if(mch == 16)
{
system("CLS");
cout<<" LINKED LIST PROGRAM "<<endl<<endl;
struct Node *newHead;
struct Node *head = new Node;
initNode(head,10);
display(head);
addNode(head,20);
display(head);
addNode(head,30);
display(head);
addNode(head,35);
display(head);
addNode(head,40);
display(head);
insertFront(&head,5);
display(head);
int numDel = 5;
Node *ptrDelete = searchNode(head,numDel);
if(deleteNode(&head,ptrDelete))
cout << "Node "<< numDel << " deleted!\n";
display(head);
cout << "The list is reversed\n";
reverse(&head);
display(head);
cout << "The list is copied\n";
copyLinkedList(head,&newHead);
display(newHead);
cout << "Comparing the two lists...\n";
cout << "Are the two lists same?\n";
if(compareLinkedList(head,newHead))
cout << "Yes, they are same!\n";
else
cout << "No, they are different!\n";
cout << endl;
numDel = 35;
ptrDelete = searchNode(newHead,numDel);
if(deleteNode(&newHead,ptrDelete)) {
cout << "Node "<< numDel << " deleted!\n";
cout << "The new list after the delete is\n";
display(newHead);
}
cout << "Comparing the two lists again...\n";
cout << "Are the two lists same?\n";
if(compareLinkedList(head,newHead))
cout << "Yes, they are same!\n";
else
cout << "No, they are different!\n";
cout << endl;
cout << "Deleting the copied list\n";
deleteLinkedList(&newHead);
display(newHead);
}
else if(mch == 17)
{
exit(0);
}
else
{
system("CLS");
cout<<" Choice is to 1-17 only!";
}
//prompt user
char ch = 'n' || ch == 'N';
cout <<endl<<endl<<" Do you still want to continue [Y | N]: ";
cin >> ch;
cout <<endl;
if (ch == 'Y' || ch == 'y')
continue;
else
exit(0);
}
return 0;
}
void mergesort(int low, int high){
int mid;
if(low<high){
mid=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1, high);
merge(low, mid, high);
}
}
void merge(int low, int mid, int high) {
int a, b, c, d;
int newarray[10];
a=low;
b=low;
c=mid+1;
while((a<=mid)&&(c<=high)) {
if(array[a]<=array[c]) {
newarray[b]=array[a];
a++;
}
else{
newarray[b]=array[c];
c++;
}
b++;
}
if(a>mid){
for(d=c;d<=high;d++){
newarray[b]=array[d];
b++;
}
}
else{
for(d=a; d<=mid;d++) {
newarray[b]=array[d];
b++;
}
}
for(d=low;d<=high;d++) {
array[d]=newarray[d];
}
}
void recursiveFunction(int a){
int first = 0, second = 1, next, x;
for(x = 0; x<a; x++)
{
if(x <= 1){
next = x;
}
else{
next = first + second;
first = second;
second = next;
}
cout<<" "<<next;
}
return;
}

Вам также может понравиться