C#基础训练营 - 02 - 运算器

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

public double jia(double a,double b)

{

return a + b;

}

public double jian(double a, double b)

{

return a - b;

}

public double cheng(double a, double b)

{

return a * b;

}

public double chu(double a, double b)

{

return a / b;

}

public double mo(double a, double b)

{

return a % b;

}

private void button1_Click(object sender, EventArgs e)

{

double a = 100;

double b = 50;

// string[] opt = new string[] { "+", "-", "*", "/", "%" };

// string[] opfun = new string[] { "jia", "jian", "cheng", "chu", "mo" };

string op = cmbOp.Text.Trim();

if (double.TryParse(txtA.Text, out a) && double.TryParse(txtB.Text, out b))

{

double c = 0d;

if (op == "+")

c = jia(a, b);

else if (op == "-")

c = jian(a, b);

else if (op == "*")

c = cheng(a, b);

else if (op == "/")

c = chu(a, b);

else if (op == "%")

{

c = mo(a, b);

}

else

c = 0;

MessageBox.Show($"{a}{op}{b}={c}", "运算");

}

else

{

MessageBox.Show("填写错误!");

}

}

private void Form1_Load(object sender, EventArgs e)

{

txtA.Text = "100";

txtB.Text = "50";

}

}

相关推荐
晔子yy7 分钟前
【JAVA探索之路】从头开始讲透、实现单例模式
java·开发语言·单例模式
阿正的梦工坊5 小时前
JavaScript 微任务与宏任务完全指南
开发语言·javascript·ecmascript
知行合一。。。6 小时前
Python--05--面向对象(属性,方法)
android·开发语言·python
青梅橘子皮6 小时前
C语言---指针的应用以及一些面试题
c语言·开发语言·算法
浅时光_c6 小时前
3 shell脚本编程
linux·开发语言·bash
Evand J7 小时前
【三维轨迹目标定位,CKF+RTS,MATLAB程序】基于CKF与RTS平滑的三维非线性目标跟踪(距离+方位角+俯仰角)
开发语言·matlab·目标跟踪
今天又在写代码7 小时前
java-v2
java·开发语言
competes8 小时前
慈善基金投资底层逻辑应用 顶层代码低代码配置平台开发结构方式数据存储模块
java·开发语言·数据库·windows·sql
Ulyanov8 小时前
用Pyglet打造AI数字猎人:从零开始的Python游戏开发与强化学习实践
开发语言·人工智能·python
独自归家的兔9 小时前
OCPP 1.6 协议详解:StatusNotification 状态通知指令
开发语言·数据库·spring boot·物联网