no-tard, mid urgent, Help request: Calculating rational problems (making a calculator)

Hello there pepz and potential future colleagues,

I'm facing a problem concerning one particular school assignment called "Rational calculator". Basically what it does is, it returns result of inserted rational expression in exact value, which can be either integer or fraction. The thing is, I got no idea how to add particular operator inside the program that could easily add, subtract or multiply numbers inserted, that would be within brackets for example.

Thanks :)

Re: no-tard, mid urgent, Help request: Calculating rational problems (making a calculator)

You could make the user choose through some sort of menu before typing in numbers, however, this would be rather irritating and clunky.

What you would want to do is to check for a '+', '-' or a '*' sign in the string that the user inputs, then divide the part of the string that is before that letter, and the part that's after and then convert them into ints. After that you simply calculate them using the sign that was in the equation.

For example, something like this:

Code :

String equation = "2*1132";
int part1, part2;
if(equation.indexOf('*') != -1){ //Checks if the string contains '*', else the method returns -1
/*Note, I don't know if this string to int conversion works, however there are other easy ways to do it otherwise*/
part1 = (int) equation.substring(0, equation.indexOf('*')-1);
//(Above) Creates a substring that begins at the start of the equation string and ends before the math sign
part2 = (int) equation.substring(equation.indexOf('*')+1, equation.length());
//(Above) Creates a substring that begins at 1 character after the math-sign and ends at the end of the equation-string
System.out.println(part1 + " * " + part2 + " = " + part1*part2); //Prints out the equation and answer
}//End if
//=====================================================//
//=====================================================//
else if(equation.indexOf('+') != -1){ //Checks if the string contains '+', else the method returns -1
part1 = (int) equation.substring(0, equation.indexOf('+')-1);
part2 = (int) equation.substring(equation.indexOf('+')+1, equation.length());
System.out.println(part1 + " + " + part2 + " = " + part1+part2); //Prints out the equation and answer
}//End else-if
//=====================================================//
//=====================================================//
else if(equation.indexOf('-') != -1){ //Checks if the string contains '-', else the method returns -1
part1 = (int) equation.substring(0, equation.indexOf('-')-1);
part2 = (int) equation.substring(equation.indexOf('-')+1, equation.length());
System.out.println(part1 + " - " + part2 + " = " + part1-part2); //Prints out the equation and answer
}//End else-if
//=====================================================//
//=====================================================//
else{
System.out.println("You entered something invalid, stupid. I mean " + equation + " isn't even a proper equation!");
}//End else

Should resort in the first "if" and part1 becoming '2', part2 "1132".

In this case the printed line would be "2 * 1132 = 2264".

Note that this only works with one sign in the equation, so for example "1*5+2" wouldn't work with this way of coding.

Also note that if this is for some java class, and your teacher writes an equation wrong (like 69 instead of 6+9), it would result in "your" program telling him/her that he/she is stupid ("You entered something invalid, stupid. I mean 69 isn't even a proper equation").

--- Update ---

Just realized, you wrote "pepz" and then I came :eek: