Ver Mensaje Individual
  #1 (permalink)  
Antiguo 01/11/2003, 02:18
fdnoelia
 
Fecha de Ingreso: octubre-2003
Ubicación: Cornella de Llobregat
Mensajes: 12
Antigüedad: 20 años, 7 meses
Puntos: 0
Necesito ayuda con esta calculadora

Tengo que hacer una calculadora que tengo este codigo que se compila bien pero no funciona correctamente. Alguien me podría hecar una manita:
CODIGO:

import java.io.*;
import java.util.*;
import Calculadora;


public class Calculadora{
String []operadores={"(",")","^","*","/","M","S","+","-"};

//leemos la expresion introducida por el usuario
public String [] leer_expresion (String exp){
int i=0;
String[]expresion= new String [exp.length()];
System.out.println("Longitud de la expresion: "+exp.length());
while (i<exp.length()){
System.out.println("La i vale: "+i);
int suma=0;
if (Character.isDigit(exp.charAt(i))){//devuelve true si es un digito
while(i<exp.length()&&Character.isDigit(exp.charAt (i))){
Character a = new Character(exp.charAt(i));
String f=a.toString();
Integer num=new Integer(f);
int b =num.intValue();
suma=suma*10+b;
i++;
}
Integer r=new Integer (suma);
expresion [i-1]=r.toString();
System.out.println("Leido numero: "+expresion [i-1]);
}else{
Character z= new Character (exp.charAt(i));
expresion [i]=z.toString();
System.out.println("Leido caracter: "+ expresion[i]);
i++;
}
}
i=0;
while(i<expresion.length){
if(expresion[i]==null){
expresion[i]=" ";
}
i++;
}
return expresion;
}

//comprobamos la coherencia de los parentesis
public void coherencia_parentesis(String[]expresion){
int abierto=0;
int cerrado=0;
int i=0;
int resta=0;
while (i<expresion.length){
if (expresion[i].equals("(")){
abierto++;
}
if (expresion[i].equals(")")){
cerrado++;
}
i++;
}
resta=abierto-cerrado;
if (resta!=0){
System.out.println("ERROR EN LA COLOCACION DE LOS PARENTESIS");
}
}

public String[]trozo_expresion(String[]a){
int i=0;
int j=0;
String[]exp=new String[a.length];
boolean fin=false;
boolean parentesis_abierto=false;
//while (!fin&& i<a.length){
while(!parentesis_abierto && i<a.length-1){
if(a[i].charAt(0)=='('){
parentesis_abierto=true;
i--;
}
i++;
}
if (parentesis_abierto){
while(!fin){
if(a[i].charAt(0)==')'){
fin=true;
}
exp[j]=a[i];
a[i]=" ";
i++;
j++;
}
while(j<a.length){
exp[j]=" ";
j++;
}
}else{
i=0;
j=0;
while(i<a.length){
exp[j]=a[i];
a[i]=" ";
i++;
j++;
}
}
return exp;
}

//solo cuenta operadores, descarta lo demas, numeros espacios y parentesis
public String [] caracteres (String[]expresion){
int i=0;
int j=0;
int a=0;
while(i<expresion.length){
if(expresion[i] != " " && !Character.isDigit(expresion[i].charAt(0))
&& expresion[i] !="(" && expresion[i] !=")"){
a++;
}
i++;
}
i=0;
String[]aux= new String[a];
while(i<expresion.length){
if (expresion[i] != " " && !Character.isDigit(expresion[i].charAt(0))
&& expresion[i] !="(" && expresion[i] !=")"){
aux[j]=expresion[i];
j++;
}
i++;//devuelve un vector que cuenta solo operandos
}
return aux;
}


public int prioridades(String []v, String b){
int prioridad=0;
if(b.charAt(0)=='^'){
prioridad=0;
}else if(b.charAt(0)=='*' || b.charAt(0)=='/'){
prioridad=1;
}else if(b.charAt(0)=='M'){
prioridad=2;
}else if(b.charAt(0)=='S'){
prioridad=3;
}else if(b.charAt(0)=='+'||b.charAt(0)=='-'){
prioridad=4;
}
return prioridad;
}

//metodo para ordenar los operadores
public void ordenar_operadores(String[]expresion, String[]operadores){
for (int i=0; i<expresion.length-1; i++){
String a=expresion[i];
int mayor = prioridades(operadores, expresion[i]);
int j;
int posicion=i;
for (j=i+1; j<expresion.length; j++){
String oper= expresion[j];
int prioridad= prioridades(operadores, expresion[j]);
if (prioridad < mayor){
a=expresion[j];
posicion=j;
}
}
expresion[posicion]=expresion[i];
expresion[i]=a;
}
}

public int orden (String[] trozo, String operandos){
boolean fin=false;
int i=0;
int posicion=0;//La i es la posición del operando con mas prioridad
while(i<trozo.length && !fin){
if(operandos ==trozo[i]){
fin=true;
posicion=i;
i--;
}
i++;//da la posición a calcular
}
return posicion;
}



public int posicion(String[]parte,String operandos){
boolean fin = false;
int i=0;
int posicion = 0;//i posicion del operando con más prioridad
while(i<parte.length && !fin){
if(operandos==parte[i]){
fin=true;
posicion=i;
i--;
}
i++;//posicion de los operando del trozo a calcular
}
return posicion;
}

public int calculo(String a, String b, String c){
int res=0;
int f=Integer.parseInt(a);
int g=Integer.parseInt(c);
if (b.charAt(0)=='^'){
res=(int) Math.exp(f);
res=(int) Math.exp(g);
}else if(b.charAt(0)=='*'){
res=f*g;
}else if(b.charAt(0)=='/'){
res=f/g;
}else if(b.charAt(0)=='M'){
res=f%g;
}else if(b.charAt(0)=='+'){
res=f+g;
}else if(b.charAt(0)=='-'){
res=f-g;
}else if(b.charAt(0)=='v'){
res=(int)Math.sqrt(f);
res=(int)Math.sqrt(g);
}
return res;
}

public String[] canvio(String[]v){
int i=0;
String[] a=new String[v.length];
while(i<v.length){
a[i]=" ";
i++;
}
return a;
}

public String calcular_parentesis(String[]a,String[]operandos){
int i=0;
int res=0;
boolean fin=false;
System.out.println("La longitud de los operandos es: "+operandos.length);
System.out.println("La longitud del trozo es: "+a.length);
while(i<operandos.length){
System.out.println("H O L A");
int pos=posicion(a,operandos[i]);
//i--;
if(a[pos].charAt(0)!='S'){
System.out.println("entra....");
int pos1=pos-1;
int pos2=pos+2;
while(a[pos1]==" "){
pos1--;
}
System.out.println("La posicion1 es: "+pos1);
while(a[pos2]==" "){
pos2++;
}
System.out.println("La posicion2 es: "+pos2);
res=calculo(a[pos1],a[pos],a[pos2]);
System.out.println("El resultado es: "+res);
String as=Integer.toString(res);
a[pos1]=as;
a[pos]=" ";
a[pos2]=" ";
}else{
int pos1=pos;
while(a[pos1+1]==" "){
pos1++;
}
int nombre=Integer.parseInt(a[pos1]);
res=(int)(Math.sqrt((double)nombre));
String as=Integer.toString(res);
a[pos]=as;
a[pos1]= " ";
}
i++;
}
String resul=Integer.toString(res);
return resul;
}

//metodo devolvera true si hay numeros para calcular
public boolean hay_numeros(String []expresion){
int i=0;
boolean lleno=false;
while (i<expresion.length && !lleno){
if(expresion[i]!=" "){
lleno=true;
}
i++;
}
System.out.println("¿Hay numeros?: " + lleno);
return lleno;
}

public int calcular(String[]v){
int i=0;
int res=0;
boolean fin=false;
System.out.println("La longitud es: "+v.length);
while(i<v.length-1 && !fin){
int f=Integer.parseInt(v[i]);// el primero es un nombre
System.out.println("EL nombre es: "+v[i]+" la i es "+i+" y "+v[i+1]);
if (i==v.length||v[i+1]==" "){
fin=true;
}
if (v[i+1]!=" "){
if(v[i+1].charAt(0)=='('){
String[]z=new String[v.length];//expresion o v
// g=resto(v,resul);
int g=Integer.parseInt(v[i+2]);
String b=v[i+1];
if (b.charAt(0)=='^'){
res=f^g;
String j=Integer.toString(res);
v[i+2]=j;
}else if(b.charAt(0)=='*'){
res=f*g;
String j=Integer.toString(res);
v[i+2]=j;
}else if(b.charAt(0)=='/'){
res=f/g;
String j=Integer.toString(res);
v[i+2]=j;
}else if(b.charAt(0)=='M'){
res=f%g;
String j=Integer.toString(res);
v[i+2]=j;
}else if(b.charAt(0)=='+'){
res=f+g;
String j=Integer.toString(res);
v[i+2]=j;
}else if(b.charAt(0)=='-'){
res=f-g;
String j=Integer.toString(res);
v[i+2]=j;
}
v[i]=" ";
v[i+1]=" ";
i=i+2;
}
}else{
res=(int)(Math.sqrt((double) f));
String j=Integer.toString(res);
v[i+1]=j;
v[i]=" ";
i=i+1;
}
}
return res;
}

}
}