做实验时,打开自己之前下载过的Visual Studio ,由于登录名之类的问题突然不能用啦,在csdn上看啦相关其他博主的介绍之后,也不知道怎么的给弄好啦,尴尬的是,忘记截图
啦,enmmm,总之,好啦。直接步入正题吧
这次主要是编写一个简单的计算器,老师推荐我们使用C#,所以就用C#,因为其他的也不会啊,哈哈,自己太笨啦,enmmm,慢慢学习吧。
一、首先使用Visual Studio创建出C#环境和简单的框架
1.打开Visual Studio,选择创建新项目。

2.选择相应的项目和内容,点击下一步。

3.给自己的项目起一个名字,由于做的是一个简单的计算器,就起名为calculator吧,然后点击创建。

4.这次主要使用Button和TextBox这两个,然后右边属性栏可以调节大小,字体和名称等,有一个大致的轮廓。

5.在格式里面对齐按钮,在右侧的属性栏中对按钮的名称,大小等属性按照自己的喜好进行修改,最终成果如图所示。

二、对各个按钮进行程序设计,编写各自的功能
1.对各个数字进行编写,主要为点击是在textbox上显示。

2.对各个运算符进行编写,实现相应的运算功能。

3.对AC进行编写,当按下AC是 textbox1 与 textbox2 同时清空。

4.等于符号的编写,当按下等于时,在 textbox1 上显示出运算结果,在 textbox2 上显示运算式子。

三、简单对一些式子进行计算,看是否正确。
1.这里有两个 textbox ,一个显示运算式子,一个显示最终运算结果,便于检验。


最后附上代码
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 Lnum,Rnum,Result;
String flag;
int i = 0;
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += '1';
textBox2.Text += '1';
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text += '2';
textBox2.Text += '2';
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text += '3';
textBox2.Text += '3';
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text += '4';
textBox2.Text += '4';
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text += '5';
textBox2.Text += '5';
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text += '6';
textBox2.Text += '6';
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += '7';
textBox2.Text += '7';
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text += '8';
textBox2.Text += '8';
}
private void dian_Click(object sender, EventArgs e)
{
textBox1.Text += '.';
textBox2.Text += '.';
}
private void buttonAdd_Click(object sender, EventArgs e)
{
Lnum = Convert.ToDouble(textBox1.Text);
textBox2.Text = Lnum.ToString() + "+";
flag = "+";
textBox1.Text = "";
}
private void buttonEqual_Click(object sender, EventArgs e)
{
Rnum = Convert.ToDouble(textBox1.Text);
textBox2.Text += "=";
if (flag == "+") Result = Lnum + Rnum;
else if (flag == "-") Result = Lnum - Rnum;
else if (flag == "*") Result = Lnum * Rnum;
else if (flag == "/") Result = Lnum / Rnum;
else if (flag == "%") Result = Lnum % Rnum;
else
{
Lnum = Convert.ToDouble(textBox1.Text);
Result = Lnum;
}
textBox1.Text = Result.ToString();
}
private void buttonAc_Click(object sender, EventArgs e)
{
Lnum = Rnum = 0;
textBox1.Text = "";
textBox2.Text = "";
}
private void buttonSubt_Click(object sender, EventArgs e)
{
Lnum = Convert.ToDouble(textBox1.Text);
textBox2.Text = Lnum.ToString() + "-";
flag = "-";
textBox1.Text = "";
}
private void buttonTimes_Click(object sender, EventArgs e)
{
Lnum = Convert.ToDouble(textBox1.Text);
textBox2.Text = Lnum.ToString() + "*";
flag = "*";
textBox1.Text = "";
}
private void buttonDivided_Click(object sender, EventArgs e)
{
Lnum = Convert.ToDouble(textBox1.Text);
textBox2.Text = Lnum.ToString() + "/";
flag = "/";
textBox1.Text = "";
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button11_Click(object sender, EventArgs e)
{
Lnum = Convert.ToDouble(textBox1.Text);
textBox2.Text = Lnum.ToString() + "%";
flag = "%";
textBox1.Text = "";
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text += '9';
textBox2.Text += '9';
}
private void button0_Click(object sender, EventArgs e)
{
textBox1.Text += '0';
textBox2.Text += '0';
}
}
}
缺陷与不足
1.无法对一些不符合错误的式子判断
2.“/0”操作不报错
3.无法对含有多个运算符的连续算式进行运算。
有能力的话再进行完善完善吧。
好啦,这就是全部的内容啦,感谢观看
8348

被折叠的 条评论
为什么被折叠?



