import java.util.Scanner;
public class Equations {
/*
* Không rõ đề bài lắm, trường hợp trên 2 toán tử tính đại là đúng. VD:
* a = -b*c
* -a/b = c
*
* Theo định lý HuyLe ^0^ đề bài thỏa chỉ khi thỏa 1 trong 2 điều kiện sau:
* (Định lý HuyLe thì HuyLe cũng chưa chứng minh được, cứ tin đại đi @_@)
* a [ + - ] b [ + - * / ] c = 0
* OR
* a [ * / ] b [ + - ] c = 0
*
*/
//----------------------------------------------------------------------------
public static void main(String[] args) {
// input
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: "); int a = sc.nextInt();
System.out.print("Enter b: "); int b = sc.nextInt();
System.out.print("Enter c: "); int c = sc.nextInt();
// process
float value=1; // value = a [operator1] b [operator2] c
int oper2Range;
// oper2Range=4: + - * /
// oper2Range=2: + -
done:
for (int i=0; i<4; i++) {
if (i<2) oper2Range = 4;
else oper2Range = 2;
for (int j=0; j < oper2Range; j++)
if ( (value = valueOf(a,b,c, i, j)) == 0) {
System.out.print("Your inputs can form the equation: ");
displayEquation(i, j);
break done;
}
}
if (value != 0) System.out.println("Your inputs can't form any equation.");
}
//--------- return value of ( x [operator] y [operator] z ) -----------------------------------
private static float valueOf(int a, int b, int c, int oper1, int oper2) {
if (oper2 == 2 || oper2 == 3)
return calculate(a, calculate(b, c, oper2), oper1 );
else
return calculate(c, calculate(a, b, oper1), oper2 );
}
//-------- return value of ( x [operator] y ) ------------------------------------------------------
private static float calculate (float x, float y, int oper) {
switch (oper) {
case 0: return x+y;
case 1: return x-y;
case 2: return x*y;
default: return (float) x/y;
}
}
//----------------------------------------------------------------------------------
private static void displayEquation(int oper1, int oper2) {
if (oper1 == 0 || oper1 == 1)
// a +- b */ c = 0 -> a = -+ b * c
if (oper2 == 2 || oper2 == 3)
System.out.printf("a = %cb %c c\n",
(toChar(oper1)=='+') ? '-' : '\u0000', toChar(oper2));
// a +- b +- c = 0 -> a +- b = -+ c
else
System.out.printf("a %c b = %cc\n",
toChar(oper1), (toChar(oper2)=='+') ? '-' : '\u0000');
// a */ b +- c = 0 -> a */b = -+ c
else
System.out.printf("a %c b = %cc\n",
toChar(oper1), (toChar(oper2)=='+') ? '-' : '\u0000' );
}
//-------- return a "char" operator from an "int" operator---------------------------
private static char toChar(int operator) {
switch (operator) {
case 0: return '+';
case 1: return '-';
case 2: return '*';
default: return '/';
}
}
}