Saturday 21 February 2015

how to implement threads in c

how to implement threads in c




Pthreads are a simple and effective way of creating a multi-threaded application. 
When a thread is created using pthread_create, both the original thread and the new thread share the same code base and the same memory – it’s just like making two function calls at the same time.


All C programs using pthreads need to include the pthread.h header file.







#include<stdio.h>
#include<pthread.h>

pthread_t thread[2];


static void *function1(void *_){

 int i;
 for(i=0;i<10;i++){
  printf("\nThread1 says=%d",i);
  sleep(1);

}
}

static void *function2(void *_){

 int i;
 for(i=0;i<5;i++){

  printf("\nThread2 says=%d",i);
  sleep(5);
}
}

int main(){

    pthread_create(&thread[0], NULL , function1, NULL);
    pthread_create(&thread[1], NULL , function2, NULL);

    pthread_exit(NULL);
return 1;
}

Thursday 12 February 2015

NQueen Problem in C



 NQueen Problem in C



 




#include<stdio.h>
#include<math.h>
#define TRUE 1
#define FALSE 0
#define MAXBOARDSIZE 8
int board[MAXBOARDSIZE];
int n;
int steps;
static int solno=1;
void PrintBoard()
{
    int i, k,z;

    printf("\nSolution %d:\n", solno++);
    printf("No of Steps=%d:\n", steps);
    for (i = 0; i < n; i++)
    {
        for (k = 0; k < board[i]; k++)
            printf("\t-");
        printf("\tQ%d",i+1);
        for(z=k;z<n-1;z++)
            printf("\t-");
        printf("\n");
    }
}

int IsPlaceSafe(int row, int col)
{
    int i;

    for(i=0; i<row; i++)
    {
        if( (board[i] == col) || (abs(board[i]-col) == abs(i-row)) )
            return FALSE;
    }
    return TRUE;
}

void NQueens(int row)
{
    int col;
    for(col=0; col<n; col++)
    {
        steps++;
        if(IsPlaceSafe(row, col))
        {
            board[row] = col;
            if(row == n-1)
                PrintBoard();
            else
                NQueens(row+1);
        }
    }
}

int main()

    int c=1;
    while(c==1){
    printf("Enter board size: ");
    scanf("%d", &n);
    if(n>MAXBOARDSIZE || n<4){
     printf("\nNo Solution Exit\n");
    }else{
    NQueens(0);
    }
    printf("\nPress 1 to Continue and 0 to exit");
    scanf("%d",&c);
    solno=1;
    steps=0;
    }
    return 0;
}

Wednesday 4 February 2015

DFA implementation in java

DFA implementation in java







--------APP CLASS--------
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.*;


public class App {
JFrame f1,f2,f3,f4,f5,f_dailog;
JButton JB_dfa,JB_nfa,JB_new,JB_select,JB_cDFA[];
String path="";
ArrayList<String> records;
int TT[][];
int start_state=0,final_state=0,no_state=0,no_var=0;
String inputString="";
DFA obj_DFA;
public App(){
f1=new JFrame();
f1.setSize(600, 420);
f1.setVisible(true);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setResizable(false);
f1.setLocationRelativeTo(null);
f1.setLayout(null);
f1.setTitle("FINITE AUTOMATA | DEVELOPED BY SHAHARYAR SHAUKAT");
try{
BufferedImage bf = ImageIO.read(new File("icon//BG1.png"));  
f1.setContentPane(new Bg(bf));
}catch(Exception e){
}
JB_dfa=new JButton("DFA");
JB_dfa.setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_dfa.setBounds(50, 200, 80, 50);
JB_dfa.setBackground(new Color(109, 183, 246));
JB_dfa.setForeground(new Color(255,255,255));
JB_dfa.setFont(new Font("Garamond",  Font.BOLD , 20));
f1.add(JB_dfa);
JB_dfa.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
f2.setVisible(true);
f1.setVisible(false);
}
});
JB_nfa=new JButton("NFA");
JB_nfa.setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_nfa.setBounds(200, 200, 80, 50);
JB_nfa.setBackground(new Color(109, 183, 246));
JB_nfa.setForeground(new Color(255,255,255));
JB_nfa.setFont(new Font("Garamond",  Font.BOLD , 20));
f1.add(JB_nfa);
JB_nfa.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
});
f1.validate();
// FRAME 2
f2=new JFrame();
f2.setSize(600, 420);
f2.setVisible(false);
f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f2.setResizable(false);
f2.setLocationRelativeTo(null);
f2.setLayout(null);
f2.setTitle("FINITE AUTOMATA | DEVELOPED BY SHAHARYAR SHAUKAT");
try{
BufferedImage bf = ImageIO.read(new File("icon//BG1.png"));  
f2.setContentPane(new Bg(bf));
}catch(Exception e){
}
JB_new=new JButton("CREATE NEW DFA");
JB_new.setToolTipText(" Provide Transition Table ");
JB_new.setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_new.setBounds(30, 120, 300, 50);
JB_new.setBackground(new Color(109, 183, 246));
JB_new.setForeground(new Color(255,255,255));
JB_new.setFont(new Font("Garamond",  Font.BOLD , 25));
JB_new.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
f_dailog.setVisible(true);
}
});
f2.add(JB_new);
JB_select=new JButton("SELECT  DFA");
JB_select.setToolTipText(" Select Sample DFA From given List  ");
JB_select.setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_select.setBounds(30, 220, 300, 50);
JB_select.setBackground(new Color(109, 183, 246));
JB_select.setForeground(new Color(255,255,255));
JB_select.setFont(new Font("Garamond",  Font.BOLD , 25));
JB_select.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
f2.setVisible(false);
f3.setVisible(true);
}
});
f2.add(JB_select);
// USER INPUT 
f_dailog=new JFrame("");
f_dailog.setSize(300, 200);
f_dailog.setVisible(false);
f_dailog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f_dailog.setResizable(false);
f_dailog.setLocationRelativeTo(null);
f_dailog.setLayout(null);
JLabel lbl_noState=new JLabel("NO. of States");
lbl_noState.setBounds(10, 10, 80, 40);
JTextField txt_noState=new JTextField();
txt_noState.setBounds(90, 10, 40, 40);
JLabel lbl_noVar=new JLabel("NO. of Var");
lbl_noVar.setBounds(160, 10, 80, 40);
JTextField txt_noVar=new JTextField();
txt_noVar.setBounds(220, 10, 40, 40);
f_dailog.add(lbl_noState);
f_dailog.add(lbl_noVar);
f_dailog.add(txt_noState);
f_dailog.add(txt_noVar);
//FRAME 3 Pre selected
f3=new JFrame();
f3.setSize(600, 420);
f3.setVisible(false);
f3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f3.setResizable(false);
f3.setLocationRelativeTo(null);
f3.setLayout(null);
f3.setTitle("FINITE AUTOMATA | DEVELOPED BY SHAHARYAR SHAUKAT");
try{
BufferedImage bf = ImageIO.read(new File("icon//BG1.png"));  
f3.setContentPane(new Bg(bf));
}catch(Exception e){
}
JB_cDFA=new JButton[5];
JB_cDFA[0]=new JButton("DFA that accepts all string which occur with even no. of 0s and 1s");
JB_cDFA[0].setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_cDFA[0].setBounds(5, 100, 350, 30);
JB_cDFA[0].setBackground(new Color(109, 183, 246));
JB_cDFA[0].setForeground(new Color(255,255,255));
JB_cDFA[0].setFont(new Font("Garamond",  Font.BOLD , 9));
JB_cDFA[0].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
path="files//0.txt";
readFile(path);
f3.setVisible(false);
f5.setVisible(true);
System.out.println("");
for(int i=0;i<records.size();i++){
System.out.println(records.get(i));
}
System.out.println("------------");
System.out.println("no-states"+no_state);
System.out.println("start_states"+start_state);
System.out.println("final-states"+final_state);
System.out.println("no_var="+no_var);
System.out.println("--------------");
for(int i=0;i<no_state;i++){
System.out.print("\t"+TT[i][0]);
System.out.print("\t"+TT[i][1]);
System.out.print("\n");
}
obj_DFA=new DFA(TT,start_state,final_state,no_state,no_var);
Display obj_Display=new Display(TT);
obj_Display.setBounds(10, 100, 980, 480);
f5.add(obj_Display);
Thread t_Display=new Thread(obj_Display);
t_Display.start();

}
});
f3.add(JB_cDFA[0]);
JB_cDFA[1]=new JButton("DFA that accepts all string which ends with 0 and 1");
JB_cDFA[1].setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_cDFA[1].setBounds(5, 150, 350, 30);
JB_cDFA[1].setBackground(new Color(109, 183, 246));
JB_cDFA[1].setForeground(new Color(255,255,255));
JB_cDFA[1].setFont(new Font("Garamond",  Font.BOLD , 9));
JB_cDFA[1].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
path="files//1.txt";
readFile(path);
f3.setVisible(false);
f5.setVisible(true);
System.out.println("");
for(int i=0;i<records.size();i++){
System.out.println(records.get(i));
}
System.out.println("------------");
System.out.println("no-states"+no_state);
System.out.println("start_states"+start_state);
System.out.println("final-states"+final_state);
System.out.println("no_var="+no_var);
System.out.println("--------------");
for(int i=0;i<no_state;i++){
System.out.print("\t"+TT[i][0]);
System.out.print("\t"+TT[i][1]);
System.out.print("\n");
}
obj_DFA=new DFA(TT,start_state,final_state,no_state,no_var);
Display obj_Display=new Display(TT);
obj_Display.setBounds(10, 100, 980, 480);
f5.add(obj_Display);
Thread t_Display=new Thread(obj_Display);
t_Display.start();

}
});
f3.add(JB_cDFA[1]);
JB_cDFA[2]=new JButton("DFA that accepts all string which have 2nd last symbol is 1");
JB_cDFA[2].setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_cDFA[2].setBounds(5, 200, 350, 30);
JB_cDFA[2].setBackground(new Color(109, 183, 246));
JB_cDFA[2].setForeground(new Color(255,255,255));
JB_cDFA[2].setFont(new Font("Garamond",  Font.BOLD , 9));
f3.add(JB_cDFA[2]);
JB_cDFA[3]=new JButton("DFA that accepts all string which have 011 as substring");
JB_cDFA[3].setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_cDFA[3].setBounds(5, 250, 350, 30);
JB_cDFA[3].setBackground(new Color(109, 183, 246));
JB_cDFA[3].setForeground(new Color(255,255,255));
JB_cDFA[3].setFont(new Font("Garamond",  Font.BOLD , 9));
f3.add(JB_cDFA[3]);
// DISPLAY FRAME
f5=new JFrame();
f5.setSize(1000, 600);
f5.setVisible(false);
f5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f5.setResizable(false);
f5.setLocationRelativeTo(null);
f5.setLayout(null);
f5.setTitle("FINITE AUTOMATA | DEVELOPED BY SHAHARYAR SHAUKAT");
final JTextField input=new JTextField();
JButton start=new JButton("START");
start.setCursor(new Cursor(Cursor.HAND_CURSOR));
start.setBounds(30, 10, 120, 50);
start.setBackground(new Color(109, 183, 246));
start.setForeground(new Color(255,255,255));
start.setFont(new Font("Garamond",  Font.BOLD , 20));
start.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
inputString=input.getText();
System.out.println(inputString);
obj_DFA.input=inputString;
Thread t_DFA=new Thread(obj_DFA);
t_DFA.start();
}
});
f5.add(start);
JButton pause=new JButton("PAUSE");
pause.setCursor(new Cursor(Cursor.HAND_CURSOR));
pause.setBounds(850, 10, 120, 50);
pause.setBackground(new Color(109, 183, 246));
pause.setForeground(new Color(255,255,255));
pause.setFont(new Font("Garamond",  Font.BOLD , 20));
pause.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
});
f5.add(pause);
input.setBounds(200, 10, 600, 50);
input.setFont(new Font("SansSerif", Font.BOLD, 25));
f5.add(input);
}
public static void main(String args[]){
new App();
}
public void readFile(String filename)
{
 records = new ArrayList<String>();
 try
 {
   BufferedReader reader = new BufferedReader(new FileReader(filename));
   String line;
   while ((line = reader.readLine()) != null)
   {
     records.add(line);
   }
   reader.close();
   storeValue();
   
 }
 catch (Exception e)
 {
   System.err.format("Exception occurred trying to read '%s'.", filename);
   e.printStackTrace();
  
 }
}
//
public void storeValue(){
no_state=records.size()-2;
start_state=records.get(0).charAt(1)-48;
final_state=records.get(1).charAt(1)-48;
no_var=records.get(2).trim().split("\\s+").length;
TT=new int[no_state][no_var];
for(int i=0;i<no_state;i++){
String[] words=records.get(i+2).split("\\s+");
for(int j=0;j<2;j++){
TT[i][0]=words[0].charAt(1)-48;
TT[i][1]=words[1].charAt(1)-48;
}
}
}
// End Class

}


----------------DFA CLASS--------------


public class DFA implements Runnable {
int TT[][];
static String input="";
static int start_state=0,final_state=0,no_state=0,no_var=0;
public static int present_state=0;
static int result=3,ptr=0;
public DFA(){
}
public DFA(int TT[][],int start_state ,int final_state ,int no_state,int no_var){
this.TT=TT;
this.start_state=start_state;
this.final_state=final_state;
this.no_state=no_state;
this.no_var=no_var;
present_state=start_state;
}
@Override
public void run() {
// TODO Auto-generated method stub
   result=0;
Algo();
}
public void Algo(){
for(int i=0;i<input.length();i++){
ptr=i;
char ch=input.charAt(i);
int ch_value=(int)ch-48;
int temp_state=present_state;
present_state=TT[temp_state][ch_value];
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(present_state==final_state){
System.out.println("ACCEPTED");
result=1;
}else{
System.out.println("NOT ACCEPTED");
result=2;
}
}

}


------------BG CLASS----------

import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JComponent;
public class Bg extends JComponent {
Image i;
public Bg(Image i) {
this.i = i;
}
public void paintComponent(Graphics g) {
g.drawImage(i, 0, 0, null);  // Drawing image using drawImage method
 
}
}