%3Cp%20class%3D%22c1%22%3EA%20rational%20number%20is%20a%20number%20that%20can%2
ID: 3552946 • Letter: #
Question
%3Cp%20class%3D%22c1%22%3EA%20rational%20number%20is%20a%20number%20that%20can%20be%20expressed%0Aas%20a%20fraction%20whose%20numerator%20and%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3Edenominator%20are%20integers.%20Examples%20of%20rational%0Anumbers%20are%200.75%20(which%20is%20%C2%BE)%20and%201.125%20(which%20is%209%2F8).%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3EThe%20value%20%3F%20is%20not%20a%20rational%20number%3B%20it%20cannot%20be%0Aexpressed%20as%20a%20ratio%20of%20two%20integers.%20%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3BWorking%20with%20rational%20numbers%20on%20the%20computer%0Ais%20often%20a%20problem.%20Inaccuracies%20in%20floating%20point%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3Erepresentation%20can%20yield%20imprecise%20results.%20For%0Aexample%20the%20result%20of%20a%20C%23%20expression%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E1.0%2F3.0*3.0%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3Eis%20likely%20to%20be%20a%20value%20like%200.999999%20rather%20than%0A1.0.%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3EDesign%20and%20build%20a%20rational%20number%20calculator%0Aprogram.%20This%20calculator%20should%20read%20the%20mathematical%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3Eexpression%20as%20an%20user%20input%20and%20add%20(%2B)%2C%20subtract%0A(-)%2C%20multiply%20(*)%20or%20divide%20(%2F)%20the%20rational%20numbers.%20The%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3Eprogram%20should%20do%20the%20calculation%20using%20c%23%20operator%0Aprecedence%20rule%3B%20*%20and%20%2F%20are%20perform%20before%20%2B%20and%20-.%20It%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3Ealso%20search%20for%20the%20maximum%20common%20divider%20to%0Asimplify%20the%20result.%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3EThe%20following%20is%20a%20sample%20run%20of%20the%0Aprogram%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3EInput%20the%20mathematical%20expression%3A%201%2F3%20*%202%2F4%20%2B%0A5%2F9%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3EOutput%3A%201%2F3%20*%202%2F4%20%2B%205%2F9%20%3D%2078%2F108%20%3D%2013%2F18%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3Eor%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3EInput%20the%20mathematical%20expression%201%2F2%20%2B%201%2F4%20*%205%2F3%20%E2%80%93%0A3%2F8%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3EOutput%3A%201%2F2%20%2B%201%2F4%20*%205%2F3%20%E2%80%93%203%2F8%20%3D%20104%2F192%20%3D%2052%2F96%20%3D%0A13%2F24%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3EReminder%3A%20The%20program%20should%20validate%20the%20input%0Avalues%20and%20show%20the%20appropriate%20message%20when%20an%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3Eerror%20occurs.%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3EHint%3A%20The%20split%20method%20in%20a%20String%20class%20finds%20all%0Athe%20substrings%20in%20a%20string%20that%20are%20separated%20by%20one%20or%0Amore%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3Echaracters%2C%20returning%20a%20string%20array.%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3BExample%3A%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3Bpublic%20class%20SplitTest%20%7B%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3Bpublic%20static%20void%20Main()%20%7B%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3Bstring%20words%20%3D%20%22this%20is%20a%20list%20of%20words%2C%20with%3A%0Aa%20bit%20of%20punctuation.%22%3B%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3Bstring%20%5B%5D%20split%20%3D%20words.Split(new%20Char%20%5B%5D%20%7B'%20'%2C%0A'%2C'%2C%20'.'%2C%20'%3A'%7D)%3B%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3Bforeach%20(string%20s%20in%20split)%20%7B%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3Bif%20(s.Trim()%20!%3D%20%22%22)%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3BConsole.WriteLine(s)%3B%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%7D%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%7D%26nbsp%3B%3C%2Fp%3E%0A%3Cp%20class%3D%22c1%22%3E%26nbsp%3B%7D%3C%2Fp%3E%0AExplanation / Answer
import java.util.Scanner;
public class Rational {
public static void main(String[] args) {
System.out.println("Enter the expression :");
Scanner ss=new Scanner(System.in);
String expr = ss.nextLine();
InfixEval fix=new InfixEval();
System.out.println(expr+"="+fix.InfixEval(expr));
double a = fix.InfixEval(expr);
System.out.println(a);
String aString = Double.toString(a);
String[] fraction = aString.split("\.");
int denom = ((int)Math.pow(10, fraction[1].length()));
int numer = Integer.parseInt(fraction[0] + "" + fraction[1]);
int gcd =GCD(numer,denom);
int numer1=numer/gcd;
int denom1=denom/gcd;
System.out.println("Final value in rational form "+numer1 + "/" + denom1);
}
public static int GCD(int a, int b) {
if (b==0) return a;
return GCD(b,a%b);
}
}
---------------------------------------------------------------------------------------------------------------------------------------
import java.util.Stack;
import java.util.StringTokenizer;
public class InfixEval
{
public double InfixEval(String expression)
{
//remove white space and add evaluation operator
expression=expression.replaceAll("[ ]", "")+"=";
String operator="*/+-=";
//split up the operators from the values
StringTokenizer tokenizer=new StringTokenizer(expression, operator, true);
Stack operatorStack=new Stack();
Stack valueStack=new Stack();
while(tokenizer.hasMoreTokens())
{
//add the next token to the proper stack
String token=tokenizer.nextToken();
if(operator.indexOf(token)<0)
valueStack.push(token);
else
operatorStack.push(token);
//perform any pending operations
resolve(valueStack, operatorStack);
}
//return the top of the value stack
String lastOne=(String)valueStack.pop();
return Double.parseDouble(lastOne);
}
public int getPriority(String op)
{
if(op.equals("*") || op.equals("/"))
return 1;
else if(op.equals("+") || op.equals("-"))
return 2;
else if(op.equals("="))
return 3;
else
return Integer.MIN_VALUE;
}
public void resolve(Stack values,
Stack operators)
{
while(operators.size()>=2)
{
String first=(String)operators.pop();
String second=(String)operators.pop();
if(getPriority(first)<getPriority(second))
{
operators.push(second);
operators.push(first);
return;
}
else
{
String firstValue=(String)values.pop();
String secondValue=(String)values.pop();
values.push(getResults(secondValue, second, firstValue));
operators.push(first);
}
}
}
public String getResults(String operand1, String operator, String operand2)
{
System.out.println("Performing "+
operand1+operator+operand2);
double op1=Double.parseDouble(operand1);
double op2=Double.parseDouble(operand2);
if(operator.equals("*"))
return ""+(op1*op2);
else if(operator.equals("/"))
return ""+(op1/op2);
else if(operator.equals("+"))
return ""+(op1+op2);
else if(operator.equals("-"))
return ""+(op1-op2);
else
return null;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.