本帖最后由 wuaixql 于 2017-12-16 11:55 编辑
昨天刚刚学了C#的WinForm窗口程序,就自己尝试去做了一个计算器,先上图
界面是丑了一点,哈哈! 大牛勿喷,代码奉上,喜欢的童鞋可以拿去研究,其实挺简单的
[C#] 纯文本查看 复制代码 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 JSQ
{
public partial class Frmjsq : Form
{
public Frmjsq()
{
InitializeComponent();
}
private void btn1_Click(object sender, EventArgs e)
{
string n = txtShow.Text;
txtShow.Text = n + "1";
}
private void btn2_Click(object sender, EventArgs e)
{
string n = txtShow.Text;
txtShow.Text = n + "2";
}
private void btn3_Click(object sender, EventArgs e)
{
string n = txtShow.Text;
txtShow.Text = n + "3";
}
private void btn4_Click(object sender, EventArgs e)
{
string n = txtShow.Text;
txtShow.Text = n + "4";
}
private void btn5_Click(object sender, EventArgs e)
{
string n = txtShow.Text;
txtShow.Text = n + "5";
}
private void btn6_Click(object sender, EventArgs e)
{
string n = txtShow.Text;
txtShow.Text = n + "6";
}
private void btn7_Click(object sender, EventArgs e)
{
string n = txtShow.Text;
txtShow.Text = n + "7";
}
private void btn8_Click(object sender, EventArgs e)
{
string n = txtShow.Text;
txtShow.Text = n + "8";
}
private void btn9_Click(object sender, EventArgs e)
{
string n = txtShow.Text;
txtShow.Text = n + "9";
}
private void btn0_Click(object sender, EventArgs e)
{
string n = txtShow.Text;
txtShow.Text = n + "0";
}
private void btnjia_Click(object sender, EventArgs e)
{
string n = txtShow.Text;
txtShow.Text = n + "+";
}
private void btnjian_Click(object sender, EventArgs e)
{
string n = txtShow.Text;
txtShow.Text = n + "-";
}
private void btncheng_Click(object sender, EventArgs e)
{
string n = txtShow.Text;
txtShow.Text = n + "*";
}
private void btnchu_Click(object sender, EventArgs e)
{
string n = txtShow.Text;
txtShow.Text = n + "/";
}
private void btndy_Click(object sender, EventArgs e)
{
string nums = txtShow.Text;
int fuhaowz = 0;
if ( nums[nums.Length - 1] == '+' ||
nums[nums.Length - 1] == '-' ||nums[nums.Length - 1] == '*'
|| nums[nums.Length - 1] == '/')
{
MessageBox.Show("错误");
txtShow.Text = null;
}
else {
char[] fuhao = { '+', '-', '*', '/' };
for (int i = 0; i < nums.Length; i++)
{
for (int j = 0; j < fuhao.Length; j++)
{
if (fuhao[j] == nums[i])
{
fuhaowz = i;
break;
}
}
}
char b = nums[fuhaowz];
string[]m= nums.Split(b);
double n1 = Double.Parse(m[0]);
double n2 = Double.Parse(m[1]);
switch (b)
{
case '+':
txtShow.Text =( n1 + n2).ToString();
break;
case '-':
txtShow.Text = (n1 - n2).ToString();
break;
case '*':
txtShow.Text = (n1 * n2).ToString();
break;
case '/':
txtShow.Text = (n1 / n2).ToString();
break;
}
}
//for (int i = 0; i < nums.Length; i++)
//{
// else
// {
// if (nums[i] == '+' || nums[0] == '-' || nums[0] == '*' || nums[0] == '/')
// {
// fuhao = nums[i].ToString();
// }
// else
// {
// if (nums[i] == '+' || nums[0] == '-' || nums[0] == '*' || nums[0] == '/')
// {
// fuhao = nums[i].ToString();
// }
// }
// }
//}
}
private void btnqc_Click(object sender, EventArgs e)
{
txtShow.Text = null;
}
}
}
|