第一次写界面程序。希望各位前辈看了给些意见,求喷,求批评,各种求。
这个是界面:
下面是这个窗口的源码:
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 calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double relt = 0; //存储实型结果
double temp = 0; //存数待计算量
string butline = ""; //顶行字符串
string topline = ""; //底行字符串
//标志进行什么运算,并且判断是否是连续运算(不是的话返回0)
int a = 0, b = 0, c = 0, d = 0;
int jdgFlag()
{
if (a == 1)
return 1;
else if (b == 1)
return 2;
else if (c == 1)
return 3;
else if (d == 1)
return 4;
else
return 0;
}
//判断有没有输入数据
int chkInit()
{
if (string.Compare(butline, "") == 1)
return 0;
else
return 1;
}
//判断是否输入过小数点
int chkPoint()
{
if (butline.Contains("."))
return 1;
else
return 0;
}
//该函数执行计算的功能,第一个参数判断进行哪种运算,第二个参数判断按钮是否为等号
void inCacul(int flag, int n)
{
temp = Convert.ToDouble(butline);
topline += butline;
switch (flag)
{
case 1:
relt += temp;
if (n == 1)
a = 0;
break;
case 2:
relt -= temp;
if (n == 1)
b = 0;
break;
case 3:
relt *= temp;
if (n == 1)
c = 0;
break;
case 4:
relt /= temp;
if (n == 1)
d = 0;
break;
default: break;
}
butline = Convert.ToString(relt);
}
//下面十个函数是对0-9十个数字按钮的按键响应
private void button1_Click(object sender, EventArgs e)
{
butline += "1";
txtOut.Text = topline + "\r\n" + butline;
}
private void button2_Click(object sender, EventArgs e)
{
butline += "2";
txtOut.Text = topline + "\r\n" + butline;
}
private void button3_Click(object sender, EventArgs e)
{
butline += "3";
txtOut.Text = topline + "\r\n" + butline;
}
private void button4_Click(object sender, EventArgs e)
{
butline += "4";
txtOut.Text = topline + "\r\n" + butline;
}
private void button5_Click(object sender, EventArgs e)
{
butline += "5";
txtOut.Text = topline + "\r\n" + butline;
}
private void button6_Click(object sender, EventArgs e)
{
butline += "6";
txtOut.Text = topline + "\r\n" + butline;
}
private void button7_Click(object sender, EventArgs e)
{
butline += "7";
txtOut.Text = topline + "\r\n" + butline;
}
private void button8_Click(object sender, EventArgs e)
{
butline += "8";
txtOut.Text = topline + "\r\n" + butline;
}
private void button9_Click(object sender, EventArgs e)
{
butline += "9";
txtOut.Text = topline + "\r\n" + butline;
}
private void button0_Click(object sender, EventArgs e)
{
butline += "0";
txtOut.Text = topline + "\r\n" + butline;
}
//小数点按钮
private void buttonPoint_Click(object sender, EventArgs e)
{
if (chkPoint() == 0) //判断之前有没有按过小数点,按过的话就不响应
{
butline += ".";
txtOut.Text = topline + "\r\n" + butline;
}
}
//加号按钮的处理
private void buttonAdd_Click(object sender, EventArgs e)
{
if (chkInit() != 1)
{
int flag = jdgFlag();
if (flag != 0) //判断是否是连续运算
{
inCacul(flag, 0); //如果是连续运算,则将上次结果计算出来
d = b = c = 0; //去除上次运算的标志
a = 1; //为下次加法运算做标识
if (flag != 1 && flag != 2) //如果不是同一运算级别的话重新显示算式
topline = Convert.ToString(relt) + "+";
else
topline += "+"; //如果是同一运算级别的话连续显示算式
butline = "";
txtOut.Text = topline + "\r\n" + butline;
}
else
{
relt = Convert.ToDouble(butline);
topline = butline + "+";
butline = "";
txtOut.Text = topline + "\r\n" + butline;
a = 1;
}
}
}
//减号按钮的处理
private void buttonSub_Click(object sender, EventArgs e)
{
if (chkInit() != 1)
{
int flag = jdgFlag();
if (flag != 0)
{
inCacul(flag, 0);
a = c = d = 0;
b = 1;
if (flag != 1 && flag != 2)
topline = Convert.ToString(relt) + "-";
else
topline += "-";
butline = "";
txtOut.Text = topline + "\r\n" + butline;
}
else
{
relt = Convert.ToDouble(butline);
topline = butline + "-";
butline = "";
txtOut.Text = topline + "\r\n" + butline;
b = 1;
}
}
else //减号同时作为负号的处理
{
butline = "-";
txtOut.Text = topline + "\r\n" + butline;
}
}
//乘号按钮的处理
private void buttonMul_Click(object sender, EventArgs e)
{
if (chkInit() != 1)
{
int flag = jdgFlag();
if (flag != 0)
{
inCacul(flag, 0);
a = b = d = 0;
c = 1;
if (flag != 3 && flag != 4)
topline = Convert.ToString(relt) + "*";
else
topline += "*";
butline = "";
txtOut.Text = topline + "\r\n" + butline;
}
else
{
relt = Convert.ToDouble(butline);
topline = butline + "*";
butline = "";
txtOut.Text = topline + "\r\n" + butline;
c = 1;
}
}
}
//除号按钮的处理
private void buttonDiv_Click(object sender, EventArgs e)
{
if (chkInit() != 1)
{
int flag = jdgFlag();
if (flag != 0)
{
inCacul(flag, 0);
a = b = c = 0;
d = 1;
if (flag != 3 && flag != 4)
topline = Convert.ToString(relt) + "/";
else
topline += "/";
butline = "";
txtOut.Text = topline + "\r\n0" + butline;
}
else
{
relt = Convert.ToDouble(butline);
topline = butline + "/";
butline = "";
txtOut.Text = topline + "\r\n" + butline;
d = 1;
}
}
}
//等号按钮的处理
private void buttonEql_Click(object sender, EventArgs e)
{
if (chkInit() != 1)
{
int flag = jdgFlag();
if (flag != 0)
{
inCacul(flag, 1);
topline += "=";
txtOut.Text = topline + "\r\n" + butline;
topline = Convert.ToString(relt);
}
else
{
topline = butline + "=";
txtOut.Text = topline + "\r\n" + butline;
}
}
}
private void buttonDel_Click(object sender, EventArgs e)
{
relt = 0;
temp = 0;
butline = "";
topline = "";
a = b = c = d = 0;
txtOut.Text = "\r\n0";
}
private void buttonMes_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
}
}
生成的可执行文件在这里: http://pan.baidu.com/share/link?shareid=1083603754&uk=1327215172