calculator - Computing negative numbers in Java -



calculator - Computing negative numbers in Java -

okay... want around concept of computing negative numbers. self-learning , running lot of difficulty. how implement negative input?

my code:

public class mainsystem { public static void main(string[] args) { seek (scanner console = new scanner(system.in)) { string input; system.out.print(">>> "); input = console.nextline(); splitentry(input); system.out.println("the console closed."); } } private static void splitentry(string input) { string function = "[+\\-*/]+"; //placing them in index string[] token = input.split(function);//and double num1 = double.parsedouble(token[0]); double num2 = double.parsedouble(token[1]); //double answer; string operator = input.tochararray()[token[0].length()] + ""; if (operator.matches(function) && (token[0] + token[1] + operator).length() == input.length()) { system.out.println("operation " + operator + ", first number " + token[0] + " sec number " + token[1]); } else { system.out.println("your entry of " + input + " invalid"); } if (operator.matches(function) && (token[0] + token[1] + operator).length() == input.length()) { double result = 0; if (operator.equals("+")) { // simplified using formatters result = num1 + num2; } else if (operator.equals("-")) { result = num1 - num2; } else if (operator.equals("*")) { result = num1 * num2; } else if (operator.equals("/")) { result = num1 / num2; } system.out.printf("your first number %.2f %s sec number %.2f = makes %.2f%n", num1, operator, num2, result); } } }

how allow calculator place -2 + 5 , reply 3?

every time seek that, programme crashes? create negative numbers computed

ok, think have something. sorry late answer, hope can still utilize it. first allow me explain, changed quite lot of code.

scanning input input read 1 finish string, in code. instead of splitting code string[], 'checking' chars 1 1 , seek decide whether operator or number.

if find number or operator, set in list instead of array, because list can grow dynamically , don't know how many numbers , operators there are,yet.

private static arraylist<string> getsequence(string input) { string number = "[\\d,.]+"; // regex observe number 12 or 1.2 or 1,2 string function = "[+\\-*/]+"; // regex observe operator +-*/ char[] inputchars = input.tochararray(); // converting input string array arraylist<string> sequence = new arraylist<>(); // list, returned string lastnumber = ""; // number can consist of multiple chars, string buffer "collect" every char until number finish (char c : inputchars) { // loop through every char in char[] system.out.println("checking " + c); if ((new string("" + c)).matches(function)) { // convert our char string , seek match "function" regex system.out.println("its operator"); if (!lastnumber.isempty()) { sequence.add(lastnumber); // if observe operator, must check if still have number in our buffer. add together lastly number our list } sequence.add("" + c); // , add together our operator our list lastnumber = ""; // saw operator, "lastnumber" buffer should cleared } else if ((new string("" + c)).matches(number)) { // if observe digit/number system.out.println("its part of number"); lastnumber += c; // since char part of number, add together "lastnumber" buffer // need continue, since don't know if number finished } } // if finished analyzing char array, there might lastly number in our buffer if (!lastnumber.isempty()) { sequence.add(lastnumber); // lastly number added } homecoming sequence; // our sequence finish , homecoming }

so when our input 1+2+3 method homecoming list:

{"1", "+", "2", "+", "3"}

evaluating sequence next phone call method evaluate(sequence) evaluate our list. pretty close way, made adjustments

private static void evaluate(arraylist<string> sequence) { double number1 = 0.0; // first number intermediary result while (true) { // don't know how long equation is, here have infinite-loop. not idea! seek { // seek evaluate next string of our sequence number1 = getnextnumber(sequence); // next number system.out.println("number1 = " + number1); char operator = getnextoperator(sequence); // next operator system.out.println("operator = " + operator); double number2 = getnextnumber(sequence); system.out.println("number2 = " + number2); // sec number switch (operator) { // replaced if statements switch (but same) case '+': number1 += number2; // add together sec number first number , "store" result in first number break; case '-': number1 -= number2; break; case '*': number1 *= number2; break; case '/': number1 /= number2; break; } } grab (java.lang.indexoutofboundsexception e) { // here break out of loop. can 100% sure @ point reached end of list. when @ end of list , seek access "next" element of list (which of course of study not exist) indexoutofboundsexception , know, finished here system.out.println("result " + number1); break; // break out of loop } } }

the of import part here store result of operation 2 numbers, in first number. if have 1+2+4, first evaluate 1+2, store result first number , evaluate 3+4

helper methods still need methods observe either operator or number;

private static char getnextoperator(arraylist<string> sequence) { string function = "[+\\-*/]+"; // same regex before observe operator // list looks // list.get(0) same array[0] if (sequence.get(0).matches(function)) { homecoming sequence.remove(0).charat(0); } homecoming ' '; }

this uses nice trick here: sequence.get(0) have @ first element in list , homecoming value. sequence.remove(0) homecoming value , delete element list @ same time. original first element no longer there , sec element becomes first...

so first our list looks { "-", "2"} , after calling getnextoperator() looks {"2"}, because remove operator.

and same getting next number:

private static double getnextnumber(arraylist<string> sequence) { string sign = "[+-]+"; // regex test if string sign (+ or -) string number = "[\\d,.]+"; //regex test if string number (with or without . or , double number1 = 0; if (sequence.get(0).matches(sign) && sequence.get(1).matches(number)) { // first 1 should sign , sec 1 number number1 = double.parsedouble(sequence.remove(0) + sequence.remove(0)); } else if (sequence.get(0).matches(number)) { // normal number number1 = double.parsedouble(sequence.remove(0)); } homecoming number1; }

well , that's it. have set , phone call in main function this:

public static void main(string[] args) { seek (scanner console = new scanner(system.in)) { string input; system.out.print(">>> "); input = console.nextline(); arraylist<string> sequence = getsequence(input); // analyze input , list evaluate(sequence); // evaluate list system.out.println("the console closed."); } }

well, sorry changing much. couldn't think of (maybe more efficient) way that. calculator not "smart" , there lot of things won't handle (like letters or braces) hope it's start. @ to the lowest degree should work negative , positive numbers , should able handle longer equations.

cheers!

java calculator

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -