huyuhang-c++-day02

字符型 (char型)

作用: 字符型变量⽤于显⽰单个字符

  • C和C++中字符型变量只占⽤1个字节
  • 字符型变量并不是把字符本⾝放到内存中存储,⽽是将对应的ASCII编码放⼊到存储单元

ASCII 码⼤致由以下两部分组成:

  • ASCII ⾮打印控制字符: ASCII 表上的数字 0-31 分配给了控制字符,⽤于控制像打印机等⼀些外围 设备。
  • ASCII 打印字符:数字 32-126 分配给了能在键盘上找到的字符,当查看或打印⽂档时就会出现
    在这里插入图片描述

转义字符

作⽤:⽤于表⽰⼀些不能显⽰出来的ASCII字符
现阶段我们常⽤的转义字符有: \n \ \t

#include <iostream>
using namespace std;
// 转义字符
int main(){
    // \本身代表转义的意思, 他已经不是本身\的意思了
    cout<< "\\" << endl; // 只输出一个斜杠
    //    换行
    cout<< "hello\nhaha" << endl; // 只输出一个斜杠
    // 等价于tab键
    cout<< "hello\thaha" << endl; // 只输出一个斜杠
}

字符串型

作⽤:⽤于表⽰⼀串字符
两种风格

  1. C风格字符串 : char 变量名[] = “字符串值”
#include <iostream>
using namespace std;
// 字符串
int main(){
    char str1[] = "hello world";
    cout<< str1 << endl;
}
  1. C++风格字符串 : string 变量名 = “字符串值”
#include <iostream>
#include <string>
using namespace std;
// 字符串
int main(){
    char str1[] = "hello world";
    cout<< str1 << endl;
    string str = "hello world";
    cout<< str << endl;
}

c++风格字符串需要引入头文件#include <string>

布尔数据类型 bool

布尔数据类型代表真或假的值
布尔数据类型代表真或假的值
bool类型只有两个值:

  • true — 真(本质是1)
  • false — 假(本质是0)
    bool类型占1个字节⼤⼩
#include <iostream>
using namespace std;

int main(){
    bool flag = true;
    cout<< flag << endl;// 1
    flag = false;
    cout<< flag << endl;// 0
    cout<< "bool size" <<sizeof(flag)<< endl;// 1
}

console输入

cin >> 变量

#include <iostream>
using namespace std;

int main(){
    // 整型输入
    int a;
    cout << "please input a number "<<endl;
    cin>>a;
    cout << "a = "<< a << endl;

    // 浮点型输入
    double d;
    cout << "please input double number "<<endl;
    cin>>d;
    cout << "d = "<< d << endl;

    // 字符型输入
    char ch;
    cout << "please input a char "<<endl;
    cin>>ch;
    cout << "ch = "<< ch << endl;

    // 字符串型输入
    string str;
    cout << "please input a string "<<endl;
    cin>>str;
    cout << "str = "<< str << endl;

    // 布尔型输入
    bool flag;
    cout << "please input a bool value "<<endl;
    cin>>flag;
    cout << "flag = "<< flag << endl;
}

image.png

运算符

算术运算符: 四则运算
赋值运算符: 将表达式的值赋给变量
比较运算符: 表达式的比较, 返回布尔值
逻辑运算符: 根据表达式的值返回真或者假

#include <iostream>
using namespace std;
// 运算符示例
int main() {
    int a1 = 10;
    int b1 = 3;

    cout<< a1 + b1 << endl;
    cout<< a1 - b1 << endl;
    cout<< a1 * b1 << endl;
    cout<< a1 / b1 << endl; // 两个整数相除整除结果依然是整数
    // 除数不能为 0
//    b1 = 0;
//    cout<<" a1 / b1 = "<< a1 / b1 << endl; // 会有错误

    int a2 = 10;
    int b2 = 20;
    cout<< a2 / b2 << endl; // 0
    // 小数相除
    double d1 = 0.5;
    double d2 = 0.25;
    double res = d1/d2;
    cout<<"d1/d2 = " <<res<< endl;
}


取模

#include <iostream>
using namespace std;
// 运算符示例  取模
int main() {
    int a1 = 10;
    int b1 = 3;
    cout<< a1 % b1 << endl; // 1
    // 除数不能为 0
//    b1 = 0;
//    cout<<" a1 / b1 = "<< a1 / b1 << endl; // 会有错误
    // 取模除数也不能为0
    int a2 = 10;
    int b2 = 20;
    cout<< a2 % b2 << endl; // 10
    // 小数不能取模
//    double d1 = 0.5;
//    double d2 = 0.25;
//    double res = d1%d2;
//    cout<<"d1/d2 = " <<res<< endl;
}

自增运算

#include <iostream>
using namespace std;
// 自增运算符
int main() {
    // 后置递增
    int a = 10;
    a++; // 等价于 a = a + 1
    cout << "a = " << a << endl;
    // 前置递增
    int b = 10;
    ++b; //
    cout << "b = " << b << endl;
    //  区别
    // 前置递增是先对变量进行++, 然后再参与计算表达式
    int a2 = 10;
    int b2 = ++a2*10;
    cout << "b2 = " << b2 << endl;
    cout << "a2 = " << a2 << endl;

    // 后置递增是先参与计算表达式, 然后再对变量进行++
    int a3 = 10;
    int b3 = a3++*10;
    cout << "b3 = " << b3 << endl;
    cout << "a3 = " << a3 << endl;

}

赋值运算符

#include <iostream>

using namespace std;

// 赋值运算符
int main() {
    //
    int a = 10;
    cout << "a = " << a << endl;
    a = 100;
    a += 2; // a = a + 2
    cout << "a = " << a << endl;
    a -= 2;
    cout << "a = " << a << endl;
    a *= 2;
    cout << "a = " << a << endl;
    a /= 2;
    cout << "a = " << a << endl;
    a %= 2;
    cout << "a = " << a << endl;
}

比较运算符

#include <iostream>
using namespace std;
// 比较运算符
int main() {
    int a = 10;
    int b = 20;
    cout <<(a == b) << endl; // 0 false
    cout <<(a != b) << endl; // 1 true
    cout <<(a > b) << endl; // 0 false
    cout <<(a < b) << endl; // 1 true
    cout <<(a <= b) << endl; // 1 true
    cout <<(a >= b) << endl; // 0 false
    int res = (a == b);
    cout <<"res = "<< res << endl; // 0 false
    bool flag = (a == b);
    cout <<"flag = "<< flag << endl; // flag = 0
    //    在c++/c 中比较运算 真 用 1 表示 假用 0 表示

}

逻辑运算符

#include <iostream>

using namespace std;

// 逻辑运算符
int main() {
    // 非
    int a = 100;
    cout << !a << endl; // 0
    cout << !!a << endl; // 1
    // 与
    int b = 10;
    int c = 10;
    cout << (b && c) << endl; // 1
    // 或
    int d = 0;
    cout << (d || c) << endl; // 1

}

image.png

程序流程结构

C/C++⽀持最基本的三种程序运⾏结构:顺序结构、选择结构、循环结构

  • 顺序结构:程序按顺序执⾏,不发⽣跳转
  • 选择结构:依据条件是否满⾜,有选择的执⾏相应功能
  • 循环结构:依据条件是否满⾜,循环多次执⾏某段代码
    选择结构
单行if
if(条件){
 条件为真时执行的事情
}

栗子1

#include <iostream>
using namespace std;
// 选择结构
int main() {
    int score;
    cout<< "please input your score"<< endl;
    cin >> score;
    cout<< "your score is "<< score<< endl;
    if (score >= 600){
        cout<< "your score is very good "<< endl;
    }
}

if(条件){
 条件为真时执行的事情
}else{
 不满足条件为真时执行的事情
}
#include <iostream>
using namespace std;
// 成绩level显示器
int main() {
    int score;
    cout<< "please input your score"<< endl;
    cin >> score;
    cout<< "your score is "<< score<< endl;
    if (score >= 90 && score <= 100){
        cout<< "your score level is A "<< endl;
    }else if (score >= 80 && score < 90){
        cout<< "your score level is B "<< endl;

    }else if (score >= 60 && score < 80){
        cout<< "your score level is C "<< endl;

    }else if (score >= 0 && score < 60){
        cout<< "your score level is D "<< endl;
    }else{
        cout<< "your score is error "<< endl;
    }
}

作业1:三只小猪称重
有三只⼩猪ABC,请分别输⼊三只⼩猪的体重,并且判断哪只⼩猪最重?

作业2:BMI计算器


计算公式为:BMI=体重÷身高^2。(体重单位:千克;身高单位:米。)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值