Showing posts with label conversion. Show all posts
Showing posts with label conversion. Show all posts

Tuesday, February 17, 2015

C Program and Algorithm for Conversion of an Expression from Infix to Postfix

In infix notation or expression operators are written in between the operands while in postfix notation every operator follows all of its operands.

Example:
Infix Expression: 5+3*2
Postfix Expression: 5 3 2*+.

Algorithm for Conversion of an Expression from Infix to Postfix

Let Q be any infix expression and we have to convert it to postfix expression P. For this the following procedure will be followed.

1. Push left parenthesis onto STACK and add right parenthesis at the end of Q.

2. Scan Q from left to right and repeat step 3 to 6 for each element of Q until the STACK is empty.

3. If an operand is encountered add it to P.

4. If a left parenthesis is encountered push it onto the STACK.

5. If an operator is encountered, then
  • Repeatedly pop from STACK and add to P each operator which has same precedence as or higher precedence than the operator encountered.
  • Push the encountered operator onto the STACK.

6. If a right parenthesis is encountered, then
  • Repeatedly pop from the STACK and add to P each operator until a left parenthesis is encountered.
  • Remove the left parenthesis; do not add it to P.

7. Exit

Also Read: C Program and Algorithm for Evaluation of a Postfix Expression
Also Read: What is Quick Sort? Algorithm and C Program to Implement Quick Sort

An example of converting infix expression into postfix form, showing stack status after every step is given below. Here RPN stands for reverse polish notation (postfix notation).

An example of converting infix expression into postfix form, showing stack status after every step


C Program for Conversion of an Expression from Infix to Postfix

// Operator supported: +,-,*,/,%,^,(,)
// Operands supported: all single character operands

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

#define MAX 50

typedef struct stack
{
    int data[MAX];
    int top;
}stack;

int precedence(char);
void init(stack *);
int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *,int);
int top(stack *);   //value of the top element
void infix_to_postfix(char infix[],char postfix[]);

void main()
{
    char infix[30],postfix[30];
    printf("Enter an infix expression(eg: 5+2*4): ");
    gets(infix);
    infix_to_postfix(infix,postfix);
    printf("
Postfix expression: %s",postfix);
}

void infix_to_postfix(char infix[],char postfix[])
{
    stack s;
    char x,token;
    int i,j;    //i-index of infix,j-index of postfix
    init(&s);
    j=0;

    for(i=0;infix[i]!=
Read more »

Saturday, January 24, 2015

ActionScript 3 Number to String Conversion

In this lesson, were going to learn how to convert numbers to strings in AS3.

If you youd like to do ActionScript 3 Number to String conversions, then you can use the toString() method of the Number class.

Here is an example:
var myNumber:Number = 7;
var myString:String = myNumber.toString();
Here, we start off by creating a number - myNumber with a value of 7. In the next line, that number is converted into a string and is assigned to a variable named myString.

Why would I want to convert numbers to strings?
One example of how this can be useful is if youd like to calculate some number value and then display it inside a text field.

For example:
var value1:Number = 7;
var value2:Number = 2;
var total:Number = value1 + value2;

myTextField.text = total.toString();
// Assume that myTextField is an instance of the TextField class
// and that it has already been created and added to the stage
Here, were creating two numbers (7 and 2), which are then added together (which sums up to 9). The sum is then displayed inside a text field. Without the toString() method, Flash will give us an error message if we try to assign a Number value inside the text field. The error message will state:
1067: Implicit coercion of a value of type Number to an unrelated type String.
This means that we are trying to force a number to be a string. A text field can not contain Number data so we will have to convert it to a String instead. Although, 9 as a Number looks the same as "9" as a String, you must still explicitly differentiate between the two inside your ActionScript 3 code. This makes AS3 number to string conversions pretty useful.
 
If the number is a decimal (e.g. .5), Flash will add a leading 0 when it performs the number to string conversion (i.e. .5 will become "0.5").

Also, the toString() method of the Number class accepts one parameter for the radix. The radix lets you specify which numeric base (from 2 to 36) to use when the number is converted into a string. For example, if youd like the number to be interpreted as an octal (base 8), then you pass a value of 8 to the radix parameter:
var myNumber:Number = 14;
trace( myNumber.toString(8) );
If we used base 10, then this will still output 14. But since we specified base 8 instead, this will output 16. If no radix is specified, a default value of 10 (decimal) is used.

So that is how you do ActionScript 3 number to string conversions.
Read more »