problem description
uses toString ();
what result do you expect? What is the error message actually seen?
I expect to use scientific counting to display the results directly
the environmental background of the problems and what methods you have tried
I"m working on an Android calculator
related codes
/ / Please paste the code text below (do not replace the code with pictures)
List<Button> list = new ArrayList<Button>();//
List<Integer> thisnum= new ArrayList<Integer>();//
List<Character> stack = new ArrayList<Character>();//
Stack<Character> SOP = new Stack<Character>();//(
List<String> L = new ArrayList<String>();//
Stack<BigDecimal> scalc = new Stack<BigDecimal>();//
boolean ClearA = false;
case R.id.buttonA://
BigDecimal re;
ClearA=true;
//STACKcompress()
if(STACKcompress()){//
re = suffixToResult();//
if(!textView1.getText().equals("")){
textView.setText(re.toString());
}
}else if(stack.size()==1){ //
Character character = stack.get(0);
if(character!="-"){
textView1.setText("");
stack.clear();
}else{
textView.setText("-");
}
}
return;
private boolean STACKcompress () {
/ / convert parameter infix expression expression to suffix expression and store it in L, return L
L.clear();
SOP.clear();
Log.d("stack",stack.toString());
int dian =0; //
boolean dia=false; //
for (int i = 0; i < stack.size(); iPP) //
{
char element = stack.get(i);
if ("0" <= element && element <= "9") {
if(dia){
dianPP;
}
thisnum.add(element-"0");
Log.d("(int) element", String.valueOf(element-"0"));
if(i +1==stack.size()){
BigDecimal ten=BigDecimal.valueOf(10);
BigDecimal zxc= BigDecimal.valueOf(0);
for(int a=0;a<thisnum.size();aPP){
zxc=zxc.multiply(ten);
zxc=zxc.add(BigDecimal.valueOf(thisnum.get(a)));
}
if(dia){
for(int a=0;a<dian;aPP){
zxc=zxc.divide(ten);
}
dia=false;
dian=0;
}
L.add(""+zxc);
thisnum.clear();
Log.d("String(zxc) i+1 ", ""+zxc);
}
} else if (SwitchOperator(element)) {
if(stack.size()==1&&element!="-"){
Log.d("element in if",""+element);
return false;
}
if(!thisnum.isEmpty()){
BigDecimal ten=BigDecimal.valueOf(10);
BigDecimal zxc= BigDecimal.valueOf(0);
for(int a=0;a<thisnum.size();aPP){
zxc=zxc.multiply(ten);
zxc=zxc.add(BigDecimal.valueOf(thisnum.get(a)));
}
if(dia){
for(int a=0;a<dian;aPP){
zxc=zxc.divide(ten);
}
dia=false;
dian=0;
}
L.add(""+zxc);
thisnum.clear();
Log.d("String(zxc) i+1 ", ""+zxc);
}
while(SopTureOrFlase(element)) {
L.add(""+SOP.pop());
}
if(i +1!=stack.size()) {
SOP.push(element);
}else if(stack.size()!=1){
return false;
}
}else if(element=="."){
dia=true;
}else if(element=="%"){
if(!thisnum.isEmpty()) {
BigDecimal ten=BigDecimal.valueOf(10);
BigDecimal zxc = BigDecimal.valueOf(0);
for (int a = 0; a < thisnum.size(); aPP) {
zxc=zxc.multiply(ten);
zxc=zxc.add(BigDecimal.valueOf(thisnum.get(a)));
}
Log.d("thisnum", thisnum.toString());
thisnum.clear();
zxc=zxc.divide(BigDecimal.valueOf(100));
Log.d("zxc", "" + zxc);
L.add("" + zxc);
}
} else{
textView.setText(" :");
Log.e("","");
this.onStop();
}
}
while (!SOP.isEmpty()) //sopL
{
L.add(""+SOP.pop());
}
return true;
}
private BigDecimal suffixToResult() { //
BigDecimal temp_value ;
for (int i = 0; i < L.size(); iPP) {
String element1 = L.get(i);
Log.d("L",L.toString());
char element = element1.charAt(0);//
Log.d("String.valueOf(BigDecimal.valueOf(element))", String.valueOf(BigDecimal.valueOf(element)));
if (SwitchOperator(element)) {
Log.d("", String.valueOf(element));
//2 num1 num2 :num2num1
//2
try{
BigDecimal num1 = scalc.pop();
BigDecimal num2 = scalc.pop();
switch (element) {
case "+":
temp_value = num2.add(num1) ;
break;
case "-":
temp_value = num2.subtract(num1) ;
break;
case "*":
temp_value = num2.multiply(num1);
break;
case "/":
temp_value = num2.divide(num1) ;
break;
default:
temp_value = BigDecimal .valueOf(999);
break;
}
//temp_value
TextView1("");
scalc.push(temp_value);
}catch (EmptyStackException e) {
TextView1("");
return BigDecimal .valueOf(-1);
}
//element num2 num1 temp_value
} else {
BigDecimal bd = new BigDecimal(element1);
Log.d("bd", bd.toString()) ;
scalc.push(bd);
}
}Log.d("scalc",scalc.toString());
BigDecimal x=scalc.pop();
Log.d("x",x.toString());
return x.stripTrailingZeros();
}