diff --git a/05_Arrays/3_PrintingArrays.c b/05_Arrays/3_PrintingArrays.c index e69de29..85ccdf0 100644 --- a/05_Arrays/3_PrintingArrays.c +++ b/05_Arrays/3_PrintingArrays.c @@ -0,0 +1,16 @@ +#include + +int main() { + // Declare and initialize an array + int arr[] = {1, 2, 3, 4, 5}; + int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array + + + printf("Array elements: "); + for (int i = 0; i < size; i++) { + printf("%d ", arr[i]); + } + printf("\n"); + + return 0; +} diff --git a/infix_postfix.c b/infix_postfix.c new file mode 100644 index 0000000..2c86ede --- /dev/null +++ b/infix_postfix.c @@ -0,0 +1,175 @@ +#include +#include /* for exit() */ +#include /* for isdigit(char ) */ +#include + +#define SIZE 100 + + + +char stack[SIZE]; +int top = -1; + + + +void push(char item) +{ + if(top >= SIZE-1) + { + printf("\nStack Overflow."); + } + else + { + top = top+1; + stack[top] = item; + } +} + + +char pop() +{ + char item ; + + if(top <0) + { + printf("stack under flow: invalid infix expression"); + getchar(); + + exit(1); + } + else + { + item = stack[top]; + top = top-1; + return(item); + } +} + + +int is_operator(char symbol) +{ + if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-') + { + return 1; + } + else + { + return 0; + } +} + + +int precedence(char symbol) +{ + if(symbol == '^')/* exponent operator, highest precedence*/ + { + return(3); + } + else if(symbol == '*' || symbol == '/') + { + return(2); + } + else if(symbol == '+' || symbol == '-') /* lowest precedence */ + { + return(1); + } + else + { + return(0); + } +} + +void InfixToPostfix(char infix_exp[], char postfix_exp[]) +{ + int i, j; + char item; + char x; + + push('('); /* push '(' onto stack */ + strcat(infix_exp,")"); /* add ')' to infix expression */ + + i=0; + j=0; + item=infix_exp[i]; /* initialize before loop*/ + + while(item != '\0') /* run loop till end of infix expression */ + { + if(item == '(') + { + push(item); + } + else if( isdigit(item) || isalpha(item)) + { + postfix_exp[j] = item; /* add operand symbol to postfix expr */ + j++; + } + else if(is_operator(item) == 1) /* means symbol is operator */ + { + x=pop(); + while(is_operator(x) == 1 && precedence(x)>= precedence(item)) + { + postfix_exp[j] = x; /* so pop all higher precendence operator and */ + j++; + x = pop(); /* add them to postfix expresion */ + } + push(x); + + push(item); /* push current oprerator symbol onto stack */ + } + else if(item == ')') /* if current symbol is ')' then */ + { + x = pop(); /* pop and keep popping until */ + while(x != '(') /* '(' encounterd */ + { + postfix_exp[j] = x; + j++; + x = pop(); + } + } + else + { + printf("\nInvalid infix Expression.\n"); /* the it is illegeal symbol */ + getchar(); + exit(1); + } + i++; + + + item = infix_exp[i]; /* go to next symbol of infix expression */ + } + + + if(top>0) + { + printf("\nInvalid infix Expression.\n"); /* the it is illegeal symbol */ + getchar(); + exit(1); + } + if(top>0) + { + printf("\nInvalid infix Expression.\n"); /* the it is illegeal symbol */ + getchar(); + exit(1); + } + + + postfix_exp[j] = '\0'; /* add sentinel for puts() fucntion */ + +} + +/* main function begins */ +int main() +{ + char infix[SIZE], postfix[SIZE]; /* declare infix string and postfix string */ + + + printf("ASSUMPTION: The infix expression contains single letter variables and single digit constants only.\n"); + printf("\nEnter Infix expression : "); + gets(infix); + + InfixToPostfix(infix,postfix); /* call to convert */ + printf("Postfix Expression: "); + puts(postfix); /* print postfix expression */ + + return 0; +}