You are on page 1of 5

package com.mycompany.

mycalc2;
import
import
import
import
import
import
import

android.app.Activity;
android.os.Bundle;
android.view.Menu;
android.view.MenuItem;
android.view.View;
android.widget.Button;
android.widget.TextView;

public class MainActivity extends Activity {


TextView textView;
TextView textView2;
static int unit=1;
double unit2=.1;
static int Dot=1;
static int num=0;
static int op=0;
static int temp5=0;
static int check=1;
static int temp4=5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView);
textView2=(TextView)findViewById((R.id.textView2));
textView.setText("0");
textView2.setText(("0"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
boolean isempty=true;
public void numbers(View sender) {
Button bt = (Button) sender;
textView2.append(bt.getText());
if (Dot==2)
{
if (unit2<.1)
{
temp3 = temp3 + unit2 * Double.parseDouble((bt.getText().toStrin
g()));
} else
{
temp3 = Double.parseDouble(bt.getText().toString());
temp3 = temp3 * .1;
}
unit2=unit2*.1;
}
else
{

if (unit > 2)
{
temp1 = temp1 * unit + Double.parseDouble((bt.getText().toSt
ring()));
}
else
{
temp1 = Double.parseDouble(bt.getText().toString());
}
unit =10;
check=0;
}
if(num==temp5){
num++;
}
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
double temp1=0;
double temp2=0;
double temp3=0;
static short operationToDo=0;
public void operators(View sender)
{
Button bt=(Button)sender;
if(check!=1)
{
op++;
temp5++;
}
unit = 1;
unit2 = .1;
Dot = 1;
textView2.append(bt.getText());
switch (operationToDo) {
case 0:
temp2 = temp2 + temp1 + temp3;
break;
case 1:

temp2 = temp2 - temp1 - temp3;


temp4=1;
break;
case 2:
if(temp2==0)
{
temp2 = 1;
temp2 = temp2 * (temp1 + temp3);
}
else
{
temp2 = temp2 * (temp1 + temp3);
}
temp4=2;
break;
case 3:
temp2 = temp2 / (temp1 + temp3);
temp4=3;
break;
}
if (bt.getText().toString().equals("+")) {
temp4=0;
operationToDo = 0;
}
if (bt.getText().toString().equals("-"))
{
temp4=1;
operationToDo = 1;
}
if (bt.getText().toString().equals("*"))
{
temp4=2;
operationToDo = 2;
}
if (bt.getText().toString().equals("/"))
{
temp4=3;
operationToDo = 3;
}
isempty = true;
}
public void decimal(View sender)
{
textView2.append(".");
Dot=2;
}
public void percentage(View sender)
{
Button bt=(Button)sender;
if(bt.getText().toString().equals("%"))
{
temp2=temp2*100;
textView.setText(Double.toString(temp2));
textView2.setText("");
}
}
public void reset(View sender)

{
Button bt=(Button)sender;
if(bt.getText().toString().equals("C"))
{
temp1=0;
temp2=0;
temp3=0;
temp4=5;
temp5=0;
num=0;
op=0;
check=1;
operationToDo=0;
textView.setText("");
textView2.setText("");
isempty=true;
unit=1;
unit2=.1;
Dot=1;
}
}
public void equals(View sender)
{
Button bt=(Button)sender;
if(bt.getText().toString().equals("="))
if(num!=op)
{
if (temp4 == 0)
{
temp2 = temp2 + temp1 + temp3;
}
if (temp4 == 1)
{
temp2 = temp2 - temp1 - temp3;
}
if (temp4 == 2)
{
temp2 = temp2 *(temp1 + temp3);
}
if (temp4 == 3)
{
temp2 = temp2 / (temp1 + temp3);
}
}
if(op==0 && )
{
textView.setText(textView2.getText());
}
else {
textView.setText(Double.toString(temp2));
}
textView2.setText("");
temp1=0;
temp3=0;
temp4=5;
temp5=0;
num=0;
op=0;
unit=1;
unit2=.1;

Dot=1;
check=1;
}
}
int l=digit.length();
delchar=digit.charAt(l);
textView2.setText(digit);
if (delchar == '1' || delchar== '2' || delchar== '3' || delchar== '4' ||
delchar== '5'
|| delchar== '6' || delchar== '7' || delchar== '8' || delchar==
'9' )
{
deleted = deleted+ digit.charAt(l);
digit = digit.substring(0, digit.length() - 1);
}
else
{
if(delchar == '+')
{
textView2.setText(digit);
temp2=temp2-Integer.parseInt(deleted);
}
}
}

You might also like