C++学习第二天

数组

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

const int array_size = 10; // 定义一个常量,表示数组的长度

int main(){
    int my_numbers[5] = {0}; // 声明一个包含5个int元素的数组,并将每个元素都初始化为零

    char my_characters[5] =  {"0"}; // 声明一个包含5个char元素的数组,并将每个元素都初始化为'0'

    int my_numbers_1[5] = {1, 2, 3, 4, 5}; // 声明一个包含5个int元素的数组,并将每个元素都初始化为1,2,3,4,5

    int my_numbers_2[5] = {1, 2}; // 声明一个包含5个int元素的数组,并将前两个元素初始化为1,2,其余元素初始化为零

    int my_numbers_3[array_size] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // 声明一个包含10个int元素的数组,并将每个元素都初始化为1,2,3,4,5,6,7,8,9,10

    //如果知道数组中每个元素的初始值,可不指定数组包含的元素数:
    int my_numbers_4[] = {1, 2, 3, 4, 5}; // 声明一个包含5个int元素的数组,并将每个元素都初始化为1,2,3,4,5

    //修改数组中的元素:
    my_numbers_4[0] = 10; // 将数组中的第一个元素修改为10
    
    cout << my_numbers_4[0] << endl;

    return 0;
}

多维数组

#include <iostream>
using namespace std;


int main(){
    // 声明一个二维数组,命名为 solar_panel_ids,它有2行3列
    int solar_panel_ids[2][3];

    // 给每个电池板指定id
    int solar_panel_ids_1[2][3] = {{0, 1, 2}, {3, 4, 5}};
   
    // 等价于上面这种初始化
    int solar_panel_ids_2[2][3] = {0, 1, 2, 3, 4, 5};


    return 0;
}

动态数组

#include <iostream>
#include <vector>
using namespace std;

/*
为减少占用的内存,可不使用前面介绍的静态数组,而使用动态数组,并在运行阶段根据需要增大动态数组。

C++提供了 std::vector,这是一种方便且易于使用的动态数组

避免超越数组边界至关重要,这被称为缓冲区溢出。将输入用作索引前应对其进行检查,这有助于避免跨越数组边界

动态数组让程序员无需在编译阶段考虑其最大长度,使用动态数组可更好地管理内存,以免分配过多的内存,而又不使用它们。
*/

int main(){
    // 声明一个动态数组,初始大小为3
    vector<int> dynamic_array (3); 

    dynamic_array[0] = 1;
    dynamic_array[1] = 2;
    dynamic_array[2] = 3;

    cout << "Number of integers in the array: " << dynamic_array.size() << endl;

    cout << "Enter another number of the array: " << endl;
    int another_number = 0;
    cin >> another_number;
    dynamic_array.push_back(another_number);//push_back()函数将新元素添加到动态数组的末尾

    cout << "Number of integers in the array: " << dynamic_array.size() << endl; 
    // 索引从零开始,而 size()返回矢量包含的元素数,因此最后一个元素的索引为size()-1。
    cout << "The last number in the array: " << dynamic_array[dynamic_array.size() - 1] << endl;



    return 0;
}

C++字符串

#include <iostream>
#include <string>
using namespace std;

/*
C 风格字符串是特殊的 char 数组,用终止空字符‘\0’标识末尾。

更重要的是,您了解到,C++提供了更佳的选择——std::string,它包含一些方便的函数,让您能够判断字符串的长度、拼接字符串等
*/

int main(){
    string greeting ("Hello, world!"); // 初始化字符串
    cout << greeting << endl; 

    cout << "Enter a line of text: " << endl;
    string first_line;
    getline(cin, first_line);  // 读取一行文本

    cout << "Enter another line of text: " << endl;
    string second_line;
    getline(cin, second_line);  // 读取另一行文本

    cout << "Result of concatenation: " << endl;
    string concat = first_line + " " + second_line;
    cout << concat << endl;  // 输出拼接后的字符串

    cout << "Copy of concatenated string: " << endl;
    string copy = concat;
    cout << copy << endl;  // 输出拼接后的字符串的副本

    cout << "Length of concat string: " << concat.length() << endl;  // 输出拼接后的字符串的长度


    return 0;
}

前缀运算符和后缀运算符

#include <iostream>
using namespace std;


int main(){
    int my_int = 101;
    cout << "Start value of integer being operated: " << my_int << endl;

    int post_fix_increase = my_int++;
    cout << "Result of Postfix increment: " << post_fix_increase << endl;
    cout << "After Postfix increment, my_int is: " << my_int << endl;

    cout << endl;

    my_int = 101; // reset the value of my_int
    int pre_fix_increase = ++my_int;
    cout << "Result of Prefix increment: " << pre_fix_increase << endl;
    cout << "After Prefix increment, my_int is: " << my_int << endl;

    cout << endl;

    my_int = 101; // reset the value of my_int
    int post_fix_decrement = my_int--;
    cout << "Result of Postfix decrement: " << my_int-- << endl;
    cout << "After Postfix decrement, my_int is: " << my_int << endl;

    cout << endl;

    my_int = 101; // reset the value of my_int
    int pre_fix_decrement = --my_int;
    cout << "Result of Prefix decrement: " << pre_fix_decrement << endl;
    cout << "After Prefix decrement, my_int is: " << my_int << endl;


    return 0;
}

逻辑运算符

/*
not !
and &&
or ||
exclusive or ^
*/

/*
在if语句中使用逻辑NOT和AND运算符(!和&&)进行条件处理
*/

#include <iostream>
using namespace std;


int main(){
    cout << "Use boolean values (0 / 1) to answer the questions" << endl;

    cout << "Is it raining?";
    bool raining = false;
    cin >> raining;

    cout << "Do you have buses on the streets?";
    bool buses = false;
    cin >> buses;

    // Conditional statement uses logical AND and NOT
    if (raining && !buses)
        cout << "You cannot go to work!" << endl;
    else
        cout << "You can go to work!" << endl;
    
    if ((!raining) && buses)
        cout << "Enjoy the sun and have a day!" << endl;


    return 0;
}

/*使用逻辑运算符NOT和OR帮助判断您能否购买那辆梦寐以求的汽车

下面内容,没有写对应得程序

对整数的各位执行NOT、AND、OR和XOR运算,以演示按位运算符的用法

移位运算符将整个位序列向左或向右移动,其用途之一是将数据乘以或除以2n。
<<  1 乘以 2
>>  1 除以 2

符合运算符
+= -= /= %=
*/

#include <iostream>
using namespace std;


int main(){
    cout << "Answer questions with 0 or 1" << endl;
    cout << "Is there a deep discount on your favorite car? ";
    bool discount = false;
    cin >> discount;

    cout << "Did you get a fantastic bonus?";
    bool fantastic_bonus = false;
    cin >> fantastic_bonus;

    if (discount || fantastic_bonus)
        cout << "Congratulations, you can buy your dream car!" << endl;
    else
        cout << "Sorry, you can't buy your dream car." << endl;


    return 0;
}

控制程序流程

/*
if..else
    function_1 : 将字符串复制到char数组中之前,检查数组的容量

嵌套if
if {
    if {}...
}

swith-case: switch-case结构非常适合与枚举常量结合使用。
switch(expression){
    case constant1: 
        statement1;
    case constant2: 
        statement2;
    default: statementN;
}

运算符 ?: --- 三目运算符
int max = (a > b ? a : b); max为a, b中大的那个


goto语句
some_function(){
jump_to_point: //called a label
    code....

    goto jump_to_point;
}


while循环
while (expression){
    // 只要expression为true,就将执行该语句块。因此,必须确保expression在特定条件下将为false,否则while循环将永不停止。
    // condition evaluates to true
    statement_block;
}


do-while循环:来确保语句至少执行一次。
do{
    statement_block;

}while(condition);


for循环:
在for循环的初始化表达式中,可初始化多个变量。
for (initialization; condition; increment){
    statement_block;
}

使用continue 和 break修改循环的行为

嵌套循环遍历多维数组
    function_2:使用嵌套循环遍历二维int数组的元素
    function_3: 使用嵌套循环计算斐波纳契数列
*/

#include <iostream>
#include <string>
using namespace std;


// void function_1(){
//     char Buffer[20] = {'\0'};

//     cout << "Enter a line of text: " << endl;
//     string line_entered;
//     getline(cin, line_entered);

//     if (line_entered.length() < 20){
//         //现在好像运行不了了
//         strcpy(Buffer, line_entered.c_str()); //c_str()将string转换为C风格字符串
//         cout << "Buffer contains: " << Buffer << endl;
//     }
    
// }

void function_2(){
    const int MAX_ROWS = 3;
    const int MAX_COLS = 4;

    // 2D array of integers
    int my_int[MAX_ROWS][MAX_COLS] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

    for (int row = 0; row < MAX_ROWS; row++){
        for (int col = 0; col < MAX_COLS; col++)
            cout << "my_int[" << row << "][" << col << "] = " << my_int[row][col] << endl;
    }
}

void function_3(){
    const int NUMBERS_TO_CALCULATE = 3;
    cout << "This program will calculate " << NUMBERS_TO_CALCULATE << " Fibonacci numbers." << endl;

    int num1 = 0, num2 = 1;
    char want_more = '\0';
    cout << num1 << " " << num2 << " ";

    do{
        for (int index = 0; index < NUMBERS_TO_CALCULATE; index++){
            cout << num1 + num2 << " ";

            int temp = num2;
            num2 = num1 + num2;
            num1 = temp;
        }

        cout << endl << "Do you want to calculate more Fibonacci numbers? (y/n) ";
        cin >> want_more;

    }while(want_more == 'y' || want_more == 'Y');

    cout << "Goodbye!" << endl;

}


int main(){
    // function_1();
    // function_2();
    function_3();

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值