June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,3 +1,4 @@
|
|||
/*Corrected by Abhishek Ghosh, 6th November 2017*/
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
|
|
@ -31,7 +32,7 @@ void AddToken(stack<Entry_>* stack, const string& token)
|
|||
else
|
||||
{ // it's an operator
|
||||
if (stack->size() < 2)
|
||||
throw exception("Stack underflow");
|
||||
cout<<"Stack underflow";
|
||||
auto rhs = stack->top();
|
||||
Parenthesize(&rhs, token, false);
|
||||
stack->pop();
|
||||
|
|
@ -58,7 +59,7 @@ string ToInfix(const string& src)
|
|||
}
|
||||
}
|
||||
if (stack.size() != 1)
|
||||
throw exception("Incomplete expression");
|
||||
cout<<"Incomplete expression";
|
||||
return stack.top().expr_;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,127 @@
|
|||
/*Abhishek Ghosh, 7th November 2017*/
|
||||
|
||||
#include<stdlib.h>
|
||||
#include<string.h>
|
||||
#include<stdio.h>
|
||||
|
||||
char** components;
|
||||
int counter = 0;
|
||||
|
||||
typedef struct elem{
|
||||
char data[10];
|
||||
struct elem* left;
|
||||
struct elem* right;
|
||||
}node;
|
||||
|
||||
typedef node* tree;
|
||||
|
||||
int precedenceCheck(char oper1,char oper2){
|
||||
return (oper1==oper2)? 0:(oper1=='^')? 1:(oper2=='^')? 2:(oper1=='/')? 1:(oper2=='/')? 2:(oper1=='*')? 1:(oper2=='*')? 2:(oper1=='+')? 1:(oper2=='+')? 2:(oper1=='-')? 1:2;
|
||||
}
|
||||
|
||||
int isOperator(char c){
|
||||
return (c=='+'||c=='-'||c=='*'||c=='/'||c=='^');
|
||||
}
|
||||
|
||||
void inorder(tree t){
|
||||
if(t!=NULL){
|
||||
if(t->left!=NULL && isOperator(t->left->data[0])==1 && (precedenceCheck(t->data[0],t->left->data[0])==1 || (precedenceCheck(t->data[0],t->left->data[0])==0 && t->data[0]=='^'))){
|
||||
printf("(");
|
||||
inorder(t->left);
|
||||
printf(")");
|
||||
}
|
||||
else
|
||||
inorder(t->left);
|
||||
|
||||
printf(" %s ",t->data);
|
||||
|
||||
if(t->right!=NULL && isOperator(t->right->data[0])==1 && (precedenceCheck(t->data[0],t->right->data[0])==1 || (precedenceCheck(t->data[0],t->right->data[0])==0 && t->data[0]!='^'))){
|
||||
printf("(");
|
||||
inorder(t->right);
|
||||
printf(")");
|
||||
}
|
||||
else
|
||||
inorder(t->right);
|
||||
}
|
||||
}
|
||||
|
||||
char* getNextString(){
|
||||
if(counter<0){
|
||||
printf("\nInvalid RPN !");
|
||||
exit(0);
|
||||
}
|
||||
return components[counter--];
|
||||
}
|
||||
|
||||
tree buildTree(char* obj,char* trace){
|
||||
tree t = (tree)malloc(sizeof(node));
|
||||
|
||||
strcpy(t->data,obj);
|
||||
|
||||
t->right = (isOperator(obj[0])==1)?buildTree(getNextString(),trace):NULL;
|
||||
t->left = (isOperator(obj[0])==1)?buildTree(getNextString(),trace):NULL;
|
||||
|
||||
if(trace!=NULL){
|
||||
printf("\n");
|
||||
inorder(t);
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
int checkRPN(){
|
||||
int i, operSum = 0, numberSum = 0;
|
||||
|
||||
if(isOperator(components[counter][0])==0)
|
||||
return 0;
|
||||
|
||||
for(i=0;i<=counter;i++)
|
||||
(isOperator(components[i][0])==1)?operSum++:numberSum++;
|
||||
|
||||
return (numberSum - operSum == 1);
|
||||
}
|
||||
|
||||
void buildStack(char* str){
|
||||
int i;
|
||||
char* token;
|
||||
|
||||
for(i=0;str[i]!=00;i++)
|
||||
if(str[i]==' ')
|
||||
counter++;
|
||||
|
||||
components = (char**)malloc((counter + 1)*sizeof(char*));
|
||||
|
||||
token = strtok(str," ");
|
||||
|
||||
i = 0;
|
||||
|
||||
while(token!=NULL){
|
||||
components[i] = (char*)malloc(strlen(token)*sizeof(char));
|
||||
strcpy(components[i],token);
|
||||
token = strtok(NULL," ");
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argC,char* argV[]){
|
||||
int i;
|
||||
tree t;
|
||||
|
||||
if(argC==1)
|
||||
printf("Usage : %s <RPN expression enclosed by quotes> <optional parameter to trace the build process>",argV[0]);
|
||||
else{
|
||||
buildStack(argV[1]);
|
||||
|
||||
if(checkRPN()==0){
|
||||
printf("\nInvalid RPN !");
|
||||
return 0;
|
||||
}
|
||||
|
||||
t = buildTree(getNextString(),argV[2]);
|
||||
|
||||
printf("\nFinal infix expression : ");
|
||||
inorder(t);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
function parseRPNstring(rpns)
|
||||
infix = []
|
||||
rpn = split(rpns)
|
||||
for tok in rpn
|
||||
if all(isnumber, tok)
|
||||
push!(infix, parse(Int, tok))
|
||||
else
|
||||
last = pop!(infix)
|
||||
prev = pop!(infix)
|
||||
push!(infix, Expr(:call, Symbol(tok), prev, last))
|
||||
println("Current step: $infix")
|
||||
end
|
||||
end
|
||||
infix
|
||||
end
|
||||
|
||||
unany(s) = replace(string(s), r"Any\[:\((.+)\)\]", s"\1")
|
||||
|
||||
println("The final infix result: ", parseRPNstring("3 4 2 * 1 5 - 2 3 ^ ^ / +") |> unany, "\n")
|
||||
println("The final infix result: ", parseRPNstring("1 2 + 3 4 + ^ 5 6 + ^") |> unany)
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
// version 1.2.0
|
||||
|
||||
import java.util.Stack
|
||||
|
||||
class Expression(var ex: String, val op: String = "", val prec: Int = 3) {
|
||||
|
||||
constructor(e1: String, e2: String, o: String) :
|
||||
this("$e1 $o $e2", o, OPS.indexOf(o) / 2)
|
||||
|
||||
override fun toString() = ex
|
||||
|
||||
companion object {
|
||||
const val OPS = "-+/*^"
|
||||
}
|
||||
}
|
||||
|
||||
fun postfixToInfix(postfix: String): String {
|
||||
val expr = Stack<Expression>()
|
||||
val rx = Regex("""\s+""")
|
||||
for (token in postfix.split(rx)) {
|
||||
val c = token[0]
|
||||
val idx = Expression.OPS.indexOf(c)
|
||||
if (idx != -1 && token.length == 1) {
|
||||
val r = expr.pop()
|
||||
val l = expr.pop()
|
||||
val opPrec = idx / 2
|
||||
if (l.prec < opPrec || (l.prec == opPrec && c == '^')) {
|
||||
l.ex = "(${l.ex})"
|
||||
}
|
||||
if (r.prec < opPrec || (r.prec == opPrec && c != '^')) {
|
||||
r.ex = "(${r.ex})"
|
||||
}
|
||||
expr.push(Expression(l.ex, r.ex, token))
|
||||
}
|
||||
else {
|
||||
expr.push(Expression(token))
|
||||
}
|
||||
println("$token -> $expr")
|
||||
}
|
||||
return expr.peek().ex
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val es = listOf(
|
||||
"3 4 2 * 1 5 - 2 3 ^ ^ / +",
|
||||
"1 2 + 3 4 + ^ 5 6 + ^"
|
||||
)
|
||||
for (e in es) {
|
||||
println("Postfix : $e")
|
||||
println("Infix : ${postfixToInfix(e)}\n")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue