2025-02-06

This commit is contained in:
2025-02-06 22:43:19 +08:00
parent 9480e81242
commit af0d9cdab6
82 changed files with 12564 additions and 0 deletions
+114
View File
@@ -0,0 +1,114 @@
### **一、标准输入输出函数**`<stdio.h>`
| 函数 | 功能 | 示例 |
| --------- | -------------------- | ------------------------------------ |
| `printf` | 格式化输出 | `printf("Hello, %s!\n", "World");` |
| `scanf` | 格式化输入 | `scanf("%d", &num);` |
| `putchar` | 输出单个字符 | `putchar('A');` |
| `getchar` | 输入单个字符 | `char c = getchar();` |
| `puts` | 输出字符串并换行 | `puts("Hello World!");` |
| `gets` | 读取一行字符串 | `char str[100]; gets(str);` |
| `fopen` | 打开文件 | `FILE *fp = fopen("test.txt", "r");` |
| `fclose` | 关闭文件 | `fclose(fp);` |
| `fread` | 从文件读取数据 | `fread(buffer, 1, size, fp);` |
| `fwrite` | 向文件写入数据 | `fwrite(data, 1, size, fp);` |
| `fprintf` | 格式化写入文件 | `fprintf(fp, "Value: %d\n", value);` |
| `fscanf` | 格式化从文件读取数据 | `fscanf(fp, "%d", &value);` |
------
### **二、字符串处理函数**`<string.h>`
| 函数 | 功能 | 示例 |
| --------- | ------------------------ | --------------------------------- |
| `strlen` | 计算字符串长度 | `size_t len = strlen("Hello");` |
| `strcpy` | 复制字符串 | `strcpy(dest, src);` |
| `strncpy` | 复制指定长度字符串 | `strncpy(dest, src, 5);` |
| `strcat` | 拼接字符串 | `strcat(dest, src);` |
| `strncat` | 拼接指定长度字符串 | `strncat(dest, src, 5);` |
| `strcmp` | 比较两个字符串 | `strcmp(str1, str2);` |
| `strncmp` | 比较指定长度的字符串 | `strncmp(str1, str2, 5);` |
| `strchr` | 查找字符在字符串中的位置 | `char *p = strchr(str, 'a');` |
| `strstr` | 查找子串 | `char *p = strstr(str, "test");` |
| `strtok` | 分割字符串 | `char *token = strtok(str, ",");` |
------
### **三、数学函数**`<math.h>`
| 函数 | 功能 | 示例 |
| ------- | ---------------- | --------------------------------------- |
| `abs` | 计算整数绝对值 | `int value = abs(-10);` |
| `fabs` | 计算浮点数绝对值 | `double value = fabs(-3.14);` |
| `pow` | 计算幂 | `double result = pow(2.0, 3.0); // 2^3` |
| `sqrt` | 计算平方根 | `double result = sqrt(9.0);` |
| `ceil` | 向上取整 | `double result = ceil(3.14); // 4.0` |
| `floor` | 向下取整 | `double result = floor(3.14); // 3.0` |
| `round` | 四舍五入 | `double result = round(3.5); // 4.0` |
| `sin` | 计算正弦值 | `double result = sin(M_PI / 2); // 1.0` |
| `cos` | 计算余弦值 | `double result = cos(0); // 1.0` |
| `tan` | 计算正切值 | `double result = tan(M_PI / 4); // 1.0` |
------
### **四、内存操作函数**`<string.h>`
| 函数 | 功能 | 示例 |
| --------- | ------------ | ------------------------------------ |
| `memset` | 设置内存的值 | `memset(buffer, 0, sizeof(buffer));` |
| `memcpy` | 内存拷贝 | `memcpy(dest, src, n);` |
| `memcmp` | 比较内存区域 | `memcmp(ptr1, ptr2, n);` |
| `memmove` | 内存区域移动 | `memmove(dest, src, n);` |
------
### **五、动态内存管理函数**`<stdlib.h>`
| 函数 | 功能 | 示例 |
| --------- | -------------------- | ------------------------------------------- |
| `malloc` | 动态分配内存 | `int *p = (int *)malloc(10 * sizeof(int));` |
| `calloc` | 动态分配并初始化内存 | `int *p = (int *)calloc(10, sizeof(int));` |
| `realloc` | 重新分配内存大小 | `p = (int *)realloc(p, 20 * sizeof(int));` |
| `free` | 释放动态分配的内存 | `free(p);` |
------
### **六、时间与日期函数**`<time.h>`
| 函数 | 功能 | 示例 |
| ----------- | ------------------ | ---------------------------------------- |
| `time` | 获取当前时间戳 | `time_t t = time(NULL);` |
| `clock` | 获取程序运行时间 | `clock_t c = clock();` |
| `difftime` | 计算时间差 | `double diff = difftime(t1, t2);` |
| `localtime` | 转换为本地时间结构 | `struct tm *lt = localtime(&t);` |
| `strftime` | 格式化时间为字符串 | `strftime(buffer, 100, "%Y-%m-%d", lt);` |
------
### **七、随机数函数**`<stdlib.h>`
| 函数 | 功能 | 示例 |
| ------- | -------------- | ------------------------------ |
| `rand` | 生成随机数 | `int r = rand();` |
| `srand` | 设置随机数种子 | `srand((unsigned)time(NULL));` |
------
### **八、进程控制函数**`<stdlib.h>`
| 函数 | 功能 | 示例 |
| -------- | ------------ | ---------------- |
| `exit` | 退出程序 | `exit(0);` |
| `system` | 执行系统命令 | `system("dir");` |
------
### **九、其他常用函数**
| 函数 | 功能 | 示例 |
| ---------------- | -------------- | ----------------------------------------- |
| `atoi` | 字符串转整数 | `int num = atoi("123");` |
| `atof` | 字符串转浮点数 | `double num = atof("3.14");` |
| `itoa`(非标准) | 整数转字符串 | `itoa(123, buffer, 10);` |
| `qsort` | 快速排序 | `qsort(arr, n, sizeof(int), cmp);` |
| `bsearch` | 二分查找 | `bsearch(key, arr, n, sizeof(int), cmp);` |
Binary file not shown.

After

Width:  |  Height:  |  Size: 252 KiB

@@ -0,0 +1,115 @@
### 1.创建项目
<img src="https://yp.smallkun.cn/markdown/image-20241222165137489.png!compress" alt="image-20241222165137489" style="zoom:50%;" />
创建项目时选择空项目
### 2.配置输出控制台
<img src="https://yp.smallkun.cn/markdown/image-20241222165231278.png!compress" alt="image-20241222165231278" style="zoom:50%;" />
右键项目名称选择`属性`
配置属性->链接器->系统->子系统设置为`控制台 (/SUBSYSTEM:CONSOLE)`
<img src="https://yp.smallkun.cn/markdown/image-20241222165309647.png!compress" alt="image-20241222165309647" style="zoom:50%;" />
### 3.安装连接软件
- 64位编译器选择**Windows (x86, 64-bit), ZIP Archive**(mysql-connector-c-6.1.11-winx64.zip)
- 32位编译器选择**Windows (x86, 32-bit), ZIP Archive**(mysql-connector-c-6.1.11-win32.zip)
https://downloads.mysql.com/archives/c-c/?version=6.1.11&amp;os=src
![image-20240518184737189](https://yp.smallkun.cn/markdown/image-20240518184737189.png!compress!compress)
安装完后会在C:\Program Files (x86)\MySQL\MySQL Connector C 6.1目录下
### 4.配置头文件和lib文件
**配置头文件路径**
C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include
右键项目名称点击属性 找到 配置属性下的VC++目录 将上述路径加入到`包含目录`
<img src="https://yp.smallkun.cn/markdown/image-20241222170322231.png!compress" alt="image-20241222170322231" style="zoom:50%;" />
点击最右侧下拉框后点击编辑
![image-20241222170447324](https://yp.smallkun.cn/markdown/image-20241222170447324.png!compress)
**配置lib文件路径**
C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\lib
右键项目名称点击属性 找到 配置属性->链接器->常规 将上述路径加入到`附加库目录`
<img src="https://yp.smallkun.cn/markdown/image-20241222170720851.png!compress" alt="image-20241222170720851" style="zoom:50%;" />
**配置附加依赖性**
配置属性——>链接器——>输入——>附加依赖项
<img src="https://yp.smallkun.cn/markdown/image-20241222170820644.png!compress" alt="image-20241222170820644" style="zoom:50%;" />
在其中加入`libmysql.lib`
<img src="https://yp.smallkun.cn/markdown/image-20241222171057161.png!compress" alt="image-20241222171057161" style="zoom:50%;" />
### 5.编写数据库连接代码
```c
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
void init_conn(MYSQL* mysql) {
mysql_init(mysql); // 初始化 MySQL 连接
if (!mysql_real_connect(mysql, "localhost", "root", "root", "user", 3306, NULL, CLIENT_IGNORE_SIGPIPE)) {
printf("连接数据库时发生错误!\n");
printf("%s\n", mysql_error(mysql));
}else{
printf("连接成功!\n");
}
}
int main(void){
MYSQL mysql;
int ret = 0;
init_conn(&mysql);
return 0;
}
```
### 6.配置my.ini
运行代码后会发现
<img src="https://yp.smallkun.cn/markdown/image-20240518190612777.png!compress!compress" alt="image-20240518190612777" style="zoom:50%;" />
要永久地关闭 MySQL 中的 SSL 连接功能,需要进行以下步骤:
编辑 MySQL 配置文件 my.cnf(或 my.ini),一般位于 MySQL 安装目录的 /etc 或 /etc/mysql 子目录下。
并添加以下配置:
Windows下此文件路径在C:\ProgramData\MySQL\MySQL Server 5.7\my.ini
```ini
ssl=0
```
1. 保存文件并退出编辑器。
2. 重启 MySQL 服务。
**测试连接**
![image-20241222171136732](https://yp.smallkun.cn/markdown/image-20241222171136732.png!compress)
+40
View File
@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "mysql_utils.h"
/*
DBConnection *db_init(const char *host, const char *user, const char *password, const char *database, unsigned int port)
void db_close(DBConnection *db)
int db_common(DBConnection *db, const char *query)
MYSQL_RES *db_query(DBConnection *db, const char *query)
void process_result(MYSQL_RES *result)
*/
void clearUser(DBConnection *db){
db_common(db, "TRUNCATE users");
printf("Çå¿Õ³É¹¦!!! \n");
}
int addUser(DBConnection *db, char *username, char *password){
char sql[100];
sprintf(sql, "INSERT INTO users() VALUES(DEFAULT, \"%s\", \"%s\")", username, password);
printf("%s\n", sql);
return db_common(db, sql);
}
int main(void){
DBConnection *conn;
conn = db_init("localhost", "root", "root", "user", 3306);
//process_result(db_query(conn, "SELECT * FROM users"));
printf("%d\n", addUser(conn, "root", "root"));
return 0;
}
@@ -0,0 +1,108 @@
#ifndef MYSQL_H
#include <mysql.h>
#endif
#ifndef STDIO_H
#include <stdio.h>
#endif
#ifndef STDLIB_H
#include <stdlib.h>
#endif
#ifndef STRING_H
#include <string.h>
#endif
//对MySQL连接指针变量 创建结构体变量并取别名
typedef struct {
MYSQL *conn;
} DBConnection;
DBConnection *db_init(const char *host, const char *user, const char *password, const char *database, unsigned int port) {
//动态内存分配连接的地址
DBConnection *db = (DBConnection *)malloc(sizeof(DBConnection));
if (db == NULL) {
printf("内存分配错误\n");
return NULL;
}
//对连接地址进行动态内存分配
db->conn = mysql_init(NULL);
if (db->conn == NULL) {
printf("初始化失败\n");
free(db);
return NULL;
}
//对连接地址进行配置
if (mysql_real_connect(db->conn, host, user, password, database, port, NULL, 0) == NULL) {
printf("配置连接失败: %s\n", mysql_error(db->conn));
mysql_close(db->conn);
free(db);
return NULL;
}
mysql_set_character_set(db->conn, "GBK");//设置输入输出结果字符串为GBK
return db;
}
//关闭连接函数
void db_close(DBConnection *db) {
if (db != NULL) {
if (db->conn != NULL) {
mysql_close(db->conn);
}
free(db);
}
}
//更新、删除、新增 通用函数
int db_common(DBConnection *db, const char *query) {
if (mysql_query(db->conn, query)) {
printf("common query failed: %s\n", mysql_error(db->conn));
return 0;
}
if (strncmp(query, "INSERT", 6) == 0) {
return (int)mysql_insert_id(db->conn);
}
return (int)mysql_affected_rows(db->conn);
}
//查询通用函数
MYSQL_RES *db_query(DBConnection *db, const char *query) {
MYSQL_RES *result;
if (mysql_query(db->conn, query)) {
printf("查询失败: %s\n", mysql_error(db->conn));
return NULL;
}
result = mysql_store_result(db->conn);
if (result == NULL) {
printf("获取查询结果失败: %s\n", mysql_error(db->conn));
}
return result;
}
//处理查询结果函数
void process_result(MYSQL_RES *result) {
MYSQL_ROW row;
unsigned int num_fields;
unsigned int i;
if (result == NULL) {
fprintf(stderr, "Result set is NULL.\n");
return;
}
//获取字段数
num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result))) {
for (i = 0; i < num_fields; i++) {
printf("%s\t", row[i] ? row[i] : "NULL");
}
printf("\n");
}
}
+3
View File
@@ -0,0 +1,3 @@
## 解决vs2010程序运行结束后关闭
右键项目名称->属性->链接器->系统->子系统->设置成控制台
@@ -0,0 +1,106 @@
1. **函数指针示例**:
```c
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
// 声明函数指针
int (*ptr_add)(int, int);
// 将 add 函数的地址赋值给函数指针
ptr_add = add;
// 通过函数指针调用函数
int result = ptr_add(5, 3);
printf("Result: %d\n", result); // 输出: Result: 8
return 0;
}
```
2. **指针函数示例**:
```c
#include <stdio.h>
#include <stdlib.h>
int* allocate_array(int size) {
// 动态分配内存并返回指针
return (int*)malloc(size * sizeof(int));
}
int main() {
// 调用指针函数获取动态分配的数组
int* arr = allocate_array(5);
// 使用动态分配的数组
for (int i = 0; i < 5; i++) {
arr[i] = i * 2;
printf("%d ", arr[i]);
}
printf("\n"); // 输出: 0 2 4 6 8
// 释放动态分配的内存
free(arr);
return 0;
}
```
3. **回调函数示例**:
```c
#include <stdio.h>
// 回调函数的类型定义
typedef int (*CompareFn)(int, int);
// 使用回调函数的排序函数
void sort_array(int arr[], int size, CompareFn compare_fn) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (compare_fn(arr[j], arr[j+1]) > 0) {
// 使用回调函数比较元素大小
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
// 升序比较函数
int compare_ascending(int a, int b) {
return a - b;
}
// 降序比较函数
int compare_descending(int a, int b) {
return b - a;
}
int main() {
int numbers[] = {5, 2, 8, 1, 9};
int size = sizeof(numbers) / sizeof(numbers[0]);
printf("Ascending sort:\n");
sort_array(numbers, size, compare_ascending);
for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]); // 输出: 1 2 5 8 9
}
printf("\n");
printf("Descending sort:\n");
sort_array(numbers, size, compare_descending);
for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]); // 输出: 9 8 5 2 1
}
printf("\n");
return 0;
}
```
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.