diff --git a/week03/.clang-format b/week03/.clang-format new file mode 100644 index 0000000..5bf2f83 --- /dev/null +++ b/week03/.clang-format @@ -0,0 +1,2 @@ +BasedOnStyle: LLVM +IndentWidth: 4 \ No newline at end of file diff --git a/week03/Makefile b/week03/Makefile new file mode 100644 index 0000000..9dc6271 --- /dev/null +++ b/week03/Makefile @@ -0,0 +1,13 @@ +all: ex1 ex2 ex3 ex4 + +ex1: ex1.c + gcc $< -o $@ + +ex2: ex2.c + gcc $< -o $@ -lm + +ex3: ex3.c + gcc $< -o $@ -lm + +ex4: ex4.c + gcc $< -o $@ -lm diff --git a/week03/a.out b/week03/a.out new file mode 100755 index 0000000..35d4d40 Binary files /dev/null and b/week03/a.out differ diff --git a/week03/ex1 b/week03/ex1 new file mode 100755 index 0000000..de6b5ff Binary files /dev/null and b/week03/ex1 differ diff --git a/week03/ex1.c b/week03/ex1.c new file mode 100644 index 0000000..d18c788 --- /dev/null +++ b/week03/ex1.c @@ -0,0 +1,33 @@ +#include +#include +#include + +int foo(int age) { + int result; + + time_t t = time(NULL); + result = localtime(&t)->tm_year + 1900 - age; + + return result; +} + +int main() { + const int x = 10; + const int *q = &x; + const int *const p = malloc(sizeof(int) * 5); + for (int *i = (int *)p; i < p + 5; ++i) { + *i = x; + printf("%p%c", i, (i < p + 4 ? ' ' : '\n')); + } + + for (int *i = (int *)p; i < p + 5; ++i) { + scanf("%d", i); + } + + for (int *i = (int *)p; i < p + 5; ++i) { + *i = foo(*i); + printf("%d%c", *i, (i < p + 4 ? ' ' : '\n')); + } + + free((int *)p); +} \ No newline at end of file diff --git a/week03/ex1.sh b/week03/ex1.sh new file mode 100644 index 0000000..1192c72 --- /dev/null +++ b/week03/ex1.sh @@ -0,0 +1,2 @@ +gcc ex1.c -o ex1 +./ex1 \ No newline at end of file diff --git a/week03/ex2 b/week03/ex2 new file mode 100755 index 0000000..c46d35d Binary files /dev/null and b/week03/ex2 differ diff --git a/week03/ex2.c b/week03/ex2.c new file mode 100644 index 0000000..e956394 --- /dev/null +++ b/week03/ex2.c @@ -0,0 +1,28 @@ +#include +#include + +typedef struct Point { + float x, y; +} Point; + +float distance(Point p1, Point p2) { + return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); +} + +float area(Point A, Point B, Point C) { + return 0.5 * fabsf(A.x * B.y - B.x * A.y + B.x * C.y - C.x * B.y + + C.x * A.y - A.x * C.y); +} + +int main() { + Point A = {2.5, 6}; + Point B = {1, 2.2}; + Point C = {10, 6}; + + float f = distance(A, B); + printf("A -- B distance is %f\n", f); + + + float a = area(A, B, C); + printf("Area of triangle ABC is %f\n", a); +} \ No newline at end of file diff --git a/week03/ex2.sh b/week03/ex2.sh new file mode 100644 index 0000000..c30d5a0 --- /dev/null +++ b/week03/ex2.sh @@ -0,0 +1,2 @@ +gcc ex2.c -o ex2 -lm +./ex2 \ No newline at end of file diff --git a/week03/ex3 b/week03/ex3 new file mode 100755 index 0000000..71970d2 Binary files /dev/null and b/week03/ex3 differ diff --git a/week03/ex3.c b/week03/ex3.c new file mode 100644 index 0000000..72a9232 --- /dev/null +++ b/week03/ex3.c @@ -0,0 +1,195 @@ +#include +#include +#include + +struct Directory; +struct File; + +struct File { + int id; + unsigned long size; + char data[2048]; + char name[64]; + + struct Directory *directory; // The parent directory +}; + +struct Directory { + int nf; + int nd; + char path[4096]; + + struct File *files[256]; + struct Directory *directories[8]; +}; + +typedef struct Directory Directory; +typedef struct File File; + +// Operations on files +void add_to_file(File *file, const char *content); +void append_to_file(File *file, const char *content); +void pwd_file(File *file); + +// Operations on directories +void add_file(File *file, Directory *dir); +void add_dir(Directory *dir1, Directory *dir2); // given to you + +// Helper functions +void show_dir(Directory *dir); +void show_file(File *file); +void show_file_detailed(File *file); + +void init_dir(Directory *dir, char *name); +void init_file(File *file, char *name); + +int cur_id = 0; + +int main() { + + char content1[] = "int printf(const char * format, ...);"; + char content2[] = "int main(){printf('Hello World');}"; + char content3[] = "//This is a comment in C language"; + char content4[] = "Bourne Again Shell!"; + + Directory home, bin, root; + init_dir(&root, "/"); + init_dir(&home, "home"); + init_dir(&bin, "bin"); + + // Example: the path of the folder home is /home + + // Add subdirectories + add_dir(&home, &root); + add_dir(&bin, &root); + + File bash, ex31, ex32; + init_file(&bash, "bash"); + init_file(&ex31, "ex3_1.c"); + init_file(&ex32, "ex3_2.c"); + + // + + add_file(&bash, &bin); + add_file(&ex31, &home); + add_file(&ex32, &home); + + add_to_file(&bash, content4); + add_to_file(&ex31, content1); + add_to_file(&ex32, content3); + + append_to_file(&ex31, content2); + + show_dir(&root); + show_file_detailed(&bash); + show_file_detailed(&ex31); + show_file_detailed(&ex32); + + pwd_file(&bash); + pwd_file(&ex31); + pwd_file(&ex32); + + return EXIT_SUCCESS; +} + +// Helper functions + +// Displays the content of the Directory dir +void show_dir(Directory *dir) { + printf("\nDIRECTORY\n"); + printf(" path: %s\n", dir->path); + printf(" files:\n"); + printf(" [ "); + for (int i = 0; i < dir->nf; i++) { + show_file(dir->files[i]); + } + printf("]\n"); + printf(" directories:\n"); + printf(" { "); + + for (int i = 0; i < dir->nd; i++) { + show_dir(dir->directories[i]); + } + printf("}\n"); +} + +// Prints the name of the File file +void show_file(File *file) { printf("%s ", file->name); } + +// Shows details of the File file +void show_file_detailed(File *file) { + printf("\nFILE\n"); + printf(" id: %d\n", file->id); + printf(" name: %s\n", file->name); + printf(" size: %lu\n", file->size); + printf(" data:\n"); + printf(" [ %s ]\n", file->data); +} + +// Implementation: Operations on files + +// Adds the content to the File file +void add_to_file(File *file, const char *content) { + if (file && content) { + strcpy(file->data, content); + file->size = strlen(file->data) + 1; + } +} + +// Appends the content to the File file +void append_to_file(File *file, const char *content) { + if (file && content) { + int sz = strlen(file->data); + strcpy(file->data + sz, content); + file->size = strlen(file->data) + 1; + } +} + +// Prints the path of the File file +void pwd_file(File *file) { + if (file) { + printf("%s/%s\n", file->directory->path, file->name); + } +} + +// Implementation: Operations on directories + +// Adds the File file to the Directory dir +void add_file(File *file, Directory *dir) { + if (file && dir) { + dir->files[dir->nf] = file; + dir->nf++; + + file->directory = dir; + } +} + +// Given to you +// Adds the subdirectory dir1 to the directory dir2 +void add_dir(Directory *dir1, Directory *dir2) { + if (dir1 && dir2) { + dir2->directories[dir2->nd] = dir1; + dir2->nd++; + + int sz1 = strlen(dir1->path), sz2 = strlen(dir2->path); + if (dir2->path[sz2 - 1] == '/') { + memmove(dir1->path + sz2, dir1->path, sz1 + 1); + } else { + memmove(dir1->path + sz2 + 1, dir1->path, sz1 + 1); + *(dir1->path + sz2) = '/'; + } + memcpy(dir1->path, dir2->path, sz2); + } +} + +void init_dir(Directory *dir, char *name) { + dir->nd = dir->nf = 0; + strcpy(dir->path, name); +} + +void init_file(File *file, char *name) { + file->data[0] = '\0'; + file->id = cur_id++; + strcpy(file->name, name); + file->size = 0; +} diff --git a/week03/ex3.sh b/week03/ex3.sh new file mode 100644 index 0000000..c56587a --- /dev/null +++ b/week03/ex3.sh @@ -0,0 +1,2 @@ +gcc ex3.c -o ex3 +./ex3 \ No newline at end of file diff --git a/week03/ex4 b/week03/ex4 new file mode 100755 index 0000000..ce1f908 Binary files /dev/null and b/week03/ex4 differ diff --git a/week03/ex4.c b/week03/ex4.c new file mode 100644 index 0000000..8cc5256 --- /dev/null +++ b/week03/ex4.c @@ -0,0 +1,155 @@ +#include +#include + + +void* aggregate(void* base, size_t size, int n, void* initial_value, void* (*opr)(const void*, const void*)); + +void* addInt(const void* a, const void* b){ + int *result = malloc(sizeof(int)); + *result = *(double*)a + *(int*)b; + return result; +} + +void* addDouble(const void* a, const void* b){ + double *result = malloc(sizeof(double)); + *result = *(double*)a + *(double*)b; + return result; +} + +void* mulInt(const void* a, const void* b){ + int *result = malloc(sizeof(int)); + *result = *(double*)a * *(int*)b; + return result; +} + +void* mulDouble(const void* a, const void* b){ + double *result = malloc(sizeof(double)); + *result = *(double*)a + *(double*)b; + return result; +} + +void* meanInt(const void* a, const void* b){ + double *result = malloc(sizeof(double)); + *result = (*(double*)a + *(int*)b) / 2.0; + return result; +} + +void* meanDouble(const void* a, const void* b){ + double *result = malloc(sizeof(double)); + *result = (*(double*)a + *(double*)b) / 2.0; + return result; +} + +void* aggregate(void* base, size_t size, int n, void* initial_value, void* (*opr)(const void*, const void*)){ + + void* output; + + if (size==sizeof(int)){ // base is a pointer to an integer + if (opr == meanInt) { + output = malloc(sizeof(double)); + *(double *)output = *(double*)initial_value; + for (int i = 0; i < n; ++i) { + double* res = opr(output, ((int*)base) + i); + *(double*)output = *res; + free(res); + } + } else { + output = malloc(sizeof(int)); + *(int *)output = *(int*)initial_value; + for (int i = 0; i < n; ++i) { + int* res = opr(output, ((int*)base) + i); + *(int*)output = *res; + free(res); + } + } + }else{ // base is a pointer to a double + for (int i = 0; i < n; ++i) { + double* res = opr(output, ((double*)base) + i); + *(double*)output = *res; + free(res); + } + } + + return output; +} + + + + +int main(){ + + int* ints = malloc(sizeof(int)*5); + double* doubles = malloc(sizeof(double)*5); + + double *init_mult = malloc(sizeof(double)); + double *init_add = malloc(sizeof(double)); + double *init_mean_int = malloc(sizeof(double)); + double *init_mean_double = malloc(sizeof(double)); + *init_mult = 1; + *init_add = 0; + *init_mean_int = ints[0]; + *init_mean_double = doubles[0]; + + // Addition + + int* result1a; + + result1a = aggregate(ints, sizeof(int), 5, init_add, addInt); + + printf("%d\n", *result1a); + free(result1a); + + double* result2a; + + result1a = aggregate(doubles, sizeof(double), 5, init_add, addDouble); + + printf("%f\n", *result2a); + free(result2a); + + + // Multiplication + + int* result1m; + + //; + result1m = aggregate(ints, sizeof(int), 5, init_mult, mulInt); + + printf("%d\n", *result1m); + free(result1m); + + double* result2m; + + result2m = aggregate(doubles, sizeof(double), 5, init_mult, mulDouble); + printf("%f\n", *result2m); + free(result2m); + + + // Mean + + double* result1mean; + + result1mean = aggregate(ints, sizeof(int), 5, init_mean_int, meanInt); + + printf("%lf\n", *result1mean); + free(result1mean); + + double* result2mean; + + result2mean = aggregate(doubles, sizeof(double), 5, init_mean_double, meanDouble); + + printf("%f\n", *result2mean); + free(result2mean); + + + // free the pointers + //; + free(ints); + free(doubles); + free(init_add); + free(init_mult); + free(init_mean_double); + free(init_mean_int); + + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/week03/ex4.sh b/week03/ex4.sh new file mode 100644 index 0000000..a8c4a2c --- /dev/null +++ b/week03/ex4.sh @@ -0,0 +1,2 @@ +gcc ex4.c -o ex4 +./ex4 \ No newline at end of file