Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Sunday 28 April 2019

IOPP Program NO.1 A

Program No.1.A

Demonstrate the following coding standards by writing a C program to evaluate a postfix evaluation:
 1. Naming convention.
 2. Active Names for functions.
 3. Consistent indentations and brace styles. 
4. Proper parenthesizing to resolve the ambiguity. 
5. Proper documentation. 

Solution:

#include<stdio.h>

struct stack{
int array[20];
int top;
}s;

void push(int element)
{
        s.array[++s.top] = element;
}

int pop()
{
        return s.array[s.top--];
}

int main()
{
        char exp[20];
        char *e;
        int n1,n2,n3,num;
        s.top=-1;
        printf("Enter the expression :: ");
        scanf("%s",exp);
        e = exp;
        while(*e != '\0')
        {
                if(isdigit(*e))
                {
                        num = *e - 48;
                        push(num);
                }
                else
                {
                        n1 = pop();
                        n2 = pop();
                        switch(*e)
                        {
                                case '+':
                                {
                                        n3 = n1 + n2;
                                        break;
                                }
                                case '-':
                                {
                                        n3 = n2 - n1;
                                        break;
                                }
                                case '*':
                                {
                                        n3 = n1 * n2;
                                        break;
                                }
                                case '/':
                                {
                                        n3 = n2 / n1;
                                        break;
                                }
                        }
                        push(n3);
                }
                e++;
        }
        printf("\nThe result of expression %s  =  %d\n\n",exp,pop());
        return 0;


}

2 comments: