

Java symbolic calculator shunting yard code#
The EvaluateArithmetic function in above code is used to find the result of arithmetic operation of a single operator. Simple calculator in C implementing Shunting-yard algorithm. (there should be only one token left in the stack)ĭouble result = If(token.Type = ConstantToken) //if constantĮlse if(token.Type = OperatorToken) //if operator NSMutableArray *stack = init] įor(int index = 0 index < index+=1) + (double)Evaluate:(NSMutableArray*)postfixTokens This algorithm is explained here, in detail with a live usable demo.Īfter the input expression is converted from infix-form to postfix-form, the following code is used to evaluate the result of postfix-form of the expression. Typedef enum OperatorAssociativity Įvaluating the Expression This program uses Shunting-yard Algorithm to convert the input expression from infix to postfix form before evaulating the result. OperatorAssociativity (nonatomic) OperatorSymbol (nonatomic, retain) NSString (nonatomic) int (nonatomic) int (nonatomic) OperatorAssociativity TokenType, PrimitiveDataType, OperatorSymbol and OperatorAssociativity are enumerations defined as follows. NSString (nonatomic) PrimitiveDataType (nonatomic, retain) NSString Operator : NSObject We have created the following two classes namely Constant and Operator in order to represent a token in the expression. That is, a token can be a number or operator. Int (nonatomic) TokenType (nonatomic, retain) NSString (nonatomic) int input expression can contain numbers and operators only. The Token class given below, is used to hold a single token in the input expression. You can enter the full-expression and find the evaluated result in a single step.Īn expression is an array of tokens. Using this expression-calculator, you can find the result of (8+5)*(7+2) in a single step. Also, you have to memorize these results (or use Memory option in calculator) in order to do the third calculation ( 13*9 in this case).

Shunting-yard algorithm, which modifies Jesse Browns original code. No need to do multiple steps or memorize the intermediate results, for calculating the result of a single arithmetic expression.įor example, in order to evaluate the result of (8+5)*(7+2) in a traditional calculator, you have to calculate the result of (8+5) and (7+2) separately and then multiply the result of these two sub-expressions ( 13 and 9 in this case). Useful as a Calculator or for helping you write your own Programming Language. Using this expression-calculator in your iPhone, you can do advanced arithmetic calculations in a single step.
