• C++ 语法基础课1 —— 变量、输入输出、顺序语句


    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 1819return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    android Intent(意图)
    HTML+CSS+Jquery实现北大官网所有效果
    33.JavaScript映射与集合(Map、Set)数据类型基础知识介绍与使用
    优秀的 Verilog/FPGA开源项目介绍(三十一)- OFDM
    Matplotlib双轴教程
    CentOS7 安装 kafka
    R语言使用使用as_tibble函数和names函数基于一个dataframe的列名列表创建一个新的dataframe
    Algorithm Review 6
    【华为上机真题 2022】| 差点没过
    精通Nginx(05)-http工作机制、指令和内置变量
  • 原文地址:https://blog.csdn.net/qq_42731062/article/details/125954435