Nothing Special   »   [go: up one dir, main page]

DSL Pro

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

Program Code:

def average(listofmarks):
sum=0
count=0
for i in range(len(listofmarks)):
if listofmarks[i]!=-999:
sum+=listofmarks[i]
count+=1
avg=sum/count
print("Total Marks : ", sum)
print("Average Marks : {:.2f}".format(avg))

def Maximum(listofmarks):
for i in range(len(listofmarks)):
if listofmarks[i]!=-999:
Max=listofmarks[0]
break
for i in range(1,len(listofmarks)):
if listofmarks[i]>Max:
Max=listofmarks[i]
return(Max)

def Minimum(listofmarks):
for i in range(len(listofmarks)):
if listofmarks[i]!=-999:
Min=listofmarks[0]
break
for i in range(1,len(listofmarks)):
if listofmarks[i]<Min:
Min=listofmarks[i]
return(Min)

def absentcount(listofmarks):
count=0
for i in range(len(listofmarks)):
if listofmarks[i]==-999:
count+=1
return(count)

def maxFrequency(listofmarks):
i=0
Max=0
print("Marks | Frequency")
for j in listofmarks:
if (listofmarks.index(j)==i):
print(j," | ",listofmarks.count(j))
if listofmarks.count(j)>Max:
Max=listofmarks.count(j)
mark=j
i=i+1
return(mark,Max)

marksinFDS=[]
numberofstudents=int(input("Enter total number of students : "))
for i in range(numberofstudents):
marks=int(input("Enter marks of student "+str(i+1)+" : "))
marksinFDS.append(marks)

flag=1
while flag==1:
print("\n\n--------------------MENU--------------------\n")
print("1. Total and Average Marks of the Class")
print("2. Highest and Lowest Marks in the Class")
print("3. Number of Students absent for the test")
print("4. Marks with Highest Frequency")
print("5. Exit\n")
ch=int(input("Enter your Choice (from 1 to 5) :"))

if ch==1:
average(marksinFDS)
a = input("Do you want to continue (yes/no) :")
if a == "yes":
flag = 1
else:
flag = 0
print("Thanks for using this program!")

elif ch==2:
print("Highest Score in Class : ", Maximum(marksinFDS))
print("Lowest Score in Class : ", Minimum(marksinFDS))
a = input("Do you want to continue (yes/no) :")
if a == "yes":
flag = 1
else:
flag = 0
print("Thanks for using this program!")

elif ch==3:
print("Number of Students absent in the test : ", absentcount(marksinFDS))
a = input("Do you want to continue (yes/no) :")
if a == "yes":
flag = 1
else:
flag = 0
print("Thanks for using this program!")

elif ch==4:
mark,fr = maxFrequency(marksinFDS)
print("Highest frequency is of marks {0} that is {1} ".format(mark,fr))
a = input("Do you want to continue (yes/no) :")
if a == "yes":
flag = 1
else:
flag = 0
print("Thanks for using this program!")

elif ch==5:
flag=0
print("Thanks for using this program!")

else:
print("!!Wrong Choice!! ")
a=input("Do you want to continue (yes/no) :")
if a=="yes":
flag=1
else:
flag=0
print("Thanks for using this program!")
Program Code:

def LongestWord():
str = []
n = int(input("How many words are there in the list?"))
for word in range(0,n):
element = input("Enter the words: ")
str.append(element)

max_len = len(str[0])
temp = str[0]
for word in str:
if(len(word)>max_len):
max_len = len(word)
temp = word
print("The word with maximum length is: ",temp)
print ("its length = ",max_len)

def Frequency():
str = input("Enter some string: ")
dict = {}
for i in str:
keys = dict.keys()
if i in keys:
dict[i] += 1
else:
dict[i] = 1
print(dict)
def Palindrome():
str = input("\n Enter some string: ")
if(str == str[::-1]):
print("\n The given string is palindrome")
else:
print("\n The given string is not palindrome")

def Find_Substr():
str = input("\n Enter some statement: ")
word = input("\n Enter the substring to be searched: ")
for i in range(len(str) - len(word)+1):
if (str[i:i+len(word)] == word):
return i
return 'Not Found'

def OccurWords():
str = input("\n Enter some statement: ")
counts = dict()
words = str.split()
for word in words:
if word in counts:
counts[word]+= 1
else:
counts[word] = 1
print(counts)

print("\n Program for String operations")


while(True):
print("\n 1. To display word with the longest length")
print("\n 2. To determine the frequency of occurrence of particular character in the string")
print("\n 3. To check whether given string is palindrome or not")
print("\n 4. To display index of first appearance of the substring")
print("\n 5. To count the occurrences of each word in a given string")
print("\n Enter your choice")
choice = int(input())
if(choice == 1):
LongestWord()
elif(choice == 2):
Frequency()
elif(choice == 3):
Palindrome()
elif(choice == 4):
print(Find_Substr())
elif(choice == 5):
OccurWords()
else:
print("Exitting")
break
Program Code:

def SaddlePoint(arr,n):
col=flag=0
for i in range(0,n):
minVal = arr[i][0]
for j in range(0,n):
if (minVal > arr[i][j]):
minVal = arr[i][j]
col = j
maxVal = arr[i][col]

for i in range(0,n):
for j in range(0,n):
if (maxVal < arr[i][col]):
maxVal = arr[k][col]

if (minVal == maxVal):
print ("Saddle Point Found")
flag = 1
print("Number is : %d" % maxVal)

if flag == 0:
print("No Saddle Point Found in Array")

print("\n\t Program for Saddle Point Demo")


n = int(input("Enter the order of the matrix: "))
arr = [[0 for col in range(n)] for row in range(n)]

for row in range(0,n):


for col in range(0,n):
item = int(input("Enter the element: "))
arr[row][col]= item
print("\n You have entered following matrix...")
for row in range(0,n):
for col in range(0,n):
print(arr[row][col],end = " ")
print()

SaddlePoint(arr,n)
Program Code:

def add_matrix(A,B):
result = [[A[i][j] + B[i][j] for j in range(len(A[0]))] for i in range(len(A))]

print("The Addition of Two Matrices...")


for r in result:
print(r)

def sub_matrix(A,B):
result = [[A[i][j] - B[i][j] for j in range(len(A[0]))] for i in range(len(A))]

print("The Subtraction of Two Matrices...")


for r in result:
print(r)

def transpose(A):
result = [[0 for col in range(col_num)] for row in range(row_num)]
for i in range(len(A)):
for j in range(len(A[0])):
result[j][i] = A[i][j]

print("Transposed Matrix is ...")


for r in result:
print(r)

def Mul_matrix(A,B):
result = [[0 for col in range(col_num)] for row in range(row_num)]
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]

print("Matrix Multiplication is ...")


for r in result:
print(r)

row_num = int(input("Input number of rows: "))


col_num = int(input("Input number of columns: "))
A = [[0 for col in range(col_num)] for row in range(row_num)]
for row in range(row_num):
for col in range(col_num):
item = int(input("Enter the elements in first matrix: "))
A[row][col]= item

print("The first matrix is...")


print(A)

B = [[0 for col in range(col_num)] for row in range(row_num)]


for row in range(row_num):
for col in range(col_num):
item = int(input("Enter the elements in second matrix: "))
B[row][col]= item

print("The second matrix is...")


print(B)

print("\n Program for Matrix Operations")


while(True):
print("\n 1. Addition of Two Matrices")
print("\n 2. Subtraction of Two Matrices")
print("\n 3. Multiplication of Two Matrices")
print("\n 4. Transpose of Matrix")
print("\n 5. Exit ")
print("\n Enter your choice: ")
choice = int(input())
if(choice == 1):
add_matrix(A,B)
elif(choice == 2):
sub_matrix(A,B)
elif(choice == 3):
Mul_matrix(A,B)
elif(choice == 4):
transpose(A)
else:
print("\n Exitting")
break
Program Code:

def Selection_Sort(marks):
for i in range(len(marks)):
min_idx = i
for j in range(i + 1, len(marks)):
if marks[min_idx] > marks[j]:
min_idx = j
marks[i], marks[min_idx] = marks[min_idx], marks[i]

print("Marks of students after performing Selection Sort on the list : ")


for i in range(len(marks)):
print(marks[i])

def Bubble_Sort(marks):
n = len(marks)
for i in range(n - 1):
for j in range(0, n - i - 1):

if marks[j] > marks[j + 1]:


marks[j], marks[j + 1] = marks[j + 1], marks[j]

print("Marks of students after performing Bubble Sort on the list :")


for i in range(len(marks)):
print(marks[i])

def top_five_marks(marks):
print("Top",len(marks),"Marks are : ")
print(*marks[::-1], sep="\n")

marks=[]
n = int(input("Enter number of students whose marks are to be displayed : "))
print("Enter marks for",n,"students (Press ENTER after every students marks): ")
for i in range(0, n):
ele = int(input())
marks.append(ele)

print("The marks of",n,"students are : ")


print(marks)

flag=1;
while flag==1:
print("\n---------------MENU---------------")
print("1. Selection Sort of the marks")
print("2. Bubble Sort of the marks")
print("3. Exit")
ch=int(input("\n\nEnter your choice (from 1 to 3) : "))

if ch==1:
Selection_Sort(marks)
a=input("\nDo you want to display top marks from the list (yes/no) : ")
if a=='yes':
top_five_marks(marks)
else:
print("\nThanks for using this program!")
flag=0

elif ch==2:
Bubble_Sort(marks)
a = input("\nDo you want to display top five marks from the list (yes/no) : ")
if a == 'yes':
top_five_marks(marks)
else:
print("\nThanks for using this program!")
flag = 0

elif ch==3:
print("\nThanks for using this program!!")
flag=0

else:
print("\nEnter a valid choice!!")
print("\nThanks for using this program!!")
flag=0
Program Code:

def InsertSort(arr,n):
i=1
for i in range(n):
temp = arr[i]
j = i-1
while((j>=0) & (arr[j]>temp)):
arr[j+1] = arr[j]
j = j-1
arr[j+1] = temp

print(arr)
print("Top Five Scores are...")
for i in range (len(arr)-1,1,-1):
print(arr[i])

def ShellSort(arr,n):
d = n//2
while d > 0:
for i in range(d,n):
temp = arr[i]
j=i
while(j >= d and arr[j-d] >temp):
arr[j] = arr[j-d]
j -= d

arr[j] = temp
d = d//2

print(arr)
print("Top Five Scores are...")
for i in range (len(arr)-1,1,-1):
print(arr[i])

print("\nHow many students are there?")


n = int(input())
array = []
i=0
for i in range(n):
print("\n Enter percentage marks")
item = float(input())
array.append(item)

print("You have entered following scores...\n")


print(array)
while(True):
print("Main Menu")
print("\n 1. Insertion Sort")
print("\n 2. Shell Sort")
print("\n 3. Exit")
print("\n Enter your Choice: ")
choice = int(input())
if(choice == 1):
print("\n The sorted Scores are...")
InsertSort(array,n)
elif(choice ==2):
print("\n The sorted Scores are...")
ShellSort(array,n)
else:
print("Exitting")
break
Program Code:

def Quick(arr,low,high):
if(low<high):
m=Partition(arr,low,high)
Quick(arr,low,m-1)
Quick(arr,m+1,high)

def Partition(arr,low,high):
pivot = arr[low]
i=low+1
j=high
flag = False
while(not flag):
while(i<=j and arr[i]<=pivot):
i=i+1
while(i<=j and arr[j]>=pivot):
j=j-1

if(j<i):
flag = True
else:
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp

temp = arr[low]
arr[low] = arr[j]
arr[j] = temp
return j

print("\nHow many students are there?")


n = int(input())
array = []
i=0
for i in range(n):
print("\n Enter percentage marks")
item = float(input())
array.append(item)

print("You have entered following scores...\n")


print(array)

print("\n The sorted Scores are...")


Quick(array,0,n-1)
print(array)
print("Top Five Scores are...")
for i in range (len(array)-1,1,-1):
print(array[i])
Program Code:

#include<iostream>
#include<string.h>
using namespace std;
struct node
{ int prn,rollno;
char name[50];
struct node *next;
};
class info
{ node
*s=NULL,*head1=NULL,*temp1=NULL,*head2=NULL,*temp2=NULL,*head=NULL,*temp=NULL;
int b,c,i,j,ct;
char a[20];
public:
node *create();
void insertp();
void insertm();
void delm();
void delp();
void dels();
void display();
void count();
void reverse();
void rev(node *p);
void concat();
} ;
node *info::create()
{ node *p=new(struct node);
cout<<"enter name of student ";
cin>>a;
strcpy(p->name,a);
cout<<"\n enter prn no. of student \n";
cin>>b;
p->prn=b;
cout<<"enter student rollno";
cin>>c;
p->rollno=c;
p->next=NULL;
return p;
}
void info::insertm()
{
node *p=create();
if(head==NULL)
{ head=p;
}
else
{ temp=head;
while(temp->next!=NULL)
{ temp=temp->next; }
temp->next=p;
}
}
void info::insertp()
{
node *p=create();

if(head==NULL)
{ head=p;
}
else
{ temp=head;
head=p;
head->next=temp->next;
}
}

void info::display()
{ if(head==NULL)
{ cout<<"linklist is empty";
}
else
{
temp=head;
cout<<" prn rolln0 NAME \n";
while(temp->next!=NULL)
{ cout<<" \n"<<temp->prn<<" "<<temp->rollno<<" "<<temp->name;
temp=temp->next;
}
cout<<" "<<temp->prn<<" "<<temp->rollno<<" "<<temp->name;
}
}
void info::delm()
{ int m,f=0;
cout<<"\n enter the prn no. of student whose data you want to delete";
cin>>m;
temp=head;
while(temp->next!=NULL)
{
if(temp->prn==m)
{ s->next=temp->next;
delete(temp); f=1;
}
s=temp;
temp=temp->next;
} if(f==0)
{ cout<<"\n sorry memeber not deleted "; }
}
void info::delp()
{ temp=head;
head=head->next;
delete(temp);
}
void info::dels()
{
temp=head;
while(temp->next!=NULL)
{ s=temp;
temp=temp->next;
} s->next=NULL;
delete(temp);
}
void info::count()
{ temp=head; ct=0;
while(temp->next!=NULL)
{ temp=temp->next; ct++; }
ct++;
cout<<" Count of members is:"<<ct;
}
void info::reverse()
{ rev(head); }
void info::rev(node *temp)
{ if(temp==NULL)
{ return; }
else
{ rev(temp->next); }
cout<<" "<<temp->prn<<" "<<temp->rollno<<" "<<temp->name;
}

void info::concat()
{ int k,j;
cout<<"enter no. of members in list1";
cin>>k;
head=NULL;
for(i=0;i<k;i++)
{ insertm();
head1=head;

} head=NULL;
cout<<"enter no. of members in list2";
cin>>j;
for(i=0;i<j;i++)
{ insertm();
head2=head;
} head=NULL;

temp1=head1;
while(temp1->next!=NULL)
{ temp1=temp1->next; }
temp1->next=head2;
temp2=head1;
cout<<" prn rolln0 NAME \n";
while(temp2->next!=NULL)
{
cout<<"\n "<<temp2->prn<<" "<<temp2->rollno<<" "<<temp2->name<<"\n";;
temp2=temp2->next;
}
cout<<"\n "<<temp2->prn<<" "<<temp2->rollno<<" "<<temp2->name<<"\n";
}
int main()
{ info s;
int i;

char ch;
do{
cout<<"\n choice the options";
cout<<"\n 1. To insert president ";
cout<<"\n 2. To insert member ";
cout<<"\n 3. To insert secretary ";
cout<<"\n 4. To delete president ";
cout<<"\n 5. To delete member ";
cout<<"\n 6. To delete secretary ";
cout<<"\n 7. To display data ";
cout<<"\n 8. Count of members";
cout<<"\n 9. To display reverse of string ";
cout<<"\n 10.To concatenate two strings ";
cin>>i;
switch(i)
{ case 1: s.insertp();
break;
case 2: s.insertm();
break;
case 3: s.insertm();
break;
case 4: s.delp();
break;
case 5: s.delm();
break;
case 6: s.dels();
break;
case 7: s.display();
break;
case 8: s.count();
break;
case 9: s.reverse();
break;
case 10: s.concat();

break;
default: cout<<"\n unknown choice";
}
cout<<"\n do you want to continue enter y/Y \n";
cin>>ch;

}while(ch=='y'||ch=='Y');

return 0;
}
Program Code:

#include<iostream>
using namespace std;
struct node{
int seatc,seatr;
string status;
struct node *next ,*prev;
}*head[10],*last[10];

class ticket{
public:
ticket(){
for(int j=0 ; j<10 ; j++){
head[j]=last[j]=NULL;
struct node* temp;
for(int i=1 ; i<=7 ; i++){
temp=create_node(i,j+1);
if(head[j]==last[j] && head[j]==NULL){
head[j]=last[j]=temp;
head[j]->next=last[j]->next=NULL;
head[j]->prev=last[j]->prev=NULL;

}
else{
temp->next=head[j];
head[j]->prev=temp;
head[j]=temp;
head[j]->prev=last[j];
last[j]->next=head[j];
}
}
}
}

node* create_node(int x,int y){


struct node*temp;
temp=new(struct node);
if(temp==NULL){
cout<<"\nMemory not allocated";
return 0;
}
else{
temp->seatc=x;
temp->seatr=y;
temp->status="A";
temp->next=NULL;
temp->prev=NULL;
return temp;
}
}
void book(){
int x,y;
cout<<"\nEnter row and column";
cin>>x>>y;
struct node* temp;
temp=head[x-1];
for(int i=0 ; i<7 ; i++){
if(temp->seatc==y){
if(temp->status=="A"){
temp->status="B";
}
else{
cout<<"\nSORRY !! Already booked!!";
}
}
temp=temp->next;
}
display();
}

void cancel(){
int x,y;
cout<<"\nEnter row and column to cancel booking : ";
cin>>x>>y;
struct node* temp;
temp=head[x-1];
for(int i=0 ; i<7 ; i++){
if(temp->seatc==y){
if(temp->status=="B"){
temp->status="A";
}
else{
cout<<"\nSORRY !! Already unbooked!!";
}
}
temp=temp->next;
}
display();
}

void display(){
struct node* temp;
for(int j=0 ; j<10 ; j++){
temp=head[j];
for(int i=0 ; i<7 ; i++){
cout<<temp->seatr<<","<<temp->seatc;
cout<<""<<temp->status<<"\t";
temp=temp->next;
}
cout<<"\n";
}
}
};

int main(){
ticket t;
int ch;
t.display();
do{
cout<<"\n1.Book Ticket \n2.Cancel Booking \n3.EXIT";
cin>>ch;
switch(ch){
case 1:t.book();break;
case 2:t.cancel();break;
}
}while(ch!=3);

return 0;
}
Program Code:

#include<iostream>
using namespace std;
int size;
struct SLL_Node
{
int start;
int end;
int min;
int max;
int flag;
struct SLL_Node *next;
}*head;
class App_Shedule
{
public:
void create_Shed();
void display_Shed();
void book_App();
void cancel_App();
void sort_App();
}A1;
int main()
{
int ch;
char ans;
do
{
cout<<"\n\n *** Menu ***";
cout<<"\n 1. Create Appointment Schedule";
cout<<"\n 2. Display Free Slots";
cout<<"\n 3. Book an Appointment";
cout<<"\n 4. Cancel an Appointment";
cout<<"\n 5. Sort slots based on Time";
cout<<"\n\n\t Enter your choice: ";
cin>>ch;

switch(ch)
{
case 1: A1.create_Shed();
break;
case 2: A1.display_Shed();
break;

case 3: A1.book_App();
break;

case 4: A1.cancel_App();
break;

case 5: A1.sort_App();
break;
default: cout<<"\n\t Wrong choice!!!";
}
] cout<<"\n\n\t Do you wanna continue? (y/n) : ";
cin>>ans;
}while(ans == 'y');
}
void App_Shedule :: create_Shed()
{
int i;
struct SLL_Node *temp, *last;
head = NULL;
cout<<"\n\n\t How many Appointment Slots: ";
cin>>size;

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


{
temp = new(struct SLL_Node);
cout<<"\n\n\t Enter Start Time: ";
cin>>temp->start;
cout<<"\n\t Enter End Time: ";
cin>>temp->end;
cout<<"\n\n\t Enter Minimum Duration: ";
cin>>temp->min;
cout<<"\n\t Enter Maximum Duration: ";
cin>>temp->max;
temp->flag = 0;
temp->next = NULL;
if(head == NULL)
{
head = temp;
last = head;
}
else
{
last->next = temp;
last = last->next;
}
}
}
void App_Shedule :: display_Shed()
{
int cnt = 1;
struct SLL_Node *temp;
cout<<"\n\n\t ****Appointment Schdule****";
cout<<"\n\n\t Srno.\tStart\tEnd\tMin_Dur\tMax_Dur\tStatus";
temp = head;
while(temp != NULL)
{
cout<<"\n\n\t "<<cnt;
cout<<"\t "<<temp->start;
cout<<"\t "<<temp->end;
cout<<"\t "<<temp->min;
cout<<"\t "<<temp->max;
if(temp->flag)
cout<<"\t-Booked-";
else
cout<<"\t--Free--";
temp = temp->next;
cnt++;
}
}
void App_Shedule :: book_App()
{
int start;
struct SLL_Node *temp;
cout<<"\n\n\t Please enter Appointment time: ";
cin>>start;
temp = head;
while(temp != NULL)
{
if(start == temp->start)
{
if(temp->flag == 0)
{
cout<<"\n\n\t Appointment Slot is Booked!!!";
temp->flag = 1;
}
else
cout<<"\n\n\t Appointment Slot is not Available!!!";
}
temp = temp->next;
}
}

void App_Shedule :: cancel_App()


{
int start;
struct SLL_Node *temp;
cout<<"\n\n\t Please enter Appointment time to Cancel: ";
cin>>start;
temp = head;
while(temp != NULL)
{
if(start == temp->start)
{
if(temp->flag == 1)
{
cout<<"\n\n\t Your Appointment Slot is Canceled!!!";
temp->flag = 0;
}
else
cout<<"\n\n\t Your Appointment was not Booked!!!";
}
temp = temp->next;
}
}
void App_Shedule :: sort_App()
{
int i,j,val;
struct SLL_Node *temp;
for(i=0; i < size-1; i++)
{
temp = head;
while(temp->next != NULL)
{
if(temp->start > temp->next->start)
{
val = temp->start;
temp->start = temp->next->start;
temp->next->start = val;

val = temp->end;
temp->end = temp->next->end;
temp->next->end = val;

val = temp->min;
temp->min = temp->next->min;
temp->next->min = val;

val = temp->max;
temp->max = temp->next->max;
temp->next->max = val;

}
temp = temp->next;
}
}
cout<<"\n\n\t The Appointments got Sorted!!!";
}
Program Code:

#include <iostream>
using namespace std;
#define size 10

class stackexp
{
int top;
char stk[size];
public:
stackexp()
{
top=-1;
}
void push(char);
char pop();
int isfull();
int isempty();
};

void stackexp::push(char x)
{
top=top+1;
stk[top]=x;
}

char stackexp::pop()
{
char s;
s=stk[top];
top=top-1;
return s;
}

int stackexp::isfull()
{
if(top==size)
return 1;
else
return 0;
}

int stackexp::isempty()
{
if(top==-1)
return 1;
else
return 0;
}

int main()
{
stackexp s1;
char exp[20],ch;
int i=0;
cout << "\n\t!! Parenthesis Checker..!!!!" << endl;
cout<<"\nEnter the expression to check whether it is in well form or not : ";
cin>>exp;
if((exp[0]==')')||(exp[0]==']')||(exp[0]=='}'))
{
cout<<"\n Invalid Expression.....\n";
return 0;
}
else
{
while(exp[i]!='\0')
{
ch=exp[i];
switch(ch)
{
case '(':s1.push(ch);break;
case '[':s1.push(ch);break;
case '{':s1.push(ch);break;
case ')':s1.pop();break;
case ']':s1.pop();break;
case '}':s1.pop();break;
}
i=i+1;
}
}
if(s1.isempty())
{
cout<<"\nExpression is well parenthesised...\n";
}
else
{
cout<<"\nSorry !!! Invalid Expression or not in well parenthesized....\n";
}
return 0;
}
Program Code:

#include<iostream>
#include<conio.h>
using namespace std;
class stack
{
public:
char stack_array[50];
int top;
stack()
{
top=-1;
}
void push(char symbol)
{ if(full())
cout<<"\nStack overflow:\n";
else
{ top=top+1;
stack_array[top]=symbol;
}
}
char pop()
{ if(empty())
return('#');
else
return(stack_array[top--]);
}
int empty()
{ if(top==-1)
return(1);
else
return(0);
}
int full()
{ if(top==49)
return(1);
else
return(0);
}
private:
char infix[50];
char postfix[50];
public:
void read()
{
cout<<"\nEnter an infix expression:";
cin>>infix;
}
int white_space(char symbol)
{ if(symbol==' ' || symbol=='\t' || symbol=='\0')
return 1;
else
return 0;
}
void ConvertToPostfix()
{ int prev,p;
char entry;
p=0;
for(int i=0;infix[i]!='\0';i++)
{
if(!white_space(infix[i]))
{ switch(infix[i])
{
case '(': push(infix[i]);
break;
case ')': while((entry=pop())!='(')
postfix[p++]=entry;
break;
case '+':
case '-':
case '*':
case '/':
if(!empty())
{ prev=prior(infix[i]);
entry=pop();
while(prev<=prior(entry))
{ postfix[p++]=entry;
if(!empty())
entry=pop();
else
break;
}
if(prev>prior(entry))
push(entry);
}
push(infix[i]);
break;
default:
postfix[p++]=infix[i];
break;
}
}
}
while(!empty())
postfix[p++]=pop();
postfix[p]='\0';
cout<<"\nThe postfix expression is: "<<postfix<<endl;
}
int prior(char symbol)
{ switch(symbol)
{ case '/': return(4);
case '*': return(3);
case '+': return(2);
case '-': return(1);
case '(': return(0);
default: return(-1);
}
}
};
int main()
{ char choice='y';
stack expr;
while(choice=='y')
{expr.read();
expr.ConvertToPostfix();
cout<<"\n\nDo you want to continue ? (y/n): ";
cin>>choice;
}
return 0;
}
Program Code:

#include <iostream>
#define MAX 10
using namespace std;
struct queue
{ int data[MAX];
int front,rear;
};
class Queue
{ struct queue q;
public:
Queue(){q.front=q.rear=-1;}
int isempty();
int isfull();
void enqueue(int);
int delqueue();
void display();
};
int Queue::isempty()
{
return(q.front==q.rear)?1:0;
}
int Queue::isfull()
{ return(q.rear==MAX-1)?1:0;}
void Queue::enqueue(int x)
{q.data[++q.rear]=x;}
int Queue::delqueue()
{return q.data[++q.front];}
void Queue::display()
{ int i;
cout<<"\n";
for(i=q.front+1;i<=q.rear;i++)
cout<<q.data[i]<<" ";
}
int main()
{ Queue obj;
int ch,x;
do{ cout<<"\n 1.Insert Job\n 2.Delete Job\n 3.Display\n 4.Exit\n Enter your choice : ";
cin>>ch;
switch(ch)
{ case 1: if (!obj.isfull())
{ cout<<"\n Enter data : \n";
cin>>x;
obj.enqueue(x);
cout<<endl;
}
else
cout<< "Queue is overflow!!!\n\n";
break;
case 2: if(!obj.isempty())
cout<<"\n Deleted Element = "<<obj.delqueue()<<endl;
else
{ cout<<"\n Queue is underflow!!!\n\n"; }
cout<<"\nRemaining Jobs : \n";
obj.display();
break;
case 3: if (!obj.isempty())
{ cout<<"\n Queue contains : \n";
obj.display();
}
else
cout<<"\n Queue is empty!!!\n\n";
break;
case 4: cout<<"\n Exiting Program.....";
}
}while(ch!=4);
return 0;
}
Program Code:

#include<iostream>
using namespace std;
#define SIZE 5
class dequeue
{

int a[10],front,rear,count;

public:
dequeue();
void add_at_beg(int);
void add_at_end(int);
void delete_fr_front();
void delete_fr_rear();
void display();
};

dequeue::dequeue()
{
front=-1;
rear=-1;
count=0;
}

void dequeue::add_at_beg(int item)


{
int i;
if(front==-1)
{
front++;
rear++;
a[rear]=item;
count++;
}
else if(rear>=SIZE-1)
{
cout<<"\nInsertion is not possible,overflow!!!!";
}
else
{
for(i=count;i>=0;i--)
{
a[i]=a[i-1];
}
a[i]=item;
count++;
rear++;
}
}

void dequeue::add_at_end(int item)


{
if(front==-1)
{
front++;
rear++;
a[rear]=item;
count++;
}
else if(rear>=SIZE-1)
{
cout<<"\nInsertion is not possible,overflow!!!";
return;
}
else
{
a[++rear]=item;
}
}

void dequeue::display()
{

for(int i=front;i<=rear;i++)
{
cout<<a[i]<<" "; }
}

void dequeue::delete_fr_front()
{
if(front==-1)
{
cout<<"Deletion is not possible:: Dequeue is empty";
return;
}
else
{
if(front==rear)
{
front=rear=-1;
return;
}
cout<<"The deleted element is "<<a[front];
front=front+1;
}
}

void dequeue::delete_fr_rear()
{
if(front==-1)
{
cout<<"Deletion is not possible:Dequeue is empty";
return;
}
else
{
if(front==rear)
{
front=rear=-1;
}
cout<<"The deleted element is "<< a[rear];
rear=rear-1;
}
}

int main()
{
int c,item;
dequeue d1;
do
{
cout<<"\n\n****DEQUEUE OPERATION****\n";
cout<<"\n1-Insert at beginning";
cout<<"\n2-Insert at end";
cout<<"\n3_Display";
cout<<"\n4_Deletion from front";
cout<<"\n5-Deletion from rear";
cout<<"\n6_Exit";
cout<<"\nEnter your choice<1-4>:";
cin>>c;
switch(c)
{
case 1:
cout<<"Enter the element to be inserted:";
cin>>item;
d1.add_at_beg(item);
break;

case 2:
cout<<"Enter the element to be inserted:";
cin>>item;
d1.add_at_end(item);
break;

case 3:
d1.display();
break;

case 4:
d1.delete_fr_front();
break;
case 5:
d1.delete_fr_rear();
break;

case 6:
exit(1);
break;

default:
cout<<"Invalid choice";
break;
}
}while(c!=7);
return 0;
}
Program Code:

#include<iostream>
#include<windows.h>
using namespace std;
const int MAX=5;

class PizzaParlour
{
int front,rear;
int orders[MAX];
public:
PizzaParlour()
{
front=rear=-1;
}
bool addOrder(int data);
void serveOrder();
void display();
};
bool PizzaParlour::addOrder(int id){
if(rear==-1)
{
front=rear=0;
orders[rear]=id;
return true;
}
else
{
int pos=(rear+1)%MAX;
if(pos==front)
{
cout<<"\nCafe is Full.Please wait.\n";
return false;
}
else
{
rear=pos;
orders[rear]=id;
return true;
}
}
}

void PizzaParlour::serveOrder()
{
if(front==-1)
{
cout<<"\n No Orders in Cafe.[Cafe is Empty)\n";
return;
}
else
{
cout<<"\n Order No. "<<orders[front]<<" is processed.\n";
if(front==rear)
{
front=rear=-1;
}
else
{
front=(front+1)%MAX;
}
}
}

void PizzaParlour::display()
{
int i=0;
if(front==-1)
{
cout<<"\nCafe is Empty.No orders.\n";
return;
}
else
{
cout<<"Order Id's: \n";
for(i=front;i!=rear;i=((i+1)%MAX))
{
cout<<orders[i]<<" ";
}
cout<<orders[rear];
}
}
void intro()
{ char name[50]={"\n Vaibhav Cafe \n"};
for(int i=0;name[i]!='\0';i++)
{
Sleep(50);
cout<<name[i];
}
}
int main()
{
int ch,id=0;
PizzaParlour q;
do
{
cout<<"\n-----------------";
intro();
cout<<"-----------------";
cout<<"\n****Menu*****\n";
cout<<"1. Accept order\n";
cout<<"2. Serve order\n";
cout<<"3. Display orders\n";
cout<<"4. Exit";
cout<<"\nChoice: ";
cin>>ch;
switch(ch)
{
case 1:
id++;
if(q.addOrder(id))
{
cout<<"Thank you for order.Order id is : "<<id;
}
else
{
id--;
}
break;

case 2: q.serveOrder();
break;

case 3: q.display();
break;
}
}while(ch!=4);
cout<<"\nThank You.Keep Visiting.";
}

You might also like