diff --git a/src/main/java/EquationSolver.java b/src/main/java/EquationSolver.java new file mode 100644 index 00000000..c4aba974 --- /dev/null +++ b/src/main/java/EquationSolver.java @@ -0,0 +1,93 @@ +import java.util.ArrayList; + +public class EquationSolver { + static String eq = "1/3"; + static String num = ""; + static ArrayList hold; + + public static void main(String[] args) { + hold = new ArrayList(); + for (int i = 0; i < eq.length(); i++) { + if (eq.charAt(eq.length() - 1) == '+' || eq.charAt(eq.length() - 1) == '-' + || eq.charAt(eq.length() - 1) == '/' || eq.charAt(eq.length() - 1) == '*') { + System.out.println("Not A Valid Equation"); + System.exit(0); + } + + if (Character.isDigit(eq.charAt(i))) { + num += "" + eq.charAt(i); + + } else { + // int add = Integer.parseInt(num); + hold.add(num); + num = ""; + + } + + if (eq.charAt(i) == '+') { + hold.add("+"); + } + if (eq.charAt(i) == '-') { + hold.add("-"); + } + if (eq.charAt(i) == '*') { + hold.add("*"); + } + if (eq.charAt(i) == '/') { + hold.add("/"); + } + /* + * 5+6*7-8/2 5+42-8/2 5+42-4 47-4 43 + */ + } + hold.add(num); + num = ""; + + + boolean done = false; + String[] symbols = { "*", "/", "+", "-" }; + int currentIndex = 0; + String currentSym = symbols[currentIndex]; + while (!done) { + boolean found = false; + + for (int i = 0; i < hold.size(); i++) { + if (hold.get(i).equals(currentSym)) { + double fVal = Double.parseDouble(hold.get(i - 1)); + double sVal = Double.parseDouble(hold.get(i + 1)); + double solution = 0; + if (hold.get(i).equals("*")) { + solution = fVal * sVal; + } + if (hold.get(i).equals("/")) { + solution = fVal / sVal; + } + if (hold.get(i).equals("+")) { + solution = fVal + sVal; + } + if (hold.get(i).equals("-")) { + solution = fVal - sVal; + } + hold.remove(i - 1); + hold.remove(i - 1); + String ans = "" + solution; + hold.set(i - 1, ans); + found = true; + break; + } + } + if (!found) { + currentIndex++; + + if (currentIndex >= symbols.length) { + done = true; + } else { + + currentSym = symbols[currentIndex]; + } + } + } + + } + +} diff --git a/src/main/java/org/jointheleague/discord_bot_example/Bot.java b/src/main/java/org/jointheleague/discord_bot_example/Bot.java index 06e330bf..4f3fdb4a 100644 --- a/src/main/java/org/jointheleague/discord_bot_example/Bot.java +++ b/src/main/java/org/jointheleague/discord_bot_example/Bot.java @@ -31,6 +31,7 @@ public Bot(String token, String channelName) { helpListener = new _HelpListener(channelName); } + public void connect(boolean printInvite) { api = new DiscordApiBuilder().setToken(token).login().join(); @@ -47,9 +48,9 @@ public void connect(boolean printInvite) { api.addMessageCreateListener(dl); helpListener.addHelpEmbed(dl.getHelpEmbed()); - CurrencyConverter cc = new CurrencyConverter(channelName); - api.addMessageCreateListener(cc); - helpListener.addHelpEmbed(cc.getHelpEmbed()); +// CurrencyConverter cc = new CurrencyConverter(channelName); +// api.addMessageCreateListener(cc); +// helpListener.addHelpEmbed(cc.getHelpEmbed()); ToDoList list = new ToDoList(channelName); api.addMessageCreateListener(list); @@ -127,6 +128,9 @@ public void connect(boolean printInvite) { DiscordZoomAccess dza = new DiscordZoomAccess(channelName); api.addMessageCreateListener(dza); helpListener.addHelpEmbed(dza.getHelpEmbed()); + + EquatonSolver eq = new EquatonSolver(channelName); + api.addMessageCreateListener(eq); CovidCaseGetter covid = new CovidCaseGetter(channelName); api.addMessageCreateListener(covid); @@ -192,6 +196,7 @@ public void connect(boolean printInvite) { api.addMessageCreateListener(new RandomCase(channelName)); api.addMessageCreateListener(new GetTime(channelName)); api.addMessageCreateListener(new ScreenCapture(channelName)); + api.addMessageCreateListener(new LisztsLists(channelName)); api.addMessageCreateListener(new StarSignSeeker(channelName)); api.addMessageCreateListener(new War(channelName)); //api.addMessageCreateListener(new Depression(channelName)); diff --git a/src/main/java/org/jointheleague/discord_bot_example/Launcher.java b/src/main/java/org/jointheleague/discord_bot_example/Launcher.java index fdbfb858..fabc5d46 100644 --- a/src/main/java/org/jointheleague/discord_bot_example/Launcher.java +++ b/src/main/java/org/jointheleague/discord_bot_example/Launcher.java @@ -25,6 +25,7 @@ public void launch(String[] args) { // Load all of the bots for every channel for (int i = 0; i < channels.length; i++) { new Bot(n.getToken(), channels[i]).connect(i == 0); + } } } diff --git a/src/main/java/org/jointheleague/modules/EquatonSolver.java b/src/main/java/org/jointheleague/modules/EquatonSolver.java new file mode 100644 index 00000000..f1defb1e --- /dev/null +++ b/src/main/java/org/jointheleague/modules/EquatonSolver.java @@ -0,0 +1,197 @@ +package org.jointheleague.modules; + +import java.util.ArrayList; +import java.util.Random; + +import org.javacord.api.event.message.MessageCreateEvent; + +import net.aksingh.owmjapis.api.APIException; + +public class EquatonSolver extends CustomMessageCreateListener { + private static final String command = "!equation"; + + public EquatonSolver(String channelName) { + super(channelName); + + } + + @Override + public void handle(MessageCreateEvent event) throws APIException { + // TODO Auto-generated method stub + + if (event.getMessageContent().contains("!equation")) { + String eqws = event.getMessageContent().substring(9); + String eq = ""; + for (int i = 0; i < eqws.length(); i++) { + if (eqws.charAt(i) == ' ') { + continue; + } else { + eq += eqws.charAt(i); + + } + } + //eq is good here + String num = ""; + ArrayList hold; + + hold = new ArrayList(); + for (int i = 0; i < eq.length(); i++) { + + if (Character.isDigit(eq.charAt(i))) { + num=""; + num += "" + eq.charAt(i); + + } else { + if(num.equals("")) { + + } + // int add = Integer.parseInt(num); + else { + hold.add(num); + } + + + } + + if (eq.charAt(i) == '+') { + hold.add("+"); + } + if (eq.charAt(i) == '-') { + hold.add("-"); + } + if (eq.charAt(i) == '*') { + hold.add("*"); + } + if (eq.charAt(i) == '/') { + hold.add("/"); + } + if (eq.charAt(i) == '%') { + hold.add("%"); + + } + if (eq.charAt(i) == '(') { + hold.add("("); + } + if (eq.charAt(i) == ')') { + hold.add(")"); + } + + } +// ArrayList input = new ArrayList(); +// +// for (int i = start; i < end; i++) { +// +// input.add("" + eq.charAt(i)); +// +// +// } +// +// +// + +// solveEquation(input); +// System.out.println("input " +solveEquation(input)); +// hold.add(0, solveEquation(input)); + + event.getChannel().sendMessage(solveEquation(hold)); + } + + } + + public String solveEquation(ArrayList hold) { + + boolean done = false; + String[] symbols = {"*", "/", "%", "-", "+" }; + int currentIndex = 0; + String currentSym = symbols[currentIndex]; + while (!done) { + boolean found = false; + + for (int i = 0; i < hold.size(); i++) { +//System.out.println("hold.geti is " + hold.get(i)); + if (hold.get(i).equals(currentSym)) { + double fVal = Double.parseDouble(hold.get(i - 1)); + double sVal = Double.parseDouble(hold.get(i + 1)); + double solution = 0; + + if (hold.get(i).equals("*")) { + solution = fVal * sVal; + + } + if (hold.get(i).equals("/")) { + solution = fVal / sVal; + + } + if (hold.get(i).equals("+")) { + solution = fVal + sVal; + + } + if (hold.get(i).equals("-")) { + solution = fVal - sVal; + + } + if (hold.get(i).equals("%")) { + solution = fVal % sVal; + + } + + + hold.remove(i - 1); + hold.remove(i - 1); + String ans = "" + solution; + hold.set(i - 1, ans); + found = true; + break; + } + if(hold.get(i).equals("(")) { + + solveParenth(hold, i); + } + + } + if (!found) { + currentIndex++; + + if (currentIndex >= symbols.length) { + done = true; + } else { + + currentSym = symbols[currentIndex]; + } + } + } + String ans = hold.get(0); + return (ans); + + } + public void solveParenth(ArrayList hold, int place) { + ArrayList in = new ArrayList(); + int end = -1; + for(int i = place+=1;i