Need Help in developing a Programmer's Calculator

danskieness

Distinguished
Aug 25, 2011
2
0
18,510
This is my project for this coming finals

What i've been trying to do is exactly how the windows 7 calculator works
(for windows users run -> calc then select view and choose programmer)
converting from any base(bin,hex,oct,dec) to any base(bin,hex,oct,dec). But a bonus feature would be adding solutions

I've managed to make progress but i still lack alot

I wanna ask you guys, if could help me in debugging some stuffs like when i mash the decimal button, the inputted values just increases, also i need help in some validation, like blocking numbers 3-9 when your converting from binary


Code:
package com.example.numbersys;
import java.util.Stack;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.text.Editable;
import android.text.TextWatcher;


public class MyActivity extends Activity implements TextWatcher
{    
    EditText txtDecimal;
    TextView txtBinary,txtOctal,txtHexadecimal,finnsol,octsol,hexsol;
    long continv = 0, bse, res, multi, tobemul;
    int cur_selected=R.id.chdec,ctr,ctr2,ctr3,txtlen,rseto; 
    String txt, errcde;
    char invchar;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        txtDecimal=(EditText)findViewById(R.id.txtDecimal);
        txtBinary=(TextView)findViewById(R.id.txtBinary);
        txtOctal=(TextView)findViewById(R.id.txtOctal);
        txtHexadecimal=(TextView)findViewById(R.id.txtHexadecimal);
        txtDecimal.addTextChangedListener(this);
        finnsol=(TextView)findViewById(R.id.finnsol);
        
                      
    }
    public void beforeTextChanged(CharSequence sequence,int start,int count,int after)
    {
    }
    public void afterTextChanged(Editable editable)
    {
    }
    public void onTextChanged(CharSequence sequence,int start,int before,int count)
    {
    	
    }
    
        
        public void calculate(int base,TextView txtView,TextView texsol)
    {
    	StringBuffer solb = new StringBuffer();
        if(txtDecimal.getText().toString().trim().length()==0)
        {
            txtView.setText("");
            texsol.setText("");
            return;
        }
        try
        {
            Stack<Object> stack=new Stack<Object>();
            int number=Integer.parseInt(txtDecimal.getText().toString());
            
            while (number>0)
            {
                int remainder=number%base; // find remainder
                
                solb.append(remainder + " = " + number + " % " +  base + "\n");
                
          
                if(remainder<10)
                
                {
                    stack.push(remainder);
                    
                }
                else
                {
                    switch (remainder)
                    
                    {
                    case 10:
                        stack.push("A");
                        break;
                    case 11:
                        stack.push("B");
                        break;
                    case 12:
                        stack.push("C");
                        break;
                    case 13:
                        stack.push("D");
                        break;
                    case 14:
                        stack.push("E");
                        break;
                    case 15:
                        stack.push("F");
                        break;
                }
            }
            number/=base; 
        }
        StringBuffer buffer=new StringBuffer();
        while (!stack.isEmpty())
        {
                buffer.append(stack.pop().toString());
        }
        solb.append("Result : " + buffer.toString());
        txtDecimal.setText(buffer.toString());
        texsol.setText(solb.toString());
        
    }
       catch (Exception e)
    {
        txtView.setText(e.getMessage());
    }
}
        
        		private void decitobin() 
	{			txt=txtDecimal.getText().toString();
        			bse = 2;
        			StringBuffer solb = new StringBuffer();
    					txtlen = txt.length();
    					
    						//tobecnvrt = Integer.parseInt(txt);
    						rseto =  txtlen - 2;
    						for (ctr2 = 0 ; ctr2 <= txtlen - 1 ; ctr2++)
    						{
    							if (ctr2 < txtlen - 1)
    							{
    								multi = bse;
    							}
    							else
    							{
    								multi = 1;
    							}
    							for (ctr3 = rseto ; ctr3 > 0 ; ctr3--)
    							{
    								multi *= bse;
    							}
    							rseto--;
    							tobemul = Character.digit(txt.charAt(ctr2),10);
    							res += (tobemul * multi);
    						}
    						solb.append(res + "=" + tobemul + "*" + multi + "\n");
    						 txtDecimal.setText(String.valueOf(res));
    						//solb.append("Result : " + buffer.toString());
    					     //texsol.setText(solb.toString());
    }
    					
    					
    					
    				
    			
    		
    		
    	 
    	
    public void onRadioButtonClicked(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();
        if (cur_selected != view.getId()){
        
	        // Check which radio button was clicked
	        switch(view.getId()) {
	            case R.id.chbin:
	                if (checked)
	                	calculate(2,txtBinary,finnsol);
	              cur_selected = R.id.chbin;
	                break;
	            case R.id.choct:
	                if (checked)
	                	calculate(8,txtOctal,finnsol); 
	                cur_selected = R.id.choct;
	                break;
	            case R.id.chhex:
	            	if (checked)
	            		calculate(16,txtHexadecimal,finnsol);
	            	cur_selected = R.id.chhex;
	                break;
	                
	            case R.id.chdec:
	                if (checked)
	                	cur_selected=R.id.chdec;
	                	decitobin();
	                
	                	
	               
	                break;
	        }
        }
    }
    
    

}