#!/bin/bash

keypad_fn()
{
  clear
  echo -e "\033[33G\033[7m*    MCALC    *\033[0m"
  echo
  echo -e "\033[33G7      8      9"
  echo -e "\033[33G4      5      6"
  echo -e "\033[33G1      2      3"
  echo -e "\033[33G+      0      -"
  echo -e "\033[33G*      /      ="
  echo
}

calc_fn()
{
  keypad_fn
  row=9
  until [ $row -eq 24 ]
  do
    tput cup $row
    echo -n "$no. $num"
    read input
      if   echo "$input" | grep -q ^quit; then
           clear
           exit 0
      elif echo "$input" | grep -q ^clear; then
           row=24
           no=1
      elif [ -z "$num$input" ]; then
           row=$((row+1))
           no=$((no+1))
      else
           sum=`echo "scale=2; $num$input" | bc`
           column=`echo "$no. $num$input" | wc -m`
           tput cup $row $column
           echo -n "= $sum"
           row=$((row+1))
           no=$((no+1))
           stty -echo
           read -n 1 key
           stty echo
           case $key in
             [0-9])
               num=$key
               ;;
             +|-|\*|/)
               num=$sum$key
               ;;
             *)
               unset num
               ;;
           esac
      fi
  done
  calc_fn
}

unset num
no=1
calc_fn
