C#表达式和运算符

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C#表达式和运算符

Mwyldnje2003   2022-09-28 我要评论

类型转换

Convert.To类型()

1、表达式

将变量和字面值(在使用运算符时,他们都称作操作数)与运算符组合起来就得到了表达式,它是计算的基本构建
简单的操作包括所有的基本书序操作,如加减乘除;还有专门用于处理布尔值的逻辑运算以及赋值运算。
比如表达式:c=a+b

让用户输入他的语文和数学成绩,计算他的总成绩

using System;
namespace 表达式
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 5;
            int b = 10;
            int c = a + b;
            Console.WriteLine(c);


            Console.WriteLine("请输入你的语文成绩");
            double chinese = Convert.ToDouble( Console.ReadLine());   //接收用户输入(字符串),并转换成Dobule类型
            Console.WriteLine("请输入你的数学成绩");
            double math = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("您的总成绩是{0},平均成绩是{1}", chinese+math, (math+chinese)/2);  // 字符串连接方式可以用“+”
            Console.ReadKey();

        }
    }
}

1.2 运算符分类

按操作数的个数

  • 一元运算符:处理一个操作数(int a=10)
  • 二元运算符:处理两个操作数 a>b
  • 三元运算符:处理三个操作数 (?:

按运算类型:

  • 数学运算符
  • 赋值运算符
  • 关系运算符
  • 布尔原酸符
  • 位运算符 (按为取反)
  • 其他运算符(is as)

  • var1=var2++ :先用后加,var1等于var2,var2的值加1
  • var1=++var2:先加后用(va2=)

2、数学运算符

  • var2=10
  • var1=var2++ 先用后加(var1=var2=10, var2的值加一(var2=10+1))
  • var1=++var2 先加后用 (var2加一(var2=10+1),var1等于var2+1)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 数学运算符
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 5;
            int b = 3;
            int c = a / b;
            double d = a / b;
            
            double e = 5.0;
            double f = 3.0;
            double g = e / f;
            Console.WriteLine("c的值是{0},d的值是{1},g的值是{2}", c, d,g);
            Console.ReadKey();
        }
    }
}

结果:c的值是1,d的值是1,g的值是1.66666666666667

++/–

3、赋值运算符

4、关系运算符

5、布尔运算符

注:&&/||与&/|区别:&&/||可用于对数值执行操作,实际上,他们处理的是在储存在变量中的一系列位而不是变量的值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 布尔运算符
{
    class Program
    {
        static void Main(string[] args)
        {
            //&与运算符,||或运算符,!取反操作符,^异或运算符
            int a = 10;
            int b = 15;
            Console.WriteLine((10==a++)||(16==b--));
            Console.WriteLine("a的值是{0},b的值是{1}",a,b);  //输出结果是a的值是11,b的值是15
            //或运算符||前一个操作为true时,不在执行后一个操作内容,所是上面的结果
            Console.ReadKey();            
            
        }
    }
}

6、位运算符

位(bit):在计算机中存储所有数据都采用二进制,那么二进制的为,便是我们所有的bit
1Byte=8bit 1字节=8位
1k=1024Byte
1M=1024k
1G=1024M
1T=1024G

在大俗代码中都不适用这些运算符,但应知道这样的运算符存在。它们主要用于高度优化的代码,在这些代码中,使用其他数学操作的开销太高。因此它们通常用于驱动程序或系统代码

~ 按位取反
&与运算
|或运算
^异或
<<左移
>>右移

6.1 &按位与运算

6.2 或|按位运算

6.3 异或^按位运算符

6.4按位取反~按位预算符

6.5 左移<<运算符

var3=var1<<var2 var1向左移动var2位,将所得的值付给var3

6.6右移

var3=var1>>var2 var1向右移动var2位,将所得的值付给var3

左移一位,相当于乘以2;
右移一位,当当与除以2;然后去除非整数部分

7、其他运算符

7.1 字符连接运算符+

将两个字符串连接在一起组成一个新的字符串
备注:C#中“+”共三种用法

  • 数学算数运算符 var1=var2+var3
  • 乘以正1 var1 =+var2 (var1=var2*(+1))
  • 字符串连接 str1=“my name is” str2=“darly” str3=str1+" "+str2,则str3=“my name is darly” 7.2 is运算符

用于动态检查对象的运行时类型是否与给定类型兼容

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

namespace 其他运算符
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10;
            double b = 3.14;
            string c = "it is a string";
            bool mybool = false;
            mybool = a is int;
            Console.WriteLine("a is int\t"+mybool);   // \t是跳转到下一个制表符
            mybool = b is double;
            Console.WriteLine("b is doubule\t" + mybool);
            mybool = c is string;
            Console.WriteLine("c is string\t" + mybool);
            Console.ReadKey();
        }
    }
}

7.3 三元运算符

表达式1?表达式2:表达式3 表达式1为true则结果为表达式2,否则结果为表达式3
输出语句“I have 数量 pen” 当数量为1时为pen,数量大于1时为pens

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 其他运算符
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入您拥有的钢笔数量");
            int qty = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("I have {0} pen{1}", qty,qty>1?"s":"");
            //Console.ReadKey();
            // 比较用户输入数据与5的关系
            Console.WriteLine("请您输入要比较的数字");
            int number1 = Convert.ToInt32(Console.ReadLine());
            string result = number1 > 5 ? "大于等于" : "小于";
            Console.WriteLine("您输入的数字{0}5",result);
            Console.ReadKey();
        }
    }
}

8、运算优先级

总结练习

编程实现1532855秒是几天几小时几分钟几秒

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 总结练习
{
    class Program
    {
        static void Main(string[] args)
        {
            // 方法1,利用取整再取余的方式逐级获取响应的单位值
            Console.WriteLine("请输入你要计算的秒数");
            int day = 24 * 60 * 60;
            int hour = 60 * 60;
            //int total = 1232855;
            int total = Convert.ToInt32(Console.ReadLine());
            int days = total / day;    //计算天数   利用总秒数对一天一共多少秒取整,得到天数
            int hours = (total % day) / hour; // 利用总秒数对一天一共多少秒取余数,得到不够一天的秒数,然后对一小时包含秒数整,得到小时数;
            int minutes = (total % day % hour)/60; //同样思路,利用两次取余得到不足一小时的数量,再对60秒取整得到分钟数
            int minutes2 = total % 3600 / 60;  //计算分钟的方式2,总秒数对一小时取余得到不足一小时的秒数然后取整60秒得到分钟数
            int second = total % 60; //用总数对60秒取余得到不足一分钟的秒数,就是秒

            Console.WriteLine("{0}天{1}小时{2}分钟{3}秒",days,hours,minutes,second);
            Console.WriteLine(minutes2+"分钟");
            Console.ReadLine();

        }
    }
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们