php hit counter

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rational Number Calculator
VU Help • House Banner VU Help Virtual University Students - Assignments, Solved Papers, GDB & Exam Help Student Help Forum • Collaborative • Free Support
#3
Its a java based computer science assignment

I have to make a rational number calculator, it will constantly ask for input from the user and will act like the example above. I need it in a more basic version.

Here is a program I found that does the job, but its way to complex for my class level:


import java.util.Scanner;

public class RationalCalculator2 {

Scanner kbd;
private RationalNumber operand1;
private RationalNumber operand2;
/**
* operation:
* 0 : add;
* 1 : subtract;
* 2 : multiply;
* 3 : divide;
* 4 : compare;
*/
private int operation;

public RationalCalculator2()
{
kbd = new Scanner(System.in);
}
/**
* gets input from console line, and initializes operands and operation
* @return true if new expression is inputed, false if user wants to quit
*/
public boolean getInput()
{
String[] lineParts = kbd.nextLine().split("/");
if(lineParts.length == 1)
if(lineParts[0].equalsIgnoreCase("q")) return false;
if(lineParts.length == 4) //divide case
{
operation = 3;
operand1 = new RationalNumber(Integer.parseInt(lineParts[0]), Integer.parseInt(lineParts[1]));
operand2 = new RationalNumber(Integer.parseInt(lineParts[2]), Integer.parseInt(lineParts[3]));
}
else //not divide
{
String[] midPart = lineParts[1].trim().split("\\s"); //should yield 3 parts, i.e: 3, +, 4
switch(midPart[1].trim().charAt(0)) // grab the operator
{
case '+':
{
operation = 0;
operand1 = new RationalNumber(Integer.parseInt(lineParts[0].trim()), Integer.parseInt(midPart[0].trim()));
operand2 = new RationalNumber(Integer.parseInt(midPart[2].trim()), Integer.parseInt(lineParts[2].trim()));
break;
}
case '-':
{
operation = 1;
operand1 = new RationalNumber(Integer.parseInt(lineParts[0].trim()), Integer.parseInt(midPart[0].trim()));
operand2 = new RationalNumber(Integer.parseInt(midPart[2].trim()), Integer.parseInt(lineParts[2].trim()));
break;
}
case '*':
{
operation = 2;
operand1 = new RationalNumber(Integer.parseInt(lineParts[0].trim()), Integer.parseInt(midPart[0].trim()));
operand2 = new RationalNumber(Integer.parseInt(midPart[2].trim()), Integer.parseInt(lineParts[2].trim()));
break;
}
case '=':
{
operation = 4;
operand1 = new RationalNumber(Integer.parseInt(lineParts[0].trim()), Integer.parseInt(midPart[0].trim()));
operand2 = new RationalNumber(Integer.parseInt(midPart[2].trim()), Integer.parseInt(lineParts[2].trim()));
break;
}
}
}
return true;
}
/**
* adds
*/
private void add(){ // operand1 = operand1 + operand2
//operand1.den = operand1.den * operand2.den; //wrong output because you change den to new value,
operand1.num = operand1.num * operand2.den + operand2.num * operand1.den; // but you want the old value here.
operand1.den = operand1.den * operand2.den;
operand1.reduce();
//toString();
}
/**
* subtracts
*/
private void subtract(){ // operand1 = operand1 - operand2
// operand1.den = operand1.den * operand2.den; //see add explanation
operand1.num = operand1.num * operand2.den - operand2.num * operand1.den;
operand1.den = operand1.den * operand2.den;
operand1.reduce();
//toString ();
}

/**
* multiplies
*/
private void multiply(){ // operand1 = operand1 * operand2
operand1.den = operand1.den * operand2.den;
operand1.num = operand1.num * operand2.num;
operand1.reduce();
//toString ();
}
/**
* divides
*/
private void divide(){ // operand1 = operand1 / operand2
operand1.den = operand1.den * operand2.num;
operand1.num = operand1.num * operand2.den;
operand1.reduce();
//toString ();
}
/**
* compares
* @return
*/
private boolean equals(){ // Is this == other?

operand1.reduce();
operand2.reduce();
if( operand1.num == operand2.num && operand1.den == operand2.den){
return true;
}else{
return false;
}
}

/**
* takes operation and performs it on operands
* @return answer in string form;
*/
public String calculate()
{
switch(operation)
{
case 0:
{
add();
break;
}
case 1:
{
subtract();
break;
}
case 2:
{
multiply();
break;
}
case 3:
{
divide();
break;
}
case 4:
{
return ""+equals();
}
}
return operand1.toString();
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
RationalCalculator2 calc = new RationalCalculator2();
System.out.println("Enter an equation of two rational number in format: \"n / d operation n /d\"\nType \"q\" to quit.");
calc.getInput();
do
{
System.out.println(""+calc.calculate());
}while(calc.getInput());
}

/**
* Rational Number.
* This is all a number should know: it's value and how to reduce itself;
* @author GoldfishGrenade
*
*/
class RationalNumber
{
private int num; //numerator
private int den; //denominator

public RationalNumber(int num, int den)
{
this.num = num;
this.den = den;
}
public String toString()
{
reduce();
return ""+num+" / "+den;

}
private void reduce(){
int yes = 0;
int smaller;
int num1=this.num,den1=this.den;
// you forgot the if num = 0 case
if(num == 0)
{
den = 1;
return;
}
if(num < 0 && den < 0)
{
num *= -1;
den *= -1;
}
if(num<0)
{ num1=-1*num;}
if(den<0)
{ den1=-1*den;}
if ( num1 < den1 ){
smaller = num1;
}else{
smaller = den1;}

for ( int div = smaller; div >= 2; div-- ){
if ( num1 % div == 0 && den1 % div == 0 ){
yes = div;
break;
}
}
if ( yes != 0 ){
num /= yes;
den /= yes;
}
}
}
}
Reply


Messages In This Thread
Rational Number Calculator - by Cs151 - 05-07-2011, 05:45 PM
Rational Number Calculator - by Xpert - 05-07-2011, 07:17 PM
Rational Number Calculator - by Cs151 - 05-08-2011, 06:03 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)
VU Help • House Banner VU Help Virtual University Students - Assignments, Solved Papers, GDB & Exam Help Student Help Forum • Collaborative • Free Support