码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C++ 语法基础课1 —— 变量、输入输出、顺序语句


    文章目录

    • 1. 变量的定义
    • 2. 输入输出
      • 2.1 整数的输入输出
      • 2.2 字符串的输入输出
      • 2.3 输入输出多个不同类型的变量
    • 3. 表达式
      • 3.1 整数的加减乘除四则运算
      • 3.2 浮点数(小数)运算
      • 3.3 整数变量的自增自减
      • 3.4 变量的类型转换
    • 4. 顺序语句
      • 4.1 输出第二个整数
      • 4.2 计算 (a + b) * c 的值
      • 4.3 带余除法
      • 4.4 求反三位数
      • 4.5 输出菱形
      • 5. 其他
      • 5.1 浮点数的比较
      • 5.2 A + B(scanf中间会入读空格)
      • 5.3 hello world

    1. 变量的定义

    • 变量必须先定义,才可以使用。不能重名
    • 变量定义的方式如下:
    #include
    using namespace std;
    
    int main()
    {
    	int a = 5;
    	int b = a, d = 10/2;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    类型关键字
    布尔型bool
    字符型char
    整型int
    浮点数型float
    双浮点型double

    2. 输入输出

    2.1 整数的输入输出

    #include
    using namespace std;
    
    int main()
    {
    	int a,b;
    	cin >> a >> b;
    	count << a+b << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2.2 字符串的输入输出

    #include
    #include
    
    using namespace std;
    
    int main()
    {
    	string str;
    	cin >> str;
    	cout << str;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.3 输入输出多个不同类型的变量

    #include
    #include
    using namespace std;
    
    int main()
    {
    	int a, b;
    	string str;
    	
    	cin >> a;
    	cin >> b >> str;
    
    	cout << str << "!!!" << a+b << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3. 表达式

    3.1 整数的加减乘除四则运算

    #include
    #include
    using namespace std;
    
    int main()
    {
    	int a = 6 + 3 * 4 / 2 - 2;
    	cout << a << endl;
    	int b = a * 10 + 5 / 2;
    	cout << b << endl;
    	cout << 23 * 56 - 78 / 3 << endl;
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    运算符描述实例
    +把两个操作数相加A + B 得到 30
    -操作数1减去操作数2A - B 得到 -10
    *两个操作数相乘A * B 得到 200
    /分子除以分母B / A 得到 2
    %取模运算符,整数后的余数B % A 得到 0

    3.2 浮点数(小数)运算

    #include
    #include
    using namespace std;
    
    int main()
    {
    	float x = 1.5, y = 3.2;
    	cout << x * y << ' ' << x + y << endl;
    	cout << x - y << ' ' << x / y << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3.3 整数变量的自增自减

    #include
    #include
    using namespace std;
    
    int main()
    {
    	int a = 1;
    	int b = a ++;
    	cout << a << ' '<< b << endl;
    	int c = ++ a;
    	cout << a << ' '<< c << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3.4 变量的类型转换

    #include
    #include
    using namespace std;
    
    int main()
    {
    	float x = 123.12;
    	int y = (int)x;
    	cout << x << ' ' << y << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4. 顺序语句

    4.1 输出第二个整数

    #include
    #include
    using namespace std;
    
    int main()
    {
    	int a, b, c;
    	cin >> a >> b >> c;
    	cout << b << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4.2 计算 (a + b) * c 的值

    #include
    #include
    using namespace std;
    
    int main()
    {
    	int a, b, c;
    	cin >> a >> b >> c;
    	cout << (a + b) * c << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4.3 带余除法

    #include
    #include
    using namespace std;
    
    int main()
    {
    	int a, b;
    	cin >> a >> b;
    	int c = a / b, d = a % b;
    	cout << c << ' ' << d << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    4.4 求反三位数

    #include
    #include
    using namespace std;
    
    int main()
    {
    	int n;
    	cin >> n;
    	
    	int a = n % 10;// 个位
    	n = n / 10;
    	int b = n % 10;// 十位
    	n = n / 10;
    	int c = n;// 百位
    
    	cout << a << b << c << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    4.5 输出菱形

    #include
    #include
    using namespace std;
    
    int main()
    {
    	char c;
    	cin >> c;
    	cout << "  " << c << endl;
    	cout << " " << c << c << c << endl;
    	cout << c << c << c << c << c << endl;
    	cout << "  " << c << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    5. 其他

    5.1 浮点数的比较

    #include
    #include
    using namespace std;
    
    const double eps=1e-6
    
    int main()
    {
        double x = 1.23456789;
        double a = x*x;
        double b =sqrt(a);
        
        printf("%.10f\n",b);
        if(fabs(x-b)<eps) puts("相等");
        
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    5.2 A + B(scanf中间会入读空格)

    #include
    #include
    
    using namespace std;
    
    int main()
    {
        int a,b;
        scanf("%d%d",&a,&b);
        printf("a+b=%d\na*b=%d\n",a+b,a*b);
        
        char a,b;//会读入空格,所以输入中间不能有空格
        scanf("%c%c",&a,&b);
        printf("%c %c\n",a,b);
        
        // int:%d
        // float:%f
        // double:%lf
        // char:%c
        
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    5.3 hello world

    #include
    #include
    
    using namespace std;
    
    int main()
    {
        cout << "Hello World" << endl;
        
        bool false/true;    1byte 1字节   1Byte=8bit 1字节=8比特
        char 'a','b',' ','\n';  1byte
        int -2147483648~2147483647  4byte
        float 1.23,1.23e-2,6-7有效数字  4byte
        double 15-16有效数字    8byte
        
        long long -2^63~2^63-1  8byte
        long double 18到19位
        
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    数据结构:AVL树讲解(C++)
    数据结构实战开发教程(三)线性表的本质和操作、顺序存储结构的抽象实现、顺序存储线性表的分析、数组类的创建
    基于matlab实现的光折射反射(不同界面)程序
    Mybatis一级二级缓存
    【面试题】面试官:说说你对js中的 防抖 和 节流 的理解
    link 和 @important 的区别
    Linux安装Keepalived
    百度SEO优化TDK介绍(分析下降原因并总结百度优化SEO策略)
    【 java 常用类】java 比较器
    Pycharm终端没法敲代码让我看日志重装也那样!
  • 原文地址:https://blog.csdn.net/qq_42731062/article/details/125954435
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号