PPL Lab File
PPL Lab File
PPL Lab File
Aim: - To Implement all major functions of string.h in single C program using switch case to select specific
function from user choice (like strlen, strcat, strcpy, strcmp, strrev).
Code:-
#include <bits/stdc++.h>
int main(){
int ch;
char s1[50],s2[50],c;
do{
system("cls");
printf ("\nProgram to implement major functions of string.h");
printf ("\n^^^^^^^ ^^ ^^^^^^^^^ ^^^^^ ^^^^^^^^^ ^^ ^^^^^^^^");
printf("\n1. Strlen\n2. Strcat\n3. Strcpy\n4. Strcmp\n5. Strrev");
printf("\nEnter your choice:- ");
scanf("%d",&ch);
fflush(stdin);
switch(ch){
case 1:printf("Enter a string (max length - 50) : ");gets(s1);
printf("Length of string '%s' is %d",s1,strlen(s1));break;
Code:-
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int i;
struct node *next;
}node1;
void reverselinkedlist(node1 *head){
if(head->next==NULL)
printf("%d->",head->i);
else{
reverselinkedlist(head->next);
printf("%d->",head->i);
}
}
int main(){
char hh;
do{
char ch;
node1 *head=NULL,*tail=NULL;
do{
system("cls");
printf("Program to reverse a linked list (Iterative and Recursive)");
printf("\n^^^^^^^ ^^ ^^^^^^^ ^ ^^^^^^ ^^^^ ^^^^^^^^^^ ^^^ ^^^^^^^^^^");
printf("\nCreating linked list...");
printf("\nEnter an integer:- ");
if(head==NULL){
head = malloc(sizeof(node1));
scanf("%d",&head->i);
head->next=NULL;
tail = head;
}
else{
node1 *n1 = malloc(sizeof(node1));
scanf("%d",&n1->i);
n1->next=NULL;
tail->next=n1;
tail=n1;
}
printf("Linked list is:- ");
node1 *n2=head;
printf(" ");
while(n2!=NULL){
printf("%d->",n2->i);
n2 = n2->next;
}
printf("NULL");
printf("\nDo you want to enter more elements in the list (y/n) ? ");
fflush(stdin);
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
char ch1;
do{
int choice;
system("cls");
printf("Entered Linked List is:- ");
node1 *n2=head;
printf(" ");
while(n2!=NULL){
printf("%d->",n2->i);
n2 = n2->next;
}
printf("NULL");
printf("\n1. Reverse a linked list iteratively");
printf("\n2. Reverse a linked list recursively");
printf("\nEnter your choice - ");
scanf("%d",&choice);
switch(choice){
case 1:printf("");
node1 *n2 = head;
node1 *c,*chead=NULL;
while(n2!=NULL){
c = malloc(sizeof(node1));
c->i = n2->i;
if(chead==NULL){
c->next = NULL;
chead = c;}
else{
c->next = chead;
chead = c;}
n2 = n2->next;
}
node1 *x = chead;
printf("\nReversed linked list is : ");
while(x!=NULL){
printf("%d->",x->i);
x=x->next;
}
printf("NULL");break;
default:printf("\nWrong choice");break;
}
printf("Do you want to reverse linked list again using a different choice (y/n)? ");
fflush(stdin);
scanf("%c",&ch1);
}while((ch1=='Y')||(ch1=='y'));
printf("\nDo you want to run program again (y/n)? ");
fflush(stdin);
scanf("%c",&hh);
}while((hh=='y')||(hh=='Y'));
return 0;
}
Outputs
Experiment-3
Code:-
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
struct Stack{
unsigned capacity;
int top;
int *array;
};
void moveDisksBetweenTwoPoles(struct Stack *src, struct Stack *dest, char s, char d){
int pole1TopDisk = pop(src);
int pole2TopDisk = pop(dest);
if (pole1TopDisk == INT_MIN){
push(src, pole2TopDisk);
moveDisk(d, s, pole2TopDisk);
}
else if (pole2TopDisk == INT_MIN){
push(dest, pole1TopDisk);
moveDisk(s, d, pole1TopDisk);
}
else if (pole1TopDisk > pole2TopDisk){
push(src, pole1TopDisk);
push(src, pole2TopDisk);
moveDisk(d, s, pole2TopDisk);
}
else{
push(dest, pole2TopDisk);
push(dest, pole1TopDisk);
moveDisk(s, d, pole1TopDisk);
}
}
void tohIterative(int num_of_disks, struct Stack *src, struct Stack *aux, struct Stack *dest)
{
int i, total_num_of_moves;
char s = 'S', d = 'D', a = 'A';
if (num_of_disks % 2 == 0){
char temp = d;
d = a;
a = temp;
}
total_num_of_moves = pow(2, num_of_disks) - 1;
for (i = num_of_disks; i >= 1; i--)
push(src, i);
for (i = 1; i <= total_num_of_moves; i++){
if (i % 3 == 1)
moveDisksBetweenTwoPoles(src, dest, s, d);
else if (i % 3 == 2)
moveDisksBetweenTwoPoles(src, aux, s, a);
else if (i % 3 == 0)
moveDisksBetweenTwoPoles(aux, dest, a, d);
}
}
int main(){
unsigned num_of_disks;
system("cls");
printf("\nEnter No. of Disks:- ");
scanf("%d",&num_of_disks);
struct Stack *src, *dest, *aux;
src = createStack(num_of_disks);
aux = createStack(num_of_disks);
dest = createStack(num_of_disks);
tohIterative(num_of_disks, src, aux, dest);
return 0;
}
Output
Experiment-4
Aim:- WAP in C++ to count the numbers of object of a class with the help of static data member, funtion and
constructor.
Code:-
#include <iostream>
using namespace std;
class test{
int objNo;
static int objCnt;
public:
test(){
objNo = ++objCnt;
}
~test(){
--objCnt;
}
void printObjNumber(void){
cout << "object number :" << objNo << "\n";
}
static void printObjCount(void){
cout << "count:" << objCnt<< "\n";
}
};
int test::objCnt;
int main(){
system("cls");
test t1, t2;
test::printObjCount();
test t3;
test::printObjCount();
t1.printObjNumber();
t2.printObjNumber();
t3.printObjNumber();
return 0;
}
Output
Experiment-5
Aim:- WAP in C++ & Java to declare a class Time with data members mm for minutes, ss for seconds and hh
for hours. Define a parameterize constructor to assign time to its objects. Add two time objects using
member function and assign to third objects. Implement all possible cases of time.
Code:-
1. C++
#include <iostream>
using namespace std;
class Time{
private:
int hh;
int mm;
int ss;
public:
Time(int h =0,int m = 0, int s = 0){
hh = h;
mm = m;
ss = s;
}
int main(){
Time t3;
int hh, mm, ss;
system("cls");
cout<<"\n\nTime format --> HH:MM:SS";
cout<<"\nEnter Time 1:- "; cin>>hh>>mm>>ss;
Time t1(hh,mm,ss);
cout<<"\nEnter Time 2:- "; cin>>hh>>mm>>ss;
Time t2(hh,mm,ss);
cout<<"\nTwo times are:"<<endl;
t1.display();
t2.display();
t3 = t1.add(t2);
cout<<"\nSUM = ";
t3.display();
return 0;
}
2. JAVA
import java.util.*;
public class Time {
public int hh, mm, ss;
public Time() {}
public Time(int hh, int mm, int ss) {
this.hh = hh;
this.mm = mm;
this.ss = ss;
}
public void setTime(Time obj1, Time obj2) {
this.ss = obj1.ss + obj2.ss;
int extra = this.ss / 60;
this.ss %= 60;
this.mm = obj1.mm + obj2.mm + extra;
extra = this.mm / 60;
this.mm %= 60;
this.hh = obj1.hh + obj2.hh + extra;
this.hh %= 24;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int hh, mm, ss;
System.out.println("\n\t Time format --> HH:MM:SS");
System.out.print ("\t Enter Time 1:- ");
hh = scan.nextInt();
mm = scan.nextInt();
ss = scan.nextInt();
Time obj1 = new Time(hh, mm, ss);
System.out.print ("\t Enter Time 2:- ");
hh = scan.nextInt();
mm = scan.nextInt();
ss = scan.nextInt();
Time obj2 = new Time(hh, mm, ss);
Time obj3 = new Time();
obj3.setTime(obj1, obj2);
System.out.print ("\t Sum = ");
System.out.print (obj3.hh + ":" + obj3.mm + ":" + obj3.ss);
}
}
Experiment-6
Aim:- WAP in C++ to define a class Complex to represents set of all complex numbers. Overload ‘+’ operator
to add two complex numbers using member function of the class and overload ‘*’ operator to multiply
two complex numbers using friend function of the class complex.
Code:-
#include <iostream>
using namespace std;
class Complex{
private:
float real;
float imag;
public:
Complex(): real(0), imag(0){ }
void input(){
int a[2];
for(int i=0; i<2; i++){
cin>>a[i];
}
real = a[0]; imag = a[1];
}
Complex operator + (Complex c){
Complex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
friend Complex operator * (Complex c1, Complex c2);
void output(){
if(imag < 0)
cout << "Complex number: "<< real<< "i" << imag ;
else
cout << "Complex number: " <<real << " + i" <<imag;
}
};
Complex operator * (Complex c1, Complex c2)
{
Complex c3;
c3.real=c1.real*c2.real;
c3.imag=c1.imag*c2.imag;
return(c3);
}
int main(){
Complex b, c, result;
int ch;
cout<<"\nEnter 1st complex number:- ";b.input();
cout<<"Enter 2nd complex number:- ";c.input();
cout<<"Complex Numbers operations\n1. Addition\n2. Multiplication";
cout<<"\nEnter choice:- ";cin>>ch;
switch(ch){
case 1:result = b + c;
result.output(); break;
case 2:result = b*c;
result.output();break;
}
return 0;
}
Outputs
1. Addition
2.Multiplication
Experiment-7
Aim:- Implement simple multi-threaded server to perform all mathematical operations parallel in Java.
Code:-
Server Class:
import java.io.*;
import java.net.*;
class Server {
public static void main(String[] args)
{
ServerSocket server = null;
try {
// ClientHandler class
private static class ClientHandler implements Runnable {
private final Socket clientSocket;
// Constructor
public ClientHandler(Socket socket)
{
this.clientSocket = socket;
}
String line;
while ((line = in.readLine()) != null) {
String[] arr=line.split(" ");
int res=0,p,q;
p=Integer.parseInt(arr[1]);q=Integer.parseInt(arr[2]);
if(line.charAt(0)=='1'){
res=p+q;
}
if(line.charAt(0)=='2'){
res=p-q;
}
if(line.charAt(0)=='3'){
res=p*q;
}
if(line.charAt(0)=='4'){
res=p/q;
}
String text="choice: "+String.valueOf(line.charAt(0))+"\nAnd the Numbers are "+p+" "+q;
// writing the received message from
// client
System.out.printf(
" Sent from the client: %s\n",
text);
line=Integer.toString(res);
out.println(line);
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
clientSocket.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Client Class:
import java.io.*;
import java.net.*;
import java.util.*;
// Client class
class Client {
// driver code
public static void main(String[] args)
{
// establish a connection by providing host and port
// number
// writing to server
PrintWriter out = new PrintWriter(
socket.getOutputStream(), true);
// reading from server
BufferedReader in
= new BufferedReader(new InputStreamReader(
socket.getInputStream()));
while (!"exit".equalsIgnoreCase(line)) {