Home > Net >  How to write a bash program with a quadratic function?
How to write a bash program with a quadratic function?

Time:12-06

I need a program that calculates zeros from a square function like below with only one difference if I enter variables that are not integers or floating point, a syntax error is displayed. eg "there are no such values" Anyone know how to do it?

#!/bin/bash
echo "PROGRAM DO OBLICZANIA DELTY"

echo "Podaj A: "
read A
echo "Podaj B: "
read B
echo "Podaj C: "
read C
DELTA="$((B*B-4*A*C))"
PIERW=`echo "scale=5 ; sqrt($DELTA)" | bc`
CZYZERO=`echo "$DELTA==0" | bc`
CZYWIEKSZE=`echo "$DELTA>0" | bc`
echo "Delta wynosi: $DELTA , jej pierwiastek to: $PIERW"

if [ "$CZYZERO" = 1 ]; then
    P0=`echo "scale5 ;(-1*$B)/(2*$A)" | bc`
    echo "Rownanie posiada jeden pierwiastek: $P0"
elif [ "$CZYWIEKSZE" = 1 ]; then
    P1=`echo "scale=5 ;(-1*$B-$PIERW)/(2*$A)" | bc`
    P2=`echo "scale=5 ;(-1*$B $PIERW)/(2*$A)" | bc`
    echo "Rozwnanie posiada dwa pierwiastki: $P1 i $P2"
else
    echo "To rownanie nie posiada pierwiastkow"
fi

CodePudding user response:

It sounds like you are trying to write a program that calculates the zeros of a quadratic function. The challenge you are facing is that you want the program to handle input values that are not valid numbers, such as letters or symbols.

One way to solve this problem is to use the case statement in Bash to check the input values and handle them appropriately. The case statement allows you to match the input values against a series of patterns and then execute a specific set of commands for each pattern that matches.

Here is an example of how you could use the case statement to check the input values and handle them appropriately in your program:

#!/bin/bash

# Get the input values
a=$1
b=$2
c=$3

# Check the input values and handle them appropriately
case $a in
    # If the value is a valid number, calculate the zeros
    [0-9]* | [0-9]*.[0-9]* )
        # Calculate the zeros of the quadratic function
        zero1=$((-b   sqrt(b*b - 4*a*c)) / (2*a))
        zero2=$((-b - sqrt(b*b - 4*a*c)) / (2*a))

        # Print the zeros
        printf "Zero 1: %s\n" $zero1
        printf "Zero 2: %s\n" $zero2
    ;;
    # If the value is not a number, print an error message
    * )
        printf "Error: Invalid input value '%s'\n" $a
    ;;
esac

In this example, the case statement checks the value of $a and uses a pattern to match it against different types of input values. If the value is a valid number, it calculates the zeros of the quadratic function and prints them. If the value is not a number, it prints an error message.

You can use similar logic to check the values of $b and $c and handle them in the same way. This should allow your program to handle input values that are not valid numbers and display an appropriate error message.

CodePudding user response:

#!/bin/bash echo "PROGRAM DO OBLICZANIA DELTY"

echo "Podaj A: "
read A
echo "Podaj B: "
read B
echo "Podaj C: "
read C
A=$1
B=$2
C=$3
case $A in [0-9]* | [0-9]*.[0-9]* )
case $B in [0-9]* | [0-9]*.[0-9]* )
case $C in [0-9]* | [0-9]*.[0-9]* )
DELTA="$((B*B-4*A*C))"
PIERW=`echo "scale=5 ; sqrt($DELTA)" | bc`
CZYZERO=`echo "$DELTA==0" | bc`
CZYWIEKSZE=`echo "$DELTA>0" | bc`
echo "Delta wynosi: $DELTA , jej pierwiastek to: $PIERW"

if [ "$CZYZERO" = 1 ]; then
    P0=`echo "scale5 ;(-1*$B)/(2*$A)" | bc`
    echo "Rownanie posiada jeden pierwiastek: $P0"
elif [ "$CZYWIEKSZE" = 1 ]; then
    P1=`echo "scale=5 ;(-1*$B-$PIERW)/(2*$A)" | bc`
    P2=`echo "scale=5 ;(-1*$B $PIERW)/(2*$A)" | bc`
    echo "Rozwnanie posiada dwa pierwiastki: $P1 i $P2"
else
    echo "To rownanie nie posiada pierwiastkow"
fi

it still does not work

  • Related