]> Softwares of Agnibho - equsol.git/blob - equsol
Initialized repo
[equsol.git] / equsol
1 #! /bin/bash
2
3 # Copyright (c) 2017 Agnibho Mondal
4 # All rights reserved
5 #
6 # This file is part of Equsol.
7 #
8 # Equsol is free software: you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation, either version 3 of the License, or (at your option) any later
11 # version.
12 #
13 # Equsol is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15 # PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Equsol. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # Agnibho Mondal
21 # contact@agnibho.com
22 # www.agnibho.com
23
24 DIR="/home/agnibho/program/equsol/data/";
25
26 function calc {
27 echo -e "\n$1\n";
28 IFS="=" read LHS RHS <<< "$1";
29 readarray -t VARS <<< "$(grep -oP "\[.*?\]" <<< "$RHS")";
30 EQ=$RHS;
31 for v in "${VARS[@]}"
32 do
33 read -p "$(echo "$v" | sed -E "s/\[|\]//g") = " val;
34 if ! [[ $val =~ ^-?[0-9]+([.][0-9]+)?$ ]];then
35 echo "Invalid input."
36 exit 1;
37 fi
38 var=$(echo "$v" | sed -e 's/[]\/$*.^|[]/\\&/g');
39 EQ="$(echo "$EQ" | sed "s/$var/$val/g")";
40 done
41 RES=$(echo "$EQ"|bc -l|sed "s/0*$//g"|sed "s/\.$//g"|sed "s/^\./0./g");
42 echo -e "\n$(echo "$LHS" | sed -E "s/\[|\]|^ | $//g") = \033[1m$RES\033[0m";
43 }
44
45 if [ -f "$DIR$1" ];then
46 EQS=();
47 while read line;do
48 if grep -q "^#" <<<"$line";then
49 continue;
50 elif grep -q "=" <<< "$line";then
51 EQS+=("$line");
52 fi
53 done < "$DIR$1"
54 if [ ${#EQS[@]} -gt 1 ];then
55 for i in "${!EQS[@]}"
56 do
57 echo "($i) ${EQS[$i]}";
58 done
59 read -p "Enter equation number: " i;
60 if ! [[ $i =~ ^[0-9]+$ ]];then
61 echo "Invalid input.";
62 exit 1;
63 fi
64 if [ -n "${EQS[$i]}" ];then
65 calc "${EQS[$i]}";
66 else
67 echo "Invalid equation number.";
68 exit 1;
69 fi
70 elif [ ${#EQS[@]} -eq 1 ];then
71 calc "${EQS[0]}";
72 else
73 echo "No equation found";
74 exit 1;
75 fi
76 else
77 echo "Available equations: ";
78 ls "$DIR";
79 fi