C++ 光速入门指南day02

2021年7月3日 8点热度 0条评论 来源: __Miracle__

运算符

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

#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() { 
   #include <iostream>
using namespace std;
int main (){ 
    int a = 10, b = 3, c = 3;
    cout<< !a <<endl;
    cout<< !0 <<endl;
    cout<< (a && b)<<endl;
    cout<< (a == b && b==c)<<endl;
    cout<< (b == c && a == b)<<endl;
    cout<< (b == c && c == b)<<endl;
    cout<< (a == b || b==c)<<endl;
}

}

程序流程结构

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,请分别输⼊三只⼩猪的体重,并且判断哪只⼩猪最重?

#include <iostream>
using namespace std;


int main(){ 
    int piga;
    int pigb;
    int pigc;
    int res = 0; // 最终的那个
    cout << "weights of three pigs" << endl;
    cin >> piga;
    cin >> pigb;
    cin >> pigc;
    if (piga >= pigb){ 
        res = piga;
    }else{ 
        res = pigb;
    }
    if (res <= pigc){ 
        res = pigc;
    }
    cout<< "max weight = "<< res<< endl;
}

作业2:BMI计算器


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

#include <iostream>
using namespace std;

int main(){ 
    double weight;
    double height;
    double BMI;
    cout << "input your weight" << endl;
    cin>>weight;
    cout << "input your height" << endl;
    cin>>height;
    BMI = weight/(height*height);
    if(BMI <= 18.4){ 
        cout << "thin and your BMI is " << BMI << endl;
    }else if(BMI > 18.4 && BMI <= 23.9){ 
        cout << "normal and your BMI is " << BMI << endl;
    }else if(BMI > 23.9 && BMI <=27.9){ 
        cout << "overweight and your BMI is " << BMI << endl;
    }else if(BMI > 27.9 ){ 
        cout << "fat and your BMI is " << BMI << endl;
    }else { 
        cout << "follow the instructions" << endl;
    }
}

    原文作者:__Miracle__
    原文地址: https://blog.csdn.net/qq_39276337/article/details/117488358
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。