C++基础部分的笔记

#include<iostream>
#include<string>    //C++字符串的头文件
using namespace std;

#define day 7

int BubbleSort(int *arr, int len);
int SelectSort(int* arr, int len);
int QuickSort(int* arr, int begin, int end);

void Swap(int &a, int &b);


int main()
{    
    srand((unsigned int)time(NULL));
    //个人比较习惯使用的快捷键
    //    光标移动至行尾(ctrl +)
    //    行注释    (ctrl /)
    //    块注释    (ctrl shift /)
    //    列出成员    (ctrl j)
    //    选中当前字符串    (ctrl w)
#if 0
    //1、变量:给一段指定的地址空间起名,方便操作这段内存    数据类型 变量名 = 初始值;
    int varible = 10;
    cout << "varible=" << varible << endl;

    //2、常量:用来记录程序中不可改变的数据
    //    2.1、宏常量 #define 常量名 常量值
    cout << "一周有 " << day << " 天" << endl;
    //    2.2、const修饰的变量        const 数据类型 变量名 = 初始值

    //3、标识符命名规则 3.1标识符不能是关键字
    //                    3.2标识符只能是字母、数字、下划线构成
    //                    3.3标识符只能以字母和下划线开头
    //                    3.4标识符的字母需要区分大小写

    //4、整型 short(2) int(4) long(4) long long(8)

    //5、sizeof 统计 数据类型 所占内存的大小

    //6、实型 float(4) 3.14f double(8) 默认六位有效数字            !!!精度转换
    //科学计数法:3e2 ==》300

    //7、字符型char(1) 单引号 A-65 a-97
    {
        char ch[] = "abc";
        cout << " ==== " << (int)ch[3] << endl;    //字符串的结束标志的ascll为0
    }

    //8、转义字符 换行符(\n)水平制表符(\t)

    //9、字符串型 
    //    9.1 C风格字符串    char 变量名[]= "字符串值";
    char ch[] = "liu\0nian";
    cout << "ch = " << ch << endl; //cout 输出字符 遇到字符串结束标志(\0)

    //    9.2 C++风格字符串    string 变量名 = "字符串值";

    //10、布尔类型 真true-1 假false-0  占用内存大小1字节
    cout << "bool占用内存大小为" << sizeof(bool) << endl;

    //11、数据的输入

    //12、运算符 
    //    12.1、算术运算符 
    //        12.1.1、除数不能为零(/ %)
    //        12.1.2、两个小数相除 可能为小数
    //        12.1.3、取模运算必须是整数或者!!!未区分范围的枚举类型!!!
    //        12.1.4.1、前加加(++a)
    system("cls");
    int a1 = 10;
    int b1 = ++a1 * 10;
    cout << "a1 = " << a1 << endl;
    cout << "b1 = " << b1 << endl;

    //        12.1.4.2、后加加(b++)
    {
        int a1 = 10;
        int b1 = a1++ * 10;
        cout << "a1 = " << a1 << endl;
        cout << "b1 = " << b1 << endl;
    }

    float f1 = 0.8;
    float f2 = 0.5;
    cout << " f1/f3 = " << f1 / f2 << endl;

    //    12.2、赋值运算符 
    //    12.3、比较运算符 
    //    12.4、逻辑运算符 
    system("cls");
    //13、程序流程结构 
    //    13.1、顺序结构 

    //    13.2、选择结构    if else        三目运算符:表达式1?表达式2:表达式3
    int a = 10;
    int b = 20;
    int c;
    c = (a != b) ? a : b;    //若表达式1为真 返回表达式2 否则返回表达式3
    cout << "c=" << c << endl;
    //                    switch case default 表达式只能为整型 或者 字符型
    //    13.3、循环结构 while(循环条件){循环体} for
    int num1 = rand() % 99 + 1;        //猜数字的小游戏
    int num2 = -1;
    while (1)
    {
        cout << "猜数字:" << endl;
        cin >> num2;
        if (num1 < num2)
            cout << "大了" << endl;
        else if (num1 > num2)
            cout << "小了" << endl;
        else
        {
            cout << "恭喜你猜中了!" << endl;
            break;
        }
    }

    for (int i = 100; i < 1000; i++)    //水仙花数
    {
        int a = i % 10;            //个位
        int b = i / 10 % 10;    //十位
        int c = i / 100;        //百位
        if (i == a * a * a + b * b * b + c * c * c)
            cout << i << "\t";
    }
#endif    
    //14、一维数组 连续的内存空间 相同的数据类型 数组类型 数组名[数组长度]
    int arr[10] = { 0 };
    for (int i = 0; i < 10; i++)
    {
        arr[i] = rand() % 99 + 1;
    }

    for (int i = 0; i < 10; i++)
    {
        cout << arr[i] << "\t";
    }
    cout << endl;
    QuickSort(arr, 0, 9);
    for (int i = 0; i < 10; i++)
    {
        cout << arr[i] << "\t";
    }


    cout << endl;

    //15、指针
     //    15.1、常量指针 =>指针 指针的指向不可以修改 指向的值可以修改 const int *a=&b;
    //    15.2、指针常量 =>常量 指针的指向可以修改 指向的值不可以修改 int *const a=&b;

    system("pause");

    return 0;
}

void Swap(int &a, int &b)
{
    int temp = a;
    a = b;
    b = temp;
}

int BubbleSort(int *arr,int len)    //冒泡排序
{
    for (int i = 0; i < len-1; i++)
    {
        for (int j = 0; j < len-i-1; j++)
        {
            if (arr[j] < arr[j + 1])
                Swap(arr[j], arr[j + 1]);
        }
    }
    return 0;
}

int SelectSort(int* arr, int len)
{
    for (int i = 0; i < len-1; i++)
    {
        int max = i;    //记录较大值的下标
        for (int j = i+1; j < len; j++)
        {
            if (arr[max] < arr[j])
            {
                max = j;
            }
        }
        Swap(arr[i], arr[max]);
    }
    return 0;
}
int QuickSort(int* arr, int begin, int end)
{
    int i = begin;
    int j = end;
    int temp = arr[begin];
    while (i != j)
    {
        while (arr[j] >= temp && i < j)    //比基准数小
            j--;
        while (arr[i] <= temp&&i<j) //比基准数大
            i++;
        cout << "***" << endl;
        
        if(i<j)
            Swap(arr[i], arr[j]);
    }
    arr[begin] = arr[i];
    arr[i] = temp;
    QuickSort(arr, begin, j - 1);
    QuickSort(arr, i + 1, end);
    return 0;
}

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值