c#窗体简易计算器(初级版)

迈不过友情╰ 2021-09-22 12:40 536阅读 0赞

201806101638085

我是一个初学者,从这些简单的开始做起。

把简单的练熟,便是成功了一小步。

下面是代码完整版,可以实现简单的运算,可以实现连续的运算。但是有些细节没考虑。

希望大神们帮我指正。

  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.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace 简易计算器
  11. {
  12. public partial class Frm_Main : Form
  13. {
  14. int a = 0, b = 0; //储存输入数字的整型
  15. int total=0; //用于储存结果
  16. int m = 1; //用于乘法第一项乘以1
  17. string s; //用来储存输入的每个数
  18. string d; //用来辨别进行何种运算
  19. bool c=false;
  20. public Frm_Main()
  21. {
  22. InitializeComponent();
  23. }
  24. private void button1_Click(object sender, EventArgs e)
  25. {
  26. if (c == true)
  27. {
  28. textBox1.Text = "";
  29. c = false;
  30. }
  31. s += "1";
  32. textBox1.Text += "1";
  33. }
  34. private void button2_Click(object sender, EventArgs e)
  35. {
  36. if (c == true)
  37. {
  38. textBox1.Text = "";
  39. c = false;
  40. }
  41. s += "2";
  42. textBox1.Text += "2";
  43. }
  44. private void button3_Click(object sender, EventArgs e)
  45. {
  46. if (c == true)
  47. {
  48. textBox1.Text = "";
  49. c = false;
  50. }
  51. s += "3";
  52. textBox1.Text += "3";
  53. }
  54. private void button4_Click(object sender, EventArgs e)
  55. {
  56. if (c == true)
  57. {
  58. textBox1.Text = "";
  59. c = false;
  60. }
  61. s += "4";
  62. textBox1.Text += "4";
  63. }
  64. private void button5_Click(object sender, EventArgs e)
  65. {
  66. if (c == true)
  67. {
  68. textBox1.Text = "";
  69. c = false;
  70. }
  71. s += "5";
  72. textBox1.Text += "5";
  73. }
  74. private void button6_Click(object sender, EventArgs e)
  75. {
  76. if (c == true)
  77. {
  78. textBox1.Text = "";
  79. c = false;
  80. }
  81. s += "6";
  82. textBox1.Text += "6";
  83. }
  84. private void button7_Click(object sender, EventArgs e)
  85. {
  86. if (c == true)
  87. {
  88. textBox1.Text = "";
  89. c = false;
  90. }
  91. s += "7";
  92. textBox1.Text += "7";
  93. }
  94. private void button8_Click(object sender, EventArgs e)
  95. {
  96. if (c == true)
  97. {
  98. textBox1.Text = "";
  99. c = false;
  100. }
  101. s += "8";
  102. textBox1.Text += "8";
  103. }
  104. private void button9_Click(object sender, EventArgs e)
  105. {
  106. if (c == true)
  107. {
  108. textBox1.Text = "";
  109. c = false;
  110. }
  111. s += "9";
  112. textBox1.Text += "9";
  113. }
  114. private void button12_Click(object sender, EventArgs e) //实现加法
  115. {
  116. if (c == true)
  117. {
  118. textBox1.Text = "";
  119. c = false;
  120. }
  121. d = "+";
  122. a = int.Parse(s);
  123. total += a;
  124. textBox1.Text += "+";
  125. s = "";
  126. }
  127. private void button11_Click(object sender, EventArgs e) //清空
  128. {
  129. textBox1.Text = "";
  130. s = "";
  131. total = 0;
  132. }
  133. private void button14_Click(object sender, EventArgs e) // 实现减法
  134. {
  135. if (c == true)
  136. {
  137. textBox1.Text = "";
  138. c = false;
  139. }
  140. d = "-";
  141. a = int.Parse(s);
  142. if (textBox1.Text ==a.ToString()) //判断是不是第一个数
  143. total = a;
  144. else
  145. total -= a;
  146. textBox1.Text += "-";
  147. s = "";
  148. }
  149. private void button16_Click(object sender, EventArgs e) //实现除法
  150. {
  151. if (c == true)
  152. {
  153. textBox1.Text = "";
  154. c = false;
  155. }
  156. d = "/";
  157. a = int.Parse(s);
  158. if (textBox1.Text == a.ToString()) //判断是不是第一个数
  159. total = a;
  160. else
  161. total /= a;
  162. textBox1.Text += "/";
  163. s = "";
  164. }
  165. private void button15_Click(object sender, EventArgs e) //实现乘法
  166. {
  167. if (c == true)
  168. {
  169. textBox1.Text = "";
  170. c = false;
  171. }
  172. d = "*";
  173. a = int.Parse(s);
  174. m*= a;
  175. total+= m;
  176. textBox1.Text += "*";
  177. s = "";
  178. }
  179. private void button13_Click(object sender, EventArgs e) //输出结果
  180. {
  181. switch(d)
  182. {
  183. case "+":
  184. b = int.Parse(s);
  185. total += b;
  186. textBox1.Text += "=";
  187. textBox1.Text += total.ToString();
  188. break;
  189. case "*":
  190. b = int.Parse(s);
  191. total *= b;
  192. textBox1.Text += "=";
  193. textBox1.Text += total.ToString();
  194. m = 1;
  195. break;
  196. case "-":
  197. b = int.Parse(s);
  198. total -= b;
  199. textBox1.Text += "=";
  200. textBox1.Text += total.ToString();
  201. break;
  202. case "/":
  203. b = int.Parse(s);
  204. total /= b;
  205. textBox1.Text += "=";
  206. textBox1.Text += total.ToString();
  207. break;
  208. }
  209. }
  210. private void button10_Click(object sender, EventArgs e)
  211. {
  212. if (c == true)
  213. {
  214. textBox1.Text = "";
  215. c = false;
  216. }
  217. s += "0";
  218. textBox1.Text += "0";
  219. }
  220. }
  221. }

欢迎指正,以及提出更多建议。谢谢!

发表评论

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

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

相关阅读

    相关 c语言 简易计算器

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