Showing posts with label C Programing. Show all posts
Polish Mathematics of prefix, postfix , infix expressions & Algorithms
Friday, February 5
Posted by
ss
Tag :
Algorithms
,
C Programing
,
Data Structure
,
Polish Mathematics
,
postfix & infix expressions
,
prefix
,
Stack
Polish notation
The grate polish mathematician came up with a new technique for representation and calculation of arithmetic expression where operator will be before or after the operand called polish notation.Normal expression A+B
Prefix +AB
Postfix AB+
Infix A+N
Example questions --
convert the following expression to prefix & postfix{[(A+B)/C] *(D-E)}
Prefix
We have to solve above expression according to the priory of operatorsFirst we solve the brackets
={[(+AB)/C]*(-DE)}
={[/+ABC]*(-DE)}
={*/+ABC-DE}
Prefix expression is */+ABC-DE
Postfix
={[(AB+)/C]*(DE-)}
={[AB+C/]*(DE-)}
={AB+CD/DE-*}
=AB+CD/DE-*
postfix expression is AB+CD/DE-*
Algorithms for converting infix to postfix using stack
- Add a unique symbol # into stack and add it in the end of array infix. A*(B+C^D)-E^F #
- Scan the symbol of array infix one by one from left to right.
- Symbol is left parenthesis '(' then add it to the array.
- Symbol is operand then add it to array postfix.
- Symbol is operator then pop the operator which have same priority or higher priority then operator which occurred .
- Add the pop operator to array.
- Add the scaned symbole into stack.
- Symbol is right parenthesis ')' then pop all the operator from the stack.
- Symbol is # then pop all the symbol from stack & add them to array except #.
- You ave done it .
For example .
Infix expression is A*(B+C^D)-E^F
A*(B+C^D)-E^F #
| Symbole | Stack | Postfix expression |
| A | A | |
| * | * | A |
| ( | *( | A |
| B | *(+ | AB |
| + | *(+ | AB |
| C | *(+ | ABC |
| ^ | *(+^ | ABC |
| D | *(+^ | ABCD |
| ) | * | ABCD^+ |
| - | - | ABCD^+* |
| E | - | ABCD^+*E |
| ^ | -^ | ABCD^+*E |
| F | -^ | ABCD^+*EF |
| * | -* | ABCD^+*EF^ |
| ( | -*( | ABCD^+*EF^ |
| G | -*( | ABCD^+*EF^G |
| / | -*(/ | ABCD^+*EF^G |
| H | -*(/ | ABCD^+*EF^GH |
| ) | -* | ABCD^+*EF^GH/ |
| # | ABCD^+*EF^GH/*- |
We would like an interactive session. Comment your question below.
Be Updated Be safe !
Cheers !!
Introduction to Data structure
Data structure is a way to organized data in some way so that we can perform operations on the data in effective way.Some examples are listed below.
1) Size of data.
2) Amount of data.
For Implementing Data structure We need to understand Dynamic Memory Allocation
Pointers - A pointer is variable which contain the memory address of another variable.
Declaration Of Pointer variable -
int a; // Normal variable Declaration
int *a; // Pointer Variable Declaration
Operators Of Pointer -
- & - Address Operator
- * - Value at the address operator
Example of Pointer -
#include<stdio.h> // Header files
Void main()
{
int a=5;
int *p ;
p = &a ;
printf("\n %d",a); // 5
printf("\n %d",p); // address 25001
printf("\n %u",&a); //25001
printf("\n %u",&a); // 25002
printf("\n %d",*p); // 5
printf("\n %d",*(&a)); // 5
return 0;
}
Dynamic Memory Allocation
The process allocating at the time of execution is called dynamic memory allocation. The allocation and releasing of the memory space can be done with the use of some built in functions.
- sizeof()
- malloc()
- calloc()
- free()
sizeoff()
The size of operation is use to find the size of argument in terms of byte . the argument can be variable or any data type.
Example.
int a[10] ;
sizeof(a); // 20
sizeof(float); // 4
malloc()
This function is use to allocate the memory space.
Syntax - ptr =(data type *) malloc( specific size) ;
Example.
ptr =(int*) malloc(10) ;
Calloc()
This function is used to allocate the multiple blocks of memory.
syntax - ptr = (int*) calloc( number of block , size of block ) ;
Example.
ptr =(int*) calloc ( 5 , 2) ; // this allocate 5 blocks , each of 2 bytes
free()
This function is use to release the memory spaces.
Syntax - free(ptr);
Example.
free(ptr) ; // memory set to free
we would like to have an interactive session. comment your questions below.