Could someone please solve this Eclipse Calculator problem f
Could someone please solve this Eclipse Calculator problem for me? Thanks!
Write a program that reads in an operator and two operands, performs the operation, and then prints out the result.
Input The input will be one or more lines of numbers that have been typed in by the user. Each line is to have an operator followed by two operands. The operator can be one of the seven: +, -, *, /, %, power, and average.
Output The output will have for each line, the first operand followed by a space followed by the operator followed by a space followed by the second operand followed by a space followed by = followed by a space followed by the result. Each number is to be printed using \"%.2e\" format.
Sample Input + 2 3 power 2.2 3 average 2.2 1.3
Sample Output 2.00e+00 + 3.00e+00 = 5.00e+00 2.20e+00 power 3.00e+00 = 1.06e+01 2.20e+00 average 1.30e+00 = 1.75e+00
HINT 1. Here use in.next() to read the operator and in.nextDouble() to read a number. 2. After reading the two doubles, use in.nextLine() – so that you discard the rest of the \"white spaces\". 3. To print two numbers num1 and num2 using %.2e format and printf 4. Use String class\'s equals method to compare two strings: example if (s.equals(\"+\")) { … }
Solution
Ans: The code for the question is explained with comments.
public class EclipsCalc
{
public static void main(String[] args)
{
if(args.length==0)
{
System.out.println(\"No arguments passed\"); //no arguments passesd
}
else
{
int a=Integer.parseInt(args[0]);
char p=args[1].charAt(0);
int b=Integer.parseInt(args[2]);
switch(p) //math operations
{
case \'+\':
System.out.println(\"Addition \"+a+\" and \"+b+\" : \"+(a+b));
break;
case \'-\':
System.out.println(\"Subtraction \"+a+\" and \"+b+\" : \"+(a-b));
break;
case \'*\':
System.out.println(\"Multiplication \"+a+\" and \"+b+\" : \"+(a*b));
break;
case \'/\':
System.out.println(\"Div \"+a+\" and \"+b+\" : \"+(a/b));
break;
case \'%\':
System.out.println(\"Modulo \"+a+\" and \"+b+\" : \"+(a%b));
break;
default:
System.out.println(\"Please Enter \'+\', \'-\', \'*\', \'/\' & \'%\' operator only.\");
}
}
}
}
I gave you the code in java.If you want it in C please comment so that i can provide you in C


