using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using calc;
namespace calc
{
//1+2+3-5+235-6-
public partial class Form1 : Form
{
bool zeroCheck = true; //用与检查字符0的使用
bool pointCheck = false; //用于检查 小数点的使用
string temp = ""; //零时用来交换数据的变量
bool operCheck=true; //检查运算符的使用情况
bool btdengClick = false; //检查等号的使用情况
isnumber a = new isnumber();//初始化类isnum的实例;
string str1;
string str2;
double fstnum;
double secdnum;
double resnum=double.NaN;
public int i; //数组的控制变量
public int j;
string[] svalu=new string[99]; //保存用户输入的数值
string[] soper=new string[99]; //保存用户输入的字符串
public void init() //初始化程序
{
btdengClick = false;
operCheck = false;
pointCheck = false;
i = 0;
fstnum = new double();
secdnum =new double();
resnum = double.NaN;
str1 = null;
str2 = null;
Array.Clear(svalu, 0, svalu.Length);
Array.Clear(soper, 0, svalu.Length);
}
public void clearNumber()//清空所有文本
{
print2.Text = "";
btdengClick = false;
pointCheck = false;
i = 0;
fstnum = new double();
secdnum =new double();
resnum = double.NaN;
str1 = "";
str2 = "";
Array.Clear(svalu, 0, svalu.Length);
Array.Clear(soper, 0, svalu.Length);
}
public void showNum(string num) //显示数字的方法
{
if (soper[1] != null && str2 == resnum.ToString()) //如果文本框中为上次运算的结果,则再次点击按钮,清空print2的值
{
print2.Text = "";
str2 = "";
}
if (btdengClick)
{
clearNumber();
}
print2.Text = Convert.ToString(Convert.ToDouble(print2.Text + num)); //字符转换为数字→在转换为字符( 去除0)
operCheck = false;
btdengClick = false;
zeroCheck = false;
}
public void saveOper(string operName)
{
if (operCheck)
{
i = (i == 0) ? 0 : i - 1;
}
for (i=i; i< soper.Length; i++) //把操作符按顺序写在soper上
{
if (soper[0] != "")
{
soper[i] = operName;
operCheck = true;
break;
}
}
}
/// <summary>
/// 方法calculate.设置"+-*/"操作符
/// </summary>
/// <param name="calcOper"></param>
#region calculate(string calcOper)
public void calculate(string calcOper)
{
if (btdengClick) //如果用户输入了等号.在执行+,说明是在上一次的基础上连加的.
{
init(); //初始化所有参数.只保留print2中的文本 当作是用户输入.其他参数初始化
}
switch (calcOper)
{
case "-":
if (operCheck == false)
{
if (soper[0] != null) //如果有操作符,则说明是连加操作
{
saveOper("-"); //保存操作符
svalu[i] = Convert.ToString(Convert.ToDouble(print2.Text));
if (soper[2] == null) //执行连加的赋值运算.当第三个运算符为空的时候(resnum的值为第一操作数+第二操作数),
{ //当第三操作符不为空,用resnum+第三操作数.resnum=resnum+svalu
switch (soper[0])
{
case "+":
resnum = Convert.ToDouble(svalu[0]) + Convert.ToDouble(svalu[1]);
break;
case "-":
resnum = Convert.ToDouble(svalu[0]) - Convert.ToDouble(svalu[1]);
break;
case "*":
resnum = Convert.ToDouble(svalu[0]) * Convert.ToDouble(svalu[1]);
break;
case "/":
resnum = Convert.ToDouble(svalu[0]) / Convert.ToDouble(svalu[1]);
break;
}
}
else
{
switch (soper[i - 1])
{
case "+":
resnum += Convert.ToDouble(svalu[i]);
break;
case "-":
resnum -= Convert.ToDouble(svalu[i]);
break;
case "*":
resnum *= Convert.ToDouble(svalu[i]);
break;
case "/":
resnum /= Convert.ToDouble(svalu[i]);
break;
}
}
str1 = print1.Text + svalu[i]; //~~~~~~~~~~~~~~~
print1.Text = print1.Text + svalu[i] + soper[i];
print2.Text = resnum.ToString();
str2 = resnum.ToString();
}
else
{
saveOper("-");
svalu[i] = Convert.ToString(Convert.ToDouble(print2.Text)); //print2中的值给svalu数组
str1 = print1.Text + svalu[i];
print1.Text = print1.Text + svalu[i] + soper[i];
print2.Text = "";
}
i++;
}
else
{
saveOper("-");
if (svalu[0] == null)
{
svalu[0] = "0";
print1.Text = 0 + soper[i];
}
else
{
print1.Text = str1 + soper[i];
}
i++;
}
break;
case "+":
if (operCheck == false)
{
if (soper[0] != null) //如果有操作符,则说明是连加操作
{
saveOper("+"); //保存操作符
svalu[i] = Convert.ToString(Convert.ToDouble(print2.Text));
if (soper[2] == null) //执行连加的赋值运算.当第三个运算符为空的时候(resnum的值为第一操作数+第二操作数),
{ //当第三操作符不为空,用resnum+第三操作数.resnum=resnum+svalu
switch (soper[0])
{
case "+":
resnum = Convert.ToDouble(svalu[0]) + Convert.ToDouble(svalu[1]);
break;
case "-":
resnum = Convert.ToDouble(svalu[0]) - Convert.ToDouble(svalu[1]);
break;
case "*":
resnum = Convert.ToDouble(svalu[0]) * Convert.ToDouble(svalu[1]);
break;
case "/":
resnum = Convert.ToDouble(svalu[0]) / Convert.ToDouble(svalu[1]);
break;
}
}
else
{
switch (soper[i - 1])
{
case "+":
resnum += Convert.ToDouble(svalu[i]);
break;
case "-":
resnum -= Convert.ToDouble(svalu[i]);
break;
case "*":
resnum *= Convert.ToDouble(svalu[i]);
break;
case "/":
resnum /= Convert.ToDouble(svalu[i]);
break;
}
}
str1 = print1.Text + svalu[i]; //~~~~~~~~~~~~~~~
print1.Text = print1.Text + svalu[i] + soper[i];
print2.Text = resnum.ToString();
str2 = resnum.ToString();
}
else
{
saveOper("+");
svalu[i] =Convert.ToString( Convert.ToDouble( print2.Text)); //print2中的值给svalu数组
str1 = print1.Text + svalu[i];
print1.Text = print1.Text + svalu[i] + soper[i];
print2.Text = "";
}
i++;
}
else
{
saveOper("+");
if (svalu[0] == null)//判断用户是否直接按 +-*/而不输入数字
{
svalu[0] = "0";
print1.Text = 0 + soper[i];
}
else
{
print1.Text = str1 + soper[i];
}
i++;
}
break;
case "*":
if (operCheck == false)
{
if (soper[0] != null) //如果有操作符,则说明是连加操作
{
saveOper("*"); //保存操作符
svalu[i] = Convert.ToString(Convert.ToDouble(print2.Text));
if (soper[2] == null) //执行连加的赋值运算.当第三个运算符为空的时候(resnum的值为第一操作数+第二操作数),
{ //当第三操作符不为空,用resnum+第三操作数.resnum=resnum+svalu
switch (soper[0])
{
case "+":
resnum = Convert.ToDouble(svalu[0]) + Convert.ToDouble(svalu[1]);
break;
case "-":
resnum = Convert.ToDouble(svalu[0]) - Convert.ToDouble(svalu[1]);
break;
case "*":
resnum = Convert.ToDouble(svalu[0]) * Convert.ToDouble(svalu[1]);
break;
case "/":
resnum = Convert.ToDouble(svalu[0]) / Convert.ToDouble(svalu[1]);
break;
}
}
else
{
switch (soper[i - 1])
{
case "+":
resnum += Convert.ToDouble(svalu[i]);
break;
case "-":
resnum -= Convert.ToDouble(svalu[i]);
break;
case "*":
resnum *= Convert.ToDouble(svalu[i]);
break;
case "/":
resnum /= Convert.ToDouble(svalu[i]);
break;
}
}
str1 = print1.Text + svalu[i]; //~~~~~~~~~~~~~~~
print1.Text = print1.Text + svalu[i] + soper[i];
print2.Text = resnum.ToString();
str2 = resnum.ToString();
}
else
{
saveOper("*");
svalu[i] = Convert.ToString(Convert.ToDouble(print2.Text)); //print2中的值给svalu数组
str1 = print1.Text + svalu[i];
print1.Text = print1.Text + svalu[i] + soper[i];
print2.Text = "";
}
i++;
}
else
{
saveOper("*");
if (svalu[0] == null)//判断用户是否直接按 +-*/而不输入数字
{
svalu[0] = "0";
print1.Text = 0 + soper[i];
}
else
{
print1.Text = str1 + soper[i];
}
i++;
}
break;
case "/":
if (operCheck == false)
{
if (soper[0] != null) //如果有操作符,则说明是连加操作
{
saveOper("/"); //保存操作符
svalu[i] = Convert.ToString(Convert.ToDouble(print2.Text));
if (soper[2] == null) //执行连加的赋值运算.当第三个运算符为空的时候(resnum的值为第一操作数+第二操作数),
{ //当第三操作符不为空,用resnum+第三操作数.resnum=resnum+svalu
switch (soper[0])
{
case "+":
resnum = Convert.ToDouble(svalu[0]) + Convert.ToDouble(svalu[1]);
break;
case "-":
resnum = Convert.ToDouble(svalu[0]) - Convert.ToDouble(svalu[1]);
break;
case "*":
resnum = Convert.ToDouble(svalu[0]) * Convert.ToDouble(svalu[1]);
break;
case "/":
resnum = Convert.ToDouble(svalu[0]) / Convert.ToDouble(svalu[1]);
break;
}
}
else
{
switch (soper[i - 1])
{
case "+":
resnum += Convert.ToDouble(svalu[i]);
break;
case "-":
resnum -= Convert.ToDouble(svalu[i]);
break;
case "*":
resnum *= Convert.ToDouble(svalu[i]);
break;
case "/":
resnum /= Convert.ToDouble(svalu[i]);
break;
}
}
str1 = print1.Text + svalu[i]; //~~~~~~~~~~~~~~~
print1.Text = print1.Text + svalu[i] + soper[i];
print2.Text = resnum.ToString();
str2 = resnum.ToString();
}
else
{
saveOper("/");
svalu[i] = Convert.ToString(Convert.ToDouble(print2.Text)); //print2中的值给svalu数组
str1 = print1.Text + svalu[i];
print1.Text = print1.Text + svalu[i] + soper[i];
print2.Text = "";
}
i++;
}
else
{
saveOper("/");
if (svalu[0] == null)//判断用户是否直接按 +-*/而不输入数字
{
svalu[0] = "0";
print1.Text = 0 + soper[i];
}
else
{
print1.Text = str1 + soper[i];
}
i++;
}
break;
}
pointCheck = false;
zeroCheck = true;
}
#endregion
public Form1()
{
InitializeComponent();
}
private void bt2_Click(object sender, EventArgs e)
{
showNum("2");
}
private void bt3_Click(object sender, EventArgs e)
{
showNum("3");
}
private void bt4_Click(object sender, EventArgs e)
{
showNum("4");
}
private void bt5_Click(object sender, EventArgs e)
{
showNum("5");
}
private void bt6_Click(object sender, EventArgs e)
{
showNum("6");
}
private void bt7_Click(object sender, EventArgs e)
{
showNum("7");
}
private void bt8_Click(object sender, EventArgs e)
{
showNum("8");
}
private void bt9_Click(object sender, EventArgs e)
{
showNum("9");
}
private void bt0_Click(object sender, EventArgs e)
{
if (soper[1] != null && str2 == resnum.ToString()) //如果文本框中为上次运算的结果,则再次点击按钮,清空print2的值
{
print2.Text = "";
str2 = "";
}
if (btdengClick)
{
clearNumber();
}
if (zeroCheck == false)
{
print2.Text = print2.Text + 0;
}
else
{
print2.Text = "0";
}
operCheck = false;
btdengClick = false;
}
private void btpoint_Click(object sender, EventArgs e)
{
if (pointCheck == false)
{
if (soper[1] != null && str2 == resnum.ToString()) //如果文本框中为上次运算的结果,则再次点击按钮,清空print2的值
{
print2.Text = "";
str2 = "";
}
if (btdengClick)
{
clearNumber();
print2.Text = "0";
}
operCheck = false;
btdengClick = false;
pointCheck = true;
zeroCheck = false;
print2.Text += ".";
}
}
private void btjian_Click(object sender, EventArgs e)
{
calculate("-");
}
private void btchen_Click(object sender, EventArgs e)
{
calculate("*");
}
private void btchu_Click(object sender, EventArgs e)
{
calculate("/");
}
private void btjia_Click(object sender, EventArgs e)
{
calculate("+");
}
private void bt1_Click(object sender, EventArgs e)
{
showNum("1");
}
private void btdeng_Click(object sender, EventArgs e)
{
if (svalu[0] != null)
{
if (!btdengClick) //如果btdengClick=false 则说明是不是连等
{
svalu[i] = print2.Text;
if (svalu[i] == "") //如果第二操作数为空,则执行1+=运算 实现每次+1.
{
fstnum = Convert.ToDouble(svalu[i - 1]);
switch (soper[i - 1])
{
case "+":
if (double.IsNaN(resnum))
{
resnum = fstnum;
}
svalu[i] = fstnum.ToString();
resnum += fstnum;
break;
case "-":
if (double.IsNaN(resnum))
{
resnum = 0;
svalu[i] = fstnum.ToString();
break;
}
svalu[i] = fstnum.ToString();
resnum -= fstnum;
break;
case "/":
if (fstnum == 0) //如果第一操作数为0;值为0. 0/任何数都是0
{
resnum = 0;
}
if (double.IsNaN(resnum)) //第一次运算结果为1
{
resnum = 1;
}
else
{
resnum /= fstnum; //resnum连除
}
svalu[i] = fstnum.ToString();
break;
case "*":
if (double.IsNaN(resnum))
{
resnum = fstnum;
}
svalu[i] = fstnum.ToString();
resnum *= fstnum;
break;
}
print2.Text = resnum.ToString();
btdengClick = true;
}
else if (soper[1] == null) //此处当第二个运算符为空,说明是普通2元+-*/.
{ //用第一个数+第二个数 赋值给resnum
switch (soper[i - 1])
{
case "+":
resnum = Convert.ToDouble(svalu[0]) + Convert.ToDouble(svalu[1]);//修改为switch case
break;
case "-":
resnum = Convert.ToDouble(svalu[0]) - Convert.ToDouble(svalu[1]);//修改为switch case
break;
case "*":
resnum = Convert.ToDouble(svalu[0]) * Convert.ToDouble(svalu[1]);//修改为switch case
break;
case "/":
resnum = Convert.ToDouble(svalu[0]) / Convert.ToDouble(svalu[1]);//修改为switch case
break;
}
print2.Text = resnum.ToString();
btdengClick = true;
}
else //连加中的等于运算 如 1+2=3=5=7
{
// svalu[i] = print2.Text;
switch (soper[i - 1])
{
case "+":
resnum = resnum + Convert.ToDouble(svalu[i]);
break;
case "-":
resnum = resnum - Convert.ToDouble(svalu[i]);
break;
case "*":
resnum = resnum * Convert.ToDouble(svalu[i]);
break;
case "/":
resnum = resnum / Convert.ToDouble(svalu[i]);
break;
}
print2.Text = resnum.ToString();
btdengClick = true;
}
print1.Text = "";
}
else //当btdengclick=true时执行这段.说明用户执行了2次=,执行连等功能
{
switch (soper[i - 1])
{
case "+":
resnum = resnum + Convert.ToDouble(svalu[i]);
break;
case "-":
resnum = resnum - Convert.ToDouble(svalu[i]);
break;
case "*":
resnum = resnum * Convert.ToDouble(svalu[i]);
break;
case "/":
resnum = resnum / Convert.ToDouble(svalu[i]);
break;
}
print2.Text = resnum.ToString();
}
pointCheck = false;
zeroCheck = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
print2.Text = "0";
}
private void button4_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
clearNumber();
print2.Text = "0";
print1.Text = "";
}
private void button15_Click(object sender, EventArgs e)
{
if ((print2.Text != "") && (print2.Text != "0"))
{
print2.Text = Convert.ToString( 0- (Convert.ToDouble(print2.Text)));
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
print2.Copy();
}
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
print2.Paste();
}
private void 版本信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 newform = new Form2();
newform.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)//退格键
{
{
if (1 == print2.Text.Length)
{
print2.Text = "0";
}
else
{
print2.Text = print2.Text.Substring(0,print2.Text.Length - 1);
}
}
}
private void print2_TextChanged(object sender, EventArgs e)
{
}
}
}