From 157f09015f9b4aa9a447df2f5ccbbea2b1eb6f88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Thu, 26 Sep 2024 22:20:05 +0200 Subject: [PATCH] add cviko 2 --- Cviko2_1/.gitignore | 93 +++++++++++++++++++++++++++++++++++++++ Cviko2_1/Makefile | 13 ++++++ Cviko2_1/main.c | 105 ++++++++++++++++++++++++++++++++++++++++++++ Cviko2_1/types.h | 11 +++++ 4 files changed, 222 insertions(+) create mode 100644 Cviko2_1/.gitignore create mode 100644 Cviko2_1/Makefile create mode 100644 Cviko2_1/main.c create mode 100644 Cviko2_1/types.h diff --git a/Cviko2_1/.gitignore b/Cviko2_1/.gitignore new file mode 100644 index 0000000..f9bf443 --- /dev/null +++ b/Cviko2_1/.gitignore @@ -0,0 +1,93 @@ +# Created by https://www.toptal.com/developers/gitignore/api/c,visualstudiocode,linux +# Edit at https://www.toptal.com/developers/gitignore?templates=c,visualstudiocode,linux + +### C ### +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +# End of https://www.toptal.com/developers/gitignore/api/c,visualstudiocode,linux +main \ No newline at end of file diff --git a/Cviko2_1/Makefile b/Cviko2_1/Makefile new file mode 100644 index 0000000..7668f40 --- /dev/null +++ b/Cviko2_1/Makefile @@ -0,0 +1,13 @@ +all: main + +CC = gcc +override CFLAGS += -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-unused-result -Wno-unknown-pragmas -pedantic -lm + +main: main.c + $(CC) $(CFLAGS) -O1 ./*.c -o "$@" + +main-debug: main.c + $(CC) $(CFLAGS) -O1 -g ./*.c -o "$@" + +clean: + rm -f main main-* diff --git a/Cviko2_1/main.c b/Cviko2_1/main.c new file mode 100644 index 0000000..5e5f293 --- /dev/null +++ b/Cviko2_1/main.c @@ -0,0 +1,105 @@ +#include "types.h" +#include +#include +#include + +void read_print_quadratic() { + // TODO 1. Read 3 numbers: a, b and c which represent known numbers for + // quadratic equation in standard format (ax^2 + bx + c = 0) from stdin. + printf("Enter numbers a, b and c of equation ax^2 +bx + c = 0:\n"); + int a, b, c; + scanf("%d %d %d", &a, &b, &c); + + // TODO 2. Check if equation is quadratic. If so, calculate its discriminant + // and, if possible, print all roots to stdout. + if (a == 0 || (a == 0 && b == 0 && c == 0)) { + printf("Please enter a cooler formula"); + return; + } + + double d = b * b + (-4) * a * c; + printf("Discriminant: %.2f\n", d); + + if (d < 0) { + printf("Discriminant is lower than 0, cannot solve equation in R"); + return; + } + + double rootA, rootB; + + rootA = (-b + sqrt(d)) / (2*a); + rootB = (-b - sqrt(d)) / (2*a); + + if (rootA == rootB) { + printf("Equation has one solution: %.2f", rootA); + } else { + printf("Equation has two solution: %.2f and %.2f", rootA, rootB); + } + return; +} + +void read_print_is_character() { + // TODO 1. Read one character from stdin. + printf("Enter a character:\n"); + char l; + scanf(" %c", &l); + // TODO 2. Determine whether the character represents a letter (a-z, A-Z) or + // not and print the result to stdout. + if ((l >= 'a' && l <= 'z') || (l >= 'A' && l <= 'Z')) { + printf("%c IS a letter (a-z or A-Z)", l); + } else { + printf("%c is NOT a letter (a-z or A-Z)", l); + } + return; +} + +void read_print_factorial() { + printf("Enter a number for calcuation of factorial:\n"); + // TODO 1. Read one number from stdin. + int a; + scanf("%d", &a); + + // TODO 2. Print its factorial (!) to stdout. + for (int i = a - 1; i != 0; --i) { + a *= i; + } + printf("Factorial: %d", a); + return; +} + +void read_print_gcd() { + // TODO 1. Read two numbers from stdin. + printf("Enter two numbers to calculate gcd:\n"); + int a, b; + scanf("%d %d", &a, &b); + + // see https://en.wikipedia.org/wiki/Greatest_common_divisor#Euclidean_algorithm + while (b != 0) { + int temp = b; + b = a % b; + a = temp; + } + + printf("gcd is %d\n", a); + return; +} + +#ifndef TEST_BUILD + +int main() { + printf("\n--read_print_quadratic--\n"); + read_print_quadratic(); + + printf("\n--read_print_is_character--\n"); + read_print_is_character(); + + printf("\n--read_print_factorial--\n"); + read_print_factorial(); + + printf("\n--read_print_gcd--\n"); + read_print_gcd(); + + return 0; +} + +#endif diff --git a/Cviko2_1/types.h b/Cviko2_1/types.h new file mode 100644 index 0000000..a6b7a18 --- /dev/null +++ b/Cviko2_1/types.h @@ -0,0 +1,11 @@ +// OBSAH V TOMTO SOUBROU NEUPRAVUJTE! +#ifndef TYPES_H +#define TYPES_H + + void read_print_quadratic(); + void read_print_is_character(); + void read_print_is_even(); + void read_print_factorial(); + void read_print_gcd(); + +#endif