Something went wrong. Try again.
extentions for bash language
Something went wrong. Try again.
Extended_bash basic-math-cal.sh
11 kB · 432 lines
Shell
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433#!/bin/bash
# Basic Math Calculator# A demonstration of the Bash System Language framework
# Import required modulessource lib.sh
import core.systemimport math.basicimport stringimport uiimport io
# Calculator statedeclare -a CALC_HISTORY=()declare -i CALC_HISTORY_INDEX=0declare -i CALC_HISTORY_MAX=100declare -i CALC_PRECISION=2
# Calculator functionscalc_add() { local a="$1" local b="$2" if ! is_number "$a" || ! is_number "$b"; then error "Both arguments must be numbers" return ${EXIT_FAILURE:-1} fi local result=$(add "$a" "$b") echo "$result"}
calc_sub() { local a="$1" local b="$2" if ! is_number "$a" || ! is_number "$b"; then error "Both arguments must be numbers" return ${EXIT_FAILURE:-1} fi local result=$(sub "$a" "$b") echo "$result"}
calc_mul() { local a="$1" local b="$2" if ! is_number "$a" || ! is_number "$b"; then error "Both arguments must be numbers" return ${EXIT_FAILURE:-1} fi local result=$(mul "$a" "$b") echo "$result"}
calc_div() { local a="$1" local b="$2" if ! is_number "$a" || ! is_number "$b"; then error "Both arguments must be numbers" return ${EXIT_FAILURE:-1} fi if [[ "$b" -eq 0 ]]; then error "Division by zero is not allowed" return ${EXIT_FAILURE:-1} fi local result=$(div "$a" "$b") echo "$result"}
calc_mod() { local a="$1" local b="$2" if ! is_number "$a" || ! is_number "$b"; then error "Both arguments must be numbers" return ${EXIT_FAILURE:-1} fi if [[ "$b" -eq 0 ]]; then error "Modulo by zero is not allowed" return ${EXIT_FAILURE:-1} fi local result=$(mod "$a" "$b") echo "$result"}
calc_pow() { local base="$1" local exp="$2" if ! is_number "$base" || ! is_number "$exp"; then error "Both arguments must be numbers" return ${EXIT_FAILURE:-1} fi if [[ "$exp" -lt 0 ]]; then error "Negative exponents are not supported" return ${EXIT_FAILURE:-1} fi local result=$(pow "$base" "$exp") echo "$result"}
calc_sqrt() { local num="$1" if ! is_number "$num"; then error "Argument must be a number" return ${EXIT_FAILURE:-1} fi if [[ "$num" -lt 0 ]]; then error "Square root of negative number is not supported" return ${EXIT_FAILURE:-1} fi local result=$(sqrt_int "$num") echo "$result"}
calc_factorial() { local n="$1" if ! is_number "$n"; then error "Argument must be a number" return ${EXIT_FAILURE:-1} fi if [[ "$n" -lt 0 ]]; then error "Factorial of negative number is not supported" return ${EXIT_FAILURE:-1} fi local result=$(fact "$n") echo "$result"}
calc_gcd() { local a="$1" local b="$2" if ! is_number "$a" || ! is_number "$b"; then error "Both arguments must be numbers" return ${EXIT_FAILURE:-1} fi local result=$(gcd "$a" "$b") echo "$result"}
calc_lcm() { local a="$1" local b="$2" if ! is_number "$a" || ! is_number "$b"; then error "Both arguments must be numbers" return ${EXIT_FAILURE:-1} fi local result=$(lcm "$a" "$b") echo "$result"}
calc_is_prime() { local num="$1" if ! is_number "$num"; then error "Argument must be a number" return ${EXIT_FAILURE:-1} fi if is_prime "$num"; then echo "true" else echo "false" fi}
calc_fib() { local n="$1" if ! is_number "$n"; then error "Argument must be a number" return ${EXIT_FAILURE:-1} fi if [[ "$n" -lt 0 ]]; then error "Fibonacci sequence index must be non-negative" return ${EXIT_FAILURE:-1} fi local result=$(fib_n "$n") echo "$result"}
# History managementadd_to_history() { local expression="$1" local result="$2" CALC_HISTORY[$CALC_HISTORY_INDEX]="$expression = $result" ((CALC_HISTORY_INDEX++)) # Keep history within limits if [[ $CALC_HISTORY_INDEX -ge $CALC_HISTORY_MAX ]]; then # Remove oldest entry unset CALC_HISTORY[0] # Shift array local -a temp_history=("${CALC_HISTORY[@]}") CALC_HISTORY=() for ((i=1; i<${#temp_history[@]}; i++)); do CALC_HISTORY[$((i-1))]="${temp_history[$i]}" done ((CALC_HISTORY_INDEX--)) fi}
show_history() { if [[ $CALC_HISTORY_INDEX -eq 0 ]]; then echo "No calculation history yet" return fi echo "Calculation History:" echo "===================" for ((i=0; i<CALC_HISTORY_INDEX; i++)); do local entry="${CALC_HISTORY[$i]}" local expr="${entry%%=*}" local result="${entry#*=}" printf "%3d: %s = %s\n" $((i+1)) "$expr" "$result" done}
clear_history() { CALC_HISTORY=() CALC_HISTORY_INDEX=0 echo "History cleared"}
# Expression evaluatorevaluate_expression() { local expression="$1" # Remove whitespace expression=$(echo "$expression" | tr -d ' ') # Handle basic operations if [[ "$expression" =~ ^(.+)\+(.+)$ ]]; then local a="${BASH_REMATCH[1]}" local b="${BASH_REMATCH[2]}" calc_add "$a" "$b" elif [[ "$expression" =~ ^(.+)\-(.+)$ ]]; then local a="${BASH_REMATCH[1]}" local b="${BASH_REMATCH[2]}" calc_sub "$a" "$b" elif [[ "$expression" =~ ^(.+)\*(.+)$ ]]; then local a="${BASH_REMATCH[1]}" local b="${BASH_REMATCH[2]}" calc_mul "$a" "$b" elif [[ "$expression" =~ ^(.+)/(.+)$ ]]; then local a="${BASH_REMATCH[1]}" local b="${BASH_REMATCH[2]}" calc_div "$a" "$b" elif [[ "$expression" =~ ^(.+)\%(.+)$ ]]; then local a="${BASH_REMATCH[1]}" local b="${BASH_REMATCH[2]}" calc_mod "$a" "$b" elif [[ "$expression" =~ ^(.+)\^(.+)$ ]]; then local base="${BASH_REMATCH[1]}" local exp="${BASH_REMATCH[2]}" calc_pow "$base" "$exp" elif [[ "$expression" =~ ^sqrt\((.+)\)$ ]]; then local num="${BASH_REMATCH[1]}" calc_sqrt "$num" elif [[ "$expression" =~ ^factorial\((.+)\)$ ]]; then local n="${BASH_REMATCH[1]}" calc_factorial "$n" elif [[ "$expression" =~ ^gcd\((.+),(.+)\)$ ]]; then local a="${BASH_REMATCH[1]}" local b="${BASH_REMATCH[2]}" calc_gcd "$a" "$b" elif [[ "$expression" =~ ^lcm\((.+),(.+)\)$ ]]; then local a="${BASH_REMATCH[1]}" local b="${BASH_REMATCH[2]}" calc_lcm "$a" "$b" elif [[ "$expression" =~ ^is_prime\((.+)\)$ ]]; then local num="${BASH_REMATCH[1]}" calc_is_prime "$num" elif [[ "$expression" =~ ^fib\((.+)\)$ ]]; then local n="${BASH_REMATCH[1]}" calc_fib "$n" else # Try to evaluate as a simple number if is_number "$expression"; then echo "$expression" else error "Invalid expression: $expression" return ${EXIT_FAILURE:-1} fi fi}
# Interactive calculatorinteractive_calculator() { echo "Basic Math Calculator" echo "====================" echo "Enter 'help' for available commands" echo "Enter 'quit' to exit" echo while true; do echo -n "calc> " read -r input case "$input" in "quit"|"exit"|"q") echo "Goodbye!" break ;; "help"|"h") show_help ;; "history"|"hist"|"!") show_history ;; "clear"|"c") clear_history ;; ""|*) if [[ -n "$input" ]]; then local result if result=$(evaluate_expression "$input" 2>/dev/null); then echo "Result: $result" add_to_history "$input" "$result" else echo "Error: Invalid expression or calculation failed" fi fi ;; esac done}
# Help systemshow_help() { echo "Available Commands:" echo "===================" echo "Basic Operations:" echo " 5 + 3 - Addition" echo " 10 - 4 - Subtraction" echo " 6 * 7 - Multiplication" echo " 15 / 3 - Division" echo " 10 % 3 - Modulo" echo " 2 ^ 8 - Power" echo "" echo "Advanced Functions:" echo " sqrt(16) - Square root" echo " factorial(5) - Factorial" echo " gcd(12, 8) - Greatest common divisor" echo " lcm(6, 8) - Least common multiple" echo " is_prime(7) - Check if number is prime" echo " fib(10) - Fibonacci number" echo "" echo "Other Commands:" echo " help - Show this help" echo " history - Show calculation history" echo " clear - Clear history" echo " quit - Exit calculator" echo "" echo "Examples:" echo " calc> 2 + 3" echo " Result: 5" echo " calc> sqrt(16)" echo " Result: 4" echo " calc> factorial(5)" echo " Result: 120"}
# Command line interfacemain() { local command="${1:-interactive}" case "$command" in "interactive"|"") interactive_calculator ;; "eval"|"e") if [[ -z "$2" ]]; then error "Expression required for eval mode" echo "Usage: $0 eval <expression>" return ${EXIT_FAILURE:-1} fi local result if result=$(evaluate_expression "$2"); then echo "$result" return 0 else return ${EXIT_FAILURE:-1} fi ;; "help"|"h"|"-h"|"--help") show_help ;; "version"|"v"|"-v"|"--version") echo "Basic Math Calculator v1.0.0" echo "Built with Bash System Language framework" ;; *) error "Unknown command: $command" echo "Usage: $0 [command] [args]" echo "Commands: interactive, eval, help, version" return ${EXIT_FAILURE:-1} ;; esac}
# Run main function if script is executed directlyif [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@"fi