forked from bangoc/cgen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minh họa sử dụng gvec với số, chuỗi, và cấu trúc
- Loading branch information
Showing
4 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
add_executable(gvec_demo gvec_demo.c) | ||
add_executable(gvec_i_demo gvec_i_demo.c) | ||
add_executable(gvec_s_demo gvec_s_demo.c) | ||
add_executable(gvec_pp_demo gvec_pp_demo.c) | ||
|
||
foreach (p gvec_demo) | ||
foreach (p gvec_demo gvec_i_demo gvec_s_demo gvec_pp_demo) | ||
target_link_libraries(${p} bkc) | ||
endforeach() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
(C) Nguyen Ba Ngoc 2022 | ||
Minh họa sắp xếp mảng số nguyên với gvec | ||
*/ | ||
|
||
#include "cgen.h" | ||
|
||
#include <stdio.h> | ||
|
||
int main() { | ||
printf("Nhập các số không âm, hoặc nhập -1 để kết thúc: \n"); | ||
gvec_t vec = gvec_create(0, NULL); | ||
int x; | ||
for (;;) { | ||
scanf("%d", &x); | ||
if (x < 0) { | ||
break; | ||
} | ||
gvec_append(vec, gtype_l(x)); | ||
} | ||
printf("Bạn đã nhập %ld giá trị không âm\n", gvec_size(vec)); | ||
gvec_qsort(vec, gtype_qsort_l); | ||
printf("Các giá trị theo thứ tự tăng dần: "); | ||
gvec_traverse(value, vec) { | ||
printf(" %ld", value->l); | ||
} | ||
printf("\n"); | ||
gvec_free(vec); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
(C) Nguyen Ba Ngoc 2022 | ||
Minh họa sắp xếp mảng cấu trúc point với gvec | ||
*/ | ||
|
||
#include "cgen.h" | ||
|
||
#include <stdio.h> | ||
|
||
struct point { | ||
double x, y; | ||
}; | ||
|
||
#define pp(val) ((struct point*)(((gtype*)val)->v)) | ||
int gtype_qsort_point(const void *v1, const void *v2) { | ||
if (pp(v1)->x < pp(v2)->x) { | ||
return -1; | ||
} else if (pp(v1)->x > pp(v2)->x) { | ||
return 1; | ||
} else if (pp(v1)->y < pp(v2)->y) { | ||
return -1; | ||
} else if (pp(v1)->y > pp(v2)->y) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
#undef pp | ||
|
||
int main() { | ||
printf("Nhập các các điểm trên mặt phẳng hoặc điểm có tọa độ (0, 0) để kết thúc: \n"); | ||
gvec_t vec = gvec_create(0, gtype_free_v); | ||
struct point *p; | ||
for (;;) { | ||
p = malloc(sizeof(struct point)); | ||
scanf("%lf%lf", &p->x, &p->y); | ||
if (p->x == 0 && p->y == 0) { | ||
free(p); | ||
break; | ||
} | ||
gvec_append(vec, gtype_v(p)); | ||
} | ||
printf("Bạn đã nhập: %ld điểm\n", gvec_size(vec)); | ||
gvec_qsort(vec, gtype_qsort_point); | ||
printf("Các điểm theo thứ tự tăng dần (so sánh x sau đó so sánh y): "); | ||
gvec_traverse(value, vec) { | ||
struct point *p = value->v; | ||
printf("\n(%.2lf, %.2lf)", p->x, p->y); | ||
} | ||
printf("\n"); | ||
gvec_free(vec); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
(C) Nguyen Ba Ngoc 2022 | ||
Minh họa sắp xếp vec-tơ chuỗi ký tự với gvec | ||
*/ | ||
|
||
#include "cgen.h" | ||
#include "ext/io.h" | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
int main() { | ||
printf("Nhập vào các chuỗi ký tự hoặc STOP để kết thúc: \n"); | ||
gvec_t vec = gvec_create(0, gtype_free_s); | ||
char *line = NULL; | ||
for (;;) { | ||
if (!cgetline(&line, NULL, stdin)) { | ||
printf("Kết thúc tệp hoặc lỗi nhập.\n"); | ||
break; | ||
} | ||
if (strcmp(line, "STOP\n") == 0) { | ||
break; | ||
} | ||
gvec_append(vec, gtype_s(strdup(line))); | ||
} | ||
printf("Bạn đã nhập: %ld chuỗi ký tự\n", gvec_size(vec)); | ||
gvec_qsort(vec, gtype_qsort_s); | ||
printf("Các chuỗi đã nhập theo thứ tự tăng dần: "); | ||
gvec_traverse(value, vec) { | ||
printf("%s", value->s); | ||
} | ||
printf("\n"); | ||
gvec_free(vec); | ||
free(line); | ||
return 0; | ||
} |