ranging
Sign in with
Sign up | Sign in
Your question

Need Help in developing a Programmer's Calculator

Tags:
  • Windows 7
  • Apps
Last response: in Apps General Discussion
Share
April 8, 2013 8:24:48 AM

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


  1. package com.example.numbersys;
  2. import java.util.Stack;
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6.  
  7. import android.widget.EditText;
  8. import android.widget.RadioButton;
  9. import android.widget.TextView;
  10. import android.text.Editable;
  11. import android.text.TextWatcher;
  12.  
  13.  
  14. public class MyActivity extends Activity implements TextWatcher
  15. {
  16. EditText txtDecimal;
  17. TextView txtBinary,txtOctal,txtHexadecimal,finnsol,octsol,hexsol;
  18. long continv = 0, bse, res, multi, tobemul;
  19. int cur_selected=R.id.chdec,ctr,ctr2,ctr3,txtlen,rseto;
  20. String txt, errcde;
  21. char invchar;
  22.  
  23. /** Called when the activity is first created. */
  24. @Override
  25. public void onCreate(Bundle savedInstanceState)
  26. {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_my);
  29. txtDecimal=(EditText)findViewById(R.id.txtDecimal);
  30. txtBinary=(TextView)findViewById(R.id.txtBinary);
  31. txtOctal=(TextView)findViewById(R.id.txtOctal);
  32. txtHexadecimal=(TextView)findViewById(R.id.txtHexadecimal);
  33. txtDecimal.addTextChangedListener(this);
  34. finnsol=(TextView)findViewById(R.id.finnsol);
  35.  
  36.  
  37. }
  38. public void beforeTextChanged(CharSequence sequence,int start,int count,int after)
  39. {
  40. }
  41. public void afterTextChanged(Editable editable)
  42. {
  43. }
  44. public void onTextChanged(CharSequence sequence,int start,int before,int count)
  45. {
  46.  
  47. }
  48.  
  49.  
  50. public void calculate(int base,TextView txtView,TextView texsol)
  51. {
  52. StringBuffer solb = new StringBuffer();
  53. if(txtDecimal.getText().toString().trim().length()==0)
  54. {
  55. txtView.setText("");
  56. texsol.setText("");
  57. return;
  58. }
  59. try
  60. {
  61. Stack<Object> stack=new Stack<Object>();
  62. int number=Integer.parseInt(txtDecimal.getText().toString());
  63.  
  64. while (number>0)
  65. {
  66. int remainder=number%base; // find remainder
  67.  
  68. solb.append(remainder + " = " + number + " % " + base + "\n");
  69.  
  70.  
  71. if(remainder<10)
  72.  
  73. {
  74. stack.push(remainder);
  75.  
  76. }
  77. else
  78. {
  79. switch (remainder)
  80.  
  81. {
  82. case 10:
  83. stack.push("A");
  84. break;
  85. case 11:
  86. stack.push("B");
  87. break;
  88. case 12:
  89. stack.push("C");
  90. break;
  91. case 13:
  92. stack.push("D");
  93. break;
  94. case 14:
  95. stack.push("E");
  96. break;
  97. case 15:
  98. stack.push("F");
  99. break;
  100. }
  101. }
  102. number/=base;
  103. }
  104. StringBuffer buffer=new StringBuffer();
  105. while (!stack.isEmpty())
  106. {
  107. buffer.append(stack.pop().toString());
  108. }
  109. solb.append("Result : " + buffer.toString());
  110. txtDecimal.setText(buffer.toString());
  111. texsol.setText(solb.toString());
  112.  
  113. }
  114. catch (Exception e)
  115. {
  116. txtView.setText(e.getMessage());
  117. }
  118. }
  119.  
  120. private void decitobin()
  121. { txt=txtDecimal.getText().toString();
  122. bse = 2;
  123. StringBuffer solb = new StringBuffer();
  124. txtlen = txt.length();
  125.  
  126. //tobecnvrt = Integer.parseInt(txt);
  127. rseto = txtlen - 2;
  128. for (ctr2 = 0 ; ctr2 <= txtlen - 1 ; ctr2++)
  129. {
  130. if (ctr2 < txtlen - 1)
  131. {
  132. multi = bse;
  133. }
  134. else
  135. {
  136. multi = 1;
  137. }
  138. for (ctr3 = rseto ; ctr3 > 0 ; ctr3--)
  139. {
  140. multi *= bse;
  141. }
  142. rseto--;
  143. tobemul = Character.digit(txt.charAt(ctr2),10);
  144. res += (tobemul * multi);
  145. }
  146. solb.append(res + "=" + tobemul + "*" + multi + "\n");
  147. txtDecimal.setText(String.valueOf(res));
  148. //solb.append("Result : " + buffer.toString());
  149. //texsol.setText(solb.toString());
  150. }
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160. public void onRadioButtonClicked(View view) {
  161. // Is the button now checked?
  162. boolean checked = ((RadioButton) view).isChecked();
  163. if (cur_selected != view.getId()){
  164.  
  165. // Check which radio button was clicked
  166. switch(view.getId()) {
  167. case R.id.chbin:
  168. if (checked)
  169. calculate(2,txtBinary,finnsol);
  170. cur_selected = R.id.chbin;
  171. break;
  172. case R.id.choct:
  173. if (checked)
  174. calculate(8,txtOctal,finnsol);
  175. cur_selected = R.id.choct;
  176. break;
  177. case R.id.chhex:
  178. if (checked)
  179. calculate(16,txtHexadecimal,finnsol);
  180. cur_selected = R.id.chhex;
  181. break;
  182.  
  183. case R.id.chdec:
  184. if (checked)
  185. cur_selected=R.id.chdec;
  186. decitobin();
  187.  
  188.  
  189.  
  190. break;
  191. }
  192. }
  193. }
  194.  
  195.  
  196.  
  197. }

More about : developing programmer calculator

a c 216 $ Windows 7
April 8, 2013 8:33:49 AM

Shouldn't you get help from the TA?
m
0
l
April 8, 2013 8:40:07 AM

rgd1101 said:
Shouldn't you get help from the TA?


whats TA?
m
0
l
a c 216 $ Windows 7
April 8, 2013 8:42:25 AM

teacher assistance
m
0
l
Tom’s guide in the world
  • Germany
  • France
  • Italy
  • Ireland
  • UK
Follow Tom’s guide
Subscribe to our newsletter
  • add to twitter
  • add to facebook
  • ajouter un flux RSS