Advertisement
Guest User

Unlucky calculator

a guest
Mar 23rd, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4.  
  5.  
  6. public class win : Form
  7. {
  8.  
  9. Button[] b = new Button[10];
  10. Button bDot, bPlus, bSub, bMul, bDiv, bEqu, bClr;
  11. Panel panCalc;
  12. TextBox txtCalc;
  13.  
  14. Double dblAcc;
  15. Double dblSec;
  16. bool blnClear, blnFrstOpen;
  17. String strOper;
  18.  
  19. public win()
  20. {
  21. try
  22. {
  23. this.Text = "Calculator";
  24. panCalc = new Panel();
  25. txtCalc = new TextBox();
  26.  
  27. txtCalc.Location = new Point(10, 10);
  28. txtCalc.Size = new Size(150, 10);
  29. txtCalc.ReadOnly = true;
  30. txtCalc.RightToLeft = RightToLeft.Yes;
  31. panCalc.Size = new Size(200, 200);
  32. panCalc.BackColor = Color.Aqua;
  33. panCalc.Controls.Add(txtCalc);
  34. addButtons(panCalc);
  35. this.Size = new Size(200, 225);
  36. this.Controls.Add(panCalc);
  37.  
  38. dblAcc = 0;
  39. dblSec = 0;
  40. blnFrstOpen = true;
  41. blnClear = false;
  42. strOper = new String('=', 1);
  43. }
  44. catch (Exception e)
  45. {
  46. Console.WriteLine("error ...... " + e.StackTrace);
  47. }
  48. }
  49.  
  50. private void addButtons(Panel p)
  51. {
  52. for (int i = 0; i <= 9; i++)
  53. {
  54. b[i] = new Button();
  55. b[i].Text = Convert.ToString(i);
  56. b[i].Size = new Size(25, 25);
  57. b[i].BackColor = Color.White;
  58. b[i].Click += new EventHandler(btn_clk);
  59. p.Controls.Add(b[i]);
  60. }
  61. b[0].Location = new Point(10, 160);
  62. b[1].Location = new Point(10, 120);
  63. b[4].Location = new Point(10, 80);
  64. b[7].Location = new Point(10, 40);
  65.  
  66. b[2].Location = new Point(50, 120);
  67. b[5].Location = new Point(50, 80);
  68. b[8].Location = new Point(50, 40);
  69.  
  70. b[3].Location = new Point(90, 120);
  71. b[6].Location = new Point(90, 80);
  72. b[9].Location = new Point(90, 40);
  73.  
  74. bDot = new Button();
  75. bDot.Size = new Size(25, 25);
  76. bDot.Location = new Point(50, 160);
  77. bDot.BackColor = Color.White;
  78. bDot.Text = ".";
  79. bDot.Click += new EventHandler(btn_clk);
  80.  
  81. bPlus = new Button();
  82. bPlus.Size = new Size(25, 25);
  83. bPlus.Location = new Point(130, 160);
  84. bPlus.BackColor = Color.White;
  85. bPlus.Text = "+";
  86. bPlus.Click += new EventHandler(btn_Oper);
  87.  
  88. bSub = new Button();
  89. bSub.Size = new Size(25, 25);
  90. bSub.Location = new Point(130, 120);
  91. bSub.BackColor = Color.White;
  92. bSub.Text = "-";
  93. bSub.Click += new EventHandler(btn_Oper);
  94.  
  95. bMul = new Button();
  96. bMul.Size = new Size(25, 25);
  97. bMul.Location = new Point(130, 80);
  98. bMul.BackColor = Color.White;
  99. bMul.Text = "*";
  100. bMul.Click += new EventHandler(btn_Oper);
  101.  
  102. bDiv = new Button();
  103. bDiv.Size = new Size(25, 25);
  104. bDiv.Location = new Point(130, 40);
  105. bDiv.BackColor = Color.White;
  106. bDiv.Text = "/";
  107. bDiv.Click += new EventHandler(btn_Oper);
  108.  
  109. bEqu = new Button();
  110. bEqu.Size = new Size(25, 25);
  111. bEqu.Location = new Point(90, 160);
  112. bEqu.BackColor = Color.White;
  113. bEqu.Text = "=";
  114. bEqu.Click += new EventHandler(btn_equ);
  115.  
  116. bClr = new Button();
  117. bClr.Size = new Size(20, 45);
  118. bClr.Location = new Point(170, 40);
  119. bClr.BackColor = Color.Orange;
  120. bClr.Text = "AC";
  121. bClr.Click += new EventHandler(btn_clr);
  122.  
  123. p.Controls.Add(bDot);
  124. p.Controls.Add(bPlus);
  125. p.Controls.Add(bSub);
  126. p.Controls.Add(bMul);
  127. p.Controls.Add(bDiv);
  128. p.Controls.Add(bEqu);
  129. p.Controls.Add(bClr);
  130. }
  131.  
  132. private void btn_clk(object obj, EventArgs ea)
  133. {
  134. if (blnClear)
  135. txtCalc.Text = "";
  136.  
  137. Button b3 = (Button)obj;
  138.  
  139. txtCalc.Text += b3.Text;
  140.  
  141. if (txtCalc.Text == ".")
  142. txtCalc.Text = "0.";
  143. dblSec = Convert.ToDouble(txtCalc.Text);
  144.  
  145. blnClear = false;
  146. }
  147.  
  148. private static void Main()
  149. {
  150. Application.Run(new win());
  151. }
  152.  
  153. private void btn_Oper(object obj, EventArgs ea)
  154. {
  155. Button tmp = (Button)obj;
  156. strOper = tmp.Text;
  157. if (blnFrstOpen)
  158. dblAcc = dblSec;
  159. else
  160. calc();
  161.  
  162. blnFrstOpen = false;
  163. blnClear = true;
  164. }
  165.  
  166. private void btn_clr(object obj, EventArgs ea)
  167. {
  168. clear();
  169. }
  170.  
  171. private void btn_equ(object obj, EventArgs ea)
  172. {
  173. calc();
  174.  
  175. }
  176.  
  177. private void calc()
  178. {
  179.  
  180. switch (strOper)
  181. {
  182.  
  183. case "+":
  184. dblAcc += dblSec;
  185. break;
  186. case "-":
  187. dblAcc -= dblSec;
  188. break;
  189. case "*":
  190. dblAcc *= dblSec;
  191. break;
  192. case "/":
  193. dblAcc /= dblSec;
  194. break;
  195. }
  196.  
  197. strOper = "=";
  198. blnFrstOpen = true;
  199. txtCalc.Text = Convert.ToString(dblAcc);
  200. dblSec = dblAcc;
  201. }
  202.  
  203. private void clear()
  204. {
  205. dblAcc = 0;
  206. dblSec = 0;
  207. blnFrstOpen = true;
  208. txtCalc.Text = "";
  209. txtCalc.Focus();
  210.  
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement