2022.9.18从零开始学习c++的第二天。

2022.9.18从零开始学习c++的第二天。,第1张

算数运算符

#include
using namespace std;
/*
 算术运算符: + - * / % ++ --
 赋值运算符: = += -= *= /= %=
 比较运算符: == != < > <= >=
 逻辑运算符: ! && ||
*/

int main16() {
    //加减乘除
    int a1 = 10;
    int b1 = 4;
    cout << a1 + b1 << endl;
    cout << a1 - b1 << endl;
    cout << b1 - a1 << endl;
    cout << a1 / b1 << endl;//两个整型相除(除数不能是0),结果依然是整型(直接舍去小数点后面的数字)
    cout << a1 % b1 << endl;//浮点型不能做取余
    double d1 = 0.5;
    double d2 = 0.22;
    cout << d1 / d2 << endl;
    
    //前置递增(先让变量加一,再进行表达式的运算),后置递增(先进行表达式的运算,后让变量加一)
    int a2  ;
    int a3 = a2 = 10;
    int b2 = ++a2 * 10;
    int b3 = a3++ * 10;
    cout << "b2=" << b2 << endl;
    cout << "a2=" << a2 << endl;
    cout << "b3=" << b3 << endl;
    cout << "a3=" << a3 << endl;

    //赋值运算
    int c, c1, c2,c3;
    c = c1 = c2 = c3 = 100;
    c += 10;//c=c+10;
    c1 -= 10;//c=c-10;
    c2 /= 10;//c=c/10;
    c3 %= 10;//c=c%10;
    cout << "c=" << c << endl;
    cout << "c1=" << c1 << endl; 
    cout << "c2=" << c2 << endl;
    cout << "c3=" << c3 << endl;

    //比较运算符 == != < > <= >=
    int A1 = 10;
    int B1 = 20;
    cout << (A1 == B1) << endl;
    cout << (A1 != B1) << endl;
    cout << (A1 <= B1) << endl;
    cout << (A1 >= B1) << endl;

    //逻辑运算符 ! && ||
    cout << !A1 << endl;//只要不是0,其余数都为真;
    cout << !!A1 << endl;
    cout << A1 << endl;
    cout << (A1 && B1) << endl;
    cout << ((A1 > 2) && (B1 > 30)) << endl;
    cout << ((A1 > 2) || (B1 > 30)) << endl;

    system("pause");

    return 0;

}

对程序流程结构的理解

#include
using namespace std;
/*
 顺序结构:按循序执行 不发生转跳
 选择结构:依据条件选择执行
 循环结构:依据条件循环多次执行
*/
int main24() {
    int a ;
    cout << "请输入一个分数" << endl;
    cin >> a ;
    cout << "你的分数为:" << a << endl;

    if (a > 600)//if条件语句后面不能加分号(;)
    {
        cout << "恭喜你考上一本大学" << endl;
        if (a >= 700)
        {
            cout << "你能考入清华大学" << endl;
        }
        else if (a >= 680)
        {
            cout << "你能考入北京大学" << endl;
        }
        else if (a >= 650)
        {
            cout << "你能考入人民大学" << endl;
        }
    }
    else if (a >= 500) 
        {
            cout << "恭喜你考上了二本大学" << endl;
        }
    else if (a >= 400)
            {
                cout << "你考上了三本大学" << endl;
            }
    else
            {
                cout << "遗憾,你没考上本科" << endl;
            }
        
    
    system("pause");

    return 0;
}

例子:三只小猪称体重

#include
using namespace std;
int maintest1() {
    int a, b, c;
    cout << "请依次输入三只小猪的体重" << endl;
    cin >> a;
    cin >> b;
    cin >> c;
    if (a > b)
    {
        if (a > c)
        {
            cout << "第一只小猪最重,重达" << a << "kg" << endl;
            if (b > c)
            {
                cout << "第二只小猪次之,重达" << b << "kg" << endl;
                cout << "第三只小猪最轻,重达" << c << "kg" << endl;
            }
            else
            { 
                cout << "第三只小猪次之,重达" << c << "kg" << endl; 
                cout << "第二只小猪最轻,重达" << b << "kg" << endl;
            }
        }
    }
    else if (b > c)
    {
            cout << "第二只小猪最重,重达" << b << "kg" << endl;
            if (a > c)
            {
                cout << "第一只小猪次之,重达" << a << "kg" << endl;
                cout << "第三只小猪最轻,重达" << c << "kg" << endl;
            }
            else 
            {
                cout << "第三只小猪次之,重达" << c << "kg" << endl;
                cout << "第一只小猪最轻,重达" << a << "kg" << endl;
            }
     }
        else
        {
            cout << "第三只小猪最重,重达" << c << "kg" << endl;
            cout << "第二只小猪次之,重达" << b << "kg" << endl;
            cout << "第一只小猪次之,重达" << a << "kg" << endl;
         }
    
    

    system("pause");

    return 0;
}

三目运算符和switch语句

#include
using namespace std;
int main29()
{
    //三目运算符
    int a = 10;
    int b = 20;
    int c;
    c = (a > b ? a : b);//如果a>b,返回a给c,否则返回b给c。
    (a > b ? a : b) = 100;//三目运算符可以继续赋值
    cout << "c=" << c << endl;
    cout << "a=" << a << endl;
    cout << "b=" << b << endl;

    //switch语句
    //switch缺点,只能判断整型和字符型,不可以是一个区间。
    //优点,结构清晰,执行效率高。
    cout << "请给这个个电影打分" << endl;
    int score;
    cin >> score;

    switch (score)
    {
    case 10:
        cout << "你认为这个电影是经典电影" << endl;
        break;//退出当前分支
    case 9:
        cout << "你认为这个电影很好看" << endl;
        break;//退出当前分支
    case 8:
        cout << "你认为这个电影很好看" << endl;
        break;//退出当前分支
    default:
        cout << "垃圾电影" << endl;
    }
    system("pause");

    return 0;
}

while循环语句

#include
using namespace std;
//time时间随机数种子需要头文件#include
int main31()
{
    //while循环结构
    int a = 0;
    while (a < 10)
    {
        cout << a << endl;
        a++;

    }
    //猜数字游戏
    int b=rand() % 100 + 1; //生成一个(0~99)+1的随机数
    //添加时间随机数
    //srand((unsigned int)time(NULL));
    int num;
    cout << "b=" << b << endl;
    cin >> num;
    while (num != b)
    {
        cout << "很遗憾你猜错了,请重新输入" << endl;
        if (num > b)
        {
            cout << "猜测过大" << endl;
        }
        else
        {
            cout << "猜测过小"<         }
        cin >> num;
    }
    cout << "恭喜你猜对了" << endl;


    system("pause");

    return 0;
}

do while循环语句

#include
using namespace std;
int main32()
{
    //do while语句
    int a = 0;
    do
    {
        cout << a << endl;
        a++;
    
    } while (a < 10);
    //do while 和while的区别是do while会先执行循环语句再进行判断。

    //求出三位数中的所有水仙花数(一个三位数,每个位上的数字的三次幂之和等于它本身)
    int num = 100;
    int A, B, C;
    do 
    {
        A = num % 10;
        B = (num / 10) % 10;
        C = num / 100;
        if (A * A * A + B * B * B + C * C * C == num)
        {
            cout << num << "是水仙花数" << endl;
        }
        num++;
    } while (num < 1000);

    system("pause");

    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/2990725.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-23
下一篇 2022-09-23

发表评论

登录后才能评论

评论列表(0条)

保存