编写一个计算器,练习在窗体上添加控件、调整控件的布局,设置或修改控件属性,编写事件处理程序的方法。

代码:

  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.  
  11. namespace Lab07_1
  12. {
  13. public partial class form1 : Form
  14. {
  15. double a = 0;
  16. double b = 0;
  17. bool c = false;
  18. string d;
  19. public form1()
  20. {
  21. InitializeComponent();
  22. }
  23.  
  24. private void button1_Click(object sender, EventArgs e)
  25. {
  26. if (c == true)
  27. {
  28. textBox1.Text = "";
  29. c = false;
  30. }
  31. textBox1.Text += "1";
  32. }
  33.  
  34. private void textBox1_TextChanged(object sender, EventArgs e)
  35. {
  36.  
  37. }
  38.  
  39. private void button5_Click(object sender, EventArgs e)
  40. {
  41. if (c == true)
  42. {
  43. textBox1.Text = "";
  44. c = false;
  45. }
  46. textBox1.Text += "5";
  47.  
  48. }
  49.  
  50. private void button13_Click(object sender, EventArgs e)
  51. {
  52. if (c == true)
  53. {
  54. textBox1.Text = "";
  55. c = false;
  56. }
  57. textBox1.Text += "0";
  58. if (d == "/")
  59. {
  60. textBox1.Clear();
  61. MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK,
  62. MessageBoxIcon.Warning);
  63. }
  64.  
  65. }
  66.  
  67. private void button10_Click(object sender, EventArgs e)
  68. {
  69. c = true;
  70. b = double.Parse(textBox1.Text);
  71. d = "-";
  72.  
  73. }
  74.  
  75. private void 雨轩计算机_Load(object sender, EventArgs e)
  76. {
  77.  
  78. }
  79.  
  80. private void button2_Click(object sender, EventArgs e)
  81. {
  82. if (c == true)
  83. {
  84. textBox1.Text = "";
  85. c = false;
  86. }
  87. textBox1.Text += "2";
  88.  
  89. }
  90.  
  91. private void button7_Click(object sender, EventArgs e)
  92. {
  93. if (c == true)
  94. {
  95. textBox1.Text = "";
  96. c = false;
  97. }
  98. textBox1.Text += "7";
  99. }
  100.  
  101. private void button8_Click(object sender, EventArgs e)
  102. {
  103. if (c == true)
  104. {
  105. textBox1.Text = "";
  106. c = false;
  107. }
  108. textBox1.Text += "8";
  109.  
  110. }
  111.  
  112. private void button9_Click(object sender, EventArgs e)
  113. {
  114. if (c == true)
  115. {
  116. textBox1.Text = "";
  117. c = false;
  118. }
  119. textBox1.Text += "9";
  120. }
  121.  
  122. private void button4_Click(object sender, EventArgs e)
  123. {
  124. if (c == true)
  125. {
  126. textBox1.Text = "";
  127. c = false;
  128. }
  129. textBox1.Text += "4";
  130.  
  131. }
  132.  
  133. private void button6_Click(object sender, EventArgs e)
  134. {
  135. if (c == true)
  136. {
  137. textBox1.Text = "";
  138. c = false;
  139. }
  140. textBox1.Text += "6";
  141.  
  142. }
  143.  
  144. private void button3_Click(object sender, EventArgs e)
  145. {
  146. if (c == true)
  147. {
  148. textBox1.Text = "";
  149. c = false;
  150. }
  151. textBox1.Text += "3";
  152.  
  153. }
  154.  
  155. private void button14_Click(object sender, EventArgs e)
  156. {
  157. textBox1.Text = "";
  158. }
  159.  
  160. private void button15_Click(object sender, EventArgs e)
  161. {
  162. c = true;
  163. b = double.Parse(textBox1.Text);
  164. d = "+";
  165.  
  166. }
  167.  
  168. private void button11_Click(object sender, EventArgs e)
  169. {
  170. c = true;
  171. b = double.Parse(textBox1.Text);
  172. d = "*";
  173.  
  174. }
  175.  
  176. private void button12_Click(object sender, EventArgs e)
  177. {
  178. c = true;
  179. b = double.Parse(textBox1.Text);
  180. d = "/";
  181.  
  182. }
  183.  
  184. private void button16_Click(object sender, EventArgs e)
  185. {
  186. switch (d)
  187. {
  188. case "+": a = b + double.Parse(textBox1.Text); break;
  189. case "-": a = b - double.Parse(textBox1.Text); break;
  190. case "*": a = b * double.Parse(textBox1.Text); break;
  191. case "/": a = b / double.Parse(textBox1.Text); break;
  192. case "平方": a = b * b; break;
  193. case "开方": a =Math.Sqrt(b); break;
  194. case "log": a = Math.Log10(b); break;
  195. case "ln": a = Math.Log(b); break;
  196. }
  197. textBox1.Text = a + "";
  198. c = true;
  199.  
  200. }
  201.  
  202. private void button20_Click(object sender, EventArgs e)
  203. {
  204. if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
  205. else
  206. {
  207. c = true;
  208. b = double.Parse(textBox1.Text);
  209. d = "ln";
  210. }
  211. }
  212.  
  213. private void button17_Click(object sender, EventArgs e)
  214. {
  215. if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
  216. else
  217. {
  218. c = true;
  219. b = double.Parse(textBox1.Text);
  220. d = "平方";
  221. }
  222. }
  223.  
  224. private void button18_Click(object sender, EventArgs e)
  225. {
  226. if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
  227. else
  228. {
  229. c = true;
  230. b = double.Parse(textBox1.Text);
  231. d = "开方";
  232. }
  233. }
  234.  
  235. private void button19_Click(object sender, EventArgs e)
  236. {
  237. if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
  238. else
  239. {
  240. c = true;
  241. b = double.Parse(textBox1.Text);
  242. d = "log";
  243. }
  244. }
  245. }
  246. }

界面演示:

C#编写一个计算器的更多相关文章

  1. 用python编写一个计算器

    # 1 - 2 * ((60-30 +(-40.0/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2)))# 通过Pyt ...

  2. [javascript] 编写一个计算器,实现加减法

    1.代码 <script> function sum(){ //加法 var value1 = document.getElementById("num1").valu ...

  3. python 正则的使用 —— 编写一个简易的计算器

    在 Alex 的博客上看到的对正则这一章节作业是编写一个计算器,要求能计算出下面的算式. 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 + ...

  4. 编写一个通用的Makefile文件

    1.1在这之前,我们需要了解程序的编译过程 a.预处理:检查语法错误,展开宏,包含头文件等 b.编译:*.c-->*.S c.汇编:*.S-->*.o d.链接:.o +库文件=*.exe ...

  5. CSharpGL(34)以从零编写一个KleinBottle渲染器为例学习如何使用CSharpGL

    CSharpGL(34)以从零编写一个KleinBottle渲染器为例学习如何使用CSharpGL +BIT祝威+悄悄在此留下版了个权的信息说: 开始 本文用step by step的方式,讲述如何使 ...

  6. .NET Core RC2发布在即,我们试着用记事本编写一个ASP.NET Core RC2 MVC程序

    在.NET Core 1.0.0 RC2即将正式发布之际,我也应应景,针对RC2 Preview版本编写一个史上最简单的MVC应用.由于VS 2015目前尚不支持,VS Code的智能感知尚欠火候,所 ...

  7. 网络爬虫:使用Scrapy框架编写一个抓取书籍信息的爬虫服务

      上周学习了BeautifulSoup的基础知识并用它完成了一个网络爬虫( 使用Beautiful Soup编写一个爬虫 系列随笔汇总 ), BeautifulSoup是一个非常流行的Python网 ...

  8. 作业二:个人编程项目——编写一个能自动生成小学四则运算题目的程序

    1. 编写一个能自动生成小学四则运算题目的程序.(10分)   基本要求: 除了整数以外,还能支持真分数的四则运算. 对实现的功能进行描述,并且对实现结果要求截图.   本题发一篇随笔,内容包括: 题 ...

  9. 用Java语言编写一个简易画板

    讲了三篇概博客的概念,今天,我们来一点实际的东西.我们来探讨一下如何用Java语言,编写一块简易的画图板. 一.需求分析 无论我们使用什么语言,去编写一个什么样的项目,我们的第一步,总是去分析这个项目 ...

随机推荐

  1. java中如何将嵌套循环性能提高500倍

    java中如何将嵌套循环性能提高500倍 转载请注明出处https://www.cnblogs.com/funnyzpc/p/15975882.html 前面 似乎上一次更新在遥远的九月份,按照既定的 ...

  2. linux作业--第四周

    1.自建yum仓库,分别为网络源和本地源 所有Yum仓库的配置文件均需以 .repo 结尾并存放在/etc/yum.repos.d/目录中的 [base] : yum仓库唯一标识符,避免与其它仓库冲突 ...

  3. Actor model 的理解与 protoactor-go 的分析

    Overview Definition From wikipedia The actor model in computer science is a mathematical model of co ...

  4. LGP4463题解

    这玩意儿怎么看上去就很经典啊( 哦互不相同啊,那没事了( 考虑一个 \(\rm DP\).由于限制了互不相同,那么我们考虑从值域开始想. 设 \(dp_{n,k}\) 为在 \([1,n]\) 中选了 ...

  5. CTS,CLS,CLR

    1)CTS通用类型系统(Common Type System) CTS不但实现了COM的变量兼容类型,而且还定义了通过用户自定义类型的方式来进行类型扩展.任何以.NET平台作为目标的语言必须建立它的数 ...

  6. Hive框架

    1.什么是Hive? Hive 是基于Hadoop之上的数据仓库.2.什么是数据仓库? 数据仓库其实就是一个数据库 数据仓库是一个面向主题的,集成的,不可更新的,随时间不变化的数据集合,它用于支持 企 ...

  7. Play商店显示需要进行身份认证。您需要登录自己的Google帐户

    前段时间把一加6系统从H2OS换到OxygenOS,Play商店死活不能登录,网络配置等问题已经排除,重装Google全家桶也没有解决问题,最后找到原因. 解决办法:在应用列表中找到Google Pl ...

  8. kubernetes关于证书配置得问题总结

    总结证书配置 1.证书首先分为两种配置方式, 1) 一种是在集群中配置 2) 一种是在上游负载均衡中配置. 1)https证书在集群中配置,并域名直接解析到集群的ingress-nginx-contr ...

  9. C语言知识点的实例

    编译环境:Visual Studio 2012 编程语言:C @ 目录 1.memcpy与'/0' 2.volatile的使用 3.数字转字符 4.memcpy len 与指针加减 len 的区别 5 ...

  10. 端口转发工具--lcx

    简介 lcx是一款强大的内网端口转发工具,用于将内网主机开放的内部端口映射到外网主机(有公网IP)任意端口.它是一款命令行工具,当然也可以在有权限的webshell下执行,正因如此lcx常被认为是一款 ...