PPL Lab File

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Experiment-1

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;

case 2:printf("Enter first string (max length - 50) : ");gets(s1);


printf("Enter second string (max length - 50) : ");gets(s2);
printf("After concatenation, Result = %s",strcat(s1,s2));break;

case 3:printf("Enter first string (max length - 50) : ");gets(s1);


printf("Enter second string (max length - 50) : ");gets(s2);
printf("After copying second string into first:-");
strcpy(s1,s2);
printf("\nFirst String = %s",s1);
printf("\nSecond String = %s",s2);break;

case 4:printf("Enter first string (max length - 50) : ");gets(s1);


printf("Enter second string (max length - 50) : ");gets(s2);
if (strcmp(s1,s2)==0){
printf("\nBoth strings are equal");}
else{
printf("\nBoth strings are not equal");}
break;

case 5:printf("Enter a string (max length - 50) : ");gets(s1);


char s[50];
strcpy(s,s1);
printf("Reverse of string '%s' is %s",s,strrev(s1));break;

default:printf("\n Wrong choice");break;


}
printf("\nDo you want to continue ? (y/n) : ");
scanf("%c",&c);
}while((c=='y')||(c=='Y'));
return 0;
}
Outputs
Experiment-2

Aim:- Write a program in C to reverse a linked list iterative and recursive.

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;

case 2:printf("\nReversed linked list is : ");


reverselinkedlist(head);
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

Aim:- Write a program in C to implement iterative Towers of Hanoi.

Code:-
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>

struct Stack{
unsigned capacity;
int top;
int *array;
};

struct Stack* createStack(unsigned capacity){


struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
stack -> capacity = capacity;
stack -> top = -1;
stack -> array = (int*) malloc(stack -> capacity * sizeof(int));
return stack;
}

int isFull(struct Stack* stack){


return (stack->top == stack->capacity - 1);
}

int isEmpty(struct Stack* stack){


return (stack->top == -1);
}

void push(struct Stack *stack, int item){


if (isFull(stack))
return;
stack -> array[++stack -> top] = item;
}

// Function to remove an item from stack. It


// decreases top by 1
int pop(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
return stack -> array[stack -> top--];
}

//Function to show the movement of disks


void moveDisk(char fromPeg, char toPeg, int disk)
{
printf("Move the disk %d from \'%c\' to \'%c\'\n",disk, fromPeg, toPeg);
}

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;
}

Time add(Time t1){


Time temp;
temp.ss = ss + t1.ss;
temp.mm = mm + t1.mm;
temp.hh = hh + t1.hh;
if(temp.ss >= 60){
temp.mm += temp.ss / 60;
temp.ss = temp.ss % 60;
}
if(temp.mm >= 60){
temp.hh += temp.mm / 60;
temp.mm = temp.mm % 60;
}
return temp;
}
void display(){
cout<<"HH:MM:SS = "<<hh<<":"<<mm<<":"<<ss<<endl;
}
};

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 {

// server is listening on port 1234


server = new ServerSocket(1234);
server.setReuseAddress(true);

// running infinite loop for getting


// client request
while (true) {

// socket object to receive incoming client


// requests
Socket client = server.accept();

// Displaying that new client is connected


// to server
System.out.println("New client connected"
+ client.getInetAddress()
.getHostAddress());

// create a new thread object


ClientHandler clientSock
= new ClientHandler(client);

// This thread will handle the client


// separately
new Thread(clientSock).start();
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (server != null) {
try {
server.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}

// ClientHandler class
private static class ClientHandler implements Runnable {
private final Socket clientSocket;

// Constructor
public ClientHandler(Socket socket)
{
this.clientSocket = socket;
}

public void run()


{
PrintWriter out = null;
BufferedReader in = null;
try {

// get the outputstream of client


out = new PrintWriter(
clientSocket.getOutputStream(), true);

// get the inputstream of client


in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));

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

try (Socket socket = new Socket("localhost", 1234)) {

// writing to server
PrintWriter out = new PrintWriter(
socket.getOutputStream(), true);
// reading from server
BufferedReader in
= new BufferedReader(new InputStreamReader(
socket.getInputStream()));

// object of scanner class

Scanner sc = new Scanner(System.in);


String line = null;

while (!"exit".equalsIgnoreCase(line)) {

// reading from user


System.out.println("\nEnter Numbers: ");
String a1,a2;
a1=sc.nextLine();
a2=sc.nextLine();
System.out.println("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division");
String ch;
System.out.println("\nEnter Choice: ");
ch=sc.nextLine();

line=ch+' '+a1+' '+a2;

// sending the user input to server


out.println(line);
out.flush();

// displaying server reply


System.out.println("Server replied Result is: "
+ in.readLine());
}

// closing the scanner object


sc.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Output:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy