0
Answer

Calculator application

Ask a question
Javed Iqbal

Javed Iqbal

10y
535
1

Hi all,

I am developing calculator app using C#.I have successfuly implemented its standard arithmetic functions but a bit trapped in implementing scientific functions such as sin,cos.tan etc.
I take an operand1,then operator and finally second opernad to workout arithmetic calculations.In the very first attempt to do scientific calculation i for the sake of programme format first takes input followed by sin/cos/tan etc and then out result.

Here is my code:
 
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace Calculator
  10. {
  11. public partial class Form1 : Form
  12. {
  13. string input = string.Empty;
  14. string operand1 = string.Empty;
  15. string operand2 = string.Empty;
  16. char operaton;
  17. double result = 0.0;
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. }
  22. private void one_Click(object sender, EventArgs e)
  23. {
  24. this.textBox1.Text = " ";
  25. input += "1";
  26. this.textBox1.Text += input;
  27. }
  28. private void two_Click(object sender, EventArgs e)
  29. {
  30. this.textBox1.Text = " ";
  31. input += "2";
  32. this.textBox1.Text += input;
  33. }
  34. private void three_Click(object sender, EventArgs e)
  35. {
  36. this.textBox1.Text = " ";
  37. input += "3";
  38. this.textBox1.Text += input;
  39. }
  40. private void four_Click(object sender, EventArgs e)
  41. {
  42. this.textBox1.Text = " ";
  43. input += "4";
  44. this.textBox1.Text += input;
  45. }
  46. private void five_Click(object sender, EventArgs e)
  47. {
  48. this.textBox1.Text = " ";
  49. input += "5";
  50. this.textBox1.Text += input;
  51. }
  52. private void six_Click(object sender, EventArgs e)
  53. {
  54. this.textBox1.Text = " ";
  55. input += "6";
  56. this.textBox1.Text += input;
  57. }
  58. private void seven_Click(object sender, EventArgs e)
  59. {
  60. this.textBox1.Text = " ";
  61. input += "7";
  62. this.textBox1.Text += input;
  63. }
  64. private void eight_Click(object sender, EventArgs e)
  65. {
  66. this.textBox1.Text = " ";
  67. input += "8";
  68. this.textBox1.Text += input;
  69. }
  70. private void nine_Click(object sender, EventArgs e)
  71. {
  72. this.textBox1.Text = " ";
  73. input += "9";
  74. this.textBox1.Text += input;
  75. }
  76. private void dot_Click(object sender, EventArgs e)
  77. {
  78. this.textBox1.Text = " ";
  79. input += ".";
  80. this.textBox1.Text += input;
  81. }
  82. private void zero_Click(object sender, EventArgs e)
  83. {
  84. this.textBox1.Text = " ";
  85. input += "0";
  86. this.textBox1.Text += input;
  87. }
  88. private void clear_Click(object sender, EventArgs e)
  89. {
  90. this.textBox1.Text = "0";
  91. this.input = string.Empty;
  92. this.operand1 = string.Empty;
  93. this.operand2 = string.Empty;
  94. }
  95. private void plus_Click(object sender, EventArgs e)
  96. {
  97. operand1 = input;
  98. operaton = '+';
  99. input = string.Empty;
  100. }
  101. private void minus_Click(object sender, EventArgs e)
  102. {
  103. operand1 = input;
  104. operaton = '-';
  105. input = string.Empty;
  106. }
  107. private void mul_Click(object sender, EventArgs e)
  108. {
  109. operand1 = input;
  110. operaton = '*';
  111. input = string.Empty;
  112. }
  113. private void div_Click(object sender, EventArgs e)
  114. {
  115. operand1 = input;
  116. operaton = '/';
  117. input = string.Empty;
  118. }
  119. private void mod_Click(object sender, EventArgs e)
  120. {
  121. operand1 = input;
  122. operaton = '%';
  123. input = string.Empty;
  124. }
  125. private void sine_Click(object sender, EventArgs e)
  126. {
  127. operand1 = input;
  128. operaton = 's';
  129. input = string.Empty;
  130. }
  131. private void cosine_Click(object sender, EventArgs e)
  132. {
  133. operand1 = input;
  134. operaton = 'c';
  135. input = string.Empty;
  136. }
  137. private void tan_Click(object sender, EventArgs e)
  138. {
  139. operand1 = input;
  140. operaton = 't';
  141. input = string.Empty;
  142. }
  143. private void equals_Click(object sender, EventArgs e)
  144. {
  145. operand2 = input;
  146. double num1, num2;
  147. double.TryParse(operand1, out num1);
  148. double.TryParse(operand2, out num2);
  149. if (operaton == '+')
  150. {
  151. result = num1 + num2;
  152. textBox1.Text = result.ToString();
  153. }
  154. else if (operaton == '-')
  155. {
  156. result = num1 - num2;
  157. textBox1.Text = result.ToString();
  158. }
  159. else if (operaton == '*')
  160. {
  161. result = num1 * num2;
  162. textBox1.Text = result.ToString();
  163. }
  164. else if (operaton == '/')
  165. {
  166. if (num2 != 0)
  167. {
  168. result = num1 / num2;
  169. textBox1.Text = result.ToString();
  170. }
  171. else
  172. {
  173. textBox1.Text = "DIV/Zero!";
  174. }
  175. }
  176. else if (operaton == '%')
  177. {
  178. result = num1 % num2;
  179. textBox1.Text = result.ToString();
  180. }
  181. else if (operaton == 's')
  182. {
  183. int inp;
  184. int.TryParse(operand1, out inp);
  185. result = Math.Sin(inp);
  186. textBox1.Text = result.ToString();
  187. }
  188. else if (operaton == 'c')
  189. {
  190. int inp;
  191. int.TryParse(operand1, out inp);
  192. result = Math.Cos(num1);
  193. textBox1.Text = result.ToString();
  194. }
  195. else if (operaton == 't')
  196. {
  197. int inp;
  198. int.TryParse(operand1, out inp);
  199. result = Math.Tan(num1);
  200. textBox1.Text = result.ToString();
  201. }
  202. }
  203. }
  204. }

 
 

 

I want to mention here that my output for scientific functions is not correct.Also,kindly tell me error in this format as in second attempt i will convert it to standard scientific mode i.e. pressing sin/cos/tan and then operand.

Regards