C++入门基础05:表达式(表达式基础、算术运算符与赋值运算符、逻辑关系运算符、成员访问运算符与条件运算符、位运算符、移位运算符与类型转换)

C++入门基础05:表达式(表达式基础、算术运算符与赋值运算符、逻辑关系运算符、成员访问运算符与条件运算符、位运算符、移位运算符与类型转换)

一、表达式基础

#include <iostream>
//系统定义头文件一般是尖括号
#include<fstream>
#include<string>
using namespace std;

struct Demo {
    int num1 = 5;
    int num2 = 10;
};

//表达式基础
int main()
{
    //表达式
    //3+2  a+b  3*4+2

    //运算符求值顺序以及优先级
    //3*4+2   (3+5)*4+2 = == ++ -- .
    Demo demo;
    cout << demo.num1 + 5 + demo.num2 * 4 << endl;
    // 取成员的优先级比 + 和 * 要高。
    // 5+5+40 = 50

    //不同类型对象参与运算的对象转换
    float a = 10.8f;
    int b = 5;
    //先将int转为float,再参与计算
    cout << a + b << endl;
    
    //括号无视优先级
    //不知道优先级可以用括号来解决
    if ((a == b) || (a > 0)) {
        cout << "if" << endl;
    }
    
    return 0;
}

在这里插入图片描述

运算符优先级:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、算术运算符与赋值运算符

#include <iostream>
//系统定义头文件一般是尖括号
#include<fstream>
#include<string>
using namespace std;

//表达式基础
int main()
{

    //算术运算符
    // + - * / % (求余/取模)
    cout<< 3+5 << endl;
    cout<< 10-2 << endl;
    cout<< 6*8 << endl;
    cout<< 9/3 << endl;
    cout<< 10%3<< endl;
    cout << "\n" << endl;

    //赋值运算符
    // = += -= *= /= %=
    // 赋值 先加再赋值 先减再赋值 先乘再赋值 先除再赋值 求余再赋值(求余计算必须是整数)
    int a;
    a = 10;
    cout << a << endl;
    a += 5; // a = a + 5;
    cout << a << endl;
    //其他同理....
    a %= 3;
    cout << a << endl;
    cout << "\n" << endl;

    //自增/自减运算符
    //++ -- 
    a++; //a = a +1
    cout << a << endl;
    a--; //a = a -1
    cout << a << endl;
    cout << "\n" << endl;

    int b = 6;
    cout << b << endl;
    cout << b++ << endl; //先将b赋给表达式后,再做自增运算。(输出的是表达式的结果)
    cout << b << endl;
    cout << ++b << endl; //先进行自增运算,再将结果赋给表达式。(输出的是表达式的结果)
    cout << b << endl;

    return 0;
}

在这里插入图片描述

三、逻辑关系运算符

#include <iostream>
//系统定义头文件一般是尖括号
#include<fstream>
#include<string>
using namespace std;

//表达式基础
int main()
{   
    //逻辑运算符
    //!逻辑非
    //&& 逻辑与
    //|| 逻辑或
    int a = 9;
    int b = -2;
    if (a > 0 && b > 0)
        cout << "(a > 0 && b > 0):"<< (a > 0 && b > 0) << endl;
    if(a > 0 || b > 0)
        cout << "(a > 0 || b > 0):" << (a > 0 || b > 0) << endl;

    bool c = true;
    if (!c)
        cout << " c的值是假的,就是false" << endl;

    cout << "\n" << endl;

    //关系运算符
    //>> = <<= == !=
    cout << "a>b:" << (a > b) << endl;
    cout << "a>=b:" << (a >= b) << endl;
    cout << "a<b:" << (a < b) << endl;
    cout << "a<=b:" << (a <= b) << endl;
    cout << "a!=b:" << (a != b) << endl;
    cout << "a==b:" << (a == b) << endl;

    // == 与=
    if (a == 0)
        cout << "a等于1" << endl;

    return 0;
}

在这里插入图片描述

四、成员访问运算符与条件运算符

#include <iostream>
//系统定义头文件一般是尖括号
#include<fstream>
#include<string>
using namespace std;

struct Student {
    string name = "zhangsan";
    int age = 12;
};

//表达式基础
int main()
{   
    //成员访问运算符
    //. ->
    Student* stu = new Student();
    //通过指针访问成员。
    cout << stu->name << "," << stu->age << endl;
    //这里可以解引用,通过点的方式访问。
    cout << (*stu).name << "," << (*stu).age << endl;

    //条件运算符
    //?:
    //(条件表达式) ? (满足条件执行第一条) : (不满足执行第二条)
    int a = 10;
    int b = 5;
    int c;
    a > b ? (c = a) : (c = b);//求较大值
    a < b ? (c = a) : (c = b);//求较小值
    cout << "c:" << c << endl; 

    //单目运算符: ++ --
    //双目运算符: + - * / %
    //三目运算符: 条件运算符
    return 0;
}

在这里插入图片描述

五、位运算符、移位运算符与类型转换

1、位运算符

为运算符:&(按位与); |(按位或); ^(按位异或);~(非/按位取反)。
&(按位与):只要有0,就是0。
|(按位或):只要有1,就是1。
^(按位异或):相同为0,相异为1。
~(非/按位取反):0取反为1,1取反为0。

13的二进制是=>1101
数字在计算机中存储是按照二进制进行存储的,一个数按照8位进行存储的话,最高位是一个符号位,表示正数还是负数,那么对于13这个数,是一个整数,那么二进制存储就是00001101,数字在计算机中是以补码的形式进行存储的,对于正数来说,原码和补码是一样的,但是对于负数来说,原码等于补码取反再加1(比如11110010,该二进制数的原码是这个11110010先减1再取反,11110010减1为11110001,再取反(最高位不取反)10001110,原码结果为10001110,为-14)。
原码:00001101 =>13
补码:00001101
00001101 取反=> 11110010 负数补码求原码=> 补码减1再取反=> 10001110=> -14
补码:11110010
原码:10001110 => -14
在这里插入图片描述

#include <iostream>
//系统定义头文件一般是尖括号
#include<fstream>
#include<string>
using namespace std;

//位运算符与类型转换
int main()
{   
    //位运算符(针对整数)
    //为运算符:&(按位与) |(按位或) ^(按位异或)~(非/按位取反); 关系运算符(对应的是表达式):&& || !
    //整数的二进制表示
    //二进制:十进制=>2:10;3:11;4:100...
    int a = 13; //13 = 8+4+1=> 1101
    int b = 6; // 6 =4+2 =>0110
    cout << "按位与:" << (a & b) << endl;
    cout << "按位或:" << (a | b) << endl;
    cout << "按位异或:" << (a ^ b) << endl;
    cout << "按位取反:" << ~a << endl;

    return 0;
}

在这里插入图片描述

2、移位运算符

移位运算很多时候在编程中是很有用的一个操作。

13 = 8+4+1=> 1101 左移2位,就是110100 = 52。
左移就是乘以2,左移几位就是乘以几次2。13 *2 *2 = 52。
6 =4+2 =>0110 右移2位,就是01 = 1。
右移就是除以2,右移几位就是除以几次2。6 /2 /2 = 1。
在这里插入图片描述

#include <iostream>
//系统定义头文件一般是尖括号
#include<fstream>
#include<string>
using namespace std;

//位运算符与类型转换
int main()
{   
    //位运算符(针对整数)
    //为运算符:&(按位与) |(按位或) ^(按位异或)~(非/按位取反); 关系运算符(对应的是表达式):&& || !
    //整数的二进制表示
    //二进制:十进制=>2:10;3:11;4:100...
    int a = 13; //13 = 8+4+1=> 1101
    int b = 6; // 6 =4+2 =>0110

    //移位运算符
    //<<(左移)>>(右移)
    cout << "a左移2位:"<< (a << 2) << endl;
    cout << "b右移2位:" << (b >> 2) << endl;

    return 0;
}

在这里插入图片描述

3、类型转换(显式转换与隐式转换)

显式转换 与 隐式转换

#include <iostream>
//系统定义头文件一般是尖括号
#include<fstream>
#include<string>
using namespace std;

int main()
{   
    //类型转换
    //显式转换 与 隐式转换
    //隐式转换:(可能损失精度)
    //short->int
    //bool:0 1 (非0的数转为1)
    int c = 3.14;
    cout << "c:" << c << endl;

    //显式转换:
    //转换运算函数
    //static_cast const_cast

    return 0;
}

在这里插入图片描述

六、代码经验分享

#include <iostream>
//系统定义头文件一般是尖括号
#include<fstream>
#include<string>
using namespace std;

struct Student
{
    string name = "zhangsan";
    int age = 10;
    string address = "beijing";
};

int main()
{

    //1、运算符的多重含义 & * << >> 
    int i = 5;
    int& ri = i; //&出现在定义语句里,作为声明符,表示引用。
    cout << "引用ri: " << ri << endl;

    int* p; //*出现在定义语句里,作为声明符,表示指针。
    p = &i; //&出现在赋值语句里,作为取地址符。
    cout << "i的地址:" << &i << endl;
    cout << "p:" << p << endl;
    cout << "*p解引用:" << *p << endl;

    *p = i; //*出现在赋值语句里,作为解引用。
    int& r2 = *p; //&出现在定于语句里,作为声明符,表示引用,*出现在赋值语句里,作为解引用。
    cout << "引用r2: " << r2 << endl;
    cout << "引用r2地址: " << &r2 << endl;

    cout << "i: " << i << endl; //<<输出
    i = i << 5; //<<左移,移位运算

    //2、优先级拿不准用括号

    //3、sizeof(指针)
    cout << "\n" << endl;
    char str[] = "i love study C++! ";
    //数组名是可以转换为指针的。
    //sizeof求数组大小时,并不会将数组转化为指针处理。
    cout << "sizeof(str):" << sizeof(str) << endl;

    char* str1 = new char[16]; //定义一个指针,动态分配了16个char大小的存储。
    str1 = str; //数组名是可以转换为指针的。
    //这里取的是指针本身的大小,而不是指针所指向的内存的大小。
    cout << "sizeof(str1):" << sizeof(str1) << endl;
    cout << "sizeof(p):" << sizeof(p) << endl;

    cout << "str中的字符个数:" << sizeof(str) / sizeof(char) << endl; // 求数组中元素的个数
    int d[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 
    //大括号 { } 之间的值的数目不能大于我们在数组声明时在方括号 [ ] 中指定的元素数目。
    cout << "sizeof(d):" << sizeof(d) << endl;
    cout << "d中的元素个数:" << sizeof(d) / sizeof(int) << endl;

    Student* stu = new Student();
    cout << "sizeof(stu):" << sizeof(stu) << endl;
    cout << "sizeof(*stu):" << sizeof(*stu) << endl;

    //4、避免强制类型转换,会产生错误。
    //static_cast, dynamic_cast, reinterrept_cast

    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Henrik698

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值