描述对于营销型网站建设很重要飘红效果更佳,教育培训排行榜前十名,重庆网站建设营销,重复打开同一个网站怎么做前言
作为一名编程初学者#xff0c;我深知学习编程需要不断积累和记录。在这篇博客文章中#xff0c;我将分享一些我在学习C/C编程语言过程中记录的常用代码、特定函数、复杂概念以及特定功能。希望能与大家一起切磋进步#xff01; 常用代码#xff1a; 1. 输入输出操作…
前言
作为一名编程初学者我深知学习编程需要不断积累和记录。在这篇博客文章中我将分享一些我在学习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编程语言过程中记录的一些内容还有很多其他的知识点和技巧等待我们去学习和探索。编程是一个不断进步和成长的过程希望大家能够保持学习的热情不断积累经验提升自己的编程能力。
如果你有任何问题或者想要分享你自己的学习笔记请在下方留言让我们一起交流和进步吧
欢迎点赞收藏