我的编程语言学习笔记

前言

作为一名编程初学者,我深知学习编程需要不断积累和记录。在这篇博客文章中,我将分享一些我在学习C/C++编程语言过程中记录的常用代码、特定函数、复杂概念以及特定功能。希望能与大家一起切磋进步!

常用代码:


1. 输入输出操作:


   - 使用`cin`进行标准输入。
   - 使用`cout`进行标准输出。
   - 使用`scanf`进行格式化输入。
   - 使用`printf`进行格式化输出。

2. 控制结构:


   - `if-else`语句:根据条件执行不同的代码块。
   - `for`循环:重复执行特定次数的代码块。
   - `while`循环:当条件为真时,重复执行代码块。
   - `switch`语句:根据不同的值执行不同的代码块。

3.特定函数:


1. `strlen`函数:返回字符串的长度。
2. `strcpy`函数:将一个字符串复制到另一个字符串。
3. `strcmp`函数:比较两个字符串是否相等。
4. `atoi`函数:将字符串转换为整数。
5. `rand`函数:生成随机数。

4.复杂概念:


1. 指针:指向内存地址的变量,可以通过指针来访问和修改内存中的数据。
2. 动态内存分配:使用`new`关键字在运行时分配内存,使用`delete`关键字释放内存。

5. 特定功能:


1. 文件操作:打开、读取和写入文件。
2. 数据结构:如数组、链表、栈和队列等。
3. 排序算法:如冒泡排序、插入排序和快速排序等。

具体参考代码


当学习编程语言时,了解各种函数的具体用法是非常重要的。以下是对前面提到的一些函数的详细阐述和相应的参考实例:

1. `strlen`函数:


   - 作用:返回一个字符串的长度。
   - 用法示例:
     ```c++
     #include <iostream>
     #include <cstring>
     
     int main() {
         char str[] = "Hello World";
         int length = strlen(str);
         std::cout << "字符串长度为:" << length << std::endl;
         return 0;
     }
     ```

2. `strcpy`函数:


   - 作用:将一个字符串复制到另一个字符串。
   - 用法示例:
     ```c++
     #include <iostream>
     #include <cstring>
     
     int main() {
         char source[] = "Hello";
         char destination[20];
         strcpy(destination, source);
         std::cout << "复制后的字符串为:" << destination << std::endl;
         return 0;
     }
     ```

3. `strcmp`函数:


   - 作用:比较两个字符串是否相等。
   - 用法示例:
     ```c++
     #include <iostream>
     #include <cstring>
     
     int main() {
         char str1[] = "Hello";
         char str2[] = "World";
         int result = strcmp(str1, str2);
         if (result == 0) {
             std::cout << "字符串相等" << std::endl;
         } else {
             std::cout << "字符串不相等" << std::endl;
         }
         return 0;
     }
     ```

4. `atoi`函数:


   - 作用:将字符串转换为整数。
   - 用法示例:
     ```c++
     #include <iostream>
     #include <cstdlib>
     
     int main() {
         char str[] = "12345";
         int num = atoi(str);
         std::cout << "转换后的整数为:" << num << std::endl;
         return 0;
     }
     ```

5. `rand`函数:


   - 作用:生成随机数。
   - 用法示例:
     ```c++
     #include <iostream>
     #include <cstdlib>
     #include <ctime>
     
     int main() {
         srand(time(0));  // 设置种子,确保每次运行生成的随机数不同
         int randomNum = rand() % 100;  // 生成0到99之间的随机数
         std::cout << "生成的随机数为:" << randomNum << std::endl;
         return 0;
     }
     ```


 

6. 指针示例:


   ```c++
   #include <iostream>
   
   int main() {
       int num = 10;
       int* ptr = &num;  // 定义指向整数的指针,并将其指向变量num
       
       std::cout << "num的值为:" << num << std::endl;
       std::cout << "通过指针访问num的值:" << *ptr << std::endl;
       
       *ptr = 20;  // 修改指针所指向的变量的值
       std::cout << "修改后的num的值为:" << num << std::endl;
       
       return 0;
   }
   ```

7. 动态内存分配示例:


   ```c++
   #include <iostream>
   
   int main() {
       int size;
       std::cout << "请输入数组的大小:";
       std::cin >> size;
       
       int* dynamicArray = new int[size];  // 动态分配一个整数数组
   
       for (int i = 0; i < size; i++) {
           dynamicArray[i] = i + 1;
       }
   
       std::cout << "数组元素为:";
       for (int i = 0; i < size; i++) {
           std::cout << dynamicArray[i] << " ";
       }
       std::cout << std::endl;
   
       delete[] dynamicArray;  // 释放动态分配的内存
   
       return 0;
   }
   ```

8. 文件操作示例:


   ```c++
   #include <iostream>
   #include <fstream>
   
   int main() {
       std::ofstream file("data.txt");  // 创建一个名为"data.txt"的文件对象
   
       if (file.is_open()) {
           file << "Hello, World!\n";
           file << "This is a sample file.\n";
           file.close();
           std::cout << "文件写入完成." << std::endl;
       } else {
           std::cout << "无法打开文件." << std::endl;
       }
   
       return 0;
   }
   ```

9. 数据结构示例(链表):


   ```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;
   
       // 释放链表的内存
       currentNode = head;
       while (currentNode != nullptr) {
           Node* temp = currentNode;
           currentNode = currentNode->next;
           delete temp;
       }
   
       return 0;
   }
   ```

10. 排序算法示例(冒泡排序):


   ```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;
   }
   ```

上述示例代码仅作为参考,具体的使用方法可能会因编程环境或需求而略有不同。学习时建议阅读相关的文档和教程,并进行实践来加深对函数的理解和掌握。

总结

以上仅是我在学习C/C++编程语言过程中记录的一些内容,还有很多其他的知识点和技巧等待我们去学习和探索。编程是一个不断进步和成长的过程,希望大家能够保持学习的热情,不断积累经验,提升自己的编程能力。

如果你有任何问题或者想要分享你自己的学习笔记,请在下方留言,让我们一起交流和进步吧!

欢迎点赞收藏

猜你喜欢

转载自blog.csdn.net/lzyzuixin/article/details/132357206