Skip to content

Instantly share code, notes, and snippets.

@efturtle
Last active February 24, 2020 04:05
Show Gist options
  • Select an option

  • Save efturtle/ad06c3a21521c0be59c2ae4f8c183938 to your computer and use it in GitHub Desktop.

Select an option

Save efturtle/ad06c3a21521c0be59c2ae4f8c183938 to your computer and use it in GitHub Desktop.
Cientific Calculator mode 1
package com.example.calculadoramodo1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
//atributes
private TextView campo;
//variables para operar
public double a = 0;
public double b = 0;
//variable que almacena cadena para numeros
String cadena = "";
//muestra resultados anteriores
String historial = "";
boolean bandera = false;
//int para un switch, case of, we do an operation.
int signo = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume (){
super.onResume();
Toast.makeText(this, "Bienvenido de vuelta", Toast.LENGTH_SHORT).show();
}
public void button5(View view){
TextView campo = findViewById(R.id.textView); //Toma el campo
cadena += "5"; //le agrega un 5 a la cadena
campo.setText(cadena); //para el usuario
if(bandera == false){ //si no se a registrado la primera variable se llena
a = Double.parseDouble(cadena);
}else { //en caso que a ya este llenado
b = Double.parseDouble(cadena);
}
}
public void button6 (View view){
TextView campo = findViewById(R.id.textView);
cadena += "6";
campo.setText(cadena);
if(bandera == false){
a = Double.parseDouble(cadena);
}else {
b = Double.parseDouble(cadena);
}
}
public void button1 (View view){
TextView campo = findViewById(R.id.textView);
cadena += "1";
campo.setText(cadena);
if(bandera == false){
a = Double.parseDouble(cadena);
}else {
b = Double.parseDouble(cadena);
}
}
public void button2 (View view){
TextView campo = findViewById(R.id.textView);
cadena += "2";
campo.setText(cadena);
if(bandera == false){
a = Double.parseDouble(cadena);
}else {
b = Double.parseDouble(cadena);
}
}
public void button3 (View view){
TextView campo = findViewById(R.id.textView);
cadena += "3";
campo.setText(cadena);
if(bandera == false){
a = Double.parseDouble(cadena);
}else {
b = Double.parseDouble(cadena);
}
}
public void button4 (View view){
TextView campo = findViewById(R.id.textView);
cadena += "4";
campo.setText(cadena);
if(bandera == false){
a = Double.parseDouble(cadena);
}else {
b = Double.parseDouble(cadena);
}
}
public void button7 (View view){
TextView campo = findViewById(R.id.textView);
cadena += "7";
campo.setText(cadena);
if(bandera == false){
a = Double.parseDouble(cadena);
}else {
b = Double.parseDouble(cadena);
}
}
public void button8 (View view){
TextView campo = findViewById(R.id.textView);
cadena += "8";
campo.setText(cadena);
if(bandera == false){
a = Double.parseDouble(cadena);
}else {
b = Double.parseDouble(cadena);
}
}
public void button9 (View view){
TextView campo = findViewById(R.id.textView);
cadena += "9";
campo.setText(cadena);
if(bandera == false){
a = Double.parseDouble(cadena);
}else {
b = Double.parseDouble(cadena);
}
}
public void button0 (View view){
TextView campo = findViewById(R.id.textView);
cadena += "0";
campo.setText(cadena);
if(bandera == false){
a = Double.parseDouble(cadena);
}else {
b = Double.parseDouble(cadena);
}
}
//botones de operaciones, llamados por el metodo escrito en el xml tag
public void suma(View view){
TextView campo = findViewById(R.id.textView); //textview tomara valor del signo correspondiente
campo.setText("+"); //igualamos el text a el signo
signo = 1; //switch value for operation
bandera = true; //bandera prendida para llenar valor b
cadena = ""; //limpiamos el valor cadena para que no se acumulen los numeros ya puestos./
}
public void resta (View view){
TextView campo = findViewById(R.id.textView);
campo.setText("-");
signo = 2;
cadena = "";
bandera = true;
}
public void multiplicacion (View view){
TextView campo = findViewById(R.id.textView);
campo.setText("x");
signo = 3;
cadena = "";
bandera = true;
}
public void division(View view){
TextView campo = findViewById(R.id.textView);
campo.setText("/");
signo = 4;
cadena = "";
bandera = true;
}
//ySQRTx potenciaE reciproca raizCuadrada yPotenciax logaritmo10 10Pot logaritmoNatural
//the ones with only 1 operator make bandera false, otherwise not.
public void ySQRTx(View view){
TextView campo = findViewById(R.id.textView);
campo.setText("√");
signo = 5;
cadena = "";
bandera = true;
}
public void potenciaE(View view){
TextView campo = findViewById(R.id.textView);
campo.setText("e↑"+cadena);
signo = 6;
bandera = false;
}
public void reciproca(View view){
TextView campo = findViewById(R.id.textView);
campo.setText("1/"+cadena);
signo = 7;
bandera = false;
}
public void raizCuadrada(View view){
TextView campo = findViewById(R.id.textView);
campo.setText("√"+cadena);
signo = 8;
bandera = false;
}
public void yPotenciax(View view){
TextView campo = findViewById(R.id.textView);
campo.setText("↑");
signo = 9;
bandera = true;
}
public void logaritmo10(View view){
TextView campo = findViewById(R.id.textView);
campo.setText("Log");
signo = 10;
bandera = false;
}
public void Pot10(View view){
TextView campo = findViewById(R.id.textView);
campo.setText("10↑"+cadena);
signo = 11;
bandera = false;
}
public void logaritmoNatural(View view){
TextView campo = findViewById(R.id.textView);
campo.setText("LogaritmoNatural");
signo = 12;
bandera = false;
}
public void cleartext(View view){
TextView campo = findViewById(R.id.textView);
campo.setText("0");
cadena = "";
bandera = false;
a =0;
b = 0;
signo = 0;
cadena = "";
}
public void enter (View view){
TextView campo = findViewById(R.id.textView);
double temp = 0;
switch (signo){ //what ever is the signo is the operation that the user decided to make.
case 1:
temp = a+b; //operation
campo.setText(String.valueOf(temp));
bandera = false; //now a will fill in
break;
case 2:
temp = a-b;
campo.setText(String.valueOf(temp));
bandera = false;
break;
case 3:
temp = a*b;
campo.setText(String.valueOf(temp));
bandera = false;
break;
case 4:
if (a!=0 && b!=0) {
temp = a / b;
campo.setText(String.valueOf(temp));
bandera = false;
}else{
campo.setText("0");
Toast.makeText(this, "Division con 0", Toast.LENGTH_SHORT).show();
}
break;
//me falto este, no supe como calcular la raiz enesima
case 5:
double tempo = 1/a;
temp = Math.pow(b, tempo);
campo.setText(String.valueOf(temp));
bandera = false;
break;
case 6:
temp = Math.exp(a);
campo.setText(String.valueOf(temp));
bandera = false;
break;
case 7:
temp = (1/a);
campo.setText(String.valueOf(temp));
bandera = false;
break;
case 8:
temp = Math.sqrt(a);
campo.setText(String.valueOf(temp));
bandera = false;
break;
case 9:
temp = Math.pow(a,b);
campo.setText(String.valueOf(temp));
bandera = false;
break;
case 10:
temp = Math.log10(a);
campo.setText(String.valueOf(temp));
bandera = false;
break;
case 11:
temp = Math.pow(a,10.0);
campo.setText(String.valueOf(temp));
bandera = false;
break;
case 12:
temp = Math.log(a);
campo.setText(String.valueOf(temp));
bandera = false;
break;
}
signo = 0;
a=0;
b=0;
cadena = "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment