Writing code and seeing our application automating a task is a great fun to see.
Wednesday, December 8, 2010
Calculator Design
PROGRAM TO IMPLEMENT ROUND ROBIN SCHEDULING
import java.io.DataInputStream;
import java.io.IOException;
import datastruct.LinkedQueue;
import datastruct.Node;
/*
@author athiruban
PROGRAM TO IMPLEMENT ROUND ROBIN SCHEDULING
***/
public class App
{
static String[] process_name=null;
static int[] burst_time=null;
static int[] waiting_time=null;
static int[] turnaroundtime =null;
static int noofproc=0;
static  DataInputStream dis = new DataInputStream(System.in);
static  LinkedQueue lq;
static int exectime=0;
static boolean processed=false;
static int timeslice=0;
public static void readInfo()
{
processed = false;
System.out.print("\n\tEnter No Of Process : ");
try{
noofproc = Integer.parseInt(dis.readLine() ); } catch(IOException ioe){ }
//allocate memory
 process_name = new String[noofproc];
burst_time   = new int[noofproc];
waiting_time = new int[noofproc];
turnaroundtime = new int[noofproc];
lq = new LinkedQueue();
 for(int i = 0;i<noofproc;i++)
{
System.out.print("\n\tEnter Process Name : ");
try{
process_name[i] = dis.readLine(); } catch(IOException ioe){ }
 System.out.print("\n\tEnter Burst time : ");
try{
burst_time[i] = Integer.parseInt(dis.readLine() ); } catch(IOException ioe){ }
 lq.addLast(process_name[i],burst_time[i]);
}
}
 public static int getBurstByName(String str)
{
for(int i = 0 ;i<noofproc;i++) { if( str.equals(process_name[i]) ) return burst_time[i]; }
return 0;
}
 public static int getIdByName(String str)
{
for(int i = 0 ;i<noofproc;i++) { if( str.equals(process_name[i]) ) return i; }
return 0;
}
 public static void displayStatistics()
{
double wt=0;
double tt = 0;
if(processed==false){ System.out.println("Locked"); return; }
 System.out.println("\nProcess Execution Statistics");
System.out.println("----------------------------");
System.out.println("\nProcessName\t"+"BurstTime\t"+"waitingTime\t"+"Turnaround Time");
for(int i =0;i<noofproc;i++)
{
System.out.println(process_name[i]+"\t\t"+burst_time[i]+"\t\t"+waiting_time[i]+"\t\t"+turnaroundtime[i]);
wt += waiting_time[i];
tt += turnaroundtime[i];
}
System.out.println("\n\tCPU Time Slice is  :"+timeslice+" ms");
System.out.println("\n\tAverage Waiting Time is :"+(wt/noofproc)+" ms");
System.out.println("\n\tAverage Turnaround Time is :"+(tt/noofproc)+" ms");
System.out.println("\n\tTotal Execution Time is : "+exectime+" ms");
}
 public static void process()
{
Node tnode; // reference
if(process_name == null){System.out.println("Locked");return;}
 System.out.println("INITIAL STAGE");
lq.display();
System.out.print("\n\tENTER TIME SLICE > ");
try{
timeslice = Integer.parseInt(dis.readLine());
}catch(IOException ioe){ }
while(lq.getSize()>0)
{
 tnode = lq.removeFirst();
if(tnode.p_no >= timeslice) {
exectime += timeslice;
tnode.p_no -= timeslice;
}
else if(tnode.p_no > 0 && tnode.p_no < timeslice) {
exectime += tnode.p_no;
tnode.p_no =0;
}
 if(tnode.p_no > 0)
lq.addLast(tnode.name,tnode.p_no);
else {
System.out.println("\tP r o c e s s  "+tnode.name+" i s  t e r m i n a t e d");
waiting_time[getIdByName(tnode.name)]=exectime-getBurstByName(tnode.name);
turnaroundtime[getIdByName(tnode.name)]
=waiting_time[getIdByName(tnode.name)]+burst_time[getIdByName(tnode.name)];
}
lq.display();
System.out.println("\t=--------------------------------------------------=");
try{ dis.read(); } catch(IOException ioe){ }
}
processed = true;
}
 public static void main(String args[])
{
int ichoice=0;
 L1:
do{
System.out.println("\n\n");
System.out.println("\tIMPLEMENTING ROUND ROBIN SCHEDULING");
System.out.println("\t-----------------------------------");
System.out.println("\t1 TO READ DETAILS");
System.out.println("\t2 TO PROCESS QUEUE");
System.out.println("\t3 TO DISPLAY STATISTICS");
System.out.println("\t4 TO EXIT");
 System.out.print("\n\tYour Choice > ");
try{ ichoice = Integer.parseInt(dis.readLine() ); } catch(IOException ioe){ }
 switch(ichoice)
{
case 1: readInfo();
break;
case 2: process();
break;
case 3: displayStatistics();
break;
case 4:
break L1;
default:
break;
}
} while(true);
}
}
Implementing Graph Traversal (Breath First and Depth First)
// Include necessary headers
int main()
{
int ichoice,i,no=0,j;
int **graph=NULL;
char *symbl=NULL,gt,c,src,dest;
int getindex(char *,char ,int );
do{
    printf("\n\n\tGraph\n");
    printf("\n\t 1 to Create Graph \n");
    printf("\n\t 2 to Depth First Search \n");
    printf("\n\t 3 to Breath First Search\n");
    printf("\n\t 4 to Exit the program \n");
    printf("\n Your Choice -> ");
    scanf("%d",&ichoice);
    switch(ichoice) {
        case 1:
            if(graph!=NULL) {
                for(i=0;i<no;i++) 
                    free(graph[i]);
                free(graph);
                free(symbl);
            }
            printf("\n\tHow many nodes -> ");
            scanf("%d",&no);
            graph = malloc(no*sizeof(int *));
            symbl = malloc(no*sizeof(char));
            for(i=0;i<no;i++)
                graph[i] = (int *)malloc(no*sizeof(int));
            printf("\n\nGraph Type Directed or undirected -> ");
            getchar();
            gt = getchar();
            printf("\n\tEnter Nodes Avoid Repetition\n\t");
            for(i=0;i<no;i++) {
                scanf(" %c",&c);
                symbl[i] = c;
            }
            printf("\n\nEnter Edge pair\n");
            while(1) {
                scanf(" %c %c",&src,&dest);
                i=getindex(symbl,src,no);
                j=getindex(symbl,dest,no);
                if(i==-1||j==-1) break;
                if(gt =='d' || gt== 'D')
                    graph[i][j]=1;
                else
                    graph[i][j]=graph[j][i]=1;
            }
            printf("\n\nEdges created\n\n");
            break;
        case 2:
            dfs(graph,symbl,no);
            break;
        case 3:
            bfs(graph,symbl,no);
            break;
        case 4:
            exit(0);
    }
}while(1);
return 0;
}
int getindex(char *sy,char c,int n) { 
    int i;
    for(i=0;i<n;i++){
if(sy[i]==c)
return i;
    }
    return -1;
}
bfs(int **graph,char *symb,int n) {
    int x=0,*visited,i,que[20];
    int front=-1,rear=-1;
    char cc;
    visited = (int *) malloc(sizeof(int) * n);
    //Get Where to begin
    printf("\n\nWhere to Begin Traverse\n");
    getchar();
    cc = getchar();
    x=getindex(symb,cc,n);
    if(x==-1) {
        printf("\n\n\tInvalid Node....");
        return;
    }
for(i=0;i<n;i++)
        visited[i]=0;
    printf("\n\nBreath First Traversal\n");
    printf("%c ",symb[x]);
    visited[x]=1;
    rear++; 
    front++;
    que[rear]=x;
    while( front <= rear ) {
        x = que[front];
        front ++;
        for(i=0;i<n;i++) {
            if( (graph[x][i] == 1) && (visited[i] == 0) ) {
                printf("%c ",symb[i]);
                visited[i]=1;
                rear++;
                que[rear]=i;
            }
        }
    }     
}
dfs(int **graph,char *symb,int n) {
    int i,top=-1,stack[20],pop_v,j,t,*visited;
    int x=0;
    char cc;
    visited = (int *) malloc(sizeof(int ) * n); 
    for(i=0;i<n;i++)
        visited[i] = 0;
    printf("\n\nWhere to Begin Traverse\n");
    getchar();
    cc = getchar();
    x = getindex(symb,cc,n);
    if(x==-1) {
        printf("\n\tInvalid Node...");
        return ;
    }
    top++;
    stack[top] = x;
    printf("\n\nDepth First Traversal\n");
    while( top >= 0) {
        pop_v = stack[top];
        top--;
        if( visited[pop_v] == 0) {
            printf("%c ",symb[pop_v]);
            visited[pop_v] = 1;
        }
        else continue;
        for(i=n-1;i>=0;i--) {
            if( (graph[pop_v][i] == 1) && (visited[i] == 0) ) {
                top++;
                stack[top]=i;
            }
        } 
} }The author is not liable for any discrepancies......
