Saturday, 19 December 2015

First Served (FCFS) Scheduling Algorithm(Operating System)

Source Code :


#include<stdio.h>
typedef struct{
int ar,bt,wt,tt,rt,_no;

}info;
info p[10];
int main()
{

    int n,i,j;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d %d",&p[i].ar,&p[i].bt);
        p[i]._no=i;
    }
    for(i=1;i<n;i++)//sorting
    {
        for(j=i+1;j<=n;j++)
        {
            if(p[j].ar<p[i].ar)
            {
                info temp=p[j];
                p[j]=p[i];
                p[i]=temp;
            }
        }
    }
    int  time=0;
    for(i=1;i<=n;i++)
    {
        p[i].wt=p[i].rt=time-p[i].ar;
        p[i].tt=p[i].wt + p[i].bt;
        time=time+p[i].bt;
        printf("Process: %d Ar: %d br:%d  TT: %d wt:%d RT: %d\n",p[i]._no,p[i].ar,p[i].bt,p[i].tt,p[i].wt,p[i].rt);
    }
    printf("\n");


}

Shortest Job First (SJF) Scheduling Algorithm(Operating System)


Source Code : By Java


//main_file.java


import javax.swing.JFrame;


public class main_file {
public static void main(String args[])
{
shortest_job_first f = new shortest_job_first();
f.setVisible(true);
f.setBounds(100, 50, 700, 550);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

//shortest_job_first.java


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class shortest_job_first extends JFrame {
 
JPanel p1,p2,p3,p11,p12,p13,p14,p15,p16,p17;
JButton b1,b2;
JTextField process,Arrival_time,brust_time;
JLabel l1,l2,l3,top;
public shortest_job_first() {
p1= new JPanel();
p1.setBackground(Color.WHITE);
add(p1);

 p1.setLayout(new GridLayout(2,1));

 p2= new JPanel();
 p2.setBackground(Color.white);

 p1.add(p2);

 p3= new JPanel();
 p3.setBackground(Color.white);

 p1.add(p3);

 p2.setLayout(new GridLayout(7,1));///divide first panel into seven and then enter one text field after another
 p11= new JPanel();
 p11.setBackground(Color.white);
 p2.add(p11);

 p12= new JPanel();
 p12.setBackground(Color.white);
 p2.add(p12);
 p13= new JPanel();
 p13.setBackground(Color.white);
 p2.add(p13);
 p14= new JPanel();
 p14.setBackground(Color.white);
 p2.add(p14);
 p15= new JPanel();
 p15.setBackground(Color.white);
 p2.add(p15);
 p16= new JPanel();
 p16.setBackground(Color.white);
 p2.add(p16);
 p17= new JPanel();
 p17.setBackground(Color.white);
 p2.add(p17);


 l1= new JLabel("Process");
 l1.setPreferredSize(new Dimension(100,30));
 l1.setFont(new Font("Times new Roman", Font.PLAIN, 18));
 p13.add(l1);

 process= new JTextField();
 process.setPreferredSize(new Dimension(400,30));
 process.setFont(new Font("Times new Roman", Font.BOLD, 18));
 p13.add(process);

 l2= new JLabel("Arrival Time");
 l2.setPreferredSize(new Dimension(100,30));
 l2.setFont(new Font("Times new Roman", Font.PLAIN, 18));
 p14.add(l2);

 Arrival_time= new JTextField();
 Arrival_time.setPreferredSize(new Dimension(400,30));
 Arrival_time.setFont(new Font("Times new Roman", Font.BOLD, 18));
 p14.add(Arrival_time);

 l3= new JLabel("Brust Time");
 l3.setPreferredSize(new Dimension(100,30));
 l3.setFont(new Font("Times new Roman", Font.PLAIN, 18));
 p15.add(l3);

 brust_time= new JTextField();
 brust_time.setPreferredSize(new Dimension(400,30));
 brust_time.setFont(new Font("Times new Roman", Font.BOLD, 18));
 p15.add(brust_time);

 b1= new JButton("SJF");//Shortest job First button
 b1.setPreferredSize(new Dimension(100,30));
 b1.setBackground(Color.white);
 b1.setFont(new Font("Times new Roman", Font.BOLD, 18));



 //Action of b1(sjf button)
 b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

try{
       String s1 = process.getText();
       String s2 = Arrival_time.getText();
       String s3 = brust_time.getText();
       int pro = Integer.parseInt(s1);
       int min_data=99999;
       StringTokenizer token = new StringTokenizer(s2, ",");
       StringTokenizer token1 = new StringTokenizer(s3, ",");
       String[] proces = new String[1000];
       int arrival[] = new int[1000];
       int brust[] = new int[1000];
       boolean complete[] = new boolean[pro + 1];
       int i = 0;
       while (token.hasMoreElements()) {
           String tokenizer = token.nextToken();
           int data = Integer.parseInt(tokenizer);
           arrival[i] = data;
           i++;
       }

       i = 0;
       while (token1.hasMoreElements()) {
           String tokenizer = token1.nextToken();
           int data = Integer.parseInt(tokenizer);
           brust[i] = data;
           i++;
       }

       for (i = 0; i < pro; i++) {
           proces[i] = "p" + (i + 1);
       }
       int j;

       int temp;
       String sp;
     
       shorting(pro, brust, arrival, proces, complete);


       int total_brust_time = 0;
       for (i = 0; i < pro; i++) {
           total_brust_time += brust[i];
           if (brust[i] == 0) {
               complete[i] = true;
           }
       }
       int thistime = 0;


       String queue[] = new String[total_brust_time + 1000];
       int k = 0;
       int tag = 0;
       int m = 0;
       for (i = 0; ; i++) {
           tag = 0;

           for (j = 0; j < pro; j++) {
               if (complete[j] == true) {
                   m = 0;
               } else {
                   m = 1;
                   break;
               }

           }
           if (m == 0){
               break;
           }
           for (j = 0; j < pro; j++) {
               if (arrival[j] <= thistime && complete[j] == false) {
                   tag = 1;
                   queue[k] = proces[j];
                   k++;
                   thistime++;
                   brust[j]--;
                   if (brust[j] == 0) {
                       complete[j] = true;
                   }
                   break;
               }
           }
           if (tag == 0) {
               queue[k] = "st";
               k++;
               thistime++;
           }
         shorting(pro, brust, arrival, proces, complete);
       }
     
       //This part printing process into a panel or GANTT CHART
       Graphics g = p3.getGraphics();
     
       int x1 = 40, x2 = 30, y1 = 40, y2 = 40;
       int drawstrx1 = (x1 + 17);
       int drawstry1 = 55;
       int drawfirstx1 = x1 - 3;
       int drawfirsty1 = 85;
       g.setColor( Color.MAGENTA);
       g.drawString("GANTT CHART", 40, 10);
int x=0;
       for (i = 0; i < thistime; i++) {
           x=1;
           g.setColor(Color.black);
           g.drawRect(x1, x2, y1, y2);
           g.setColor(Color.blue);
           g.drawString("" + queue[i], drawstrx1, drawstry1);
           g.setColor(Color.black);
           g.drawString("" + i, drawfirstx1, drawfirsty1);
           x1 = x1 + y1;
           drawfirstx1 = x1 - 3;
           drawstrx1 = (x1+17);
          Thread.sleep(1000);
         
       }
      if(x==1)
       g.drawString("" + i, drawfirstx1, drawfirsty1);



}catch(Exception e){
   
}
}
// method for sorting (bubble sort)
private void shorting(int pro, int[] brust, int[] arrival,
String[] proces, boolean[] complete) {
int i, j, temp;
       String sp;
       boolean m;
       for (i = 0; i < pro - 1; i++) {
           for (j = i + 1; j < pro; j++) {
               if (brust[i] > brust[j]) {
                   temp = brust[i];
                   brust[i] = brust[j];
                   brust[j] = temp;

                   temp = arrival[i];
                   arrival[i] = arrival[j];
                   arrival[j] = temp;

                   sp = proces[i];
                   proces[i] = proces[j];
                   proces[j] = sp;

                   m = complete[i];
                   complete[i] = complete[j];
                   complete[j] = m;

               }
           }
       }
}
});
 p16.add(b1);



 b2= new JButton("RESET");//Reset button
 b2.setPreferredSize(new Dimension(100,30));
 b2.setBackground(Color.white);
 b2.setFont(new Font("Times new Roman", Font.BOLD, 18));
 //action of reset button
 b2.addActionListener( new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
try{
process.setText(null);
        Arrival_time.setText(null);
        brust_time.setText(null);
        p3.repaint();
   }catch(Exception ex){
   }

}
});


 p16.add(b2);




 top= new JLabel("Shortest Job First (SJF) Scheduling Algorithm");
 top.setForeground(Color.black);
 top.setFont(new Font("Times new Roman", Font.BOLD, 24));
 p12.add(top);


}
}







Saturday, 5 September 2015

UVA 489 - Hangman Judge(cpp file)




Problem Type :Ad hoc



#include<stdio.h>
#include<string.h>
#define MAX 200
int main()
{
    int round,i,j,k,len1,len2,error,correct;
    char ch[MAX], des[MAX];
    while(scanf("%d",&round)==1)
    {
        if(round==-1)
        {
            break;
        }
        scanf("%s",&ch);
        getchar();
        scanf("%s",&des);
        //printf("%s %s\n",ch,des);
        len1=strlen(ch);
        len2=strlen(des);
        error=0;
        correct=0;
        for(i=0; i<len2; i++)
        {
             int tag=0;
            for(j=0; j<len1; j++)
            {
                if(ch[j]==des[i])
                {
                      correct++;
                      tag=1;
                      ch[j]=1;
                }
            }
            if(tag==0)
            {
                error++;
            }
            if(error==7 || correct==len1)
            {
                break;
            }
        }
        printf("Round %d\n",round);
        if(error==7 )
        {

            printf("You lose.\n");
        }
        else if(correct==len1)
        {
            printf("You win.\n");
        }
        else{

            printf("You chickened out.\n");
        }
    }

}

Thursday, 11 June 2015

UVA 787 - Maximum Sub-sequence Product(java file)



Problem Type : MAX_1D_RANGE_SUM(DP)



import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<BigInteger> list = new ArrayList<BigInteger>();
        BigInteger el, end, pro, maxPro;
        end = new BigInteger("-999999");
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            el = sc.nextBigInteger();
            if (el.compareTo(end) != 0) {
                list.add(el);
            } else {
                maxPro = list.get(0);
                for (int i = 0; i < list.size(); i++) {
                    pro = BigInteger.ONE;
                    for (int j = i; j < list.size(); j++) {
                        pro = pro.multiply(list.get(j));
                        maxPro = maxPro.max(pro);
                    }
                }
                System.out.println(maxPro);
                list.clear();
            }
        }
    }

}

Wednesday, 3 June 2015

UVA 10684 - The jackpot(cpp file)




Problem Type : Max 1D range sum


Code :
#include<stdio.h>
#define mx 10003

int main()
{
    int test,i,j;
    while(scanf("%d",&test)==1)
    {
        long int max_arr,sum;
        max_arr = 0;
        sum = 0;
        if(test==0)
            break;
        for(i=0; i<test; i++)
        {
            scanf("%d",&j);
            sum+=j;
            if(sum>max_arr)
            {
                max_arr = sum;
            }

            if(sum<0)
            {
                sum=0;
            }

        }
        if(max_arr)
        {
            printf("The maximum winning streak is %ld.\n",max_arr);
        }
        else
        {
            printf("Losing streak.\n");
        }
    }
return 0;
}



UVA 541 - Error Correction(cpp file)




Code :

#include<stdio.h>
#define MAX 102
int main()
{

    int arr[MAX][MAX],test,i,j,tag,corr1,corr2,m,n,sum;
    while(scanf("%d",&test)&&test)
    {
        corr1 = 0;
        corr2 = 0;
        tag =0;
        for(i=1;i<=test;i++)
        {
            sum=0;
            for(j=1;j<=test;j++)
            {
                scanf("%d",&arr[i][j]);
                sum += arr[i][j];
            }
            if(sum%2==1)
            {
               corr1++;
                m=i;
            }
        }
        for(i=1;i<=test;i++)
        {
            sum=0;
            for(j=1;j<=test;j++)
            {
                sum+=arr[j][i];
            }
            if(sum%2==1)
            {
                corr2++;
                n=i;
            }
        }
     //   printf("%d %d\n",corr1,corr2);
       if(corr1==0 && corr2==0)
       {
           printf("OK\n");
       }
      else  if(corr1==1 && corr2==1)
       {
           printf("Change bit (%d,%d)\n",m,n);
       }
       else {

        printf("Corrupt\n");
       }
    }
}

Tuesday, 2 June 2015

UVA 10739 - String to Palindrome



Problem Type :DP  (Levenshtein distance Algorithm)

Code :

#include<stdio.h>
#include<algorithm>
#include<string.h>
#define mx 1070
using namespace std;
char ch[mx],des[mx];
int arr[mx][mx],cou,len;

void init()
{
    for(int i=0; i<len; i++)
    {
        arr[i][0]=i;
    }
    for(int i=0; i<len; i++)
    {
        arr[0][i]=i;
    }
    return;
}
void lav()
{
    int i,j;
    for(i=1; i<len; i++)
    {
        for(j=1; j<len; j++)
        {
            if(ch[j]==des[i])
            {
                arr[i][j]= arr[i-1][j-1];
            }
            else
            {
            int    p = min(arr[i-1][j],arr[i][j-1]);
                arr[i][j]=min(p,arr[i-1][j-1]);
                arr[i][j]++;
            }
        }
    }
}
int main()
{
    int i,j,test;
    scanf("%d",&test);
    getchar();
    int cas = 0;
    while(test--)
    {
        cas++;
        scanf("%s",ch);
        len = strlen(ch);
        for(i=len; i>=1; i--)
        {
            ch[i]=ch[i-1];
        }
        len++;
        ch[len]='\0';
        des[0]='k';
        j=1;
        for(i=len-1; i>=1; i--)
        {
            des[j]=ch[i];
            j++;
        }
        des[len]='\0';
        cou=0;
        init();
        lav();
        printf("Case %d: %d\n",cas,arr[len-1][len-1]/2);
        memset(arr,0,sizeof arr);
    }
    return 0;
}

ট্রিগার এর মাধ্যমে ডাটা ইনসার্ট - insert data using Database Trigger (Mysql)

সর্বপ্রথম আমরা প্রবলেমটা বুঝিঃ আমি একটা টেবিলের একটা কলামের ভ্যালুর উপর ডিপেন্ড করে আরেকটা কলামে ডাটা insert করব । এই কাজটা ট্রি...