Results 1 to 2 of 2

Thread: calculator

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default calculator

    Hi,

    I need to make sure my code is clean and in right format and acceptable by a coder. This is a simple calculator with all 4 operators. Did I do this right? Is there a better shorter way?
    I'm new to coding, Did I follow good coding format with spaces?

    Thanks.
    _____________________________

    #!/bin/bash
    timeout=1

    minus="1. Minus ( - )"
    plus="2. Plus ( + )"
    divide="3. Division ( / )"
    multiply="4. Multipication ( * )"
    quit="5. Quit"


    function calc ()
    {
    clear
    while true; do
    read -p "Val1: " num1
    if [[ $num1 =~ ^-?[0-9]+$ ]]; then
    break
    else
    echo "wrong"
    sleep $timeout
    clear
    fi
    done

    while [[ $REPLY != 0 ]]; do
    clear
    echo "Val1:" $num1
    cat <<- _EOF_

    Operator:

    $minus
    $plus
    $divide
    $multiply
    $quit

    _EOF_
    read -p "Enter Selection [1-5] > "
    echo

    if [[ $REPLY =~ ^[1-4]$ ]]; then
    if [[ $REPLY == 1 ]]; then
    op=-
    break
    fi
    if [[ $REPLY == 2 ]]; then
    op=+
    break
    fi
    if [[ $REPLY == 3 ]]; then
    op=/
    break
    fi
    if [[ $REPLY == 4 ]]; then
    op=*
    break
    fi

    if [[ $REPLY == 5 ]]; then
    echo "Bye"
    sleep $timeout
    echo " "
    exit 0
    fi

    else

    echo "Not a valid selection"
    sleep $timeout
    fi
    done
    while true; do
    clear
    echo "Val1: "$num1
    cat <<- _EOF_

    Operator:

    $minus
    $plus
    $divide
    $multiply
    $quit

    _EOF_

    read -p "Val2: " num2
    if [[ $num2 =~ ^-?[0-9]+$ ]]; then
    break
    else
    echo "wrong"
    sleep $timeout
    clear
    fi
    done


    total=$(($num1 $op $num2))
    clear
    echo "Val1: "$num1
    cat <<- _EOF_

    Operator:

    $minus
    $plus
    $divide
    $multiply
    $quit

    _EOF_

    echo "Val2: "$num2
    echo " "
    echo "Your operation is -> $num1 $op $num2 = $total"
    echo
    sleep 20
    again
    }

    function again ()
    {
    while [[ $REPLY != 0 ]]; do
    clear
    echo
    echo "Your operation is -> $num1 $op $num2 = $total"
    echo



    read -p "Start Again? [y-n]: "

    if [[ $REPLY == "y" ]]; then
    calc
    break
    fi
    if [[ $REPLY == "n" ]]; then
    echo "Goodbye"
    sleep $timeout
    echo " "
    exit 0

    else
    echo " "
    echo "Not a valid selection"
    sleep $timeout
    fi
    done
    }

    calc

  2. #2
    Member billconner's Avatar
    Join Date
    Jul 2010
    Posts
    88
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Rep Power
    4

    Default

    Quote Originally Posted by sam12 View Post
    Hi,

    I need to make sure my code is clean and in right format and acceptable by a coder. This is a simple calculator with all 4 operators. Did I do this right? Is there a better shorter way?
    I'm new to coding, Did I follow good coding format with spaces?

    Thanks.
    _____________________________

    #!/bin/bash
    timeout=1

    minus="1. Minus ( - )"
    plus="2. Plus ( + )"
    divide="3. Division ( / )"
    multiply="4. Multipication ( * )"
    quit="5. Quit"


    function calc ()
    {
    clear
    while true; do
    read -p "Val1: " num1
    if [[ $num1 =~ ^-?[0-9]+$ ]]; then
    break
    else
    echo "wrong"
    sleep $timeout
    clear
    fi
    done

    while [[ $REPLY != 0 ]]; do
    clear
    echo "Val1:" $num1
    cat <<- _EOF_

    Operator:

    $minus
    $plus
    $divide
    $multiply
    $quit

    _EOF_
    read -p "Enter Selection [1-5] > "
    echo

    if [[ $REPLY =~ ^[1-4]$ ]]; then
    if [[ $REPLY == 1 ]]; then
    op=-
    break
    fi
    if [[ $REPLY == 2 ]]; then
    op=+
    break
    fi
    if [[ $REPLY == 3 ]]; then
    op=/
    break
    fi
    if [[ $REPLY == 4 ]]; then
    op=*
    break
    fi

    if [[ $REPLY == 5 ]]; then
    echo "Bye"
    sleep $timeout
    echo " "
    exit 0
    fi

    else

    echo "Not a valid selection"
    sleep $timeout
    fi
    done
    while true; do
    clear
    echo "Val1: "$num1
    cat <<- _EOF_

    Operator:

    $minus
    $plus
    $divide
    $multiply
    $quit

    _EOF_

    read -p "Val2: " num2
    if [[ $num2 =~ ^-?[0-9]+$ ]]; then
    break
    else
    echo "wrong"
    sleep $timeout
    clear
    fi
    done


    total=$(($num1 $op $num2))
    clear
    echo "Val1: "$num1
    cat <<- _EOF_

    Operator:

    $minus
    $plus
    $divide
    $multiply
    $quit

    _EOF_

    echo "Val2: "$num2
    echo " "
    echo "Your operation is -> $num1 $op $num2 = $total"
    echo
    sleep 20
    again
    }

    function again ()
    {
    while [[ $REPLY != 0 ]]; do
    clear
    echo
    echo "Your operation is -> $num1 $op $num2 = $total"
    echo



    read -p "Start Again? [y-n]: "

    if [[ $REPLY == "y" ]]; then
    calc
    break
    fi
    if [[ $REPLY == "n" ]]; then
    echo "Goodbye"
    sleep $timeout
    echo " "
    exit 0

    else
    echo " "
    echo "Not a valid selection"
    sleep $timeout
    fi
    done
    }

    calc
    Try bc. it does all the math functions and is scriptable.

    Bill

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •