Sunday, 29 June 2014

Program For Hamming code generation,Error detection and correction:----------


#include<iostream.h>
#include<conio.h>

void main()
{
int data[7], c0, c1, c2, op,i, loc;
char a[4], b[7];
clrscr();
do
{
printf("\n 1. Hamming code generation");
printf("\n 2. Error detection and correction");\
printf("\n 3. Quit");
printf("\n 4. Enter choice :-");
scanf("%d", &op);
switch(op)
{
case 1:
printf("\n Enter a 4 bit data");
scanf("%s", a)
for(i=0; i<7;i++)
{
data[i]=0;
}
data[0]=a[0]-'0';
data[1]=a[1]-'0';
data[2]=a[2]-'0';
data[4]=a[3]-'0';
data[6]=(data[0]+data[2]+data[4])%2;
data[5]=(data[4]+data[1]+data[0])%2;
data[3]=(data[0]+data[1]+data[2])%2;
printf("\n data as encoded with hamming parity bits :\n");
printf("\n d3   d2   d1   p2   d0   p1   p0");
printf("\n");
for(i=0; i<7; i++)
{
printf("%d", data[i]);
}
break;
case 2:
printf("\nEnter a 7 bit hamming coded data :")
scanf("%s", b);
for(i=0;i<7;i++)
data[i]=b[i]-'0';
c0=(data[6]+data[0]+data[2]+data[4])%2;
c1=(data[5]+data[4]+data[1]+data[0])%2;
c2=(data[3]+data[0]+data[1]+data[2])%2;
loc=7-(4*c2+2*c1+c0);
if(loc==0)
printf("\n No Error");
else
{
printf("\nError at the location = %d", loc);
printf("\nCorrected data :");
data[loc]=(!data[loc]);
printf("\n");
for(i=0;i<7;i++)
printf("%d", data[i]);
}
break;
}
}while(op!=3);
}
Program For BOOT's Algorithm in C:--------


#include<stdio.h>
#include<conio.h>
#include<string.h>

int q1=0,n=0,count=0,Q[4],A[4],M[4],M2[4];
char b1[4],b2[4];

void input()
{
printf("\nEnter first 4-bit binary number: ");
gets(b1);
printf("\nEnter second 4-bit binary number: ");
gets(b2);
}

void initialize()
{
int a,b,i;
for(i=3;i>=0;i--)
{
a=(int)b1%10;
M[i]=a;
b1[i]/=10;
b=(int)b2%10;
Q[i]=b;
b2[i]/=10;
A[i]=0;
}
}

void add()
{
int i,a,b,c=0;
int sum[4];
for(i=3;i>=0;i--)
{
a=A[i];
b=M[i];
if((a+b+c)==0)
{
sum[i]=0;
c=0;
}
else if((a+b+c)==1)
{
sum[i]=1;
c=0;
}
else if((a+b+c)==2)
{
sum[i]=0;
c=1;
}
else if((a+b+c)==3)
{
sum[i]=1;
c=1;
}
}
for(i=0;i<4;i++)
{
A[i]=sum[i];
}
}

void twosComplement()
{
int i,b,a;
int flag=0;
for(i=0;i<n;i++)
{
if(M[i]==0) M2[i]=1;
else if(M[i]==1) M2[i]=0;
}
i=3;
while(flag==0)
{
b=M2[i];
flag=1;
if(a==1)
{
M2[i]=1;
flag=1;
break;
}
else if(a==2)
{
M2[i]=0;
i--;
}
}
}

void sub()
{
int i,a,b,c=0;
int sum[4];
twosComplement();
for(i=3;i>=0;i++)
{
a=A[i];
b=M2[i];
if((a+b+c)==0)
{
sum[i]=0;
c=0;
}
else if((a+b+c)==1)
{
sum[i]=1;
c=0;
}
else if((a+b+c)==2)
{
sum[i]=0;
c=1;
}
else if((a+b+c)==3)
{
sum[i]=1;
c=1;
}
}
for(i=0;i<n;i++) A[i]=sum[i];
}

void shift()
{
int i;
q1=Q[3];
for(i=3;i>0;i--)
{
Q[i]=Q[i-1];
}
Q[0]=A[3];
for(i=3;i>0;i--)
{
A[i]=A[i-1];
}
A[0]=A[i];
}

void display()
{
int i;
for(i=0;i<n;i++) printf("%d\t",A[i]);
for(i=0;i<n;i++) printf("%d\t%d\n",Q[i],q1);
}

void booth()
{
int i;
while(count!=0)
{
if(Q[3]==0 && q1==1)
{
add();
display();
}
else if(Q[3]==1 && q1==0)
{
sub();
display();
}
shift();
display();
printf("\nBooth's Algorithm: ");
}
printf("\nThe answer is: ");
for(i=0;i<n;i++) printf("%d",A[i]);
for(i=0;i<n;i++) printf("%d",Q[i]);
}

void main()
{
clrscr();
input();
initialize();
printf("\nA\tQ\tQ-1");
booth();
display();
printf("\n\n");

getch();
}
Program For Sum And Product For Hexadecimal Number-------------


#include<stdio.h>
#include<iostream.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>

typedef struct hex_number
{
int data[15];
int n;
}hexnumber;

void leftshift(int count, hexnumber temp)
{
temp.n=temp.n+count;
for(;count>0;count--)
temp.data[temp.n+count-1]=0;
}

void read(hexnumber temp)
{
char a[10];
int i;
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(isdigit(a[i]))
{
temp.data[i]=a[i] & 0x0F;
}
else
temp.data[i]=a[i] & 0x0F + 9;
}
temp.n=i;
}

void print(hexnumber temp)
{
int i;
for(i=0;i< temp.n;i++)
{
if(temp.data[i] <= 9)
printf("%d", temp.data[i]);
else
printf("%c", (char)(temp.data[i]+55));
}
}

hexnumber add(hexnumber a, hexnumber b)
{
hexnumber c;
int i, j, k, A,B, carry=0;
i=a.n -1;
j=b.n -1;
if(a.n > b.n)
k=a.n;
else
k=b.n;
c.n =k+1;
for(;i>=0 || j>=0 ;i--, j--, k-- )
{
A=B=0;
if(i>=0)
A =a.data[i];
if(j>=0)
B =a.data[j];
c.data[k]=(carry + A +B)%16;
carry = (carry + A +B)/16;
}
c.data[k]=carry;
return(c);
}

hexnumber multiply(hexnumber x, hexnumber y)
{
hexnumber temp, z;
int n;
int count=0, product, carry=0, i, j, digit;
for(i=y.n-1;j>=0;j--)
{
digit= y.data[i];
for(j=x.n-1;j>=0;j--)
{
product =digit * x.data[j]+carry;
temp.data[j+1]=product%16;
carry=product/16;
}
temp.data[j+1]=carry;
temp.n=x.n +1;
leftshift(count, temp);
count++;
z=add(z, temp);
}
return z;
}

void main()
{
hexnumber a,b,c;
clrscr();
printf("\n Enter first hex numbers :");
read(a);
printf("\n Enter second hex numbers :");
read(b);
c= add(a,b);
printf("\n sum=");
print(c);
printf("\n Product =");
c=multiply(a,b);
print(c);
getch();
}
Program For Binary Multiplication  :-------



#include<iostream.h>
#include<string.h>

void main()
{
int num1[8], num2[8],num3[16], carry,sum, cycle, i, j;
char a[10];
printf("\nEnter first 8 bit binary number");
scanf("%s", a);
i=strlen(a) -1;
j=7;
for(;i>=0 && j>=0;i--,j--)
num1[j]=a[i]-48;
for(;j>=0;j--)
num1[j]=0;
printf("\nEnter second 8 bit binary number");
scanf("%s", a);
i=strlen(a) -1;
j=7;
for(;i>=0 && j>=0;i--,j--)
num2[j]=a[i]-48;
for(;j>=0;j--)
num2[j]=0;
for(i=0;i<16;i++)
num3[i]=0;
for(cycle=1;cycle<=8;cycle++)
{
carry=0;
if(num2[7]==1)
{
for(j=7;j>=0;j--)
{
sum=num3[j]+num1[j]+carry;
num3[j]=sum%2;
carry=sum/2;
}
}
for(i=14;i>=0;i--)
num3[i+1]=num3[i];
num3[0]=carry;
for(i=6;i>=0;i--)
num2[i+1]=num2[i];
printf("\nResult =");
for(i=0;i<16;i++)
printf("%d", num3[i]);
}
printf("\nResult =");
for(i=0;i<16;i++)
printf("%d", num3[i]);

}
Program For Checkbox In  Java-----------


import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;
import java.awt.event.*;

/*<applet code="JTreedemo" width=400 height=200></applet>*/

public class JTreedemo extends JApplet
{
JTree tr;
JTextField f;

public void init()
{
Container c=getContentPane();



c.setLayout(new BorderLayout());

DefaultMutableTreeNode top=new DefaultMutableTreeNode("select");

DefaultMutableTreeNode a=new DefaultMutableTreeNode("colors");
top.add(a);

DefaultMutableTreeNode a1=new DefaultMutableTreeNode("red");
a.add(a1);
DefaultMutableTreeNode a2=new DefaultMutableTreeNode("Green");
a.add(a2);

DefaultMutableTreeNode b=new DefaultMutableTreeNode("fruits");
top.add(b);

DefaultMutableTreeNode b1=new DefaultMutableTreeNode("mango");
b.add(b1);
DefaultMutableTreeNode b2=new DefaultMutableTreeNode("apple");
b.add(b2);

DefaultMutableTreeNode c3=new DefaultMutableTreeNode("names");
top.add(c3);

DefaultMutableTreeNode c1=new DefaultMutableTreeNode("abc");
c3.add(c1);
DefaultMutableTreeNode c2=new DefaultMutableTreeNode("xyz");
c3.add(c2);
DefaultMutableTreeNode c4=new DefaultMutableTreeNode("pqr");
c3.add(c4);
DefaultMutableTreeNode c5=new DefaultMutableTreeNode("efg");
c3.add(c5);

tr=new JTree(top);

int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp=new JScrollPane(tr,v,h);

c.add(jsp,BorderLayout.CENTER);

 f=new JTextField(20);
c.add(f,BorderLayout.SOUTH);


tr.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
doMouseClicked(me);
}
});
}

 void doMouseClicked(MouseEvent me)
{
TreePath tp=tr.getPathForLocation(me.getX(),me.getY());
if(tp!=null)
f.setText(tp.toString());
else
f.setText("");
}


}




Program For JTreedemo In Java------------


import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;
import java.awt.event.*;

/*<applet code="JTreedemo" width=400 height=200></applet>*/

public class JTreedemo extends JApplet
{
JTree tr;
JTextField f;

public void init()
{
Container c=getContentPane();



c.setLayout(new BorderLayout());

DefaultMutableTreeNode top=new DefaultMutableTreeNode("select");

DefaultMutableTreeNode a=new DefaultMutableTreeNode("colors");
top.add(a);

DefaultMutableTreeNode a1=new DefaultMutableTreeNode("red");
a.add(a1);
DefaultMutableTreeNode a2=new DefaultMutableTreeNode("Green");
a.add(a2);

DefaultMutableTreeNode b=new DefaultMutableTreeNode("fruits");
top.add(b);

DefaultMutableTreeNode b1=new DefaultMutableTreeNode("mango");
b.add(b1);
DefaultMutableTreeNode b2=new DefaultMutableTreeNode("apple");
b.add(b2);

DefaultMutableTreeNode c3=new DefaultMutableTreeNode("names");
top.add(c3);

DefaultMutableTreeNode c1=new DefaultMutableTreeNode("abc");
c3.add(c1);
DefaultMutableTreeNode c2=new DefaultMutableTreeNode("xyz");
c3.add(c2);
DefaultMutableTreeNode c4=new DefaultMutableTreeNode("pqr");
c3.add(c4);
DefaultMutableTreeNode c5=new DefaultMutableTreeNode("efg");
c3.add(c5);

tr=new JTree(top);

int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp=new JScrollPane(tr,v,h);

c.add(jsp,BorderLayout.CENTER);

 f=new JTextField(20);
c.add(f,BorderLayout.SOUTH);


tr.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
doMouseClicked(me);
}
});
}

 void doMouseClicked(MouseEvent me)
{
TreePath tp=tr.getPathForLocation(me.getX(),me.getY());
if(tp!=null)
f.setText(tp.toString());
else
f.setText("");
}


}




Program For JTabbedPane In Java-----------


import javax.swing.*;
import javax.swing.JTabbedPane;


/*<applet code="JTDemo" width=400 height=300></applet>*/

public class JTDemo extends JApplet
{
public void init()
{

JTabbedPane jtp=new JTabbedPane();
jtp.addTab("News Papers",new nwsPanel());
jtp.addTab("Magazines",new magPanel());
jtp.addTab("websites",new webPanel());


getContentPane().add(jtp);
}
}

class nwsPanel extends JPanel
{
public nwsPanel()
{
JButton b1=new JButton("Indian Express");
add(b1);
JButton b2=new JButton("Times of India");
add(b2);
JButton b3=new JButton("Sakal");
add(b3);
JButton b4=new JButton("Lokmat");
add(b4);
}
}

class magPanel extends JPanel
{
public magPanel()
{
JCheckBox jcb1=new JCheckBox("Indian Today");
add(jcb1);
JCheckBox jcb2=new JCheckBox("Review");
add(jcb2);
JCheckBox jcb3=new JCheckBox("Policy Diary");
add(jcb3);
}
}

class webPanel extends JPanel
{
public webPanel()
{
JComboBox cb1=new JComboBox();
cb1.addItem("google.com");
cb1.addItem("yahoo.com");
cb1.addItem("mns.com");
add(cb1);
}
}