Friday 26 October 2012

menu driven shell

while [ 1 ]
do
echo " "
echo "1) Factorail no"
echo "2) Greatest of 3 nos"
echo "3) Prime no"
echo "4) Palindrome string"
echo "5) Palindrome no"
echo "6) Exit"
echo "Enter Your choice : "
read ch
case "$ch" in
1)
echo "Program for Factorial no"
echo "Enter number : "
read num
i=1
fact=1
for((i=1;i<=num;i++))
{
    fact=$(($fact*$i))
}
echo "Factorial is : " $fact
;;
2)
echo "Program for greatest no "
echo "Enter num1 : "
read n1
echo "Enter num2 : "
read n2
echo "Enter num3 : "
read n3
if [ $n1 -gt $n2 -a $n1 -gt $n3 ]
then
    echo "$n1 is greatest no"
elif [ $n2 -gt $n1 -a $n2 -gt $n3 ]
then
        echo "$n2 is greatest no"
else
    echo "$n3 is greatest no"
fi
;;
3)
echo "Program for prime no : "
echo "Enter num : "
read num
i=2
flag=1
for((i=2;i<=num-1;i++))
{
if [ $(($num % $i)) -eq 0 ]
then
    flag=0
    break
fi
}
if [ $flag -eq 1 ]
then
    echo "Prime no"
else
    echo "Not a Prime no"

fi
;;
4)
echo "Enter string : "
read str
reverse=`echo $str| rev`
if [ $reverse = $str ]
then
    echo "String is palindrome"
else
    echo "String is not palindome"
fi
;;
5)
echo "Enter number : "
read num
org=$num
rev=0
while [ $num -gt 0 ]
do
    r=$(($num%10))
    num=$(($num/10))
    rev=$(($rev*10))
    rev=$(($rev+$r))
   
done
if [ $rev -eq $org ]
then
    echo "No is palindrome"
else
    echo "No. is not palindrome"
fi
;;
6)
exit
;;
*)
echo "Invalid Choice"
;;
esac
done

database shell

printf "Enter a File Name : "
read fname
while [ 1 ]
do

echo " "
echo " ***** MENU **** "
echo "1) Create Record"
echo "2) View Record"
echo "3) Insert Record"
echo "4) Delete Record"
echo "5) Search"
echo "6) Exit"
echo "Enter your choice : "
read ch
case "$ch" in
1)
    echo "CREATE"
    echo "Enter student info : \n"
    echo "Roll No. : "
    read rno
    echo "Name : "
    read name
    echo "Year : "
    read year
    echo "Branch : "
    read branch
    echo "Percentage : "
    read per
    printf "%d\t%s\t%s\t%s\t%s\n" $rno $name $year $branch $per >> $fname
;;
2)
    echo "VIEW"
    if [ -e $fname ]
    then
        cat $fname
    else
    echo "File does not exist"
    fi

;;
3)
    echo "INSERT"
        echo "Enter student info : \n"
        echo "Roll No. : "
        read rno
        echo "Name : "
        read name
        echo "Year : "
        read year
        echo "Branch : "
        read branch
        echo "Percentage : "
        read per
        printf "%d\t%s\t%d\t%d\t%d" $rno $name $year $branch $per >> $fname
;;
4)
    echo "DELETE"
    echo "Enter roll no which u want to delete : "
    read rno
    grep "$rno" "$fname"
    ans=$?
    if [ $ans -eq 0 ]
    then
    echo "Record Found"
    grep -v "$rno" "$fname"
    rm "$fname"
    mv temp.dat "$fname"
    echo "Record deleted !!!"
    else
    echo "Record not found"
    fi
;;


5)
    echo "SEARCH"
        echo " "
        echo "Result of a particular record"
        echo "Enter roll no which u want to search: "
        read rno
        grep "$rno" "$fname"
        ans=$?
        if [ $ans -eq 0 ]
        then
        echo "Record Found"
        else
        echo "Record not found"
        fi
;;
6)
exit
;;
*)
    echo "Invalid choice"
esac
done

cmd (print n words, sum of digit)

if [ $# -ne 3 ]
then
echo " Insuffient command line arguments "
else
choice=1
echo "1) Print the word N times"
echo "2) Sum of digit no."
echo "3) Exit"
echo "Enter your choice : "
read choice
case $choice in
1)
count=$1
while [ $count -gt 0 ]
do
 echo "$2"
    count=`expr $count - 1`
done
;;
2)
num=$3
sum=0
while [ $num -gt 0 ]
do
    r=$(($num%10))
    sum=$(($sum+$r))
    num=$(($num/10))
done
    echo "Sum of digit of no is : $sum"
;;
3)
exit
;;
*)
echo "Invalid choice"
;;
esac
fi


cmd line (biggest no , reverse)

if [ $# -ne 4 ]
then
echo "Insuffiecient command line arguments"
else
choice=1
while [ $choice -ne 4 ]
do
echo "1) Biggest of 3 nos"
echo "2) Reverse no"
echo "3) Exit"
echo "Enter your choice : "
read choice
case $choice in
1)
    if [ $1 -gt $2 -a $1 -gt $3 ]
    then
        echo "$1 is greatest no"
    elif [ $2 -gt $1 -a $2 -gt $3 ]
        then
            echo "$2 is greatest no"
    else
        echo "$3 is greatest no"
    fi
;;
2)
num=$4
rev=0
while [ $num -gt 0 ]
do
    r=$(($num%10))
    rev=$(($rev*10))
    rev=$(($rev+$r))
    num=$(($num/10))
done
    echo "Reverse no. : $rev"
;;
3)
exit
;;
*)
    echo "Invalid choice"
;;
esac
done
fi


awk menu

BEGIN{
 menu()
}

{
 if(ch==1)
 factorial()
 if(ch==2)
 greater()
 if(ch==3)
 prime()
 if(ch==4)
 numpal()
 if(ch==5)
 strpal()
 if(ch==6)
 exit
}

#functions

function menu()
{
 system("clear")

 printf("\n\nenter your choice:")
 getline ch<"-"
}

function factorial()
{
 printf("enter the number:")
 getline a<"-"
 f=1
 if(a==0)
 print 1
 for(i=1;i<=a;i++)
 f=f*i
 printf("\nfactorial of %d is :%d",a,f)
 getline z"<"
 menu()

}

function greater()
{
 printf("enter the value of a:")
 getline a<"-"
 printf("enter the value of b:")
 getline b<"-"
 printf("enter the value of c:")
 getline c<"-"
 if(a>b && a>c)
 printf("\n%d is greatest",a)
 else if(b>a && b>c)
 printf("\n%d is greatest",b)
 else
 printf("\n%d is greatest",c)
 getline z<"-"
 menu()
}

function prime()
{
 printf("enter the number:")
 getline a<"-"
 i=2
 flag=1
 while(i<a)
 {
  if(a%i==0)
  {
   flag=0
   break
  }
  i++
 }
 if(flag==1)
 printf("\nit is a prime number")
 else
 printf("\nnot a prime number")
 getline z<"-"
 menu()
}

function strpal()
{
 printf("enter the string:")
 getline str<"-"
 i=1
 flag=0
 len=length(str)
 while(i<len)
 {
  ch1=substr(str,i,1)
  ch2=substr(str,len,1)
  if(ch1!=ch2)
  {
   flag=1
   break
  }
  i++
  len--
 }
 if(flag==0)
 printf("\nstring is palindrome")
 else
 printf("\nnot a palindrome")
 getline z<"-"
 menu()
}

function numpal()
{
 printf("\nenter a number:")
 getline num<"-"
 rev=0
 org=num
 while(num>0)
 {
  r=int(num%10)
  num=int(num/10)
  rev=int(rev*10+r)
 }
 if(rev==org)
 printf("\nthe number is a palindrome")
 else
 printf("\nthe number is not a palindrome")
 getline z<"-"
 menu()
}

awk database

#Title: Write a  awk program to handle student data base with options given below,
#    a) Create data base. b) View Data Base. c) Insert a record.
#    d) Delete a record. e) Modify a record. f) Result of a particular
#       student. g) Exit.

BEGIN{
main();
}
function main()
{
   
        printf("\n1. Insert")
        printf("\n2. View")
        printf("\n3. Search")
        printf("\n4. Modify")
        printf("\n5. Delete")
        printf("\n6. Result Analysis")
        printf("\n7. Exit")
        printf("\nPlease enter your choice\t")
        getline choice
       
        if(choice==1)
        {
            printf("\nEnter your roll number\t")
            getline roll_no
            printf("\nEnter the name\t")
            getline name
            printf("\nEnter the marks")
            printf("\nDBMS : ")
            getline dbms
            printf("SE : ")
            getline se
            printf("CNT : ")
            getline cnt
            printf("OS : ")
            getline os
            printf("TOC : ")
            getline toc
            printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\n",roll_no,name,dbms,se,cnt,os,toc)>>"student.txt"
            #printf("\n%d|%s",rollno,name)>>"stud1.txt";
        }
       
        else if(choice==2)
        {
            printf("\nRoll No\tName\tDBMS\tSE\tCNT\tOS\tTOC")
            printf("\n=======================================================================")
            while(getline < "student.txt"==1)
            {
                printf("\n%d\t%s\t%d\t%d\t%d\t%d\t%d",$1,$2,$3,$4,$5,$6,$7)
            }
        }

        else if(choice==3)
        {
            printf("\nEnter the roll number to be searched\t")
            getline roll_no
            flag=0
            while(getline < "student.txt"==1)
            {
                if($1==roll_no)
                {
                    printf("\n%d\t%s\t%d\t%d\t%d\t%d\t%d\n",$1,$2,$3,$4,$5,$6,$7)
                    flag=1
                    break
                }
            }
            if(flag==0)
                printf("\nRecord not found\n")
        }

        else if(choice==4)
        {
            printf("\nEnter the roll number to be modified\t")
            getline roll_no
            flag=0
            while(getline < "student.txt"==1)
            {
                if($1==roll_no)
                {
                    printf("\nEnter new roll number\t")
                    getline rno
                    printf("\nEnter the new name\t")
                    getline name
                    printf("\nEnter the new marks")
                    printf("\nDBMS : ")
                    getline dbms
                    printf("SE : ")
                    getline se
                    printf("CNT : ")
                    getline cnt
                    printf("OS : ")
                    getline os
                    printf("TOC : ")
                    getline toc
                    printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\n",roll_no,name,dbms,se,cnt,os,toc)>>"temp.txt"
                    flag=1
                }
                else
                {
                    printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\n",$1,$2,$3,$4,$5,$6,$7)>>"temp.txt"
                }
            }
            "mv temp.txt student.txt"|getline
            #"rm temp.txt"|getline

            if(flag==1)
                printf("\nRecord modified successfully\n")
            else
                printf("\nRecord not found\n")
        }
       
        else if(choice==5)
        {
            printf("\nEnter the roll number to be deleted\t")
            getline roll_no
            flag=0
            while(getline<"student.txt"==1)
            {
                if($1!=roll_no)
                {
                    printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\n",$1,$2,$3,$4,$5,$6,$7)>>"temp.txt"
                }
                else
                    flag=1
            }
            "mv temp.txt student.txt"|getline
       
            if(flag==0)
                printf("\nRecord not found\n")
            else
                printf("\nRecord deleted sucessfully\n")
        }
       
        else if(choice==6)
        {
            printf("\nEnter the roll number\t")
            getline roll_no
            flag=0
            while(getline<"student.txt"==1)
            {
                if($1==roll_no)
                {
                    printf("\nTotal = %d",($3+$4+$5+$6+$7))
                printf("\nPercentage = %2.2f ",($3+$4+$5+$6+$7)/5)
                    flag=1
                    break
                }
            }
           
            if(flag==0)
                printf("\nRecord not found")
        }

        else if(choice==7)
        {
            exit 0
        }
        printf("\n")
}       

sjf p


#include<stdio.h>
#include<malloc.h>
typedef struct process
{
    int p_name;
    int cpu_time,arrival_time,finish_time,turn_time;
    int waiting_time,start_time,cpu_temp,flag;
}process;

int main()
{
    int no,i,total_cpu_time=0,j,processes,next_proc,current,x;
    float avg_turn_time=0,avg_waiting_time=0;
    process obj[7];
    process small;
    int waiting_proc=0,minus;
    printf("\nEnter no of processes:");
    scanf("\n%d",&no);

//Accepts input from user
    for(i=0;i<no;i++)
    {
        printf("\nEnter Process ID, Arrival Time and CPU time\n");
        scanf("%d%d%d",&obj[i].p_name,&obj[i].arrival_time,&obj[i].cpu_time);
        obj[i].cpu_temp=obj[i].cpu_time;
        total_cpu_time=total_cpu_time+obj[i].cpu_time;
    }
    for(i=0;i<no;i++)
    {
        obj[i].flag=0;
    }
//Sort according to CPU time
    processes=1;
    current=0;
    next_proc=1;
    small=obj[0];
    i=1;
    printf("\n\tRunning Time\tProcess No\n");

    while(i<=total_cpu_time)
    {
        if(obj[current].cpu_temp == obj[current].cpu_time)
        {
            obj[current].start_time=i-1;
        }
        minus=i-1;
        printf("\n\t%d--%d\t\tp%d",minus,i,current);
        obj[current].cpu_temp=obj[current].cpu_temp - 1;
       
        if(obj[current].cpu_temp==0 && obj[current].flag==0)
        {
            obj[current].flag=1;
            obj[current].finish_time=i;
            printf("\np%d terminates here...",obj[current].p_name);
            printf("\nFinish Time=%d",obj[current].finish_time);
            waiting_proc=waiting_proc-1;
        }
       
        if(obj[next_proc].arrival_time==i && next_proc!=no)
        {
            next_proc=next_proc+1;
            processes=processes+1;
            waiting_proc=waiting_proc+1;
        }

        small=obj[current];
        for(x=0;x<processes;x++)
        {
            if((small.cpu_temp>obj[x].cpu_temp) && (obj[x].flag==0))
                {
                small=obj[x];
                current=x;
                }
                if((small.cpu_temp<obj[x].cpu_temp) && (small.flag==1))
                    {
                    small=obj[x];
                current=x;
                }
        }

            /*  if(waiting_proc==0 && processes==no)
           {
            for(x=0;x<processes;x++)
            {
                if(obj[x].flag==0)
                {
                    small=obj[x];       
                    current=x;
                }
             }
            }
        */
       i++;           
    }

//calculates time for each process

    for(i=0;i<no;i++)
    {
        obj[i].turn_time=obj[i].finish_time-obj[i].arrival_time;
        obj[i].waiting_time=obj[i].turn_time-obj[i].cpu_time;
       
        avg_turn_time=avg_turn_time+obj[i].turn_time;
        avg_waiting_time=avg_waiting_time+obj[i].waiting_time;
    }

//Display all times
printf("\n=======================================================================================================");
    printf("\nP_ID\t   Arrival_Time\t   CPU_time\t   Start_Time\t   Finish_Time\t   Turn_Time\t   Waiting_Time");
printf("\n=======================================================================================================");
    for(i=0;i<no;i++)
    {
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d",obj[i].p_name,obj[i].arrival_time,obj[i].cpu_time,obj[i].start_time,obj[i].finish_time,obj[i].turn_time,obj[i].waiting_time);
    }
    printf("\n");
    avg_turn_time=avg_turn_time/no;
    avg_waiting_time=avg_waiting_time/no;
   
    printf("\nAverage Turnaround Time=%f",avg_turn_time);
    printf("\nAverage Waiting Time=%f",avg_waiting_time);
    printf("\n");

return 0;
}