案例一:C语言基础语法
1.1 变量和数据类型
#include <stdio.h>
int main() {
int age = 25;
float salary = 5000.50;
char grade = 'A';
printf("Age: %d\n", age);
printf("Salary: %.2f\n", salary);
printf("Grade: %c\n", grade);
return 0;
}
1.2 运算符和表达式
#include <stdio.h>
int main() {
int a = 10, b = 5;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
printf("Sum: %d\n", sum);
printf("Difference: %d\n", difference);
printf("Product: %d\n", product);
printf("Quotient: %d\n", quotient);
printf("Remainder: %d\n", remainder);
return 0;
}
案例二:控制结构
2.1 if语句
#include <stdio.h>
int main() {
int number = 10;
if (number > 5) {
printf("Number is greater than 5\n");
} else {
printf("Number is not greater than 5\n");
}
return 0;
}
2.2 switch语句
#include <stdio.h>
int main() {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
案例三:循环结构
3.1 for循环
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("Number: %d\n", i);
}
return 0;
}
3.2 while循环
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("Number: %d\n", i);
i++;
}
return 0;
}
案例四:函数
4.1 定义和调用函数
#include <stdio.h>
void greet() {
printf("Hello, World!\n");
}
int main() {
greet();
return 0;
}
4.2 传值和传址
#include <stdio.h>
void increment(int *num) {
(*num)++;
}
int main() {
int number = 5;
printf("Before increment: %d\n", number);
increment(&number);
printf("After increment: %d\n", number);
return 0;
}
案例五:数组
5.1 一维数组
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("Number %d: %d\n", i, numbers[i]);
}
return 0;
}
5.2 二维数组
#include <stdio.h>
int main() {
int numbers[2][3] = {{1, 2, 3}, {4, 5, 6}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("Number [%d][%d]: %d\n", i, j, numbers[i][j]);
}
}
return 0;
}
案例六:指针
6.1 指针基础
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a;
printf("Value of a: %d\n", a);
printf("Address of a: %p\n", (void *)&a);
printf("Value of ptr: %p\n", (void *)ptr);
printf("Value of *ptr: %d\n", *ptr);
return 0;
}
6.2 指针数组
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
int *ptrArray[5];
for (int i = 0; i < 5; i++) {
ptrArray[i] = &numbers[i];
}
for (int i = 0; i < 5; i++) {
printf("Number %d: %d\n", i, *ptrArray[i]);
}
return 0;
}
案例七:结构体
7.1 定义和初始化结构体
#include <stdio.h>
struct Person {
char name[50];
int age;
float salary;
};
int main() {
struct Person person;
strcpy(person.name, "John Doe");
person.age = 25;
person.salary = 5000.50;
printf("Name: %s\n", person.name);
printf("Age: %d\n", person.age);
printf("Salary: %.2f\n", person.salary);
return 0;
}
7.2 结构体数组
#include <stdio.h>
struct Person {
char name[50];
int age;
float salary;
};
int main() {
struct Person people[2] = {
{"John Doe", 25, 5000.50},
{"Jane Smith", 30, 6000.00}
};
for (int i = 0; i < 2; i++) {
printf("Name: %s\n", people[i].name);
printf("Age: %d\n", people[i].age);
printf("Salary: %.2f\n", people[i].salary);
}
return 0;
}
案例八:文件操作
8.1 打开文件
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
printf("Failed to open file\n");
return 1;
}
fclose(file);
return 0;
}
8.2 读取文件
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
char buffer[100];
if (file == NULL) {
printf("Failed to open file\n");
return 1;
}
while (fgets(buffer, sizeof(buffer), file)) {
printf("%s", buffer);
}
fclose(file);
return 0;
}
案例九:动态内存分配
9.1 分配内存
#include <stdio.h>
#include <stdlib.h>
int main() {
int *numbers = (int *)malloc(5 * sizeof(int));
if (numbers == NULL) {
printf("Failed to allocate memory\n");
return 1;
}
for (int i = 0; i < 5; i++) {
numbers[i] = i;
}
free(numbers);
return 0;
}
9.2 释放内存
#include <stdio.h>
#include <stdlib.h>
int main() {
int *numbers = (int *)malloc(5 * sizeof(int));
if (numbers == NULL) {
printf("Failed to allocate memory\n");
return 1;
}
for (int i = 0; i < 5; i++) {
numbers[i] = i;
}
free(numbers);
return 0;
}
案例十:字符串处理
10.1 字符串比较
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
if (strcmp(str1, str2) == 0) {
printf("Strings are equal\n");
} else {
printf("Strings are not equal\n");
}
return 0;
}
10.2 字符串连接
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
char result[100];
strcpy(result, str1);
strcat(result, str2);
printf("Result: %s\n", result);
return 0;
}
案例十一:图形界面编程
11.1 创建窗口
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex2f(0.0, 0.0);
glVertex2f(0.5, 0.5);
glVertex2f(0.0, 0.5);
glEnd();
glFlush();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("OpenGL Window");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
11.2 绘制图形
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex2f(0.0, 0.0);
glVertex2f(0.5, 0.5);
glVertex2f(0.0, 0.5);
glEnd();
glFlush();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("OpenGL Window");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
案例十二:网络编程
12.1 创建套接字
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main() {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
printf("Failed to create socket\n");
return 1;
}
return 0;
}
12.2 连接服务器
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main() {
int sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(8080);
server.sin_addr.s_addr = inet_addr("192.168.1.1");
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) == -1) {
printf("Failed to connect to server\n");
return 1;
}
return 0;
}
案例十三:多线程编程
13.1 创建线程
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *threadFunction(void *arg) {
printf("Thread is running\n");
return NULL;
}
int main() {
pthread_t thread;
if (pthread_create(&thread, NULL, threadFunction, NULL) != 0) {
printf("Failed to create thread\n");
return 1;
}
pthread_join(thread, NULL);
return 0;
}
13.2 线程同步
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int counter = 0;
pthread_mutex_t lock;
void *threadFunction(void *arg) {
for (int i = 0; i < 1000; i++) {
pthread_mutex_lock(&lock);
counter++;
pthread_mutex_unlock(&lock);
}
return NULL;
}
int main() {
pthread_t thread1, thread2;
pthread_mutex_init(&lock, NULL);
if (pthread_create(&thread1, NULL, threadFunction, NULL) != 0 ||
pthread_create(&thread2, NULL, threadFunction, NULL) != 0) {
printf("Failed to create threads\n");
return 1;
}
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("Counter: %d\n", counter);
pthread_mutex_destroy(&lock);
return 0;
}
案例十四:数据库编程
14.1 连接数据库
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
int main() {
MYSQL *conn;
conn = mysql_init(NULL);
if (mysql_real_connect(conn, "localhost", "username", "password", "database", 0, NULL, 0) == NULL) {
printf("Failed to connect to database\n");
return 1;
}
mysql_close(conn);
return 0;
}
14.2 执行查询
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
int main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
conn = mysql_init(NULL);
if (mysql_real_connect(conn, "localhost", "username", "password", "database", 0, NULL, 0) == NULL) {
printf("Failed to connect to database\n");
return 1;
}
if (mysql_query(conn, "SELECT * FROM table_name") != 0) {
printf("Failed to execute query\n");
return 1;
}
res = mysql_use_result(conn);
while ((row = mysql_fetch_row(res)) != NULL) {
printf("%s\n", row[0]);
}
mysql_free_result(res);
mysql_close(conn);
return 0;
}
案例十五:网络爬虫
15.1 发送HTTP请求
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
void write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
((char **)stream)[0] = (char *)malloc(size * nmemb + 1);
strcpy(((char **)stream)[0], (char *)ptr);
}
int main() {
CURL *curl;
CURLcode res;
char *url = "http://example.com";
char *data;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
} else {
printf("Data: %s\n", data);
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
15.2 解析HTML页面
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/xmlparse.h>
#include <libxml/xmlstring.h>
int main() {
xmlDoc *doc;
xmlNode *root;
xmlNode *node;
doc = xmlParseFile("example.html");
root = xmlDocGetRootElement(doc);
for (node = root->children; node; node = node->next) {
if (node->type == XML_ELEMENT_NODE) {
printf("Element: %s\n", node->name);
}
}
xmlFreeDoc(doc);
xmlCleanupParser();
return 0;
}
案例十六:机器学习
16.1 数据预处理
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void preprocess_data(double **data, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] = (data[i][j] - mean(data[i], cols)) / std_dev(data[i], cols);
}
}
}
double mean(double *data, int size) {
double sum = 0.0;
for (int i = 0; i < size; i++) {
sum += data[i];
}
return sum / size;
}
double std_dev(double *data, int size) {
double mean_val = mean(data, size);
double sum = 0.0;
for (int i = 0; i < size; i++) {
sum += pow(data[i] - mean_val, 2);
}
return sqrt(sum / size);
}
16.2 线性回归
”`c
#include
void linear_regression(double **X, double **y, int rows, int cols) {
double *theta = (double *)malloc(cols * sizeof(double));
double *X_transpose = (double *)malloc(rows * cols * sizeof(double));
double *X_transpose_X = (double *)malloc(cols * cols * sizeof(double));
double *X_transpose_X_inv = (double *)malloc(cols * cols *
