C#实现简易版的计算器数

痛定思痛。 2022-01-23 13:07 353阅读 0赞

界面设计:textbook 和18个button按钮
在这里插入图片描述代码部分:

  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. //计算计算器窗口居中
  4. int left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
  5. int top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
  6. this.Location = new Point(left, top);
  7. }
  8. double num1 = 0;
  9. double num2 = 0;
  10. bool iskey = false;
  11. //1
  12. private void Button7_Click(object sender, EventArgs e)
  13. {
  14. if (iskey)
  15. {
  16. textBox1.Text = "";
  17. iskey = false;
  18. }
  19. textBox1.Text += "1";
  20. }
  21. //2
  22. private void Button6_Click(object sender, EventArgs e)
  23. {
  24. if (iskey)
  25. {
  26. textBox1.Text = "";
  27. iskey = false;
  28. }
  29. textBox1.Text += "2";
  30. }
  31. //3
  32. private void Button5_Click(object sender, EventArgs e)
  33. {
  34. if (iskey)
  35. {
  36. textBox1.Text = "";
  37. iskey = false;
  38. }
  39. textBox1.Text += "3";
  40. }
  41. //4
  42. private void Button11_Click(object sender, EventArgs e)
  43. {
  44. if (iskey)
  45. {
  46. textBox1.Text = "";
  47. iskey = false;
  48. }
  49. textBox1.Text += "4";
  50. }
  51. //5
  52. private void Button10_Click(object sender, EventArgs e)
  53. {
  54. if (iskey)
  55. {
  56. textBox1.Text = "";
  57. iskey = false;
  58. }
  59. textBox1.Text += "5";
  60. }
  61. //6
  62. private void Button9_Click(object sender, EventArgs e)
  63. {
  64. if (iskey)
  65. {
  66. textBox1.Text = "";
  67. iskey = false;
  68. }
  69. textBox1.Text += "6";
  70. }
  71. //7
  72. private void Button15_Click(object sender, EventArgs e)
  73. {
  74. if (iskey)
  75. {
  76. textBox1.Text = "";
  77. iskey = false;
  78. }
  79. textBox1.Text += "7";
  80. }
  81. //8
  82. private void Button14_Click(object sender, EventArgs e)
  83. {
  84. if (iskey)
  85. {
  86. textBox1.Text = "";
  87. iskey = false;
  88. }
  89. textBox1.Text += "8";
  90. }
  91. //9
  92. private void Button12_Click(object sender, EventArgs e)
  93. {
  94. if (iskey)
  95. {
  96. textBox1.Text = "";
  97. iskey = false;
  98. }
  99. textBox1.Text += "9";
  100. }
  101. //0
  102. private void Button18_Click(object sender, EventArgs e)
  103. {
  104. if (iskey)
  105. {
  106. textBox1.Text = "";
  107. iskey = false;
  108. }
  109. textBox1.Text += "0";
  110. }
  111. //小数点.
  112. private void Button17_Click(object sender, EventArgs e)
  113. {
  114. if (iskey)
  115. {
  116. textBox1.Text = "";
  117. iskey = false;
  118. }
  119. textBox1.Text += ".";
  120. }
  121. //CE
  122. private void Button1_Click(object sender, EventArgs e)
  123. {
  124. textBox1.Text = "";
  125. }
  126. string type = "";
  127. //=
  128. private void Button2_Click(object sender, EventArgs e)
  129. {
  130. if (iskey)
  131. {
  132. return;
  133. }
  134. iskey = true;
  135. if (textBox1.Text != "")
  136. {
  137. num2 = Convert.ToDouble(textBox1.Text);
  138. }
  139. switch (type)
  140. {
  141. case "+":
  142. textBox1.Text = (num1 + num2).ToString();
  143. break;
  144. case "-":
  145. textBox1.Text = (num1 - num2).ToString();
  146. break;
  147. case "×":
  148. textBox1.Text = (num1 * num2).ToString();
  149. break;
  150. case "/":
  151. textBox1.Text = (num1 / num2).ToString();
  152. break;
  153. case "%":
  154. textBox1.Text = (num1 % num2).ToString();
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. //-
  161. private void Button3_Click(object sender, EventArgs e)
  162. {
  163. if (textBox1.Text!="")
  164. {
  165. num1 = Convert.ToDouble(textBox1.Text);
  166. }
  167. type = "-";
  168. iskey = true;
  169. }
  170. //×
  171. private void Button4_Click(object sender, EventArgs e)
  172. {
  173. type = "×";
  174. if (textBox1.Text!="")
  175. {
  176. num1 = Convert.ToDouble(textBox1.Text);
  177. }
  178. iskey = true;
  179. }
  180. //除
  181. private void Button8_Click(object sender, EventArgs e)
  182. {
  183. type = "/";
  184. if (textBox1.Text!="")
  185. {
  186. num1 = Convert.ToDouble(textBox1.Text);
  187. }
  188. iskey = true;
  189. }
  190. //+
  191. private void Button13_Click(object sender, EventArgs e)
  192. {
  193. type = "+";
  194. if (textBox1.Text!="")
  195. {
  196. num1 = Convert.ToDouble(textBox1.Text);
  197. }
  198. iskey = true;
  199. }
  200. //取余%
  201. private void Button16_Click(object sender, EventArgs e)
  202. {
  203. type = "%";
  204. if (textBox1.Text!="")
  205. {
  206. num1 = Convert.ToDouble(textBox1.Text);
  207. }
  208. iskey = true;
  209. }
  210. }
  211. }

发表评论

表情:
评论列表 (有 0 条评论,353人围观)

还没有评论,来说两句吧...

相关阅读

    相关 c语言 简易计算器

    应度友邀请,用c写了个简易的命令行计算器,其功能大致如下: 设计简单的计算器,允许用户输入简单表达式(一元运算和二元运算表达式,以=结尾),输出计算结果,并等待用户继