Skip to content

Commit 491bfc3

Browse files
committed
添加Makefile自动化构建
1 parent 64b3737 commit 491bfc3

File tree

8 files changed

+68
-13
lines changed

8 files changed

+68
-13
lines changed

Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#IDIR =.
2+
#CC=gcc
3+
#CFLAGS=-I$(IDIR)
4+
5+
#ODIR=obj
6+
#LDIR =../lib
7+
8+
#LIBS=-lm
9+
10+
_DEPS = utils.h
11+
#DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
12+
DEPS = $(_DEPS)
13+
14+
BIN = main
15+
16+
_OBJ = $(BIN).c utils.c $(subst ./, ,$(wildcard ./chap*/*.c))
17+
#OBJ = $(patsubst %.c,$(ODIR)/%.o,$(_OBJ))
18+
OBJ = $(patsubst %.c, %.o, $(_OBJ))
19+
20+
$(BIN): $(OBJ)
21+
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
22+
23+
#$(BIN).o: main.c $(DEPS)
24+
# $(CC) -c -o $@ $< $(CFLAGS)
25+
26+
%.o: %.c %.h $(DEPS)
27+
$(CC) -c -o $@ $< $(CFLAGS)
28+
29+
.PHONY: clean run
30+
31+
run:
32+
./$(BIN)
33+
34+
clean:
35+
#rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~
36+
rm -f $(OBJ) $(BIN)

chap02/merge_sort.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
#include <limits.h>
66
#include <stdlib.h>
7+
#include "../utils.h"
78
void merge_sort(int nums[], int len) {
89
if (len == 1) return;
910
int mid = len/2;

chap06/heap_sort.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "heap_sort.h"
77
#include <limits.h>
8+
#include "../utils.h"
89
/**
910
* 维护堆的性质
1011
* 前置条件:i的左右子树都已经是最大堆
@@ -24,9 +25,7 @@ void max_heapify(int nums[], int heap_size, int i) {
2425
max = r;
2526
}
2627
if (max != i) {
27-
int tmp = nums[i];
28-
nums[i] = nums[max];
29-
nums[max] = tmp;
28+
swap(nums, i, max);
3029
max_heapify(nums, heap_size, max);
3130
}
3231
}

chap07/quick_sort.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,25 @@
44
*/
55

66
#include "quick_sort.h"
7+
#include "../utils.h"
78

8-
int partition(int nums[], int len) {
9-
int small_top = -1;
10-
int key = nums[len-1];
11-
for (int i = 0; i < len; i++) {
12-
if ()
9+
int partition(int nums[], int begin, int end) {
10+
int small_top = begin;
11+
int key = nums[end-1];
12+
for (int i = begin; i < end; i++) {
13+
if (nums[i] < key) {
14+
swap(nums, small_top, i);
15+
small_top++;
16+
}
1317
}
18+
swap(nums, small_top, end-1);
19+
return small_top;
1420
}
1521

16-
void quick_sort(int nums[], int len) {
17-
22+
void quick_sort(int nums[], int begin, int end) {
23+
if (begin < end) {
24+
int p = partition(nums, begin, end);
25+
quick_sort(nums, begin, p);
26+
quick_sort(nums, p+1, end);
27+
}
1828
}

chap07/quick_sort.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef QUICK_SORT_H
22
#define QUICK_SORT_H
33

4-
void quick_sort(int nums[], int len);
4+
void quick_sort(int nums[], int begin, int end);
55

66
#endif

main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
#include "chap02/merge_sort.h"
44
#include "chap04/maximum_subarray.h"
55
#include "chap06/heap_sort.h"
6+
#include "chap07/quick_sort.h"
67
#include <stdio.h>
78

89
int main() {
910
//int nums[] = {13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7};
1011
int nums[] = {1, 0, 4, 3, 7, 6, 5, 2, 9, 8};
11-
heap_sort(nums, 10);
12+
print_array(nums, 10);
13+
quick_sort(nums, 0, 10);
1214
// subarr sa = maximum_subarray(nums, 0, 4);
1315
print_array(nums, 10);
1416
//printf("left: %d, right: %d, sum: %d\n", sa.left, sa.right, sa.sum);

utils.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ void print_array(int nums[], int len) {
1616
}
1717
printf("]\n");
1818
}
19+
20+
void swap(int nums[], int a, int b) {
21+
if (a == b) return;
22+
int tmp = nums[a];
23+
nums[a] = nums[b];
24+
nums[b] = tmp;
25+
}

utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
#define UTILS_H
33

44
void print_array(int nums[], int len);
5-
5+
void swap(int nums[], int a, int b);
66
#endif

0 commit comments

Comments
 (0)