using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//改动一个地方 就是输入的时候的数字的显示
namespace MyCalc
{
public partial class Form1 : Form
{
private double _num1 = 0, _num2 = 0, _result = 0;
private string _operator = "";
private int _operatorCount = 0;
private int yuan = 10; //原来默认为10进制
private bool _firstNumberFlag = false;//使得边计算便输出
public Form1() //窗口设计代码
{
InitializeComponent();
}
private void buttonNum1_Click(object sender, EventArgs e) //鼠标按钮
{
if (_firstNumberFlag == true)
{
textBoxResult.Text = "1";
_firstNumberFlag = false;
}
else
textBoxResult.Text = textBoxResult.Text + "1";
textBoxResult.Focus();//焦点落到编辑框中
textBoxResult.Select(textBoxResult.Text.Length, 0);//让光标落在编辑框的尾部
}
private void buttonNum2_Click(object sender, EventArgs e)
{
if (yuan == 8 || yuan == 10)
{
if (_firstNumberFlag == true)
{
textBoxResult.Text = "2";
_firstNumberFlag = false;
}
else
textBoxResult.Text = textBoxResult.Text + "2";
}
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void buttonNum3_Click(object sender, EventArgs e)
{
if (yuan == 8 || yuan == 10)
{
if (_firstNumberFlag == true)
{
textBoxResult.Text = "3";
_firstNumberFlag = false;
}
else
textBoxResult.Text = textBoxResult.Text + "3";
}
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void buttonNum4_Click(object sender, EventArgs e)
{
if (yuan == 8 || yuan == 10)
{
if (_firstNumberFlag == true)
{
textBoxResult.Text = "4";
_firstNumberFlag = false;
}
else
textBoxResult.Text = textBoxResult.Text + "4";
}
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void buttonNum5_Click(object sender, EventArgs e)
{
if (yuan == 8 || yuan == 10)
{
if (_firstNumberFlag == true)
{
textBoxResult.Text = "5";
_firstNumberFlag = false;
}
else textBoxResult.Text = textBoxResult.Text + "5";
} textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void buttonNum6_Click(object sender, EventArgs e)
{
if (yuan == 8 || yuan == 10)
{
if (_firstNumberFlag == true)
{
textBoxResult.Text = "6";
_firstNumberFlag = false;
}
else textBoxResult.Text = textBoxResult.Text + "6";
}
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void buttonNum7_Click(object sender, EventArgs e)
{
if (yuan == 8 || yuan == 10)
{
if (_firstNumberFlag == true)
{
textBoxResult.Text = "7";
_firstNumberFlag = false;
}
else textBoxResult.Text = textBoxResult.Text + "7";
}
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void buttonNum8_Click(object sender, EventArgs e)
{
if ( yuan == 10)
{
if (_firstNumberFlag == true)
{
textBoxResult.Text = "8";
_firstNumberFlag = false;
}
else
textBoxResult.Text = textBoxResult.Text + "8";
}
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void buttonNum9_Click(object sender, EventArgs e)
{
if (yuan == 10)
{
if (_firstNumberFlag == true)
{
textBoxResult.Text = "9";
_firstNumberFlag = false;
}
else
textBoxResult.Text = textBoxResult.Text + "9";
}
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void buttonNum0_Click(object sender, EventArgs e)
{
if (_firstNumberFlag == true)
{
textBoxResult.Text = "0";
_firstNumberFlag = false;
}
else
textBoxResult.Text = textBoxResult.Text + "0";
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void buttonDot_Click(object sender, EventArgs e)//"."的按钮
{
if (yuan == 10)
{
if (_firstNumberFlag == true)
{
textBoxResult.Text = ".";
_firstNumberFlag = false;
}
else
{
if (textBoxResult.Text.IndexOf('.') == -1) //判断点的个数
textBoxResult.Text = textBoxResult.Text + ".";
}
}
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void buttonAdd_Click(object sender, EventArgs e)//运算符按钮 加法
{
_firstNumberFlag = true;
_operatorCount = _operatorCount + 1;
if (_operatorCount == 1)
{
_num1 = Convert.ToDouble(textBoxResult.Text);
_operator = "+";
}
else
{
_num2 = Convert.ToDouble(textBoxResult.Text);
if(yuan==10)
{
if (_operator == "+")
{
_num1 = _num1 + _num2;
}
else if(_operator == "-")
{
_num1 = _num1 - _num2;
}
else if (_operator== "*")
_num1 = _num1 * _num2;
else if(_operator == "/")
_num1 = _num1 / _num2;
}
else if(yuan==2)
{
_num1 =toshijinzhi (_num1 ,yuan);
_num2 =toshijinzhi (_num2 ,yuan);
if (_operator == "+")
{
_num1 = _num1 + _num2;
_num1=toerjinzhi (_num1);
}
else if(_operator == "-")
{
_num1 = _num1 - _num2;
_num1=toerjinzhi (_num1);
}
else if (_operator =="*")
{
_num1 = _num1 * _num2;
_num1=toerjinzhi (_num1);
}
else if(_operator == "/")
{
_num1 = _num1 / _num2;
_num1=toerjinzhi (_num1);
}
}
else if(yuan==8)
{
_num1 =toshijinzhi (_num1 ,yuan);
_num2 =toshijinzhi (_num2 ,yuan);
if (_operator == "+")
{
_num1 = _num1 + _num2;
_num1=tobajinzhi (_num1);
}
else if(_operator == "-")
{
_num1 = _num1 - _num2;
_num1=tobajinzhi (_num1);
}
else if (_operator =="*")
{
_num1 = _num1 * _num2;
_num1=tobajinzhi (_num1);
}
else if(_operator == "/")
{
_num1 = _num1 / _num2;
_num1=tobajinzhi (_num1);
}
}
_operator = "+";//保存运算符号到类字段_operator中
textBoxResult.Text = _num1.ToString();
}
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void buttonSubtract_Click(object sender, EventArgs e) //运算符按钮 减法
{
_firstNumberFlag = true;
_operatorCount = _operatorCount + 1;
if (_operatorCount == 1)
{
_num1 = Convert.ToDouble(textBoxResult.Text);
_operator = "-";
}
else
{
_num2 = Convert.ToDouble(textBoxResult.Text);
if (yuan == 10)
{
if (_operator == "+")
{
_num1 = _num1 + _num2;
}
else if (_operator == "-")
{
_num1 = _num1 - _num2;
}
else if (_operator == "*")
_num1 = _num1 * _num2;
else if (_operator == "/")
_num1 = _num1 / _num2;
}
else if (yuan == 2)
{
_num1 = toshijinzhi(_num1, yuan);
_num2 = toshijinzhi(_num2, yuan);
if (_operator == "+")
{
_num1 = _num1 + _num2;
_num1 = toerjinzhi(_num1);
}
else if (_operator == "-")
{
_num1 = _num1 - _num2;
_num1 = toerjinzhi(_num1);
}
else if (_operator == "*")
{
_num1 = _num1 * _num2;
_num1 = toerjinzhi(_num1);
}
else if (_operator == "/")
{
_num1 = _num1 / _num2;
_num1 = toerjinzhi(_num1);
}
}
else if (yuan == 8)
{
_num1 = toshijinzhi(_num1, yuan);
_num2 = toshijinzhi(_num2, yuan);
if (_operator == "+")
{
_num1 = _num1 + _num2;
_num1 = tobajinzhi(_num1);
}
else if (_operator == "-")
{
_num1 = _num1 - _num2;
_num1 = tobajinzhi(_num1);
}
else if (_operator == "*")
{
_num1 = _num1 * _num2;
_num1 = tobajinzhi(_num1);
}
else if (_operator == "/")
{
_num1 = _num1 / _num2;
_num1 = tobajinzhi(_num1);
}
}
_operator = "-";//保存运算符号到类字段_operator中
textBoxResult.Text = _num1.ToString();
}
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void buttonMultiply_Click(object sender, EventArgs e) //运算符按钮 乘法
{
_firstNumberFlag = true;
_operatorCount = _operatorCount + 1;
if (_operatorCount == 1)
{
_num1 = Convert.ToDouble(textBoxResult.Text);
_operator = "*";
}
else
{
_num2 = Convert.ToDouble(textBoxResult.Text);
if (yuan == 10)
{
if (_operator == "+")
{
_num1 = _num1 + _num2;
}
else if (_operator == "-")
{
_num1 = _num1 - _num2;
}
else if (_operator == "*")
_num1 = _num1 * _num2;
else if (_operator == "/")
_num1 = _num1 / _num2;
}
else if (yuan == 2)
{
_num1 = toshijinzhi(_num1, yuan);
_num2 = toshijinzhi(_num2, yuan);
if (_operator == "+")
{
_num1 = _num1 + _num2;
_num1 = toerjinzhi(_num1);
}
else if (_operator == "-")
{
_num1 = _num1 - _num2;
_num1 = toerjinzhi(_num1);
}
else if (_operator == "*")
{
_num1 = _num1 * _num2;
_num1 = toerjinzhi(_num1);
}
else if (_operator == "/")
{
_num1 = _num1 / _num2;
_num1 = toerjinzhi(_num1);
}
}
else if (yuan == 8)
{
_num1 = toshijinzhi(_num1, yuan);
_num2 = toshijinzhi(_num2, yuan);
if (_operator == "+")
{
_num1 = _num1 + _num2;
_num1 = tobajinzhi(_num1);
}
else if (_operator == "-")
{
_num1 = _num1 - _num2;
_num1 = tobajinzhi(_num1);
}
else if (_operator == "*")
{
_num1 = _num1 * _num2;
_num1 = tobajinzhi(_num1);
}
else if (_operator == "/")
{
_num1 = _num1 / _num2;
_num1 = tobajinzhi(_num1);
}
}
_operator = "*";//保存运算符号到类字段_operator中
textBoxResult.Text = _num1.ToString();
}
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void buttonDivide_Click(object sender, EventArgs e) //运算符按钮 除法
{
_firstNumberFlag = true;
_operatorCount = _operatorCount + 1;
if (_operatorCount == 1)
{
_num1 = Convert.ToDouble(textBoxResult.Text);
_operator = "/";
}
else
{
_num2 = Convert.ToDouble(textBoxResult.Text);
if (yuan == 10)
{
if (_operator == "+")
{
_num1 = _num1 + _num2;
}
else if (_operator == "-")
{
_num1 = _num1 - _num2;
}
else if (_operator == "*")
_num1 = _num1 * _num2;
else if (_operator == "/")
_num1 = _num1 / _num2;
}
else if (yuan == 2)
{
_num1 = toshijinzhi(_num1, yuan);
_num2 = toshijinzhi(_num2, yuan);
if (_operator == "+")
{
_num1 = _num1 + _num2;
_num1 = toerjinzhi(_num1);
}
else if (_operator == "-")
{
_num1 = _num1 - _num2;
_num1 = toerjinzhi(_num1);
}
else if (_operator == "*")
{
_num1 = _num1 * _num2;
_num1 = toerjinzhi(_num1);
}
else if (_operator == "/")
{
_num1 = _num1 / _num2;
_num1 = toerjinzhi(_num1);
}
}
else if (yuan == 8)
{
_num1 = toshijinzhi(_num1, yuan);
_num2 = toshijinzhi(_num2, yuan);
if (_operator == "+")
{
_num1 = _num1 + _num2;
_num1 = tobajinzhi(_num1);
}
else if (_operator == "-")
{
_num1 = _num1 - _num2;
_num1 = tobajinzhi(_num1);
}
else if (_operator == "*")
{
_num1 = _num1 * _num2;
_num1 = tobajinzhi(_num1);
}
else if (_operator == "/")
{
_num1 = _num1 / _num2;
_num1 = tobajinzhi(_num1);
}
}
_operator = "/";//保存运算符号到类字段_operator中
textBoxResult.Text = _num1.ToString();
}
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void buttonEqual_Click(object sender, EventArgs e) //运算符按钮 等于号
{
_num2 = Convert.ToDouble(textBoxResult.Text);
if (_operator == "+")
{
if(yuan==10)
{
_result = _num1 + _num2;
}
else if (yuan == 2)
{
_num1 = toshijinzhi(_num1, yuan);
_num2 = toshijinzhi(_num2, yuan);
_num1 = _num1 + _num2;
_result = toerjinzhi(_num1);
}
else if (yuan == 8)
{
_num1 = toshijinzhi(_num1, yuan);
_num2 = toshijinzhi(_num2, yuan);
_num1 = _num1 + _num2;
_result = tobajinzhi(_num1);
}
}
else if (_operator == "-")
{
if (yuan == 10)
{
_result = _num1 - _num2;
}
else if (yuan == 2)
{
_num1 = toshijinzhi(_num1, yuan);
_num2 = toshijinzhi(_num2, yuan);
_num1 = _num1 - _num2;
_result = toerjinzhi(_num1);
}
else if (yuan == 8)
{
_num1 = toshijinzhi(_num1, yuan);
_num2 = toshijinzhi(_num2, yuan);
_num1 = _num1 - _num2;
_result = tobajinzhi(_num1);
}
}
else if (_operator == "*")
{
if (yuan == 10)
{
_result = _num1 * _num2;
}
else if (yuan == 2)
{
_num1 = toshijinzhi(_num1, yuan);
_num2 = toshijinzhi(_num2, yuan);
_num1 = _num1 * _num2;
_result = toerjinzhi(_num1);
}
else if (yuan == 8)
{
_num1 = toshijinzhi(_num1, yuan);
_num2 = toshijinzhi(_num2, yuan);
_num1 = _num1 * _num2;
_result = tobajinzhi(_num1);
}
}
else if (_operator == "/")
{
if (yuan == 10)
{
_result = _num1 / _num2;
}
else if (yuan == 2)
{
_num1 = toshijinzhi(_num1, yuan);
_num2 = toshijinzhi(_num2, yuan);
_num1 = _num1 / _num2;
_result = toerjinzhi(_num1);
}
else if (yuan == 8)
{
_num1 = toshijinzhi(_num1, yuan);
_num2 = toshijinzhi(_num2, yuan);
_num1 = _num1 / _num2;
_result = tobajinzhi(_num1);
}
}
textBoxResult.Text = _result.ToString();
_operatorCount = 0;
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void buttonBackSpace_Click(object sender, EventArgs e) //回退
{
if (textBoxResult.Text.Length > 0)
textBoxResult.Text = textBoxResult.Text.Substring(0, textBoxResult.Text.Length - 1);//删除最后一个字符
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void buttonClear_Click(object sender, EventArgs e) //清空
{
_num1 = 0; _num2 = 0; _result = 0;
_operator = "";
_operatorCount = 0;
_firstNumberFlag = false;
textBoxResult.Text = "";
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void buttonSign_Click(object sender, EventArgs e)//退格的按钮
{
if (textBoxResult.Text.Length >= 1)
{
string firstChar = textBoxResult.Text.Substring(0, 1);
if (firstChar == "-")
textBoxResult.Text = "+" + textBoxResult.Text.Substring(1, textBoxResult.Text.Length - 1);
else if (firstChar == "+")
textBoxResult.Text = "-" + textBoxResult.Text.Substring(1, textBoxResult.Text.Length - 1);
else
textBoxResult.Text = "-" + textBoxResult.Text;
}
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void textBoxResult_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '0')
buttonNum0_Click(sender, e);
else if (e.KeyChar == '1')
buttonNum1_Click(sender, e);
else if (e.KeyChar == '2'&&(yuan==8||yuan==10))
buttonNum2_Click(sender, e);
else if (e.KeyChar == '3' && (yuan == 8 || yuan == 10))
buttonNum3_Click(sender, e);
else if (e.KeyChar == '4' && (yuan == 8 || yuan == 10))
buttonNum4_Click(sender, e);
else if (e.KeyChar == '5' && (yuan == 8 || yuan == 10))
buttonNum5_Click(sender, e);
else if (e.KeyChar == '6' && (yuan == 8 || yuan == 10))
buttonNum6_Click(sender, e);
else if (e.KeyChar == '7' && (yuan == 8 || yuan == 10))
buttonNum7_Click(sender, e);
else if (e.KeyChar == '8' && (yuan == 8 || yuan == 10))
buttonNum8_Click(sender, e);
else if (e.KeyChar == '9' && (yuan == 8 || yuan == 10))
buttonNum9_Click(sender, e);
else if (e.KeyChar == '+')
buttonAdd_Click(sender, e);
else if (e.KeyChar == '-')
buttonSubtract_Click(sender, e);
else if (e.KeyChar == '*')
buttonMultiply_Click(sender, e);
else if (e.KeyChar == '/')
buttonDivide_Click(sender, e);
else if (e.KeyChar == '=')
buttonEqual_Click(sender, e);
else if (e.KeyChar == 'c')
buttonClear_Click(sender, e);
else if (e.KeyChar == '.' && yuan == 10)
buttonDot_Click(sender, e);
else if (e.KeyChar == '\b')
buttonBackSpace_Click(sender, e);
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void radioButton2_CheckedChanged(object sender, EventArgs e) //二进制按钮
{
if (_operatorCount == 0 && textBoxResult.Text != "")
{
_num1 = Convert.ToDouble(textBoxResult.Text);
}
if (_operatorCount > 0) //大于等于一个数字
_num2 = Convert.ToDouble(textBoxResult.Text);
if (yuan == 10 && (_num1 != 0 || _num2 != 0))
{
//将原有的十进制转为二进制
_num1 = toerjinzhi (_num1);
_num2 = toerjinzhi(_num2);
if (_num2 == 0)
{
textBoxResult.Text = _num1.ToString();
}
else textBoxResult.Text = _num1.ToString();
}
if (yuan == 8 && (_num1 != 0 || _num2 != 0))
{
_num1 = toshijinzhi(_num1,yuan);
_num1 = toerjinzhi(_num1);
_num2 = toshijinzhi(_num2,yuan);
_num2 = toerjinzhi(_num2);
if (_num2 == 0)
{
textBoxResult.Text = _num1.ToString();
}
else textBoxResult.Text = _num1.ToString();
}
yuan = 2;
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void radioButton10_CheckedChanged(object sender, EventArgs e) //十进制按钮
{
if (_operatorCount == 0 && textBoxResult.Text != "")
{
_num1 = Convert.ToDouble(textBoxResult.Text);
}
if (_operatorCount > 0) //大于等于一个数字
_num2 = Convert.ToDouble(textBoxResult.Text);
if (yuan == 10 && (_num1 != 0 || _num2 != 0))
{
if (_num2 == 0)
{
textBoxResult.Text = _num1.ToString();
}
else textBoxResult.Text = _num1.ToString();
}
else if (yuan == 8 && (_num1 != 0 || _num2 != 0))
{
_num1 = toshijinzhi(_num1, yuan);
_num2 = toshijinzhi(_num2, yuan);
if (_num2 == 0)
{
textBoxResult.Text = _num1.ToString();
}
else textBoxResult.Text = _num1.ToString();
}
else if (yuan == 2 && (_num1 != 0 || _num2 != 0))
{
_num1 = toshijinzhi(_num1, yuan);
_num2 = toshijinzhi(_num2, yuan);
if (_num2 == 0)
{
textBoxResult.Text = _num1.ToString();
}
else textBoxResult.Text = _num1.ToString();
}
yuan = 10;
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
private void radioButton8_CheckedChanged(object sender, EventArgs e) //八进制按钮
{
if (_operatorCount == 0 && textBoxResult.Text != "")
{
_num1 = Convert.ToDouble(textBoxResult.Text);
}
if (_operatorCount > 0) //大于等于一个数字
_num2 = Convert.ToDouble(textBoxResult.Text);
if (yuan == 8 && (_num1 != 0 || _num2 != 0))
{
if (_num2 == 0)
{
textBoxResult.Text = _num1.ToString();
}
else textBoxResult.Text = _num1.ToString();
}
else if (yuan == 10 && (_num1 != 0 || _num2 != 0))
{
_num1 = tobajinzhi(_num1);
_num2 = tobajinzhi(_num2);
if (_num2 == 0)
{
textBoxResult.Text = _num1.ToString();
}
else textBoxResult.Text = _num1.ToString();
}
else if (yuan == 2 && (_num1 != 0 || _num2 != 0))
{
_num1 = toshijinzhi(_num1, yuan);
_num1 = tobajinzhi(_num1);
_num2 = toshijinzhi(_num2, yuan);
_num2 = tobajinzhi(_num2);
if (_num2 == 0)
{
textBoxResult.Text = _num1.ToString();
}
else textBoxResult.Text = _num1.ToString();
}
yuan = 8;
textBoxResult.Focus();
textBoxResult.Select(textBoxResult.Text.Length, 0);
}
public double toerjinzhi(double d) //十进制到二进制
{
double sum=0;
int[] s = new int[32];
int i = 0;
int q = (int)d;
while(q!=0)
{
s[i++] = q % 2;
q /= 2;
}
for (int j = i - 1; j >= 0; j--)
{
sum = sum * 10 + s[j];
}
return sum;
}
public double tobajinzhi(double d) //十进制到八进制
{
double sum = 0;
int[] s = new int[32];
int i = 0;
int q = (int)d;
while (q != 0)
{
s[i++] = q % 8;
q /= 8;
}
for (int j = i - 1; j >= 0; j--)
{
sum = sum * 10 + s[j];
}
return sum;
}
public double toshijinzhi(double d, int shi) //原来的进制 到十进制
{
int i = 0;
double sum = 0;
while (d >0)
{
sum += (int)d % 10 * Math.Pow(shi, i);
d = (int)d / 10;
i++;
}
return sum;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
计算器的制作
最新推荐文章于 2024-06-28 16:41:01 发布