Monday 12 August 2013

My calculator does not work well in android

My calculator does not work well in android

hi plz help me to fix problems. it is a calculator
in my class clalculator:
import static java.lang.Math.*;
@SuppressWarnings("unused")
public class Calculator
{
// 3 + 6 = 9
// 3 & 6 are called the operand.
// The + is called the operator.
// 9 is the result of the operation.
private double operand = 0;
private double waitingOperand = 0;
private String waitingOperator = "";
private double calculatorMemory = 0;
public void setOperand(double operand)
{
this.operand = operand;
}
public double getResult()
{
return operand;
}
// used on screen orientation change
public void setMemory(double calculatorMemory)
{
this.calculatorMemory = calculatorMemory;
}
// used on screen orientation change
public double getMemory()
{
return calculatorMemory;
}
public void reset()
{
operand = 0;
waitingOperator = "";
waitingOperand = 0;
}
public String toString()
{
return Double.toString(operand);
}
protected double performOperation(String operator)
{
try
{
if (operator.equals("MC"))
{
calculatorMemory = 0;
}
else if (operator.equals("M+"))
{
calculatorMemory = calculatorMemory + operand;
}
else if (operator.equals("M-"))
{
calculatorMemory = calculatorMemory - operand;
}
else if (operator.equals("MR"))
{
operand = calculatorMemory;
}
else if (operator.equals("C"))
{
try
{
reset();
} catch (Exception e)
{
reset();
}
}
else if (operator.equals("sqrt"))
{
operand = Math.sqrt(operand);
}
else if (operator.equals("tan"))
{
operand = Math.tan(Math.toRadians(operand));
waitingOperand = operand;
}
else if (operator.equals("x^3"))
{
operand = operand * operand * operand;
}
else if (operator.equals("e"))
{
operand = Math.E;
waitingOperand = operand;
}
else if (operator.equals("p"))
{
operand = Math.PI;
waitingOperand = operand;
}
else if (operator.equals("rnd"))
{
operand = Math.random();
}
else if (operator.equals("10^3"))
{
operand = operand * 1000;
}
else if (operator.equals("1/x"))
{
if (operand != 0)
{
operand = 1 / operand;
}
}
else if (operator.equals("+/-"))
{
operand = -operand;
}
else if (operator.equals("sin"))
{
operand = Math.sin(Math.toRadians(operand));
waitingOperand = operand;
}
else if (operator.equals("cos"))
{
operand = Math.cos(Math.toRadians(operand));
waitingOperand = operand;
}
else
{
performWaitingOperation();
waitingOperator = operator;
waitingOperand = operand;
}
}
catch (Exception e)
{
reset();
}
return operand;
}
protected void performWaitingOperation()
{
String w = Double.toString(waitingOperand);
if (w.equals("/") || w.equals("+") || w.equals("*") ||
w.equals("*")) return;
if (waitingOperator.equals("+"))
{
operand = waitingOperand + operand;
}
else if (waitingOperator.equals("*"))
{
operand = waitingOperand * operand;
}
else if (waitingOperator.equals("-"))
{
operand = waitingOperand - operand;
}
else if (waitingOperator.equals("/"))
{
if (operand != 0)
{
operand = waitingOperand / operand;
}
}
}
}
and in Main java file is:
import java.text.DecimalFormat;
import static java.lang.Math.*;
import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
@SuppressWarnings("unused")
public class Main extends Activity implements OnClickListener
{
private TextView calculatorDisplay;
private static final String DIGITS = "0123456789.";
private Boolean userIsInTheMiddleOfTypingANumber = false;
private static boolean fm;
DecimalFormat df = new DecimalFormat("@###########");
Context context = Main.this;
Calculator calculator;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
set();
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
// Save variables on screen orientation change
outState.putDouble("OPERAND", calculator.getResult());
outState.putDouble("MEMORY", calculator.getMemory());
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
// Restore variables on screen orientation change
calculator.setOperand(savedInstanceState.getDouble("OPERAND"));
calculatorDisplay.setText(df.format(calculator.getResult()));
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void set()
{
calculator = new Calculator();
calculator.reset();
calculatorDisplay = (TextView) findViewById(R.id.textView1);
calculatorDisplay.setText("0.0");
df.setMinimumFractionDigits(0);
df.setMinimumIntegerDigits(1);
df.setMaximumIntegerDigits(15);
findViewById(R.id.button0).setOnClickListener(this);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
findViewById(R.id.button5).setOnClickListener(this);
findViewById(R.id.button6).setOnClickListener(this);
findViewById(R.id.button7).setOnClickListener(this);
findViewById(R.id.button8).setOnClickListener(this);
findViewById(R.id.button9).setOnClickListener(this);
findViewById(R.id.buttonAdd).setOnClickListener(this);
findViewById(R.id.buttonSubtract).setOnClickListener(this);
findViewById(R.id.buttonMultiply).setOnClickListener(this);
findViewById(R.id.buttonDivide).setOnClickListener(this);
findViewById(R.id.buttonSquareRoot).setOnClickListener(this);
findViewById(R.id.buttonInvert).setOnClickListener(this);
findViewById(R.id.buttonCos).setOnClickListener(this);
findViewById(R.id.buttonSin).setOnClickListener(this);
findViewById(R.id.buttonToggleSign).setOnClickListener(this);
findViewById(R.id.buttonDecimalPoint).setOnClickListener(this);
findViewById(R.id.buttonEquals).setOnClickListener(this);
findViewById(R.id.buttonClear).setOnClickListener(this);
findViewById(R.id.buttonClearMemory).setOnClickListener(this);
findViewById(R.id.buttonAddToMemory).setOnClickListener(this);
findViewById(R.id.buttonSubtractFromMemory).setOnClickListener(this);
findViewById(R.id.buttonRecallMemory).setOnClickListener(this);
findViewById(R.id.buttonTan).setOnClickListener(this);
findViewById(R.id.buttonX3).setOnClickListener(this);
findViewById(R.id.buttonE).setOnClickListener(this);
findViewById(R.id.buttonP).setOnClickListener(this);
findViewById(R.id.buttonRandom).setOnClickListener(this);
findViewById(R.id.buttonx10).setOnClickListener(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.hide();
}
fm = false;
}
public void calc (View view)
{
String buttonPressed = ((Button) view).getText().toString();
String s1 = calculatorDisplay.getText().toString(),s2 =".";
if (s1.length()>6)
{
calculator.reset();
calculatorDisplay.setText("0.0");
fm = false;
}
if (DIGITS.contains(buttonPressed))
{
// digit was pressed
if (userIsInTheMiddleOfTypingANumber)
{
if (buttonPressed.equals(s2))
{
if (s1.equals("0.0") ||
calculatorDisplay.getText().length()<=0)
{
calculatorDisplay.setText("0.");
fm = true;
}
else
if (!fm)
{
s1 = s1 + buttonPressed;
calculatorDisplay.setText(s1);
fm = true;
}
}
else
{
calculatorDisplay.append(buttonPressed);
}
} else
{
calculatorDisplay.setText(buttonPressed);
userIsInTheMiddleOfTypingANumber = true;
}
} else
{
// operation was pressed
if (userIsInTheMiddleOfTypingANumber)
{
calculator.setOperand(Double.parseDouble(calculatorDisplay.getText().toString()));
userIsInTheMiddleOfTypingANumber = false;
}
calculator.performOperation(buttonPressed);
calculatorDisplay.setText(df.format(calculator.getResult()));
}
}
@Override
public void onClick (View view)
{
try {
calc(view);
} catch (Exception e) {
calc(view);
}
}
}
my problem: use dot and when i use sin , cos ,..... my app donot any thing
when i use it. please help me to fix problems

No comments:

Post a Comment