C#入门之简单计算器的实现。
2015-12-27 20:51
495 查看
在用C#学习的时候,师父让我先试着写一个计算器,就像任何电脑都自带的那种,想起来是觉得小case,但是实际上操作起来还是发现了不少问题的。下面的代码就是目前实现的情况,相关变量的意义也都有说明,在次就不再赘述。目前的想法是解决剩下的几个问题,后期再考虑一下优化的问题。
祭出源码:
View Code
接下来想要解决的问题是:
符号重新输入的问题:例如:1*+1=
C键、栅格键的问题
感觉自己的代码并没有怎么用到类和方法,似乎还是函数的思维。这个思想观点的转变比代码本身还要重要。
祭出源码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 计算器 { public partial class Form1 : Form { static double a = 0;//显示器变量 static double b = 0;//存储值变量 static int p = 0;//判断当前为整数还是小数 static int l = 1;//小数位 static int y = 1;//qingkongwei static double q = 0;//小数位的单位值 static double x = 0; char s='+';//运算符 class array_calc { public double shuru(int m, int n) { if (y == 0) { a = 0; if (n != 0) { a = a * 10 + m; int count = 0; } else { q = Math.Pow(0.1, l); a = a + (q * m); l++; // MessageBox.Show("‘q是’" + q + ""); } } else if (n != 0) { a = a * 10 + m; int count = 0; } else { q = Math.Pow(0.1, l); a = a + (q * m); l++; // MessageBox.Show("‘q是’" + q + ""); }; y = 1; return a; } public char fuhao(char s) { b = a; a = 0; p = 0; l = 1; // MessageBox.Show("‘b是’" + b + ""); return s; } } public Form1() { InitializeComponent(); } private void button12_Click(object sender, EventArgs e) { a = 0; textBox1.Text = "0"; b = 0; p = 0; l = 1; } private void toolTip1_Popup(object sender, PopupEventArgs e) { } private void numericUpDown1_ValueChanged(object sender, EventArgs e) { } private void textBox1_TextChanged(object sender, EventArgs e) { } private void label1_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { array_calc n1 = new array_calc(); if(p==0) textBox1.Text="" + n1.shuru(1, 1) + " "; else textBox1.Text = "" + n1.shuru(1, 0) + " "; } private void button2_Click(object sender, EventArgs e) { array_calc n2 = new array_calc(); if (p == 0) textBox1.Text = "" + n2.shuru(2, 1) + " "; else textBox1.Text = "" + n2.shuru(2, 0) + " "; } private void Form1_Load(object sender, EventArgs e) { } private void button21_Click(object sender, EventArgs e) { array_calc jia = new array_calc(); s = jia.fuhao('+'); textBox2.Text = "+"; } private void button17_Click(object sender, EventArgs e) { string result=string.Empty; switch (s) { case '+': a = b + a; result = "" + a.ToString() + ""; s = '0'; break; case '-': a = b - a; result = "" + a.ToString() + ""; s = '0'; break; case '/': if (a == 0) result = "除数不能为0!"; else { a = b / a; result = "" + a.ToString() + ""; } s = '0'; break; case '*': a = b * a; //MessageBox.Show("‘b是’" + b + ""); //MessageBox.Show("‘a是’" + a + ""); result = "" + a.ToString() + ""; s = '0'; break; case '%': a = b % a; result = "" + a.ToString() + ""; break; case '0': result = "" + a.ToString() + ""; break; default: break; } this.textBox1.Text = result; y = 0; //l++; //if (a == Math.Ceiling(a)) // p = 0; //else //{ // p = 1; // x = a; // while (x != Math.Ceiling(x)) // { // x = a * 10; // l++; // } //} } private void button13_Click(object sender, EventArgs e) { array_calc jan = new array_calc(); s = jan.fuhao('-'); textBox2.Text = "-"; } private void button14_Click(object sender, EventArgs e) { array_calc ceg = new array_calc(); s = ceg.fuhao('*'); textBox2.Text = "*"; } private void button15_Click(object sender, EventArgs e) { array_calc chu = new array_calc(); s = chu.fuhao('/'); textBox2.Text = "/"; } private void button3_Click(object sender, EventArgs e) { array_calc n3 = new array_calc(); if (p == 0) textBox1.Text = "" + n3.shuru(3, 1) + " "; else textBox1.Text = "" + n3.shuru(3, 0) + " "; } private void button4_Click(object sender, EventArgs e) { array_calc n4 = new array_calc(); if (p == 0) textBox1.Text = "" + n4.shuru(4, 1) + " "; else textBox1.Text = "" + n4.shuru(4, 0) + " "; } private void button5_Click(object sender, EventArgs e) { array_calc n5 = new array_calc(); if (p == 0) textBox1.Text = "" + n5.shuru(5, 1) + " "; else textBox1.Text = "" + n5.shuru(5, 0) + " "; } private void button6_Click(object sender, EventArgs e) { array_calc n6 = new array_calc(); if (p == 0) textBox1.Text = "" + n6.shuru(6, 1) + " "; else textBox1.Text = "" + n6.shuru(6, 0) + " "; } private void button7_Click(object sender, EventArgs e) { array_calc n7 = new array_calc(); if (p == 0) textBox1.Text = "" + n7.shuru(7, 1) + " "; else textBox1.Text = "" + n7.shuru(7, 0) + " "; } private void button8_Click(object sender, EventArgs e) { array_calc n8 = new array_calc(); if (p == 0) textBox1.Text = "" + n8.shuru(8, 1) + " "; else textBox1.Text = "" + n8.shuru(8, 0) + " "; } private void button9_Click(object sender, EventArgs e) { array_calc n9 = new array_calc(); if (p == 0) textBox1.Text = "" + n9.shuru(9, 1) + " "; else textBox1.Text = "" + n9.shuru(9, 0) + " "; } private void button23_Click(object sender, EventArgs e) { array_calc n0 = new array_calc(); if (p == 0) textBox1.Text = "" + n0.shuru(0, 1) + " "; else textBox1.Text = "" + n0.shuru(0, 0) + " "; } private void button11_Click(object sender, EventArgs e) { a = 0; b = 0; p = 0; l = 1; textBox1.Text = "0"; textBox2.Text = " "; } private void textBox2_TextChanged(object sender, EventArgs e) { } private void button16_Click(object sender, EventArgs e) { a = 0 - a; textBox1.Text =""+a+""; } private void button18_Click(object sender, EventArgs e) { array_calc qyu = new array_calc(); s = qyu.fuhao('%'); textBox2.Text = "%"; y = 0; } private void button19_Click(object sender, EventArgs e) { a = Math.Sqrt(a); textBox1.Text = "" + a + ""; textBox2.Text = "√"; y = 0; } private void button22_Click(object sender, EventArgs e) { if (a == 0) textBox1.Text = "分母不能为0!"; else { a = 1 / a; textBox1.Text = "" + a + ""; textBox2.Text = "1/X"; } y = 0; } private void button20_Click(object sender, EventArgs e) { array_calc nn = new array_calc(); p = 1; // textBox1.Text = "" + nn.shuru(1, 0) + " "; } } }
View Code
接下来想要解决的问题是:
符号重新输入的问题:例如:1*+1=
C键、栅格键的问题
感觉自己的代码并没有怎么用到类和方法,似乎还是函数的思维。这个思想观点的转变比代码本身还要重要。
相关文章推荐
- 《c#之全局观》
- C# 字段(Field)、属性(Property)、特性(Attribute)
- C#中字符串的处理
- C#委托
- C#多态
- C# webBrowser控件使用心得
- C#WebBrowser控件使用教程与技巧收集--苏飞收集
- C#视频总结
- C#-字符数组(笔记)
- 【C#学习】——接口例子
- C#面向对象设计模式纵横谈——6.Prototype 原型模式(创建型模式)
- C#学习笔记007-字符串练习
- C#中Console.WriteLine()的用法
- C#-循环滚动字幕,timer,从左至右,从右至左,暂停---ShinePans
- 深入理解c#中的const 和readonly的区别滴呀;
- CSharpGL(8)使用3D纹理渲染体数据 (Volume Rendering) 初探
- 使用C#实现读取系统配置文件的代码实例讲解
- c# 类函数static是什么意思?
- C#实现MD5加密
- C#随机验证码