基于C#窗体程序的一个简单计算器,进制转换的小数部分没有完善,初学者,继续努力中...各位大侠请指教,各位小白大家互相学习,一起进步
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;
using System.Threading;
namespace js_easy
{
public partial class Form1 : Form
{
string Fh = "";//定义符号位
double a, b, c;//定义a第一个值,b第二个值,c输出值
long s = 1;//定义小数最初为一位
public Form1()
{
InitializeComponent();
}
public void anniu(double i)//按钮
方法
快递客服问题件处理详细方法山木方法pdf计算方法pdf华与华方法下载八字理论方法下载
{
if (Fh.Equals(""))//是否当前有运算符
{
if (s == 1)//判断是否有小数点
{
a = a * 10 + i;//计算无小数的值
}
else
{
a = a + i / s;//计算有小数的值
s = s * 10;//计算下一个小数的位置
}
textBox1.Text = a.ToString();//显示计算的结果
}
else //当前有运算符的方法
{
if (!(c == 0)) { b = 0; c = 0; }//当有计算结果就清除B的值
if (s == 1)//判断是否有小数点
{
b = b * 10 + i;//计算无小数的值
}
else
{
if (i == 0)//判断在小数位后添加0
{
s = s * 10;//计算下一个小数的位置
}
else
{
b = b + i / s;//计算有小数的值
s = s * 10;//计算下一个小数的位置
}
}
textBox1.Text = b.ToString();//显示计算的结果
}
}
public void js()
{//数学计算的方法
if (Fh == "/")//判断是否除数等于0
{
if (b == 0) //是则不计算
{
textBox1.Text = "0";//被除数不能为0
return;//返回
}
else
{
c = a / b;
}
}
else//如果不是出发计算则执行其他计算
{
if (Fh.Equals("+")) { c = a + b; }
if (Fh.Equals("-")) { c = a - b; }
if (Fh.Equals("*")) { c = a * b; }
if (Fh.Equals("%")) { c = a % b; }
}
textBox1.Text = c.ToString(); //显示计算结果
a = c;//将结果给第一个变量作为
}
public void fh(string str) //接收运算符的方法
{
if (Fh.Equals(""))//当前是否已经有运算符
{
Fh = str; s = 1;//没有则接收运算符,小数点失效
}
else
{
js();
s = 1; Fh = str;//有则计算当前的值,小数点失效
}
}
/*暂不支持小数进制转换*/
public void jz2(string jg)//2进制转换
{
long temp = Convert.ToInt64(jg);
if (temp < 2)
{
textBox2.Text = temp.ToString();
}
else
{
int i = 0;
int[] array = new int[1000];
while (temp > 0)
{
array[i++] = (int)temp % 2;
temp /= 2;
}
int[] array_1 = new int[i];
for (int j = i - 1; j >= 0; j--)
array_1[j] = array[i - 1 - j];
//Array.Reverse(array_1);
foreach (int k in array_1)
textBox2.Text += k.ToString();
}
}
public void jz8(string jg)//8进制转换
{
long temp = Convert.ToInt64(jg);
if (temp < 9)
{
textBox2.Text = temp.ToString();
}
else
{
int i = 0;
int[] array = new int[1000];
while (temp > 0)
{
array[i++] = (int)temp % 8;
temp /= 8;
}
int[] array_1 = new int[i];
for (int j = i - 1; j >= 0; j--)
array_1[j] = array[i - 1 - j];
//Array.Reverse(array_1);
foreach (int k in array_1)
textBox2.Text += k.ToString();
}
}
public void jz16(string jg)//十六进制转换
{
long temp = Convert.ToInt64(jg);
if (temp < 16)
{
textBox2.Text = temp.ToString();
}
else
{
int i = 0;
int[] array = new int[1000];//浪费空间,节省时间
while (temp > 0)
{
array[i++] = (int)temp % 16;
temp /= 16;
}
int[] array_1 = new int[i];
for (int j = i - 1; j >= 0; j--)
array_1[j] = array[i - 1 - j];
foreach (int k in array_1)
textBox2.Text += k.ToString("X");
}
}
public long Jiec(long jg)//阶乘
{
long temp = jg;
if (temp == 1)
{
return 1;
}
else
{
long temp1 = Jiec(temp-1);
temp = temp * temp1;
return temp;
}
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Font = new Font("宋体", 14, FontStyle.Bold);
textBox2.Font = new Font("宋体", 14, FontStyle.Bold);
this.Text = "简单计算器";
string dir = @"image\angry.jpg";
pictureBox1.Image = Image.FromFile(dir);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Dock = DockStyle.Fill;
button1.Text = "=";
button2.Text = "2";
button3.Text = "3";
button4.Text = "4";
button5.Text = "5";
button6.Text = "C1";
button7.Text = "7";
button8.Text = "8";
button9.Text = "9";
button10.Text = "0";
button12.Text = "+";
button13.Text = "-";
button14.Text = "*";
button15.Text = "%";
button16.Text = "/";
button17.Text = "6";
button18.Text = ".";
button19.Text = "1";
button11.Text = "C2";
button20.Text = "Sin";
button21.Text = "Cos";
button22.Text = "Tan";
button23.Text = "Log10";
button24.Text="Lg";
button25.Text = "!";
button25.Font = new Font("",10,FontStyle.Bold);
button26.Text = "^2";
radioButton1.Text = "二进制";
radioButton2.Text = "八进制";
radioButton3.Text = "十六进制";
radioButton4.Text = "十 进 制";
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.BackColor = Color.White;
textBox1.Enabled = false;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox1.BackColor = Color.White;
textBox1.Enabled = false;
}
private void button19_Click(object sender, EventArgs e)
{
anniu(1);
}
private void button2_Click(object sender, EventArgs e)
{
anniu(2);
}
private void button3_Click(object sender, EventArgs e)
{
anniu(3);
}
private void button4_Click(object sender, EventArgs e)
{
anniu(4);
}
private void button5_Click(object sender, EventArgs e)
{
anniu(5);
}
private void button17_Click(object sender, EventArgs e)
{
anniu(6);
}
private void button7_Click(object sender, EventArgs e)
{
anniu(7);
}
private void button8_Click(object sender, EventArgs e)
{
anniu(8);
}
private void button9_Click(object sender, EventArgs e)
{
anniu(9);
}
private void button10_Click(object sender, EventArgs e)
{
if (!textBox1.Text.Equals("0"))
{
anniu(0);
}
}
private void button18_Click(object sender, EventArgs e)
{
if (s == 1) { s = 10; }
}
private void button14_Click(object sender, EventArgs e)
{
fh("*");
button14.BackColor = Color.BlueViolet;
}
private void button15_Click(object sender, EventArgs e)
{
fh("%");
button15.BackColor = Color.BlueViolet;
}
private void button12_Click(object sender, EventArgs e)
{
fh("+");
button12.BackColor = Color.BlueViolet;
}
private void button13_Click(object sender, EventArgs e)
{
fh("-");
button13.BackColor = Color.BlueViolet;
}
private void button16_Click(object sender, EventArgs e)
{
fh("/");
button16.BackColor = Color.BlueViolet;
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text = "0";//清空TEXTBOX的显示值
a = 0; b = 0; c = 0; s = 1; Fh = "";//初始化所有值
}
private void button1_Click(object sender, EventArgs e)
{
fh("=");
Thread.Sleep(300);//颜色改变延迟
button12.BackColor = SystemColors.Control;//还原按钮颜色
button13.BackColor = SystemColors.Control;
button14.BackColor = SystemColors.Control;
button15.BackColor = SystemColors.Control;
button16.BackColor = SystemColors.Control;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (textBox2.Text != null)
{
textBox2.Text = "";
jz2(textBox1.Text);
}
else
{
jz2(textBox1.Text);
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (textBox2.Text != null)
{
textBox2.Text = "";
jz8(textBox1.Text);
}
else
{
jz8(textBox1.Text);
}
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
if (textBox2.Text != null)
{
textBox2.Text = "";
jz16(textBox1.Text);
}
else
{
jz16(textBox1.Text);
}
}
private void button11_Click(object sender, EventArgs e)
{
textBox2.Text = "";
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
textBox2.Text = c.ToString();
}
private void button20_Click(object sender, EventArgs e)
{
double temp = Convert.ToDouble(textBox1.Text);
temp = Math.Sin(3.1415926 * temp / 180);
if (textBox2.Text != null)
{
textBox2.Text = "";
textBox2.Text = temp.ToString();
}
else
{
textBox2.Text = temp.ToString();
}
}
private void button21_Click(object sender, EventArgs e)
{
double temp = Convert.ToDouble(textBox1.Text);
temp = Math.Cos(3.1415926*temp/180);
if (textBox2.Text != null)
{
textBox2.Text = "";
textBox2.Text = temp.ToString();
}
else
{
textBox2.Text = temp.ToString();
}
}
private void button22_Click(object sender, EventArgs e)
{
double temp = Convert.ToDouble(textBox1.Text);
temp = Math.Tan(3.1415926*temp/180);
if (textBox2.Text != null)
{
textBox2.Text = "";
textBox2.Text = temp.ToString();
}
else
{
textBox2.Text = temp.ToString();
}
}
private void button23_Click(object sender, EventArgs e)
{
double temp = Convert.ToDouble(textBox1.Text);
temp = Math.Log10(temp) ;
if (textBox2.Text != null)
{
textBox2.Text = "";
textBox2.Text = temp.ToString();
}
else
{
textBox2.Text = temp.ToString();
}
}
private void button24_Click(object sender, EventArgs e)
{
double temp = Convert.ToDouble(textBox1.Text);
temp = Math.Log(temp);
if (textBox2.Text != null)
{
textBox2.Text = "";
textBox2.Text = temp.ToString();
}
else
{
textBox2.Text = temp.ToString();
}
}
private void button25_Click(object sender, EventArgs e)
{
long temp = Convert.ToInt64(textBox1.Text);
Jiec(temp);
if (textBox2.Text != null)
{
textBox2.Text = "";
textBox2.Text = Jiec(temp).ToString();
}
else
{
textBox2.Text = Jiec(temp).ToString();
}
}
private void button26_Click(object sender, EventArgs e)
{
long y = Convert.ToInt64(textBox1.Text);
double temp= Math.Pow(y,0.5);
if (textBox2.Text != null)
{
textBox2.Text = "";
textBox2.Text = temp.ToString();
}
else
{
textBox2.Text = temp.ToString();
}
}
}
}
一个基于windows phone 7的简单计算器,同样也是进制转换小数问题,我会不断完善,希望跟小白一起慢慢成长
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace WP_JS
{
public partial class MainPage : PhoneApplicationPage
{
string Fh = "";//定义符号位
double first, second, result;//定义a第一个值,b第二个值,c输出值
long dec = 1;//定义小数最初为一位
// Constructor
public void anniu(double i)//按钮方法
{
if (Fh.Equals(""))//是否当前有运算符
{
if (dec == 1)//判断是否有小数点
{
first = first * 10 + i;//计算无小数的值
}
else
{
first = first + i / dec;//计算有小数的值
dec = dec * 10;//计算下一个小数的位置
}
textBox1.Text = first.ToString();//显示计算的结果
}
else //当前有运算符的方法
{
if (!(result == 0)) { second = 0; result = 0; }//当有计算结果就清除B的值
if (dec == 1)//判断是否有小数点
{
second = second * 10 + i;//计算无小数的值
}
else
{
if (i == 0)//判断在小数位后添加0
{
dec = dec * 10;//计算下一个小数的位置
}
else
{
second = second+ i / dec;//计算有小数的值
dec = dec * 10;//计算下一个小数的位置
}
}
textBox1.Text = second.ToString();//显示计算的结果
}
}
public void js()
{//数学计算的方法
if (Fh == "/")//判断是否除数等于0
{
if (second == 0) //是则不计算
{
textBox1.Text = "0";//被除数不能为0
return;//返回
}
else
{
result=first/second;
}
}
else//如果不是出发计算则执行其他计算
{
if (Fh.Equals("+")) { result = first + second; }
if (Fh.Equals("-")) { result = first - second; }
if (Fh.Equals("*")) { result = first * second; }
if (Fh.Equals("%")) { result = first % second; }
}
textBox1.Text = result.ToString(); //显示计算结果
first = result;//将结果给第一个变量作为
}
public void fh(string str) //接收运算符的方法
{
if (Fh.Equals(""))//当前是否已经有运算符
{
Fh = str; dec = 1;//没有则接收运算符,小数点失效
}
else
{
js();
dec = 1; Fh = str;//有则计算当前的值,小数点失效
}
}
public void jz_2(string jg)
{
long temp = Convert.ToInt64(jg);
if (temp < 2)
{
textBox2.Text = temp.ToString();
}
else
{
int i = 0;
int[] array = new int[1000];
while (temp > 0)
{
array[i++] = (int)temp % 2;
temp /= 2;
}
int[] array_1 = new int[i];
for (int j = i - 1; j >= 0; j--)
array_1[j] = array[i - 1 - j];
//Array.Reverse(array_1);
foreach (int k in array_1)
textBox2.Text += k.ToString();
}
}
public void jz_8(string jg)
{
long temp = Convert.ToInt64(jg);
if (temp <8)
{
textBox2.Text = temp.ToString();
}
else
{
int i = 0;
int[] array = new int[1000];
while (temp > 0)
{
array[i++] = (int)temp % 8;
temp /= 8;
}
int[] array_1 = new int[i];
for (int j = i - 1; j >= 0; j--)
array_1[j] = array[i - 1 - j];
//Array.Reverse(array_1);
foreach (int k in array_1)
textBox2.Text += k.ToString();
}
}
public void jz_16(string jg)
{
long temp = Convert.ToInt64(jg);
if (temp <16)
{
textBox2.Text = temp.ToString();
}
else
{
int i = 0;
int[] array = new int[1000];
while (temp > 0)
{
array[i++] = (int)temp % 16;
temp /= 16;
}
int[] array_1 = new int[i];
for (int j = i - 1; j >= 0; j--)
array_1[j] = array[i - 1 - j];
//Array.Reverse(array_1);
foreach (int k in array_1)
textBox2.Text += k.ToString("X");
}
}
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
anniu(1);
}
private void button2_Click(object sender, RoutedEven