Skip to content

Commit

Permalink
Minh họa sử dụng gvec với số, chuỗi, và cấu trúc
Browse files Browse the repository at this point in the history
  • Loading branch information
bangoc committed Jan 15, 2022
1 parent 38450f3 commit f431cfb
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
5 changes: 4 additions & 1 deletion examples/gvec/CMakeLists.txt
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()
30 changes: 30 additions & 0 deletions examples/gvec/gvec_i_demo.c
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;
}
52 changes: 52 additions & 0 deletions examples/gvec/gvec_pp_demo.c
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;
}
36 changes: 36 additions & 0 deletions examples/gvec/gvec_s_demo.c
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;
}

0 comments on commit f431cfb

Please sign in to comment.