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";

}

}

相关推荐
唐青枫19 小时前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech1 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf3 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6253 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#
Artech3 天前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf
LDR0064 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术4 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园4 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob4 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享4 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm