Monday, 21 October 2013

awk

BEGIN{
        main();
        fact=1;
         rev=0;
    }
function main()
{
    do
    {
        printf("\n1.Factorial\n2.Prime Number\n3.Biggest of 3 no's\n4.Palindrome\
            \n5.Number Palindrome\n6.Exit");
        printf("\nEnter Choice  :  ");
        getline ch;
        if(ch==1)
        {
            printf("\n Enter The Number  :  ");
            getline num;
            fact=1;
            for(i=num;i>0;i--)
                {
                    fact=fact*i;
                }
            printf("\nFactorial of %d is %d\n\n",num,fact);
        }
        if(ch==2)
        {
            printf("\n Enter The Number:");
            getline num;
            if(num==1 || num==2)
               
            for(i=2;i>num;i++)
                   {
                    if(num%i==0)
                    {
                                                f=1;
                        break;
                                        }
                    else
                        f=0;
                }
            if(f==1)
                printf("%d is not a prime number",num);
            else
                printf("%d is a prime number",num);               
        }

        if(ch==3)
        {
            printf("\n Enter First Number");
            getline num1;
            printf("\n Enter Second Number");
            getline num2;
            printf("\n Enter Third Number");
            getline num3;
            if(num1>num2 && num1>num3)
                printf("%d is the greatest",num1);
            else if(num2>num3)
                printf("%d is the greatest",num2);
            else
                printf("%d is the greatest",num3);
        }
        if(ch==4)
        {
            printf("\nEnter The String:");
            getline str;
            len=length(str)
            for(i=len;i>0;i--)
            {
                rev=substr(str,i,1);
                str1=str1 rev
            }
            if(str1==str)
                printf("\nString is Palindrome");
            else
                printf("\nString is not a Palindrome");
        }
        if(ch==5)
        {
            printf("\n Enter The Number:");
            getline num;
            rev=0;
            num1=num
            while(num>=10)
              {
                rem=num%10;
                num=int(num/10);
                rev=rev+rem;
                rev=rev*10;
            }
            rev=rev+num;
            if(rev==num1)
                printf("\nNumber is Palindrome");
            else
                printf("\nNumber is not a Palindrome");
        }

    }while(ch!=6)
}

-----------------------------------------------------------

{

printf "Enter string :: ";
getline name;
l=length(name);
for(i=l;i>0;i--)
{
    rev=substr(name,i,1);
    str=str rev;

}
if(str==name)
{
printf "String is palindrome";

}
else
{

printf "String is not palindrome";
}
}
END{
}

----------------------------------------------------------
BEGIN{
FS=":"
main();
}
function create()
{
    printf("\nEnter Number of Records:");
    getline num;
    for(i=0;i<num;i++)
    {
        printf("\nEnter Roll No :");
        getline rollno;
        printf("\nEnter Name :");
        getline name;
        printf("\nEnter Marks of Subject M1 :");
        getline m1;
        printf("\nEnter Marks of Subject M2 :");
        getline m2;
        printf("\nEnter Marks of Subject M3 :");
        getline m3;
        printf("%d:%s:%d:%d:%d\n",rollno,name,m1,m2,m3)>>"stud.txt";
    }
}
function display()
{
    printf("ROLLNO\tNAME\tSUB1\tSUB2\tSUB3");
    while(getline <"stud.txt"==1)
    {
        printf("\n%d\t%s\t%d\t%d\t%d",$1,$2,$3,$4,$5);
    }
}
function modify()
{
    ff=0;
    printf("\nEnter Rollno of Student to modify:");
    getline rollno
    while(getline<"stud.txt"==1)
            {
        if(rollno==$1)
        {
                      name=$2
            ff=1;
            printf("Enter New Marks for M1,M2,M3:");
            getline m1;
             getline m2;
                              getline m3;
                printf("%d:%s:%d:%d:%d\n",rollno,name,m1,m2,m3)>>"new.txt";
        }
        else
        {
           print>"new.txt"
        }
    }
    system("rm stud.txt")
    system("mv new.txt stud.txt")
}
function delet()
{
    f=0;
    printf("\nEnter Roll No you want to delete:");
    getline rollno;
    while(getline<"stud.txt"==1)
    {
      if(rollno!=$1)
              {
        print>"new.txt"
      }
    }
    system("rm stud.txt")
    system("mv new.txt stud.txt")
}
function search()
{
    printf("\nEnter Roll No of Student to Search:");
    getline rollno;
    f=0;
    while(getline<"stud.txt"==1)
    {       
      if(rollno==$1)
      {
        printf("\nRecord is present and Name is:%s",$2);
        f=1;
        break;
      }
    }
    if(f==0)
        printf("\nRecord is not present");
}
function main()
{
    printf("\n1.Create DataBase\n2.Delete a Record\n3.Modify a Record\n4.Search a   Record\n5.DisplayDataBase");           
    printf("\nEnter your choice: ");
           getline ch
      if (ch==1)
         create();
      if (ch==2)
         delet();
        if (ch==3)
         modify();
      if (ch==4)
         search();
      if (ch==5)
          display();
      printf("\n\nDo you want to continue(1:yes,0:no):");
       getline ans
      if (ans==1)
        system("awk -f ASSN2.awk")
}

No comments:

Post a Comment