LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Can you find the error ? (160 lines) math-cases etc (https://www.linuxquestions.org/questions/programming-9/can-you-find-the-error-160-lines-math-cases-etc-88525/)

Dimitris 09-02-2003 02:55 PM

Can you find the error ? (160 lines) math-cases etc
 
Hello,

I would be pleased if you could help me as fast as possible to fix the code. I gives an error

./theproject.txt: line 138: syntax error near unexpected token `;;'
./theproject.txt: line 138: `;;'



Script :

#!/bin/bash

RANDOM=$$

let a=`expr $RANDOM % 10`
let b=`expr $RANDOM % 10`
let c=`expr $RANDOM % 10`

echo "Please enter your age:"
read age
if (( $age > 18 ))
then
echo "Generating equation..."
sleep 1
d=$(($b*$b-4*$a*$c))
if (($d<0))
then
echo "Square root is negative, equation can not be solved."
echo "Another equation will be generated for you."
./theproject.txt
fi

#z=`echo "sqrt ($b^2-4*$a*$c)" | bc -l`
#x=(-$b+$z)/2*$a

x=`echo "sqrt ( ( -$b+$d ) /2*$a )" | bc -l`

echo "Equation: " $a*x^2 + $b*x + $c

PS3="Can you solve the equation above?"

select choice in 'Yes' 'I need some help' 'No' 'Exit'
do
case $REPLY in
1)
echo "Please enter the number"
read result

if (($result==$x))
then
echo "Congratulations, you made it!"
exit 1
else
PS3="Would you like to try again?"
select choice in 'Yes' 'No'
do
case $REPLY in
1)./theproject.txt
;;
2) exit 1
;;
esac
done
fi
;;

2)
echo "To solve a quadratic equation you got to use the following formula:"
echo "(-b+-sqrt(b^2-4*a*c))/2*a"
sleep 1
PS3="Would you like to try again?"
select choice in 'Yes' 'No'
do
case $REPLY in
1)./theproject.txt
;;
2)exit 1
;;
esac
done
;;


3)

echo "It seems you can't solve it...well anyway the result is:"

d=$(($b*$b-4*$a*$c))

if (($d<0));
then
echo "D is negative, equation can not be solved!"

elif (($d==0));then
x=$((-$b/2*$a));
echo "D is equal to zero, the result is: "$x
exit 1

else

##z=`echo "sqrt ($b^$b-4*$a*$c)" | bc -l`
x=`echo "sqrt ( ( -$b+$d ) /2*$a )" | bc -l`
##x=(-$b+$z)/2*$a

echo "The two results of the equation are: "$x

fi
;;

4)
exit 1
;;
esac
done
##fi

##################################################################

else
##if (( $age < 18 ))
##then
x=$(( ( $b+$c > / $a ))#NEED TO BE DECIMAL!
echo "Generating equation..."
sleep 1
echo "Equation: " $a*x=$b+$c
PS3="Can you solve the equation above?"
select choice in 'Yes' 'I need some help' 'No' 'Exit'
do
case $REPLY in
1)
echo "Please enter the results: "
read results
if (( $results==$x ))
then
echo "Congratulations you made it!"
else
PS3="Would you like to try again?"
select choice in 'Yes' 'No'
do
case $REPLY in
1)./theproject.txt
;;
2)exit 1
;;
esac
done
fi
;;

2)
echo "To solve that kind of equation you just have to have some basic
knowledge of algebra. There is nothing else to say that go read how to
multiply and divide."
PS3="Would you like to try again?"
select choice in 'Yes' 'No'
do
case $REPLY in
1)./theproject.txt
;;
2)exit 1
;;
esac
done
;;

3)
echo "It seems you can't solve it...well anyway the result is: "
echo $x
;;

4)
exit 1
;;
esac
done
fi


Thanks ALOT for the help,
Please try it out and give me an answer if possible,
I would really appreciate it if fast :)

Bye

david_ross 09-02-2003 03:16 PM

I've only had a quick look but looking around the line number and the blocks involved it looks like hte problem is with this line:
x=$(( ( $b+$c > / $a ))#NEED TO BE DECIMAL!

Dimitris 09-02-2003 03:30 PM

Oh thanks the mistake was a bit stupid , well another problem that I have is that the generated result in the second part of the program is that it doesn't give a decimal.

How to make it show the decimal ?

Thanks again for the help!

david_ross 09-02-2003 03:35 PM

Sorry I don't really know what you mean by the second part o fthe program. Can you post what it does say (including all output) and what you want it to say.

Dimitris 09-02-2003 03:53 PM

I mean after the

##################################

that's the second part when it calculater the simple equation.
It does return an integer (1,2,3 etc...) not deciman when needed!

That's my last problem I guess.

Thanks,
Jim

david_ross 09-02-2003 04:42 PM

You can use bc. Calculate x like:
x=`echo "1+2/5" | bc -l`;


All times are GMT -5. The time now is 04:39 AM.