# When you create a calculator that calculates an expression, it is not calculated well if there are multiple operators in the expression.

Asked 2 months ago, Updated 2 months ago, 377 views

I'm working on a one-line calculator program.If you use two numbers, such as "1+1", the calculation will be successful, but if the number is more than three (e.g., 1010+10+10, 11+1+1+1+1).The answer to Example 1 is 10, and the answer to Example 2 is 3.I wrote the code below, so I would appreciate it if you could advise me!

------------------

``````package Calculator;

import java.util.Scanner;

public class Main {

public static void main(String[]args) {
Scanner scanner = new Scanner (System.in);
System.out.println("input numbers and operator";
System.out.println("operator:+,-,x, ,,%,^,r(square root)");
System.out.println("symbol:=");

while(true){
String [ ] user_input = new String [4];
for(inti=0;i<user_input.length;i++){
user_input[i] = new java.util.Scanner(System.in).nextLine();
}
if(user_input[0].equals(""){
System.out.println("Over");
break;
} else if(user_input[1].equals(""){
System.err.println("Error");
continue;
}

String [ ] ops = {"+", "-", "×", " "", "%", "^", "r"};

if(!(user_input[1].equals(ops[6]))&user_input[3].equals(""){
int calc;
int digit1 = Integer.parseInt(user_input[0]);
int digit2 = Integer.parseInt(user_input[2]);
intans=calc(digit1,digit2,user_input);
System.out.println(ans);
} else if(user_input[1].equals(ops[6])&user_input[3].equals(""){
double calc2;
double digit3 = Double.parseDouble(user_input[0]);
double result=calc2(digit3, user_input);
System.out.println(result);
}

else if(!(user_input[3].equals("")){
user_input=tempArray(user_input);
}

for(inti=0;i<user_input.length;i++){
user_input[i] = new java.util.Scanner(System.in).nextLine();
}
int calc;
int digit1 = Integer.parseInt(user_input[0]);
int digit2 = Integer.parseInt(user_input[2]);
int digit4 = Integer.parseInt(user_input[4]);

int interAns = calc (digit1, digit2, user_input);
int newAns=calc4(digit4,interAns,user_input);
System.out.println(newAns);
}
}

public static int calc(int digit1, int digit2, String[]user_input){
if(user_input[1].equals("+")}
return digit1 + digit2;
}
if(user_input[1].equals("-")}
return digit1-digit2;
}
if(user_input[1].equals("x")}
return digit1*digit2;
}
if(user_input[1].equals(" "")}
return digit1/digit2;
}

if(user_input[1].equals("%")}
return digit 1% digit2;
}
if(user_input[1].equals("^")}
return digit1^digit2;
}
return 0;
}

public static double calc2(double digit3, String[]user_input){
if(user_input[1].equals("r")){
double A = Math.sqrt (digit3);
return A;
}
return 0.0;
}

public static String[]tempArray(String[]user_input){
String [ ] temp_input = new String [ user_input.length+3 ];
for(inti=0;i<user_input.length;i++){
temp_input[i] = user_input[i];
}
user_input=temp_input;
return user_input;
}

public static int calc4(int digit4, int interAns, String[]user_input){
if(user_input[3].equals("+")}
return interAns+digit4;
}
if(user_input[3].equals("-")}
return interAns-digit4;
}
if(user_input[3].equals("x")}
return interAns* digit4;
}
if(user_input[1].equals(" "")}
return interAns/digit4;
}
if(user_input[3].equals("%")}
return interAns%digit4;
}
if(user_input[3].equals("^")}
return interAns^digit4;
}
return 0;
}
}
``````

java

2022-11-07 09:01

The logic is to prepare an array of length 4
If the num op num is exceeded (if the fourth is not an empty string)

Increase the array length by +3 to length 7
numop numop numop num
I understand that you have made it acceptable until

However, after that,

``````for(inti=0;i<user_input.length;i++){
user_input[i] = new java.util.Scanner(System.in).nextLine();
}
``````

in the section where you receive additional input. I put it from the beginning of the array again, so

``````System.out.println("=====Input so far=====");
for (inti=0; i<user_input.length;i++)
System.out.print(user_input[i]);
System.out.println();
System.out.println("==========================");
``````

It might be easy to understand if you put a debug like this

Run Results

``````java Main
input numbers and operators
operator: +, -, x, ,, %, ^, r(square root)
symbol: =
1
+
2
+
3
+
4
+
5
+
6
==== Input so far ====
3+4+5+6
========================
``````

and probably only receive 3 more
I was asked to enter a total of 11 times
The first 1+2 was missing

So if you fill this part from 0 to the 4th or later, I wonder if it will work.

``````for(inti=4;i<user_input.length;i++){
user_input[i] = new java.util.Scanner(System.in).nextLine();
}

System.out.println("=====Input so far=====");
for (inti=0; i<user_input.length;i++)
System.out.print(user_input[i]);
System.out.println();
System.out.println("==========================");
``````

Run Results

``````>java Main
input numbers and operators
operator: +, -, x, ,, %, ^, r(square root)
symbol: =
1
+
2
+
3
+
4
===== Input so far =====
1+2+3+4
======================
6
``````

It's only 6 instead of 10

``````int digit1 = Integer.parseInt(user_input[0]);
int digit2 = Integer.parseInt(user_input[2]);
int digit4 = Integer.parseInt(user_input[4]);

int interAns = calc (digit1, digit2, user_input);
int newAns=calc4(digit4,interAns,user_input);
``````

So now I'm only doing the 246th calculation.

Even if the input like 1+2*3 is correct,
like 1+(2*3) There is no requirement to calculate what you entered first
Do you mean that I should always calculate (1+2)*3 on the left side combination?

In other words, I calculated 3 at the point of 1+2
* If you only have a 1+2 result when you receive 3, you can throw away your input

Then you don't have to use an array to update the current number when you read op+digit. The stack machine that accepts the regular expression /num(op num)*"r"?/ is fine, isn't it?
That's what the old calculator used, so if you're interested in it, you can use the stack machine to find out.

It's relatively simple to write like this, and it's good for memory.

``````import java.util.Scanner;

public class Main {

public static void main(String[]args) {
Scanner scanner = new Scanner (System.in);
System.out.println("input numbers and operator";
System.out.println("operator:+,-,x, ,,%,^,r(square root)");
System.out.println("symbol:=");

while(true){

// First, the number is fixed.
int interAns = Integer.parseInt(new java.util.Scanner(System.in).nextLine());

// From now on, I will update the current number by reading op(+num).
while(true){
// Next is OP confirmed.
Stringop = new java.util.Scanner(System.in).nextLine();

// Op is empty = End of input = > Display current results and return to first input
if(op.equals(""){
System.out.println(interAns);
break;
}

// Do not request the next number only for routes.
if(op.equals("r"){
System.out.println(calc2(interAns, op));
// The calculation results will be double, so is that all right?
break;
}
// Read the following numbers except for the route and update the results halfway.
else{
int digit=Integer.parseInt(new java.util.Scanner(System.in).nextLine());
interAns = calc4 (digit, interAns, op);
}

// For debugging
System.out.println("interAns="+interAns);

// Loop to read next op
}

}
}

public static double calc2 (double digit3, Stringop) {
if(op.equals("r"){
double A = Math.sqrt (digit3);
return A;
}
return 0.0;
}

public static int calc4(int digit4, int interAns, Stringop) {
if(op.equals("+"){
return interAns+digit4;
}
if(op.equals("-"){
return interAns-digit4;
}
if(op.equals("x")}
return interAns* digit4;
}
if(op.equals(" ""){
return interAns/digit4;
}
if(op.equals("%"){
return interAns%digit4;
}
if(op.equals("^")}
return interAns^digit4;
}
return 0;
}
}
``````

Run Results

``````>java Main
input numbers and operators
operator: +, -, x, ,, %, ^, r(square root)
symbol: =
1
+
2
interAns = 3
+
3
interAns = 6
+
4
interAns = 10

10
``````

2022-11-07 09:14

## If you have any answers or tips

Popular Tags
python x 4566
android x 1593
java x 1484
javascript x 1415
c x 916
c++ x 866
ruby-on-rails x 694
php x 688
python3 x 675
html x 650