Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class main {
- //a cli calculator that takes a string ussr input and returns a value for it, update for text files
- //only uses ints, but will return float values. will update to use floats in future
- //replace set with add
- public static int calc_result;
- public static void main(String[] args){
- //System.out.println(get_equation("123+24+3567"));
- calc(get_equation("123+24+3567"));
- System.out.println(calc_result);
- }
- public static void parse_paren(ArrayList<String> eq){
- //call get_equation on deepest parenthesis
- //pretty much just calc but with parenthesis
- ArrayList<String> new_list = new ArrayList<String>();
- for (int index = 0; index < eq.size(); index++) {
- if(eq.get(index).equals(")")){
- int index_of_close = index;
- int current_index = index;
- while (!eq.get(current_index).equals("(")){
- current_index--;
- }
- //make slice of arraylist also make sure calc results is 0 before doing anything
- ArrayList<String> calc_list = new ArrayList<String>();
- calc_list = new ArrayList(eq.subList(current_index+1, index));
- calc(calc_list);
- //add on the rest of eq after this
- new_list.set(current_index, Integer.toString(calc_result));
- ArrayList<String> placeholder = new ArrayList<String>();
- for(int i = 0; i < eq.size(); i++){
- //this should be not
- if((i > current_index && i < index_of_close)){
- placeholder.add(eq.get(i));
- }
- }
- new_list.addAll(placeholder);
- break;
- }
- }
- parse_paren(new_list);
- }
- //make return type void, or not?
- public static void calc(ArrayList<String> eq) {
- //setup placeholder here as well, should be a lot easier
- int calc_result = 0;
- if(eq.size() >= 1) {
- //recursive, simplifies equation one step at a time
- //or make it left to right
- ArrayList<String> new_eq = new ArrayList<>();
- //add breaks
- for (int index = 0; index < eq.size(); index++) {
- if (eq.get(index).equals("*")) {
- new_eq.add(Integer.toString(Integer.parseInt(eq.get(index - 1)) * Integer.parseInt(eq.get(index + 1))));
- ArrayList<String> placeholder = new ArrayList<String>();
- for(int i = 0; i < eq.size(); i++){
- if(i > current_index && i < index_of_close){
- placeholder.add(eq.get(i));
- }
- }
- break;
- }
- //add conditions for division
- else if (eq.get(index).equals("/")) {
- new_eq.add(Integer.toString(Integer.parseInt(eq.get(index - 1)) / Integer.parseInt(eq.get(index + 1))));
- break;
- }
- else if (!eq.contains("/") && !eq.contains("*")) {
- //System.out.println(eq.get(index)); //???
- if (eq.get(index).equals("+")) {
- new_eq.add(Integer.toString(Integer.parseInt(eq.get(index - 1)) + Integer.parseInt(eq.get(index + 1))));
- break;
- }
- //add conditions for division
- else if (eq.get(index).equals("-")) {
- new_eq.add(index, Integer.toString(Integer.parseInt(eq.get(index - 1)) - Integer.parseInt(eq.get(index + 1))));
- break;
- }
- }
- /*
- if(eq.get(index) == "*"){
- new_eq.set(index, Integer.toString(Integer.parseInt(eq.get(index-1))*Integer.parseInt(eq.get(index+1))));
- }
- //add conditions for division
- else if(eq.get(index) == "/"){
- new_eq.set(index, Integer.toString(Integer.parseInt(eq.get(index-1))/Integer.parseInt(eq.get(index+1))));
- }
- else if(eq.get(index) == "+"){
- new_eq.set(index, Integer.toString(Integer.parseInt(eq.get(index-1))+Integer.parseInt(eq.get(index+1))));
- }
- */
- }
- calc(new_eq);
- }
- else {
- calc_result = Integer.parseInt(eq.get(0));
- }
- }
- public static ArrayList<String> get_equation(String equation){
- //doesnt hit last term not just the element if its a number WHY?!
- //because on the last run THERE IS NOT EXCEPTION
- ArrayList<String> eq = new ArrayList<>();
- String current_string = "";
- String new_string = "";
- for(int index = 0; index < equation.length(); index++){
- try {
- new_string = current_string += Integer.toString(Integer.parseInt(String.valueOf(equation.charAt(index))));
- //if last one throw?
- }
- catch (NumberFormatException e){
- eq.add(new_string);
- eq.add(String.valueOf(equation.charAt(index)));
- current_string = "";
- new_string = "";
- }
- }
- eq.add(new_string);
- return eq;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement