C#自学05—关系运算符+逻辑运算符+bool+复合运算符(二元)

1、bool类型
在c#中我们用bool类型来描述对或者错。
bool类型的值只有两个 一个true 一个false
2、逻辑运算符
&& 逻辑与
||逻辑或
!逻辑非
有逻辑运算符连接的表达式叫做逻辑表达式
逻辑运算符两边放的一般都是关系表达式或者bool类型的值。
如:
5>3 &&true
3>5||false
!表达式
逻辑表达式的结果同样也是bool类型
在这里插入图片描述
3、复合赋值运算符
int number=10;
+= :
number+=20;
number=number+20;
-=
number-=5;
number=number-5;
=
number
=5;
number=number*5;
/=
%=

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

namespace _07逻辑运算符练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //让用户输入老苏的语文和数学成绩,输出以下判断是否正确,正确输出True,错误输出False
            //1)老苏的语文和数学成绩都大于90分
            Console.WriteLine("小苏,输入你的语文成绩");
            //string strChinese = Console.ReadLine();
            int chinese = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("小苏,请输入你的数学成绩");
            int math = Convert.ToInt32(Console.ReadLine());


            //bool b = chinese > 90 && math > 90;
            bool b = chinese > 90 || math > 90;
            Console.WriteLine(b);
            Console.ReadKey();



            //2)语文和数学有一门是大于90分的

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

namespace _08判断闰年
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("请输入要判断的年份");
            //int year = Convert.ToInt32(Console.ReadLine());
            年份能够被400整除.(2000)
            年份能够被4整除但不能被100整除.(2008)


            逻辑与的优先级要高于逻辑或
            //bool b = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);

            //Console.WriteLine(b);
            //Console.ReadKey();



          //  bool b = 5 < 3 && 5 > 3;

            bool b = 5 > 3 || 4 < 3;

        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是复数类的成员函数重载运算符的示例: ```c++ class Complex { private: double real; double imag; public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} Complex operator+(const Complex& c) const { return Complex(real + c.real, imag + c.imag); } Complex operator-(const Complex& c) const { return Complex(real - c.real, imag - c.imag); } Complex operator*(const Complex& c) const { return Complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real); } Complex operator/(const Complex& c) const { double r = c.real * c.real + c.imag * c.imag; return Complex((real * c.real + imag * c.imag) / r, (imag * c.real - real * c.imag) / r); } Complex operator+=(const Complex& c) { real += c.real; imag += c.imag; return *this; } Complex operator-=(const Complex& c) { real -= c.real; imag -= c.imag; return *this; } Complex operator*=(const Complex& c) { double r = real * c.real - imag * c.imag; double i = real * c.imag + imag * c.real; real = r; imag = i; return *this; } Complex operator/=(const Complex& c) { double r = c.real * c.real + c.imag * c.imag; double nreal = (real * c.real + imag * c.imag) / r; double nimag = (imag * c.real - real * c.imag) / r; real = nreal; imag = nimag; return *this; } bool operator==(const Complex& c) const { return real == c.real && imag == c.imag; } bool operator!=(const Complex& c) const { return !(*this == c); } }; ``` 这个示例中,复数类有四个基本的运算符:加、减、乘、除。每个运算符都返回一个新的复数对象,而不是修改当前对象。此外,还有四个复合赋值运算符(+=、-=、*=、/=),它们修改当前对象并返回它。最后,还有两个比较运算符(== 和 !=),它们比较两个复数对象的实部和虚部是否相等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值