Your Ad Here

Tuesday, March 31, 2009

Generalized FA

import java.io.*;

class fa
{
public static void main(String args[])
throws IOException
{
String str;
char ch,ch1;
char ipsy[] = new char[5];
int len,temp=0,no,st1,cst,fst=0,temp1=0,isy=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter The number of States:-");
st1 = Integer.parseInt(br.readLine());
System.out.print("Enter The number of final State:-");
fst = Integer.parseInt(br.readLine());
System.out.print("Enter The number of Input Symbols:-");
isy = Integer.parseInt(br.readLine());
//System.out.println(isy);
int z=0;
do
{
System.out.println("Enter the input Symbol " + (z+1) +" : ");
ch1 = (char) br.read();
ch = (char) br.read();
ch = (char) br.read();
z++;
}while(z!=(isy));
System.out.print("Enter a String:- ");
str=br.readLine();
len= str.length();
int st[][] = new int [st1][2];
for(int x = 0 ; x < st1 ; x++)
{
for(int y = 0 ; y < isy ; y++)
{
System.out.print("Enter the state which we reach when going through " + ipsy[y] + " from state " + (x+1) + ":-");
temp = Integer.parseInt(br.readLine());
st[x][y]=temp;
}
}
cst=1;
for(int x = 0 ; x < len ; x++)
{
temp=cst;
ch = str.charAt(x);
for(int y = 0 ; y < isy ; y++)
{
if (ch==ipsy[y])
{
if (st[cst-1][y]!=0)
{
cst=st[cst-1][y];
}
}
}
System.out.println("Moving from state S" + temp + " to S" + cst );
}

if (cst==fst)
{
System.out.println("The String is acceptable");
}
else
{
System.out.println("The String is not acceptable");
}
}
}1

Tuesday, March 24, 2009

Generalized FA program for two input symbols

//You have to enter the state table for this program.....

import java.io.*;

class genfa
{
public static void main(String args[])
throws IOException
{
String str;
char ch;
int len,temp=0,no,st1,cst,fst=0,temp1=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a String:- ");
str=br.readLine();
len= str.length();
System.out.print("Enter The number of States:-");
st1 = Integer.parseInt(br.readLine());
System.out.print("Enter The number of final State:-");
fst = Integer.parseInt(br.readLine());
//System.out.print("Enter The number of Input Symbols:-");
//st = Integer.parseInt(br.readLine());
int st[][] = new int [st1][2];
for(int x=0 ; x < st1 ; x++)
{
System.out.print("Enter the state which we reach when going through 'a' from state " + (x+1) + ":-");
temp = Integer.parseInt(br.readLine());
st[x][0]=temp;
System.out.print("Enter the state which we reach when going through 'b' from state " + (x+1) + ":-");
temp = Integer.parseInt(br.readLine());
st[x][1]=temp;
}
cst=1;
for(int x=0 ; x < len ; x++)
{
temp=cst;
ch = str.charAt(x);
if (ch=='a')
{
if (st[cst-1][0]!=0)
{
cst=st[cst-1][0];
}
}
else if(ch=='b')
{
if (st[cst-1][1]!=0)
{
cst=st[cst-1][1];
}
}
System.out.println("Moving from state S" + temp + " to S" + cst );
}

if (cst==fst)
{
System.out.println("The String is acceptable");
}
else
{
System.out.println("The String is not acceptable");
}
}
}

Dfa for all languages ending with aa OR (a+b)*aa

//works for strings which contains a and b as input symbols and
//the string should end with "aa"

import java.io.*;

class aadfa
{
public static void main(String args[])
throws IOException
{
String str;
char ch;
int len,st=1,temp;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a String:- ");
str=br.readLine();
len= str.length();
for(int x=0 ; x < len ; x++)
{
ch = str.charAt(x);
temp = st;
switch(st)
{
case 1:
if (ch=='a')
{
st=2;
}
else
{
st=1;
}
break;
case 2:
if (ch=='a')
{
st=3;
}
else
{
st=1;
}
break;
case 3:
if (ch=='a')
{
st=3;
}
else
{
st=1;
}
break;
}
System.out.println("Moving from state S" + temp + " to S" + st );
}


if (st==3)
{
System.out.println("The String is acceptable");
}
else
{
System.out.println("The String is not acceptable");
}

}
}

For each

class foreach
{
public static void main(string args[])
{
int nums[]={1,2,3,4,5,6,7,8,9,10};
for(int x : nums)
{
system.out.print(x + " ");
x=x*10;
}
}
}

Simple For Loop

DECLARE
BEGIN
FOR N IN REVERSE 1..10
LOOP
DBMS_OUTPUT.PUT_LINE(N);
END LOOP;
END;
/

Find Maximum Salary

declare
eno number;
ena char(20);
salary number;
begin
select empno,ename,sal into eno,ena,salary from emp where sal=(select max(sal) from emp);
dbms_output.put_line('salary' || ' = ' || salary);
end;
/

Simple Calculator

DECLARE
N1 NUMBER:='&FIRSTNO.';
N2 NUMBER:='&SECONDNO.';
OP CHAR(1):='&OPERATOR';
X NUMBER(3);
BEGIN
IF OP = '+' THEN
X := N1 + N2;
ELSIF OP = '-' THEN
X:= N1 - N2;
ELSIF OP = '*' THEN
X:= N1 * N2;
ELSIF OP = '/' THEN
X:= N1 / N2;
END IF;
DBMS_OUTPUT.PUT_LINE('THE OUPUT FOR YOUR OPERATION ' || OP || ' WITH ' || N1 || ' FIRST NUMBER & ' || N2 || ' AS SECOND NUMBER IS = ' || X);
END;

Count Employees for each dept using cursors

declare
rec emp%rowtype;
c1 number :=0;
c2 number :=0;
c3 number :=0;
c4 number :=0;
cursor a1 is select * from emp;
begin
open a1;
loop
fetch a1 into rec;
exit when a1%notfound;
if rec.deptno = 10 then
c1 := c1 +1;
elsif rec.deptno = 20 then
c2 := c2 +1;
elsif rec.deptno = 30 then
c3 := c3 +1;
else
c4 := c3+1;
end if;
end loop;
close a1;
dbms_output.put_line('count of deptno 10 = '|| c1||' count of deptno 20 = ' ||c2|| ' count of deptno 30 = ' ||c3||' count of deptno 40 = ' || c4);
end;
/

To find a Record in Table

declare
ena emp.ename%type;
jb emp.job%type;
salary emp.sal%type;
no number := &number;
begin
select ename,job,sal into ena,jb,salary from emp where empno = no;
dbms_output.put_line('Employee No = ' || no || ' name = ' || ena || ' job = ' || jb || ' salary = ' || salary);
exception
when NO_DATA_FOUND then
dbms_output.put_line('Sorry!... RECORD NO FOUND...');
end;
/

Different Outputs

/*Output 1/*

DECLARE
A NUMBER :='&FIRSTNO.';
BEGIN
FOR X IN 1..A
LOOP
FOR Y IN 1..X
LOOP
DBMS_OUTPUT.PUT(Y || ' ');
END LOOP;
DBMS_OUTPUT.NEW_LINE( );
END LOOP;
END;

/*Output 2/*

DECLARE
A NUMBER :='&FIRSTNO.';
BEGIN
FOR X IN 1..A
LOOP
FOR Y IN 1..X
LOOP
DBMS_OUTPUT.PUT(X || ' ');
END LOOP;
DBMS_OUTPUT.NEW_LINE( );
END LOOP;
END;
/

/*Output 3/*

DECLARE
A NUMBER :='&FIRSTNO.';
BEGIN
FOR X IN REVERSE 1..A
LOOP
FOR Y IN 1..X
LOOP
DBMS_OUTPUT.PUT(X || ' ');
END LOOP;
DBMS_OUTPUT.NEW_LINE( );
END LOOP;
END;
/

/*Output 4/*

DECLARE
A NUMBER :='&FIRSTNO.';
BEGIN
FOR X IN REVERSE 1..A
LOOP
FOR Y IN 1..X
LOOP
DBMS_OUTPUT.PUT(X || ' ');
END LOOP;
DBMS_OUTPUT.NEW_LINE( );
END LOOP;
END;
/

Working with %Type

/* working with anochard datatype*/
declare
eno emp.empno%type;
ena emp.ename%type;
jb emp.job%type;
salary emp.sal%type;
begin
select empno, ename , job , sal into eno , ena , jb , salary from emp where empno = '&eno';
dbms_output.put_line('empno = ' || eno || 'ename = ' || ena || 'job = ' || jb || 'salary = ' || salary);
end;
/

Adding Salary using Cursor

declare
cursor abc is select ename,sal,sal + 500 "Newsal" from emp;
rec abc%rowtype;
begin
open abc;
loop
fetch abc into rec;
exit when abc%notfound;
dbms_output.put_line('name' || rec.ename || 'salary' || rec.sal || rec.sal ||'new salary' || rec.Newsal);
end loop;
close abc;
end;