C#入门之简单计算器的实现。

在用C#学习的时候,师父让我先试着写一个计算器,就像任何电脑都自带的那种,想起来是觉得小case,但是实际上操作起来还是发现了不少问题的。下面的代码就是目前实现的情况,相关变量的意义也都有说明,在次就不再赘述。目前的想法是解决剩下的几个问题,后期再考虑一下优化的问题。

祭出源码:

  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 计算器
 12 {
 13     public partial class Form1 : Form
 14     {
 15         static double a = 0;//显示器变量
 16         static double b = 0;//存储值变量
 17         static int p = 0;//判断当前为整数还是小数
 18         static int l = 1;//小数位
 19         static int y = 1;//qingkongwei
 20         static double q = 0;//小数位的单位值
 21         static double x = 0;
 22         char s=‘+‘;//运算符
 23
 24         class array_calc
 25         {
 26             public double shuru(int m, int n)
 27             {
 28                 if (y == 0)
 29                 {
 30                     a = 0;
 31                     if (n != 0)
 32                     {
 33                         a = a * 10 + m;
 34                         int count = 0;
 35                     }
 36                     else
 37                     {
 38                         q = Math.Pow(0.1, l);
 39                         a = a + (q * m);
 40                         l++;
 41                         //                    MessageBox.Show("‘q是’" + q + "");
 42                     }
 43                 }
 44                 else
 45                     if (n != 0)
 46                     {
 47                         a = a * 10 + m;
 48                         int count = 0;
 49                     }
 50                     else
 51                     {
 52                         q = Math.Pow(0.1, l);
 53                         a = a + (q * m);
 54                         l++;
 55                         //                    MessageBox.Show("‘q是’" + q + "");
 56                     };
 57                 y = 1;
 58                 return a;
 59
 60             }
 61             public char fuhao(char s)
 62             {
 63                 b = a;
 64                 a = 0;
 65                 p = 0;
 66                 l = 1;
 67  //               MessageBox.Show("‘b是’" + b + "");
 68                 return s;
 69             }
 70
 71         }
 72         public Form1()
 73         {
 74             InitializeComponent();
 75         }
 76
 77         private void button12_Click(object sender, EventArgs e)
 78         {
 79             a = 0;
 80             textBox1.Text = "0";
 81             b = 0;
 82             p = 0;
 83             l = 1;
 84
 85         }
 86
 87         private void toolTip1_Popup(object sender, PopupEventArgs e)
 88         {
 89
 90         }
 91
 92         private void numericUpDown1_ValueChanged(object sender, EventArgs e)
 93         {
 94
 95         }
 96
 97         private void textBox1_TextChanged(object sender, EventArgs e)
 98         {
 99
100
101         }
102
103         private void label1_Click(object sender, EventArgs e)
104         {
105
106         }
107
108         private void button1_Click(object sender, EventArgs e)
109         {
110             array_calc n1 = new array_calc();
111             if(p==0)
112                 textBox1.Text="" + n1.shuru(1, 1) + " ";
113             else
114                 textBox1.Text = "" + n1.shuru(1, 0) + " ";
115         }
116
117         private void button2_Click(object sender, EventArgs e)
118         {
119             array_calc n2 = new array_calc();
120             if (p == 0)
121                 textBox1.Text = "" + n2.shuru(2, 1) + " ";
122             else
123                 textBox1.Text = "" + n2.shuru(2, 0) + " ";
124         }
125
126         private void Form1_Load(object sender, EventArgs e)
127         {
128
129         }
130
131         private void button21_Click(object sender, EventArgs e)
132         {
133             array_calc jia = new array_calc();
134
135             s = jia.fuhao(‘+‘);
136             textBox2.Text = "+";
137         }
138
139         private void button17_Click(object sender, EventArgs e)
140         {
141             string result=string.Empty;
142             switch (s)
143             {
144                 case ‘+‘:
145                     a = b + a;
146                     result = "" + a.ToString() + "";
147                     s = ‘0‘;
148                     break;
149                 case ‘-‘:
150                     a = b - a;
151                     result = "" + a.ToString() + "";
152                     s = ‘0‘;
153                     break;
154                 case ‘/‘:
155                     if (a == 0)
156                         result = "除数不能为0!";
157                     else
158                         {
159                         a = b / a;
160                         result = "" + a.ToString() + "";
161                         }
162
163                     s = ‘0‘;
164                     break;
165                 case ‘*‘:
166                     a = b * a;
167                     //MessageBox.Show("‘b是’" + b + "");
168                     //MessageBox.Show("‘a是’" + a + "");
169                     result = "" + a.ToString() + "";
170
171                     s = ‘0‘;
172                     break;
173                 case ‘%‘:
174                     a = b % a;
175                     result = "" + a.ToString() + "";
176                     break;
177                 case ‘0‘:
178                     result = "" + a.ToString() + "";
179                     break;
180                 default:
181                     break;
182               }
183             this.textBox1.Text = result;
184
185             y = 0;
186             //l++;
187
188             //if (a == Math.Ceiling(a))
189             //    p = 0;
190             //else
191             //{
192             //    p = 1;
193             //    x = a;
194             //    while (x != Math.Ceiling(x))
195             //    {
196             //        x = a * 10;
197             //        l++;
198             //    }
199
200             //}
201
202
203         }
204
205         private void button13_Click(object sender, EventArgs e)
206         {
207             array_calc jan = new array_calc();
208
209             s = jan.fuhao(‘-‘);
210             textBox2.Text = "-";
211         }
212
213         private void button14_Click(object sender, EventArgs e)
214         {
215             array_calc ceg = new array_calc();
216             s = ceg.fuhao(‘*‘);
217             textBox2.Text = "*";
218         }
219
220         private void button15_Click(object sender, EventArgs e)
221         {
222             array_calc chu = new array_calc();
223             s = chu.fuhao(‘/‘);
224             textBox2.Text = "/";
225         }
226
227         private void button3_Click(object sender, EventArgs e)
228         {
229             array_calc n3 = new array_calc();
230             if (p == 0)
231                 textBox1.Text = "" + n3.shuru(3, 1) + " ";
232             else
233                 textBox1.Text = "" + n3.shuru(3, 0) + " ";
234         }
235
236         private void button4_Click(object sender, EventArgs e)
237         {
238             array_calc n4 = new array_calc();
239             if (p == 0)
240                 textBox1.Text = "" + n4.shuru(4, 1) + " ";
241             else
242                 textBox1.Text = "" + n4.shuru(4, 0) + " ";
243         }
244
245         private void button5_Click(object sender, EventArgs e)
246         {
247             array_calc n5 = new array_calc();
248             if (p == 0)
249                 textBox1.Text = "" + n5.shuru(5, 1) + " ";
250             else
251                 textBox1.Text = "" + n5.shuru(5, 0) + " ";
252         }
253
254         private void button6_Click(object sender, EventArgs e)
255         {
256             array_calc n6 = new array_calc();
257             if (p == 0)
258                 textBox1.Text = "" + n6.shuru(6, 1) + " ";
259             else
260                 textBox1.Text = "" + n6.shuru(6, 0) + " ";
261         }
262
263         private void button7_Click(object sender, EventArgs e)
264         {
265             array_calc n7 = new array_calc();
266             if (p == 0)
267                 textBox1.Text = "" + n7.shuru(7, 1) + " ";
268             else
269                 textBox1.Text = "" + n7.shuru(7, 0) + " ";
270         }
271
272         private void button8_Click(object sender, EventArgs e)
273         {
274             array_calc n8 = new array_calc();
275             if (p == 0)
276                 textBox1.Text = "" + n8.shuru(8, 1) + " ";
277             else
278                 textBox1.Text = "" + n8.shuru(8, 0) + " ";
279         }
280
281         private void button9_Click(object sender, EventArgs e)
282         {
283             array_calc n9 = new array_calc();
284             if (p == 0)
285                 textBox1.Text = "" + n9.shuru(9, 1) + " ";
286             else
287                 textBox1.Text = "" + n9.shuru(9, 0) + " ";
288         }
289
290         private void button23_Click(object sender, EventArgs e)
291         {
292             array_calc n0 = new array_calc();
293             if (p == 0)
294                 textBox1.Text = "" + n0.shuru(0, 1) + " ";
295             else
296                 textBox1.Text = "" + n0.shuru(0, 0) + " ";
297         }
298
299         private void button11_Click(object sender, EventArgs e)
300         {
301             a = 0;
302             b = 0;
303             p = 0;
304             l = 1;
305             textBox1.Text = "0";
306             textBox2.Text = " ";
307         }
308
309         private void textBox2_TextChanged(object sender, EventArgs e)
310         {
311
312         }
313
314         private void button16_Click(object sender, EventArgs e)
315         {
316             a = 0 - a;
317             textBox1.Text =""+a+"";
318         }
319
320         private void button18_Click(object sender, EventArgs e)
321         {
322             array_calc qyu = new array_calc();
323             s = qyu.fuhao(‘%‘);
324             textBox2.Text = "%";
325             y = 0;
326         }
327
328         private void button19_Click(object sender, EventArgs e)
329         {
330              a = Math.Sqrt(a);
331              textBox1.Text = "" + a + "";
332              textBox2.Text = "√";
333              y = 0;
334         }
335
336         private void button22_Click(object sender, EventArgs e)
337         {
338             if (a == 0)
339                 textBox1.Text = "分母不能为0!";
340             else
341             {
342                 a = 1 / a;
343                 textBox1.Text = "" + a + "";
344                 textBox2.Text = "1/X";
345             }
346             y = 0;
347
348         }
349
350         private void button20_Click(object sender, EventArgs e)
351         {
352             array_calc nn = new array_calc();
353             p = 1;
354 //            textBox1.Text = "" + nn.shuru(1, 0) + " ";
355         }
356     }
357 }

接下来想要解决的问题是:

符号重新输入的问题:例如:1*+1=
C键、栅格键的问题

感觉自己的代码并没有怎么用到类和方法,似乎还是函数的思维。这个思想观点的转变比代码本身还要重要。

时间: 2024-10-29 19:08:02

C#入门之简单计算器的实现。的相关文章

堆栈入门-简单计算器模板-中缀转后缀

题目链接 后缀表达式又称逆波兰表示法,不含括号,运算符放在两个参与运算的语法成分的后面. 后缀表达式运算求值 自左向右顺序扫描后缀表达式.最后栈中的数字就是答案. (1)如果是数字,则压入栈中. (2)如果是运算符,就从栈中弹出两个数字进行运算,将运算结果压入栈中. 中缀表达式转后缀表达式 从左向右扫描中缀表达式. (1)当输入为数字时,直接输出到后续表达式序列中. (2)当遇到开括号时,将其入栈. (3)当遇到闭括号时,先判断栈是否为空,若为空,则表示括号不匹配,报告异常并退出.若非空,则将栈

j2ee-JSP之简单计算器

来源韩顺平.j2ee视频实战教程jsp第1讲(下集) -------------------------------------------------------------------------------------------------------- 简单计算器,可以控制输入的数(仅第一个数)不能为空且不能为字符串 myCal.jsp代码 1 <!--这是计算器的界面 --> 2 <!-- 可以控制输入的数不能为空且不能为字符串 --> 3 <%@ page co

JAVA编写的简单计算器

package com.hellojava.practice.test; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.Panel; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; impo

HDU1237 简单计算器 【栈】+【逆波兰式】

版本:1.0 日期:2014.5.17 2014.6.1 版权:© 2014 kince 转载注明出处 在介绍SwitchButton之前,先来看一下系统Button是如何实现的.源码如下: @RemoteView public class Button extends TextView { public Button(Context context) { this(context, null); } public Button(Context context, AttributeSet att

[Java.web]简单计算器

项目的  WebRoot 目录下的 calculator.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html> <head> <title>计算结果</title> </head> <body> <jsp:us

NOIP19:简单计算器

/* 1.4编程基础之逻辑表达式与条件分支 19:简单计算器 总时间限制: 1000ms 内存限制: 65536kB 描述 一个最简单的计算器,支持+, -, *, / 四种运算.仅需考虑输入输出为整数的情况,数据和运算结果不会超过int表示的范围. 输入 输入只有一行,共有三个参数,其中第1.2个参数为整数,第3个参数为操作符(+,-,*,/). 输出 输出只有一行,一个整数,为运算结果.然而: 1. 如果出现除数为0的情况,则输出:Divided by zero! 2. 如果出现无效的操作符

hdoj 1237 简单计算器

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14512    Accepted Submission(s): 4920 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整

Shell 实现简单计算器功能

Shell 实现简单计算器功能,脚本如下: [[email protected] scripts]# cat jisuan.sh #!/bin/bash print_usage(){     printf $"USAGE:$0 NUM1 {+|-|*|/} NUM2\n"     exit 1 } #判断传入的参数是不是3个 if [ $# -ne 3 ]   then     print_usage fi firstnum=$1 secondnum=$3 op=$2 #对传入的参数进

Javascript 实现简单计算器实例代码

Javascript 实现简单计算器实例代码 这篇文章主要介绍了Javascript 实现简单计算器实例代码的相关资料,需要的朋友可以参考下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63