您的位置:首页 > 编程语言 > C#

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

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

祭出源码:

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键、栅格键的问题

感觉自己的代码并没有怎么用到类和方法,似乎还是函数的思维。这个思想观点的转变比代码本身还要重要。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: