You are on page 1of 3

Week6::

Task1:Evaluation of postfix expression


a=[]
def push(i):
    global a,tos
    if i.isdigit():
        a+=[i]
    else:
        cal(i)
def cal(i):
    global tos
    b,c=int(a[-2]),int(a[-1])
    a[-2]=tos
    if i=="+":
        a[-1]=b+c
        tos=b+c
    elif i=="-":
        a[-1]=b-c
        tos=b-c
    elif i=="*":
        a[-1]=b*c
        tos=b*c
    elif i=="/":
        a[-1]=b/c
        tos=b/c
    elif i=="%":
        a[-1]=b%c
        tos=b%c
    elif i=="^":
        a[-1]=b^c
        tos=b^c
    else:
        pass
s=input()
b=list(map(str,s.split()))
tos=int(b[0])
for j in b:
    push(j)
print("answer is ",a[-1])

Task2:to implement queue operations


#include<stdio.h>
int front=0,rear=0;
void insert(int k,int a[50])
{
    int maxsize=50;
    front++;
    if (front<=maxsize)
    {
        a[front]=k;
    }
    else
    {
        printf("queue is full\n");
    }
}

void delete(int a[50])


{
    rear++;
    if (rear<=front)
    {
        printf("deleted element:%d\n",a[rear]);
    }
    else
    {
        printf("queue is empty\n");
    }
    
}

void traverse(int a[50])


{
    int i;
    printf("queue is ");
    if (rear<=front)
    {
    
    for (i=rear+1;i<=front;i++)
    {
        printf("%d ",a[i]);
    }
    }
    else
    {
        printf("empty\n");
    }
}
int main()
{
    int a[50],option,k;
    printf("1.insert\n2.delete\n3.traverse\n4.stop\
n");
    
    while (1)
    {
        printf("\nenter the option:");
        scanf("%d",&option);
        switch(option){
        case 1:
        {
            printf("enter a number to insert:");
            scanf("%d",&k);
            insert(k,a);
            break;
        }
        case 2:
        {
            delete(a);
            break;
        }
        case 3:
        {
            traverse(a);
            break;
        }
        case 4:
            printf("Program terminated\n");
            exit(0);
        }
        
    }
    return 0;
    
}

You might also like