【2019-2020春学期】数据库实验1:Calcultor简单计算器
程序员文章站
2022-03-09 09:07:36
...
实验目的:
1、熟悉控件和编程
实验内容:
用Visual Studio编写一个简单计算器
目标效果:
实验步骤:
1、创建新项目
2、根据需求选择项目模板:
3、根据目标效果从工具箱添加公共组件
一开始的界面是酱婶的,为了让它和目标更加的接近,我们可以在右侧的属性栏改变它的长宽属性,对于控件的其他属性,也可以在此处进行修改
初步的效果就是酱紫的
4、为按钮添加点击事件
(1)数字
可先用代码进行测试,若点击按钮button18后出现弹窗,则说明为按钮添加点击事件的行为成功。
private void button18_Click(object sender, EventArgs e)
{
MessageBox.Show("1");
}
接着就开始为数字按钮添加点击事件,点击事件的结果就是在文本框textBox1输入字符
举例:
private void button18_Click(object sender, EventArgs e)
{
//MessageBox.Show("1");
textBox1.Text += "1"; //注意
}
(2)运算符
举例“+”:
因为在计算器中,点击运算符,会使得原文本框里的内容清空,所以此时需要用全局变量保存下来,同时该文本框清空。
private void button14_Click(object sender, EventArgs e)
{
i = Convert.ToDouble(textBox1.Text);
textBox1.Text = "";
}
(3)等号
在等号中应该判断,该运算是哪种运算,所以还应该设置一个全局变量,该变量用于判断是应该进行那种运算
private void button10_Click(object sender, EventArgs e)
{
j = Convert.ToDouble(textBox1.Text);
if (k == 1)
{
j = i + j;
}else if (k == 2)
{
j = i - j;
}else if (k == 3)
{
j = i * j;
}else if (k == 4)
{
j = i / j;
}
textBox1.Text = j.ToString();
}
(4)清零按钮C
private void button20_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
(5)1/x按钮
private void button1_Click(object sender, EventArgs e)
{
i = Convert.ToDouble(textBox1.Text);
i = 1 / i;
textBox1.Text = i.ToString();
}
(6)考虑特殊情况,比如除数为0,此时设置事件为弹出提示“除数不能为0”,且清空textBox1的内容,所以需要对 button10的点击事件进行修改
private void button10_Click(object sender, EventArgs e)
{
j = Convert.ToDouble(textBox1.Text);
if (k == 1)
{
j = i + j;
}else if (k == 2)
{
j = i - j;
}else if (k == 3)
{
j = i * j;
}else if (k == 4)
{
if (j == 0)
{
MessageBox.Show("除数不能为0");
textBox1.Text = "";
}
else
{
j = i / j;
}
}
if (!(k == 4 && j == 0))
{
textBox1.Text = j.ToString();
}
}
运行效果:
(1)四则运算:
(2)1/x
(3)清零:
(4)除数为0
点击确定以后:
代码:
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 Calculator1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double i,j,k;
private void button2_Click(object sender, EventArgs e)
{
i = Convert.ToDouble(textBox1.Text);
textBox1.Text = "";
k = 3;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button18_Click(object sender, EventArgs e)
{
//MessageBox.Show("1");
textBox1.Text += "1";
}
private void button17_Click(object sender, EventArgs e)
{
textBox1.Text += "2";
}
private void button16_Click(object sender, EventArgs e)
{
textBox1.Text += "3";
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text += "4";
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text += "5";
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text += "6";
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text += "7";
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += "8";
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text += "9";
}
private void button12_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
private void button14_Click(object sender, EventArgs e)
{
i = Convert.ToDouble(textBox1.Text);
textBox1.Text = "";
k = 1;
}
private void button19_Click(object sender, EventArgs e)
{
i = Convert.ToDouble(textBox1.Text);
textBox1.Text = "";
k = 4;
}
private void button9_Click(object sender, EventArgs e)
{
i = Convert.ToDouble(textBox1.Text);
textBox1.Text = "";
k = 2;
}
private void button10_Click(object sender, EventArgs e)
{
j = Convert.ToDouble(textBox1.Text);
if (k == 1)
{
j = i + j;
}else if (k == 2)
{
j = i - j;
}else if (k == 3)
{
j = i * j;
}else if (k == 4)
{
if (j == 0)
{
MessageBox.Show("除数不能为0");
textBox1.Text = "";
}
else
{
j = i / j;
}
}
if (!(k == 4 && j == 0))
{
textBox1.Text = j.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
i = Convert.ToDouble(textBox1.Text);
i = 1 / i;
textBox1.Text = i.ToString();
}
private void button11_Click(object sender, EventArgs e)
{
textBox1.Text += ".";
}
private void button20_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
}
}
心得体会:
心得:过程很有趣的,而且我觉得我的计算器看起来还是挺好看的
上一篇: CentOS 安装 Mogodb的步骤(在线&&离线两种)
下一篇: 实验六七 SQL的数据更新