練習1:數據類型
請寫出以下幾個數據的數據類型
整數 a
a 的地址
存放a的數組 b
存放a的地址的數組
b的地址
c的地址
指向 printf 函數的指針 d
存放 d的數組
-
整數
a
的類型
數據類型是int
-
a
的地址
數據類型是int*
(指向int
類型的指針) -
存放
a
的數組b
數據類型是int[]
(整型數組) -
存放
a
的地址的數組c
數據類型是int*[]
(指針數組,每個元素是指向int
的指針)
c的數據類型是指針數組類型,可以表示為:int * c[ ]?= {&a}; -
b
的地址
指向整型數組的指針
b的數據類型是int(*)[](
數組指針),b
的地址可以表示為: b, &b, &b[0] -
c
的地址
數據類型是int*(*)[]
(指向指針數組的指針) -
指向
printf
函數的指針d
printf
函數的類型是int(*)(const char*, ...)
,d
的數據類型是int(*)(const char*, ...)
-
存放
d
的數組
數據類型是int(*[])(const char*, ...)
(函數指針數組,每個元素是指向printf
函數的指針)。#include <stdio.h>int main() {// 打印 "Hello, World!" 到控制臺printf("Hello, World!\n");// 定義一個整型變量 a,并初始化為 10int a = 10;// 打印變量 a 的地址printf("a的地址是:%p\n", &a);// 定義一個整型數組 b,并將 a 的值存入數組中int b[] = {a};// 定義一個整型指針 c,并將 a 的地址賦值給它int * c[] = {&a};// 下面的注釋是對 b 和 c 的說明// b, &b, &b[0]; // b 是數組名,&b 是數組的地址,&b[0] 是數組第一個元素的地址// c, &c, &c[0]; // c 是指針,&c 是指針的地址,&c[0] 是指針指向的第一個元素的地址// 定義一個指向 printf 函數的指針 funcs_dint (*funcs_d)(const char*, ...) = printf; // 指向printf函數的指針// 定義一個數組 d,存放指向 printf 函數的指針int (*d[])(const char*, ...) = {printf}; // 存放指向printf函數的指針的數組return 0; // 返回 0,表示程序正常結束 }
?練習2:遞歸
請用遞歸實現計算:1+1/3-1/5+1/7-1/9+…. 1/n 的值,n通過鍵盤輸入
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;double rec_add(int n)
{double sum=0;if (n==1){return 1;}else{double sum=1.0/n;if ((n/2)%2==0){return sum+rec_add(n-2);}else{return -sum+rec_add(n-2); }}return sum;
}int main(int argc, const char *argv[])
{int n=0;printf("輸入n(奇數):");scanf("%d",&n);if (n<=0 ||n%2==0){printf("重新輸入一個正奇數\n");return 1;}double result=rec_add(n);printf("result=%f\n",result);return 0;
}
練習3:寫一個雙向鏈表的快速排序函數
#include <stdio.h>
#include <stdlib.h>// 雙向鏈表節點結構體
typedef struct Node {int data;struct Node *prev;struct Node *next;
} Node;// 創建新的節點
Node* create_node(int data) {Node *new_node = (Node*)malloc(sizeof(Node));new_node->data = data;new_node->prev = new_node->next = NULL;return new_node;
}// 在鏈表尾部插入節點
void append(Node **head, int data) {Node *new_node = create_node(data);if (*head == NULL) {*head = new_node;} else {Node *temp = *head;while (temp->next != NULL) {temp = temp->next;}temp->next = new_node;new_node->prev = temp;}
}// 打印鏈表
void print_list(Node *head) {Node *temp = head;while (temp != NULL) {printf("%d ", temp->data);temp = temp->next;}printf("\n");
}// 切分鏈表并返回基準節點
Node* partition(Node *low, Node *high) {int pivot = high->data;Node *i = low->prev;Node *j = low;while (j != high) {if (j->data <= pivot) {i = (i == NULL) ? low : i->next;int temp = i->data;i->data = j->data;j->data = temp;}j = j->next;}i = (i == NULL) ? low : i->next;int temp = i->data;i->data = high->data;high->data = temp;return i;
}// 快速排序遞歸函數
void quick_sort(Node *low, Node *high) {if (low != NULL && high != NULL && low != high && low != high->next) {Node *p = partition(low, high);quick_sort(low, p->prev);quick_sort(p->next, high);}
}// 獲取鏈表的最后一個節點
Node* get_tail(Node *head) {while (head != NULL && head->next != NULL) {head = head->next;}return head;
}int main() {Node *head = NULL;// 插入一些數據到鏈表append(&head, 5);append(&head, 3);append(&head, 8);append(&head, 4);append(&head, 1);append(&head, 7);printf("Original list: ");print_list(head);Node *tail = get_tail(head);// 執行快速排序quick_sort(head, tail);printf("Sorted list: ");print_list(head);return 0;
}