[黑马程序员课程记录]C++基础部分1

第一节课

#include <iostream>
using namespace std;
​
int main()
{
    cout << "hello world" << endl;//输出
​
    system("pause");
​
    return 0;
}

第二节课:注释

1.单行注释 //

2.多行注释/* */

第三节课:变量

1.变量存在意义:方便我们管理内存空间

#include <iostream>
using namespace std;
​
int main()
{
    int a = 10;
    cout << "a=" << a << endl;
    system("pause");
​
    return 0;
}

第四节课:常量

1.

#include <iostream>
using namespace std;
​
#define Day 7
​
int main()
{
    //Day = 14; //错误,Day是常量,一旦修改就错
​
    cout << "一周有几天:" << Day << "天" << endl;
​
    const int month = 12;
    //month = 24;//错误,const修饰的变量也称为常量
    cout << "一年有几月:" << month << "月" << endl;
    
    system("pause");
    return 0;
}

第五节课:关键字

1.定义变量和常量时不要用关键字

第六节课:标识符起名规则

第七节课:数据类型

1.数据类型的意义是:

给变量分配一个适合的内存空间

2.语法:

数据类型 变量名 = 变量初始值

3.整型:

#include <iostream>
using namespace std;
​
int main()
{
    //短整型(-32768 ~ 32767)
    short num1 = 10;
​
    //整型
    int num2 = 100;
​
    //长整型
    long num3 = 1900;
​
    // 长长整型
    long long num4 = 1000;
​
    cout << "num1=" << num1 << endl;
    cout << "num2=" << num2 << endl;
    cout << "num3=" << num3 << endl;
    cout << "num4=" << num4 << endl;
    system("pause");
​
    return 0;
}

4. sizeof关键字

#include <iostream>
using namespace std;
​
int main()
{
    //整型:short(2) int(4)   long(4)   long long(4)
    //可以利用sizeof求数据类型占用的内存大小
    //语法:sizeof(数据类型/变量)
    short num1 = 10;
    cout << "short占用内存空间多大" << sizeof(short) << endl;
    cout << "short占用内存空间多大" << sizeof(num1) << endl;
​
    int num2 = 10;
    cout << "int占用内存空间多大" << sizeof(long) << endl;
​
    long num3 = 10;
    cout << "long占用内存空间多大" << sizeof(long) << endl;
    
    long long num4 = 10;
    cout << "long long占用内存空间多大" << sizeof(long long) << endl;
    //整型大小比较
    //short《int《=long 《=long long
    system("pause");
​
    return 0;
}

5.实型(浮点型)

作用:用于表示小数。

#include <iostream>
using namespace std;
​
int main()
{
    //单精度 float
    //双精度 double
    //默认情况下,输出一个小数,会显示6位小数
    //float后面定义数字记得加f,不然默认为double
​
    float f1 = 3.14f;
​
    cout << "f1=" << f1<<endl;
​
    double d1 = 3.14;
​
    cout << "d1=" << d1 << endl;
​
    //统计float和double占用内存空间
    cout << "float占用多少空间= " << sizeof(float) << endl;//4字节
    cout << "double占用多少空间= " << sizeof(double) << endl;//8字节
​
    //科学计数法
    float f2 = 3e2f;//3*10^2
    cout << "f2=" << f2 << endl;
​
    float f3 = 3e-2f;//3*10^-2
    cout << "f3= " << f3 << endl;
​
    system("pause");
    return  0;
}

6.字符型

#include <iostream>
using namespace std;
​
int main()
{
    //字符型变量创建方式
    char ch = 'a';
    cout << ch << endl;
​
    //字符型变量所占内存大小
    cout << sizeof(char) << endl;
​
    //字符型变量错误
    //char ch2 = "b";//要用单引号
    //char ch3 = 'abcd'//只能一个字符
    
    //字符型变量对应的ASCII编码
    cout << (int)ch << endl;//a--97  A--65
​
    system("pause");
    return 0;
}

7.转义字符

#include <iostream>
using namespace std;
​
int main()
{
    //转义字符
​
    //换行符\n
    cout << "hello world \n";//等于<<endl;
​
    //反斜杠  \\
​
    cout << "\\" << endl;
​
    //水平制表\t  8个位置   作用可以整齐输出数据
​
    cout << "aaa\thello" << endl;
    cout << "aaaaa\thello" << endl;
    cout << "aaaaaaa\thello" << endl;
​
    system("pause");
    return 0;
}

8.字符串

作用:用于表示字符

两种风格

#include <iostream>
#include <string> //字符串头文件
using namespace std;
​
int main()
{
    //C风格字符串
    //注意1:char 字符串名 []
    //注意2:等号后面要用双引号 来包含字符串
    
    char str[] = "hello";
    cout << str << endl;
​
    //C++风格字符串
    //C++要包含一个头文件
    string str2 = "hello";
    cout << str2 << endl;
​
    system("pause");
    return 0;
}

9.布尔类型 bool

#include <iostream>
using namespace std;
​
int main()
{
    //创建bool数据类型
    bool flag = true;
    cout << flag << endl;
​
    flag = false;
    cout << flag << endl;
    //true  1
    //false 0
​
    cout << sizeof(flag) << endl;
​
    system("pause");
    return 0;
}

10.数据的输入

关键词:cin

语法:cin >> 变量

#include <iostream>
#include <string>
using namespace std;
​
int main()
{
    //整型
    int a = 0;
    cout << "请给a赋值" << endl;
    cin >> a;
    cout << "a=" <<a<< endl;
    //浮点型
    float f = 3.14f;
    cout << "请给f赋值" << endl;
    cin >> f;
    cout << "f=" << f << endl;
​
    //字符型
    char d = 'b';
    cout << "请给d赋值" << endl;
    cin >> d;
    cout << "d=" << d << endl;
​
    //字符串型
    string str = "abc";
    cout << "请给str赋值" << endl;
    cin >> str;
    cout << "str=" <<str<< endl;
​
    //布尔类型
    bool flag = true;
    cout << "请给flag赋值" << endl;
    cin >> flag;   //bool类型只要不是0都是真
    cout << "flag=" << flag << endl;
​
    system("pause");
    return 0;
}

第八节课 运算符

1.类型

2.算数运算符--加减乘除

#include <iostream>
using namespace std;
​
int main()
{
    //加减乘除
    int a = 2;
    int b = 3;
    cout << a + b << endl;
    cout << a - b << endl;
    cout << a * b << endl;
    cout << a / b << endl;//两个整数相除依然是整数,小数部分自动去除
    //两个数相除,除数不能为零
​
    double c = 2.3;
    double d = 4.7;
    cout << c / d << endl;//两个小数可以相除
​
    system("pause");
    return 0;
}

3.算数运算符--取余

#include <iostream>
using namespace std;
​
int main()
{
    //取模运算的本质就是取余数
    int a = 10;
    int b = 9;
    cout << a % b << endl;
​
    int a1 = 2;
    int b1 = 5;
    cout << a1 % b1 << endl;
​
    int a2 = 19;
    int b2 = 0;
    //两个数相除,除数不能为零
    //cout << a2 % b2 << endl;
​
    
    //两个小数不能取余;C++硬性规定
    /*double c = 2.11;
    double d = 3.11;
    cout << c % d << endl;*/
​
    system("pause");
    return 0;
}

4.算数运算符--前置递增与后置递增

#include <iostream>
using namespace std;
​
int main()
{
    //1.前置递增
    int a = 3;
    ++a;
    cout << "a=" << a << endl;
​
    //2.后置递增
    int b = 4;
    b++;
    cout << "b=" << b << endl;
​
    //3.前置和后置区别
    //前置是让变量先+1,然后进行表达式运算
    int c = 4;
    int d = ++c*5;
    cout << "c=" << c << endl;
    cout << "d=" << d << endl;
​
    //后置是让变量进行表达式运算,然后+1
    int e = 4;
    int f = e++*5;
    cout << "e=" << e << endl;
    cout << "f=" << f << endl;
​
    system("pause");
    return 0;
}

4.算数运算符--前置递减与后置递减

与递增类比

5.赋值运算符

#include <iostream>
using namespace std;
​
int main()
{
    //赋值运算符
​
    // = 
    int a = 10;
    a = 100;
    cout << "a=" << a << endl;
​
    // +=
    a = 10;
    a += 2;//a=a+2
    cout << "a=" << a << endl;
    
    // -=
    a = 10;
    a -= 2;//a=a-2
    cout << "a=" << a << endl;
    
    // *=
    a = 10;
    a *= 2;//a=a*2
    cout << "a=" << a << endl;
​
    // /=
    a = 10;
    a /= 2;//a=a/2
    cout << "a=" << a << endl;
​
    // %=
    a = 10;
    a %= 2;//a=a%2
    cout << "a=" << a << endl;
​
    system("pause");
    return 0;
}

6.比较运算符

#include <iostream>
using namespace std;
​
int main()
{
    //比较运算符
​
    // ==
    int a = 100;
    int b = 78;
    cout << (a == b) << endl;
​
    // !=
    cout << (a != b) << endl;
​
    // >
    cout << (a > b) << endl;
​
    // <
    cout << (a < b) << endl;
​
    // <=
    cout << (a <= b) << endl;
​
    // >=
    cout << (a >= b) << endl;
​
    system("pause");
    return 0;
}

7.逻辑运算符

#include <iostream>
using namespace std;
​
int main()
{
    //逻辑运算符  非!
    int a = 10;
    cout << !a << endl;//在C++中除了0都为真//0
​
    cout << !!a << endl;//又为真//1
​
    //逻辑运算符  与  &&
    int b = 10;
    int c = 10;
    cout << (c && b) << endl;//1
​
    b = 0;
    c = 10;
    cout << (c && b) << endl;//0
​
    //逻辑运算符 或 ||
    int e = 1;
    int f = 2;
    cout << (e || f) << endl;//1
​
    e = 0;
    f = 1;
    cout << (e || f) << endl;//1
​
    e = 0;
    f = 0;
    cout << (e || f) << endl;//0
    system("pause");
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值