Tuesday, 16 October 2012

Program to evaluate an expression entered in postfix form.

Program to evaluate an expression entered in postfix form.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>

#define MAX 50

struct postfix
{
    int stack[MAX] ;
    int top, nn ;
    char *s ;
} ;

void initpostfix ( struct postfix * ) ;
void setexpr ( struct postfix *, char * ) ;
void push ( struct postfix *, int ) ;
int pop ( struct postfix * ) ;
void calculate ( struct postfix * ) ;
void show ( struct postfix ) ;

void main( )
{
    struct postfix q ;
    char expr[MAX] ;

    clrscr( ) ;

    initpostfix ( &q ) ;


    printf ( "\nEnter postfix expression to be evaluated: " ) ;
    gets ( expr ) ;

    setexpr ( &q, expr ) ;
    calculate ( &q ) ;
    show ( q ) ;

    getch( ) ;
}

/* initializes data members */
void initpostfix ( struct postfix *p )
{
    p -> top = -1 ;
}

/* sets s to point to the given expr. */
void setexpr ( struct postfix *p, char *str )
{
    p -> s = str ;
}

/* adds digit to the stack */
void push ( struct postfix *p, int item )
{
    if ( p -> top == MAX - 1 )
        printf ( "\nStack is full." ) ;
    else
    {
        p -> top++ ;
        p -> stack[p -> top] = item ;
    }
}

/* pops digit from the stack */
int pop ( struct postfix *p )
{
    int data ;

    if ( p -> top == -1 )
    {
        printf ( "\nStack is empty." ) ;
            return NULL ;
    }

    data = p -> stack[p -> top] ;
    p -> top-- ;

    return data ;
}

/* evaluates the postfix expression */
void calculate( struct postfix *p )
{
    int n1, n2, n3 ;
    while ( *( p -> s ) )
    {
        /* skip whitespace, if any */
if ( *( p -> s ) == ' ' || *( p -> s ) == '\t' )
        {
            p -> s++ ;
            continue ;
        }

        /* if digit is encountered */
if ( isdigit ( *( p -> s ) ) )
        {
            p -> nn = *( p -> s ) - '0' ;
            push ( p, p -> nn ) ;
        }
        else
        {
            /* if operator is encountered */

            n1 = pop ( p ) ;
            n2 = pop ( p ) ;
            switch ( *( p -> s ) )
            {
                case'+' :
                      n3 = n2 + n1 ;
                      break ;

                case'-' :
                      n3 = n2 - n1 ;
                      break ;

                case'/' :
                      n3 = n2 / n1 ;
                      break ;

                case'*' :
                      n3 = n2 * n1 ;
                      break ;

                case'%' :
                      n3 = n2 % n1 ;
                      break ;

                case'$' :
                      n3 = pow ( n2 , n1 ) ;
                      break ;

                default :
                      printf ( "Unknown operator" ) ;
                      exit ( 1 ) ;
            }

            push ( p, n3 ) ;
        }
        p -> s++ ;
    }
}

/* displays the result */
void show ( struct postfix p )
{
    p.nn = pop ( &p ) ;
    printf ( "Result is: %d", p.nn ) ;
}

Code for Singly Linked list with following operations INSERT AT STARTING, INSERT AT MIDDLE, INSERT AT END, DELETE FIRST NODE, DELETE LAST NODE, DELETE MIDDLE in C Programming


Singly Linked list with below operations

1.CREATION
2.INSERT AT STARTING
3.INSERT AT MIDDLE(USER'S CHOICE)
4.INSERT AT END
5.DELETE 1ST NODE
6.DELETE LAST NODE
7.DELETE MIDDLE NODE(USER'S CHOICE)
8.DISPLAY 

#include<conio.h>
#include<stdio.h>

struct node
{
    int i;
    struct node *next;
};

void main()
{

    struct node *first;
    struct node *last;

    struct node *temp;
    int ch,user,add,cnt=0,t=0;
    struct node *p;

    clrscr();

    printf("\n\t 1.CREATION");
    printf("\n\t 2.INSERT AT STARTING");
    printf("\n\t 3.INSERT AT MIDDLE(USER'S CHOICE)");
    printf("\n\t 4.INSERT AT END");
    printf("\n\t 5.DELETE 1ST NODE");
    printf("\n\t 6.DELETE LAST NODE");
    printf("\n\t 7.DELETE MIDDLE NODE(USER'S CHOICE)");
    printf("\n\t 8.DISPLAY");
    printf("\n\t 10.EXIT");
    scanf("%d",&user);

    while(user!=10)
    {
      if(user==1)
      {
        printf("\n\t ENTER DATA  ::: ");
        first=(struct node*)malloc(sizeof(struct node));
        scanf("%d",&ch);

        first->i=ch;
        first->next=0;
        p=first;
        cnt=1;
      }
      if(user==2)
      {
            p=(struct node*)malloc(sizeof(struct node));
            printf("\n\t ENTER DATA FOR 1ST NODE");
            scanf("%d",&p->i);
            p->next=first;
            first=p;
            cnt++;
      }
      if(user==4)
      {
            p=(struct node*)malloc(sizeof(struct node));
            printf("\n\t ENTER DATA FOR LAST NODE");
            scanf("%d",&p->i);
            temp=first;
            while(temp->next!=0)
            {
                temp=temp->next;
            }
            temp->next=p;
            p->next=0;
            cnt++;
      }
      if(user==3)
      {
            printf("\n\t ENTER ANY ADDRESS BETWEEN 1 and %d",cnt);
            scanf("%d",&add);

            t=1;
            p=first;
            while(t!=add)
            {
                p=p->next;
                t++;
            }
            temp=(struct node*)malloc(sizeof(struct node));
            printf("\n\t ENTER DATA FOR NODE");
            scanf("%d",&temp->i);
            temp->next=p->next;
            p->next=temp;
            cnt++;
      }
      if(user==5)
      {
            p=first;
            first=p->next;
            free(p);
      }
      if(user==6)
      {
        p=first;
        while(p->next->next!=0)
        {
            p=p->next;
        }
        p->next=0;
        free(p->next->next);
      }
      if(user==8)
      {
        p=first;
        while(p!=0)
        {
            printf("\n\t %d",p->i);
            p=p->next;
        }
      }
      if(user==7)
      {
            printf("\n\t ENTER ANY ADDRESS BETWEEN 1 and %d",cnt);
            scanf("%d",&add);

            t=1;
            p=first;
            while(t<add-1)
            {
                p=p->next;
                t++;
            }
            temp=p->next;
            p->next=temp->next;
            free(temp);
            cnt--;

      }
      printf("\n\t 1.CREATION");
      printf("\n\t 2.INSERT AT STARTING");
      printf("\n\t 3.INSERT AT MIDDLE(USER'S CHOICE)");
      printf("\n\t 4.INSERT AT END");
      printf("\n\t 5.DELETE 1ST NODE");
      printf("\n\t 6.DELETE LAST NODE");
      printf("\n\t 7.DELETE MIDDLE NODE(USER'S CHOICE)");
      printf("\n\t 8.DISPLAY");
      printf("\n\t 10.EXIT");
      scanf("%d",&user);
       }
       getch();
}

Practical no-10


Practical no-10

#!/bin/bash
echo "1. all user list"
echo "2.current user"
echo "3.count logged users"
printf "Enter choice:"read choice
if [$choice==1];
then
Who -h
Fi

if [$choice==2];
then
who am i
fi

if [$choice==3];
then
echo "Total users loged in:"
fi

OUTPUT
***********

[root@LINTEL]$ sh user.sh
1. all user list
2. current user
3. count logged user
Enter choice:2
Root

Practical no-8: Write a awk script that uses all its Features


Practical no-8: Write a awk script that uses all its Features

As an example of using numeric expressions, look at the following script that counts the number of blank
lines in a file:

#!/bin/sh
for i in $@ ;
do
    if [ -f $i ] ; then
        echo $i
        awk ' /^ *$/ { x=x+1 ; print x ; }' $i
    else
        echo "ERROR: $i not a file." >&2
    fi
done

In the awk command, you increment the variable x and print it each time a blank line is encountered.
Because a new instance of the awk command runs for each file, the count is unique of each file.
Consider the file urls.txt, which contains four blank lines:
$ cat urls.txt
http://www.cusa.berkeley.edu/~ranga

http://www.cisco.com

ftp://prep.ai.mit.edu/pub/gnu/
ftp://ftp.redhat.com/

http://www.yahoo.com/index.html
ranga@kanchi:/home/ranga/pub

ranga@soda:/home/ranga/docs/book/ch01.doc

For urls.txt, the output of this script looks like the following:
urls.txt
1
2
3
4

Monday, 15 October 2012

Practical no-12: Shell script to search an element from an araay using binay search


Practical no-12: Shell script to search an element from an araay using binay search

echo Enter array limit
read limit
echo Enter elements
n=1
while [ $n -le $limit ]
do
read num
eval arr$n=$num
n=`expr $n + 1`
done
echo Enter key element
read key
low=1
high=$n
found=0
while [ $found -eq 0 -a $high -gt $low ]
do
mid=`expr \( $low + $high \) / 2`
eval t=\$arr$mid
if [ $key -eq $t ]
then
found=1
elif [ $key -lt $t ]
then
high=`expr $mid - 1`
else
low=`expr $mid + 1`
fi
done

if [ $found -eq 0 ]
then
echo Unsuccessfull search
else
echo Successfull search
fi

OUTPUT
***********

[root@LINTEL]$ sh temporary.sh
Enter array limit3
Enter elements1
2
3
Enter key element2
Successfull search