#!/bin/bash
input=$1
inputArray=()
str=""
for (( i=0; i<${#input}; i++ ))
do
if [[ ${input:$i:1} == "+" || ${input:$i:1} == "-" || ${input:$i:1} == "*" || ${input:$i:1} == "/" || ${input:$i:1} == "(" || ${input:$i:1} == ")" || ${input:$i:1} == "^" || ${input:$i:1} == "%" || ${input:$i:1} == "_" || ${input:$i:1} == "{" || ${input:$i:1} == "}" || ${input:$i:1} == "," ]]
then
if [[ ${#str} > 0 ]]
then
inputArray+=(${str})
fi
inputArray+=("${input:$i:1}")
str=""
elif [[ ${input:$i:1} == "!" || ${input:$i:1} == "=" || ${input:$i:1} == "<" || ${input:$i:1} == ">" ]]
then
if [[ ${i+1} < ${#input} && ${input:${i}+1:1} == "=" ]]
then
if [[ ${#str} > 0 ]]
then
inputArray+=(${str})
fi
inputArray+=("${input:$i:1}${input:${i}+1:1}")
str=""
i=$((i+1))
else
if [[ ${#str} > 0 ]]
then
inputArray+=(${str})
fi
inputArray+=("${input:$i:1}")
str=""
fi
elif [[ ${input:$i:1} == " " ]]
then
if [[ ${#str} > 0 ]]
then
inputArray+=(${str})
fi
str=""
else
str+="${input:$i:1}"
fi
done
if [[ ${#str} > 0 ]]
then
inputArray+=(${str})
fi
for i in "${inputArray[@]}"
do
echo "$i"
done
function prec () {
if [[ $1 == "," ]]
then
echo 1
elif [[ $1 == "=" ]]
then
echo 2
elif [[ $1 == "!=" ]]
then
echo 2
elif [[ $1 == ">=" ]]
then
echo 2
elif [[ $1 == "<=" ]]
then
echo 2
elif [[ $1 == "==" ]]
then
echo 2
elif [[ $1 == "<" ]]
then
echo 2
elif [[ $1 == ">" ]]
then
echo 2
elif [[ $1 == "+" ]]
then
echo 3
elif [[ $1 == "-" ]]
then
echo 3
elif [[ $1 == "*" ]]
then
echo 4
elif [[ $1 == "/" ]]
then
echo 4
elif [[ $1 == "^" ]]
then
echo 5
elif [[ $1 == "%" ]]
then
echo 10
elif [[ $1 == "max" ]]
then
echo 10
elif [[ $1 == "min" ]]
then
echo 10
elif [[ $1 == "sum" ]]
then
echo 10
elif [[ $1 == "sin" ]]
then
echo 10
elif [[ $1 == "cos" ]]
then
echo 10
elif [[ $1 == "tan" ]]
then
echo 10
elif [[ $1 == "csc" ]]
then
echo 10
elif [[ $1 == "sec" ]]
then
echo 10
elif [[ $1 == "cot" ]]
then
echo 10
elif [[ $1 == "ln" ]]
then
echo 10
elif [[ $1 == "log" ]]
then
echo 10
elif [[ $1 == "%" ]]
then
echo 11
else
echo 0
fi
}
stack=()
postfixArray=()
for (( i=0; i<${#inputArray[@]}; i++ ))
do
ii=$(prec "${inputArray[i]}")
if [[ ${ii} > 0 ]]
then
if [[ ${#stack[@]} == 0 ]]
then
stack+=("${inputArray[i]}")
elif [[ $(prec "${stack[${#stack[@]}-1]}") < ${ii} ]]
then
stack+=("${inputArray[i]}")
elif [[ "${stack[${#stack[@]}-1]}" == "^" && "${inputArray[i]}" == "^" ]]
then
stack+=("${inputArray[i]}")
else
while true
do
if [[ ${#stack[@]} == 0 ]]
then
break
fi
iii=$(prec "${stack[${#stack[@]}-1]}")
iv=$(prec "${inputArray[i]}")
if [[ ${iii} < ${iv} ]]
then
break
fi
if [[ ${stack[${#stack[@]}-1]} == "^" && ${inputArray[i]} == "^" ]]
then
break
fi
postfixArray+=("${stack[${#stack[@]}-1]}")
unset stack[${#stack[@]}-1]
done
stack+=("${inputArray[i]}")
fi
elif [[ "${inputArray[i]}" == "(" || "${inputArray[i]}" == "{" ]]
then
stack+=("${inputArray[i]}")
elif [[ "${inputArray[i]}" == ")" ]]
then
while true
do
if [[ ${#stack[@]} == 0 ]]
then
break
elif [[ "${stack[${#stack[@]}-1]}" == "(" ]]
then
break
fi
postfixArray+=("${stack[${#stack[@]}-1]}")
unset stack[${#stack[@]}-1]
done
if [[ ${#stack[@]} > 2 && "${stack[${#stack[@]}-2]}" == "_" ]]
then
unset stack[${#stack[@]}-1]
unset stack[${#stack[@]}-1]
postfixArray+=("${stack[${#stack[@]}-1]}")
fi
unset stack[${#stack[@]}-1]
elif [[ "${inputArray[i]}" == "}" ]]
then
while true
do
if [[ ${#stack[@]} == 0 ]]
then
break
elif [[ ${stack[${#stack[@]}-1]} == "{" ]]
then
break
fi
postfixArray+=("${stack[${#stack[@]}-1]}")
unset stack[${#stack[@]}-1]
done
unset stack[${#stack[@]}-1]
else
postfixArray+=("${inputArray[i]}")
fi
done
sl=${#stack[@]}
echo ""
for (( i=0; i<sl; i++ ))
do
postfixArray+=("${stack[${#stack[@]}-1]}")
unset stack[${#stack[@]}-1]
done
for i in "${postfixArray[@]}"
do
echo "$i"
done
An implementation of a postfix algorithm in bash.
To view this on TripleLog, click here.
chmod 777 postfix.sh ./postfix.sh "3+14*5"
If you are not familiar with bash, then you can try to edit the algorithm and see what happens.
FYI: There are some weird issues with spaces and tabs.
The output will be one element in each line. The infix array will be outputted first, followed by a blank line. Then the postfix array will be displayed.
You probably want to do something different.