[Day5]C# 鸡础观念- 让变数学会七十二变的高手~运算子

运算子/images/emoticon/emoticon14.gif

程序的世界中,变数是无时无刻一直在变化的,
变数的变化也成为程序的精随所在,
但为甚麽变数会一直变化呢?
原来一切都是运算子惹的祸。

算术运算子/images/emoticon/emoticon26.gif

  • 在程序之中运算子会连接两个物件,并将这两个物件在指定的运算

C#之中的算术运算子

运算子 用途 范例 类别
+ a + b 二元
- a - b 二元
* a * b 二元
/ a / b 二元
% 取余数 a % b 二元

范例:使用基姆拉尔森计算公式,来算出今天礼拜几

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告今年
            int y = 2021;
            //宣告今月
            int m = 9;
            //宣告今日
            int d = 6;

            //套用基姆拉尔森计算公式
            int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7;

            //印出日期与星期
            Console.WriteLine("2021/9/6 星期" + week);

            Console.ReadKey();
        }

    }
}

结果:

2021/9/6 星期1

在这公式中我们一口气用到了五个运算子,也体会到C#让人十分惊艳的运算

特别的除法

  • 在C#中的除法肯定跟你想像的有所差异
  • 在整数的除法中,小数点之後会自动被舍去

范例:试试看用3除以2吧(在我们观念中应该答案是1.5)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //做三除以二的运算
            int ans = 3 / 2;

            //印出解答
            Console.WriteLine("Ans : " + ans);
            Console.ReadKey();
        }

    }
}

结果:

Ans : 1

  • 由上我们可以得知整数除法小数点後会直接被向0无条件舍去
  • 除非做浮点数除法,结果才会变成浮点数

范例:验证除法後再赋值给浮点数变数与直接做浮点数除法的差异

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //做整数除法,并赋值给浮点数变数
            double x = 9 / 2;

            //直接做浮点数除法
            double y = 9.0 / 2;

            //印出答案
            Console.WriteLine("x = " + x + " ,y = " + y);
            Console.ReadKey();
        }

    }
}

结果

x = 4 ,y = 4.5

余数运算子~%/images/emoticon/emoticon07.gif

  • 使用此运算子可以得到a/b的余数
  • 与方法Math.DivRem 会得到一样的结果

范例:我们分别将20~25除以4来看看他们的余数分别是多少

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //分别将20~25与4做取余运算
            int no20 = 20 % 4;
            int no21 = 21 % 4;
            int no22 = 22 % 4;
            int no23 = 23 % 4;
            int no24 = 24 % 4;
            int no25 = 25 % 4;

            //印结果
            Console.WriteLine("20 % 4 =" + no20);
            Console.WriteLine("21 % 4 =" + no21);
            Console.WriteLine("22 % 4 =" + no22);
            Console.WriteLine("23 % 4 =" + no23);
            Console.WriteLine("24 % 4 =" + no24);
            Console.WriteLine("25 % 4 =" + no25);

            Console.ReadKey();
        }
    }
}

结果:

20 % 4 =0
21 % 4 =1
22 % 4 =2
23 % 4 =3
24 % 4 =0
25 % 4 =1

方法(Math)/images/emoticon/emoticon78.gif

  • C#也有提供我们很多数学方法
  • 我们来介绍几个比较常用的方法
功能 方法 范例 数学表示
乘幂 Math.Pow() Math.Pow(2,3) 2^3
平方根 Math.Sqrt() Math.Sqrt(4) √4
绝对值 Math.Abs() Math.Abs(-4) |-4|
  • 另外方法中还为我们定义三个栏位
数学涵义 程序表示 近似值 说明
τ Math.Tau 6.2831853071795862 弧度
π Math.PI 3.1415926535897931 圆周率
e Math.E 2.7182818284590451 自然对数底数

随堂小练习/images/emoticon/emoticon03.gif

题目:已知梯形面积公式为((上底+下底)X高/2),目前知道上底是30,下底是52,高为46

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告上底
            int x = 30;
            //宣告下底
            int y = 52;
            //宣告高
            int z = 46;

            //进行计算
            int ans = (x + y) * z / 2;

            //印出答案 
            Console.WriteLine("Ans : " + ans);

            Console.ReadKey();
        }
    }
}

结果:

Ans : 1886

递增运算子~++/images/emoticon/emoticon81.gif

  • 他是属於一元运算子
  • 增加的量为1

范例1(後置递增运算子):我们来算算我今年48岁,明年我几岁?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告我今年48岁
            int age = 48;
            Console.WriteLine("我今年" + age + "岁");

            //使用後置递增运算子,计算明年年纪
            age++;
            Console.WriteLine("我明年" + age + "岁");

            Console.ReadKey();
        }
    }
}

结果:

我今年48岁
我明年49岁

范例2(前置递增运算子):我们来比较看看前置跟後置的差异吧

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //後置递增运算子,印出它的变化过程
            Console.WriteLine("--後置运算子--");
            int x = 1;
            Console.WriteLine(x);
            Console.WriteLine(x++);
            Console.WriteLine(x);

            //前置递增运算子,印出它的变化过程
            Console.WriteLine("--前置运算子--");
            int y = 1;
            Console.WriteLine(y);
            Console.WriteLine(++y);
            Console.WriteLine(y);

            Console.ReadKey();
        }
    }
}

结果:

--後置运算子--
1
1
2
--前置运算子--
1
2
2

由此我们可以看到前置跟後置的差异会是,在运算「之後」或「之前」某数的值

既然有递增那必然有递减运算子~--

范例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //递减运算子
            int x = 5;
            //後置
            Console.WriteLine(x--);
            //前置
            Console.WriteLine(--x);

            Console.ReadKey();
        }
    }
 }

结果:

5
3

大家可能会觉得,这结果也太奇怪了吧?!
为什麽不是5,4呢?
原因很简单,因为我们先用後置递减运算子,所以他是在输出5之後,x变成4了,
接下来使用前置递减运算子,所以4先被减成3,然後才输出

指派运算子/images/emoticon/emoticon37.gif

功能 范例 等值写法 类别
+= x += 100 x = x + 100 二元
-= x -= 100 x = x - 100 二元
*= x *= 100 x = x * 100 二元
/= x /= 100 x = x / 100 二元
%= x %= 100 x = x % 100 二元

范例:来试试看加法指派运算子 +=

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //加法指派运算子 += (整数版)
            Console.WriteLine("--整数版--");
            int x = 10;
            x += 100;
            Console.WriteLine(x);

            //加法指派运算子 += (字串版)
            Console.WriteLine("--字串版--");
            string name = "孤独一只鸡";
            name += " 史上最帅!";
            Console.WriteLine(name);

            Console.ReadKey();
        }
    }
 }

结果:

--整数版--
110
--字串版--
孤独一只鸡 史上最帅!

优先等级/images/emoticon/emoticon66.gif

  • 小时候学数学时,常常会说先乘除後加减,但这麽多运算子中到底优先顺序是什麽呢?
  • 虽然有优先顺序这东西,但实际上程序撰写时,还是建议各位搭配()做使用,这样能增加程序的可读性
    https://ithelp.ithome.com.tw/upload/images/20210905/20097001WtB02pvpAV.png

当大家学会这些运算之後呢?就可以试着去练习做一些简易的加减乘除啦!

那我们今天的运算子故事就到这边了,大家明天见/images/emoticon/emoticon73.gif


<<:  #6 CSS Table x Stock Price

>>:  Day 04 | 渲染元件

[Day 2] 阿嬷都看得懂的前端与後端怎麽分

阿嬷都看得懂的前端与後端怎麽分 首先,准备一个阿嬷-民明书房《阿嬷的古早味卤肉饭怎麽煮》 阿嬷的乖孙...

入门魔法 - 常用阵列方法(二)find、findIndex

前情提要 艾草:「不知不觉也累积了不少魔力总量了,我们今天透过魔法阵列来找出你适合的属性值吧!」 「...

Day 2 Flutter介绍

Flutter架构 (一)Framework:由纯Dart语言实现的SDK 1.底下两层:底层UI函...

Day 06:小孩子才做选择-BootstrapVue 全部引入

本篇开始终於要进入解决需求的前置作业了!首先需要让网站有个基本的置顶导览列,让我们有请 Bootst...

Day 22:Ansible

今天来讲讲 Ansible。记得我在第二天的时候曾经讲过,自动化在 SRE 里面是很重要的一环。让机...