My programming language study notes

foreword

As a beginner in programming, I know that learning programming requires continuous accumulation and recording. In this blog post, I will share some commonly used codes, specific functions, complex concepts, and specific functions that I recorded during the process of learning C/C++ programming language. Hope to learn from you and make progress together!

 

Common codes:


1. Input and output operations:


   - Use `cin` for standard input.
   - Use `cout` for standard output.
   - Use `scanf` for formatted input.
   - Use `printf` for formatted output.

2. Control structure:


   - `if-else` statement: Execute different blocks of code depending on the condition.
   - `for` loop: Repeats a block of code a specified number of times.
   - `while` loop: Repeatedly executes a block of code while a condition is true.
   - `switch` statement: Execute different blocks of code depending on the value.

3. Specific functions:


1. `strlen` function: returns the length of a string.
2. `strcpy` function: copy a string to another string.
3. `strcmp` function: compares whether two strings are equal.
4. `atoi` function: Convert a string to an integer.
5. `rand` function: Generate random numbers.

4. Complex concepts:


1. Pointer: A variable pointing to a memory address, through which the data in memory can be accessed and modified.
2. Dynamic memory allocation: use the `new` keyword to allocate memory at runtime, and use the `delete` keyword to release memory.

5. Specific functions:


1. File operations: open, read and write files.
2. Data structures: such as arrays, linked lists, stacks, and queues.
3. Sorting algorithms: such as bubble sort, insertion sort, and quick sort.

specific reference code


When learning a programming language, it is very important to understand the specific usage of various functions. The following are detailed descriptions and corresponding reference examples of some of the aforementioned functions:

1. `strlen` function:


   - Function: Return the length of a string.
   - Usage example:
     ```c++
     #include <iostream>
     #include <cstring>
     
     int main() {          char str[] = "Hello World";          int length = strlen(str);          std::cout << "string The length is: " << length << std::endl;          return 0;      }      ```





2. `strcpy` function:


   - Role: Copy a string to another string.
   - Usage example:
     ```c++
     #include <iostream>
     #include <cstring>
     
     int main() {          char source[] = "Hello";          char destination[20];          strcpy(destination, source);          std::cout < < "The copied string is: " << destination << std::endl;          return 0;      }      ```






3. `strcmp` function:


   - Role: Compare two strings for equality.
   - Usage example:
     ```c++
     #include <iostream>
     #include <cstring>
     
     int main() {          char str1[] = "Hello";          char str2[] ​​= "World";          int result = strcmp(str1, str2) ;          if (result == 0) {              std::cout << "strings are equal" << std::endl;          } else {              std::cout << "strings are not equal" << std::endl;          }          return 0;      }      ```










4. `atoi` function:


   - Role: Convert a string to an integer.
   - Usage example:
     ```c++
     #include <iostream>
     #include <cstdlib>
     
     int main() {          char str[] = "12345";          int num = atoi(str);          std::cout << "Converted Integer is: " << num << std::endl;          return 0;      }      ```





5. The `rand` function:


   - Role: Generate random numbers.
   - Usage example:
     ```c++
     #include <iostream>
     #include <cstdlib>
     #include <ctime>
     
     int main() {          srand(time(0)); // Set the seed to ensure that the random number generated by each run is different          int randomNum = rand() % 100; // Generate a random number between 0 and 99          std::cout << "The generated random number is: " << randomNum << std::endl;          return 0;      }      `` `






 

6. Pointer example:


   ```c++
   #include <iostream>
   
   int main() {        int num = 10;        int* ptr = # // Define a pointer to an integer and point it to the variable num        std::cout << "The value of num : " << num << std::endl;        std::cout << "Access the value of num through a pointer: " << *ptr << std::endl;        *ptr = 20; // Modify the pointer pointed to The value of the variable        std::cout << "The value of the modified num is:" << num << std::endl;        return 0;    }    ```


       


       


       


7. Example of dynamic memory allocation:


   ```c++
   #include <iostream>
   
   int main() {        int size;        std::cout << "Please enter the size of the array: ";        std::cin >> size;        int* dynamicArray = new int[size]; // Dynamically allocate an integer array        for (int i = 0; i < size; i++) {            dynamicArray[i] = i + 1;        }        std::cout << "Array elements are:";        for (int i = 0 ; i < size; i++) {            std::cout << dynamicArray[i] << " ";        }        std::cout << std::endl;        delete[] dynamicArray; // release the dynamically allocated memory        return 0;    }    ```



       

   



   





   

   


8. Example of file operation:


   ```c++
   #include <iostream>
   #include <fstream>
   
   int main() {        std::ofstream file("data.txt"); // Create a file object named "data.txt"        if (file. is_open()) {            file << "Hello, World!\n";            file << "This is a sample file.\n";            file.close();            std::cout << "File writing complete." << std::endl;        } else {            std::cout << "Cannot open file." << std::endl;        }        return 0;    }    ```

   








   


9. Data structure example (linked list):


   ```c++
   #include <iostream>
   
   struct Node {
       int data;
       Node* next;
   };
   
   int main() {
       Node* head = nullptr;
   
       // 创建链表
       for (int i = 1; i <= 5; i++) {
           Node* newNode = new Node;
           newNode->data = i;
           newNode->next = head;
           head = newNode;
       }
   
       // 遍历链表
       Node* currentNode = head;
       while (currentNode != nullptr) {
           std::cout << currentNode->data << " ";
           currentNode = currentNode->next;
       }
       std::cout << std::endl;
   
       // Release the memory of the linked list
       currentNode = head;
       while (currentNode != nullptr) {            Node* temp = currentNode;            currentNode = currentNode->next;            delete temp;        }        return 0;    }    ```




   


10. Example of sorting algorithm (bubble sort):


   ```c++
   #include <iostream>
   
   void bubbleSort(int arr[], int size) {
       for (int i = 0; i < size - 1; i++) {
           for (int j = 0; j < size - i - 1; j++) {
               if (arr[j] > arr[j + 1]) {
                   // 交换元素
                   int temp = arr[j];
                   arr[j] = arr[j + 1];
                   arr[j + 1] = temp;
               }
           }
       }
   }
   
   int main() {
       int arr[] = {5, 2, 8, 12, 1};
       int size = sizeof(arr) / sizeof(arr[0]);
   
       bubbleSort(arr, size);
   
       std::cout << "排序后的数组为:";
       for (int i = 0; i < size; i++) {
           std::cout << arr[i] << " ";
       }
       std::cout << std::endl;
   
       return 0;
   }
   ```

The above sample code is for reference only, and the specific usage method may be slightly different depending on the programming environment or requirements. When learning, it is recommended to read relevant documents and tutorials, and practice to deepen the understanding and mastery of functions.

Summarize

The above are just some of the content I recorded in the process of learning C/C++ programming language, and there are many other knowledge points and skills waiting for us to learn and explore. Programming is a process of continuous progress and growth. I hope that everyone can maintain their enthusiasm for learning, accumulate experience, and improve their programming capabilities.

If you have any questions or want to share your own study notes, please leave a message below, let us communicate and make progress together!

Welcome to like and collect

Guess you like

Origin blog.csdn.net/lzyzuixin/article/details/132357206