【C++编程入门】2022.8.3日讲给光明社区的小朋友

引例

等差数列: 是指从第二项起,每一项与它的前一项的差等于同一个常数的一种数列。

//等差数列!
#include<iostream>
using namespace std;
int main()
{
    int a1,a2,a3,a4,a5;
    //进行变量的定义声明!如果变量没有定义声明的话程序会报错而无法运行
    //讲一下输入输出:cout 是输出! cin 是输入!
    cout << "请输入5个整数字" << endl;
    cout << "请输入第1个数字:" << endl;
    cin >> a1;
    cout << "请输入第2个数字:" << endl;
    cin >> a2;
    cout << "请输入第3个数字:" << endl;
    cin >> a3;
    cout << "请输入第4个数字:" << endl;
    cin >> a4;
    cout << "请输入第5个数字:" << endl;
    cin >> a5;
    //分支语句
    //简单的条件判断语句!如果......其他......
    if(a2-a1 == a3-a2 && a4-a3 == a3-a2 && a5-a4 == a4-a3)
    //注意! =是赋值运算符  ==才是比较运算符
    {
        cout << "该数列的第6个数字是:" << a5+a5-a4;
    }
    else
    {
        cout << "您输入发不是等差数列,请重新输入!";
    }
    return 0;
}

运行结果:
在这里插入图片描述

C++程序的基本框架

#include<iostream>
using namespace std;
int main()
{

    return 0;
}
#include<iostream>      :    头文件————说明要带哪些工具      include:包含      iostream:输入输出信息流
using namespace std     :    命名空间————说明要去哪里执行任务    use:使用      name:名字      space:空间      std(standard) :标准
int main()              :主程序————描述主要的指令
{

    return 0;
}

输出cout

#include<iostream>
using namespace std;
int main()
{
    cout << "今天我上了张老师的C++编程课" << endl;
    return 0;
}

运行结果如图所示:
在这里插入图片描述

输入cin

#include<iostream>
using namespace std;
int main()
{
    int t;//注意声明变量!
    cin >> t;
    cout << "今年张老师已经上班的天数为:";
    cout << t << endl;
    return 0;
}

运行结果如图所示:
在这里插入图片描述

变量

使用变量前必须定义好变量的类型,比如整数型(int)、字符型(char)、布尔型(bool)等

#include<iostream>
using namespace std;
int main()
{
    int age,height,grade;//此处定义了三个变量
    cout << "你今年几岁了?" << endl;
    cin >> age;
    cout << "你今年有多少厘米高?" << endl;
    cin >> height;
    cout << "你今年几年级?" << endl;
    cin >> grade;

    cout << "xxx同学今年" << age << "岁啦。" <<endl;
    cout << "他今年有" << height << "厘米高了。" <<endl;
    cout << "他今年" << grade << "年级啦。" <<endl;
    return 0;
}

运行结果如图所示:
在这里插入图片描述

不一样的=

在数学里:“=”表示左右两边相等。
在编程里:“=”表示把右边的值赋值给左边的变量。

例题1:学校要给每个班级发放口罩,现在需要统计每班人数,和每个同学发放几个口罩,以此来算出每个班总共需要几个口罩。

#include<iostream>
using namespace std;
int main()
{
    int number1,number2,all;
    //number1:班级的人数
    //number2:每人发放的口罩数
    //all:班级需要的口罩总数
    cout << "请输入你们班级的人数:" <<endl;
    cin >> number1;
    cout << "请输入每个人发几个口罩:" <<endl;
    cin >> number2;
    all = number1 * number2;
    cout << "你们班需要的口罩总数是:" <<all;
    return 0;
}

运行结果如图所示:
在这里插入图片描述

例题2:张老师在家隔离期间,每过一天就在墙壁上画一条杠来记日子。现在需要在程序里也设置一个变量,每天往这个变量里加一,最后可以统计张老师被隔离了多少天。

#include<iostream>
using namespace std;
int main()
{
    int t;
    t=0;
    t=t+1;
    t=t+1;
    t=t+1;
    t=t+1;
    cout << "今天是张老师隔离的第" << t << "天";
    return 0;
}

运行结果如图所示:
在这里插入图片描述

加减乘除

#include<iostream>
using namespace std;
int main()
{
    int a,b,t;
    cout << "请输入第1个数:" <<endl;
    cin >> a;
    cout << "请输入第2个数:" <<endl;
    cin >> b;
    t=a+b;
    cout << "两数相加运算如下:" <<endl;
    cout << a << "+" << b << "=" << t;
    return 0;
}

运行结果如图所示:
在这里插入图片描述

#include<iostream>
using namespace std;
int main()
{
    int a,b,t;
    cout << "请输入第1个数:" <<endl;
    cin >> a;
    cout << "请输入第2个数:" <<endl;
    cin >> b;
    t=a-b;
    cout << "两数相减运算如下:" <<endl;
    cout << a << "-" << b << "=" << t;
    return 0;
}

运行结果如图所示:
在这里插入图片描述

#include<iostream>
using namespace std;
int main()
{
    int a,b,t;
    cout << "请输入第1个数:" <<endl;
    cin >> a;
    cout << "请输入第2个数:" <<endl;
    cin >> b;
    t=a*b;
    cout << "两数相乘运算如下:" <<endl;
    cout << a << "*" << b << "=" << t;
    return 0;
}

运行结果如图所示:
在这里插入图片描述

#include<iostream>
using namespace std;
int main()
{
    int a,b,t;
    cout << "请输入第1个数:" <<endl;
    cin >> a;
    cout << "请输入第2个数:" <<endl;
    cin >> b;
    t=a/b;
    cout << "两数相除运算如下:" <<endl;
    cout << a << "/" << b << "=" << t;
    return 0;
}

运行结果如图所示:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值