本篇内容介绍了“C#实现计算器精简版的代码怎么写”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
一、界面设计
1.做一个显示屏
2.17个按钮(0-9,±×÷%=,CE)
二、需要实现的功能
1.输入第一个数字
2.选择运算类型
3.输入第二个数字
4.按下等号计算出结果,结果显示在显示屏上
三、实现步骤
1.先做界面
a.显示屏 textbox、listbox、label
b.使用17个button,button上的文本改成对应的数字符号
2.补充:申请两个int类型变量,第一个num1装第一个数字
第二个num2装第二个数字
(1).输入第一个数字,当点一个数字按钮,屏幕上显示一个,之前显示的数字在前面呆着
a1.添加按钮的cilck事件
a2.事件触发,将按钮代表的数字显示textbox1的text
(2).当输入符号的时候,清除屏幕,但是后台必须记录好第一个数字
b1.添加符号按钮的click事件
b2.当点任何一个符号按钮用一个变量num1装刚才输入的textbox1中的数字
(3).输入第二个数字
c1. 当点任何一个符号按钮用一个变量num2装刚才输入的textbox1中的数字
(4).按下等号按钮,显示屏上面的文本改变成两个数字的运算结果
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 简单的计算器制作
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//计算窗口加载居中的位置
int left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
int top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
this.Location = new Point(left,top);
//加载的时候获取焦点
button1.TabIndex = 0;
}
//当我们输入完第一个数字之后 在输入运算符的时候 我们要记下第一个数字num1
//当我们输入完第二个数字之后 在输入等号的时候 我们要记下第二个数字num1
double num1 = 0;
double num2 = 0;
bool iskey = false;
//ce
private void button1_Click(object sender, EventArgs e)
{
//设置清空
textBox1.Text = "";
}
//1
private void button4_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "1";
}
//2
private void button5_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "2";
}
//3
private void button6_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "3";
}
//4
private void button8_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "4";
}
//5
private void button9_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "5";
}
//6
private void button10_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "6";
}
//7
private void button12_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "7";
}
//8
private void button13_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "8";
}
//9
private void button14_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "9";
}
//0
private void button17_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "9";
}
//.
private void button16_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += ".";
}
//定义一个空的来接收符号
string type=" ";
//+
private void button15_Click(object sender, EventArgs e)
{
if(textBox1.Text != "")
{
//获取运算的第一个数字(前一个数字);将字符串类型转换为int类型(int.parse())
// num1 = int.Parse(textBox1.Text);
// num1 = Convert.ToInt32(textBox1.Text);
// 第二种转换方式convert
num1 = Convert.ToDouble(textBox1.Text);
}
type = "+";
// textBox1.Text = "";
iskey = true;
}
//-
private void button3_Click(object sender, EventArgs e)
{
if(textBox1.Text != ""){
num1 = Convert.ToDouble(textBox1.Text);
}
type = "-";
// textBox1.Text = "";
iskey = true;
}
//*
private void button7_Click(object sender, EventArgs e)
{
if(textBox1.Text != "")
{
num1 = Convert.ToDouble(textBox1.Text);
}
type = "*";
// textBox1.Text = "";
iskey = true;
}
//÷
private void button11_Click(object sender, EventArgs e)
{
if(textBox1.Text != "")
{
num1 = Convert.ToDouble(textBox1.Text);
}
type = "/";
//textBox1.Text = "";
iskey = true;
}
//%
private void button18_Click(object sender, EventArgs e)
{
iskey = true;
if (textBox1.Text != "")
{
num1 = Convert.ToDouble(textBox1.Text);
}
type = "%";
//textBox1.Text = "";
}
//=
private void button2_Click(object sender, EventArgs e)
{
if (iskey)
{
return;
}
iskey = true;
if(textBox1.Text != "")
{
num2 = Convert.ToDouble(textBox1.Text);
}
switch (type)
{
case "+":
//括号里进行计算,计算的结果转化为string类型,并显示在屏幕(textbox1)里;
textBox1.Text = (num1 + num2).ToString();
break;
case "-":
textBox1.Text = (num1 - num2).ToString();
break;
case "*":
textBox1.Text = (num1 * num2).ToString();
break;
case "/":
textBox1.Text = (num1 / num2).ToString();
break;
case "%":
textBox1.Text = (num1 % num2).ToString();
break;
}
}
}
}
“C#实现计算器精简版的代码怎么写”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。