From 03a90c10be053087e012acea660182f7f1aafa77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Thu, 3 Oct 2024 17:17:16 +0200 Subject: [PATCH] add cviko 3 --- Cviko3_1/Makefile | 10 ++++ Cviko3_1/main.c | 145 ++++++++++++++++++++++++++++++++++++++++++++++ Cviko3_1/types.h | 18 ++++++ Cviko3_2/Makefile | 27 +++++++++ Cviko3_2/main.c | 114 ++++++++++++++++++++++++++++++++++++ Cviko3_2/types.h | 14 +++++ 6 files changed, 328 insertions(+) create mode 100644 Cviko3_1/Makefile create mode 100644 Cviko3_1/main.c create mode 100644 Cviko3_1/types.h create mode 100644 Cviko3_2/Makefile create mode 100644 Cviko3_2/main.c create mode 100644 Cviko3_2/types.h diff --git a/Cviko3_1/Makefile b/Cviko3_1/Makefile new file mode 100644 index 0000000..939ae0c --- /dev/null +++ b/Cviko3_1/Makefile @@ -0,0 +1,10 @@ +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 "$@" + +clean: + rm -f main main-* diff --git a/Cviko3_1/main.c b/Cviko3_1/main.c new file mode 100644 index 0000000..bb5563d --- /dev/null +++ b/Cviko3_1/main.c @@ -0,0 +1,145 @@ +#include "types.h" +#include + +void read_print_string() { + // TODO: 1. Read a string from stdin of max-length of 100 characters. + printf("Enter a string (100 chars max):\n"); + char input[101]; + scanf("%100[^\n]", input); + + // TODO: 2. Print read string to stdout. + printf("%s\n", input); + return; +} + +void read_print_string_length() { + // TODO: 1. Read a string from stdin of max-length of 100 characters. + printf("Enter a string (100 chars max):\n"); + char input[101]; + scanf(" %100[^\n]", input); + + // TODO: 2. Print the length of the read string to stdout. (do not count + // '\0'). + for (int i = 0; i < 101; ++i) { + if (input[i] == '\0') { + printf("Length of your string is %d", i); + break; + } + } + return; +} + +void read_print_string_alphnum() { + // TODO: 1. Read a string from stdin of max-length of 100 characters. + printf("Enter a string (100 chars max):\n"); + char input[101]; + scanf(" %100[^\n]", input); + + // TODO: 2. Print both the number of letters (a-z, A-Z) and the number of + // numerals (0-9) in the read string to stdout. + int letters = 0; + int numbers = 0; + for (int i = 0; i < 101; ++i) { + if (input[i] == '\0') { + break; + } + if ((input[i] >= 'a' && input[i] <= 'z') || + (input[i] >= 'A' && input[i] <= 'Z')) { + letters++; + } else if (input[i] >= '0' && input[i] <= '9') { + numbers++; + } + } + printf("Your string contains %d letters and %d numbers", letters, numbers); + return; +} + +void read_print_string_lower() { + // TODO: 1. Read a string from stdin of max-length of 100 characters. + printf("Enter a string (100 chars max):\n"); + char input[101]; + scanf(" %100[^\n]", input); + + // TODO: 2. Print the read string with all letters in lowercase (e.g., A => a) + // to stdout. + for (int i = 0; i < 101; ++i) { + if ((input[i] >= 'A' && input[i] <= 'Z')) { + input[i] += 32; + } + } + printf("%s\n", input); + return; +} + +void read_print_string_replace() { + // TODO: 1. Read a character and a string of max-length of 100 characters from + // stdin. + printf("Enter a single character:\n"); + char c; + scanf(" %c", &c); + + printf("Enter a string (100 chars max):\n"); + char input[101]; + scanf(" %100[^\n]", input); + + // TODO: 2. Print the read string with all occurences of read string replaced + // with '_' (e.g., ('a', "abba") => _bb_) to stdout. + for (int i = 0; i < 101; ++i) { + if (input[i] == '\0') { + break; + } + if (input[i] == c) { + input[i] = '_'; + } + } + printf("%s\n", input); + return; +} + +void read_print_string_compare() { + // TODO: 1. Read 2 string, each of max-length of 100 characters, from stdin. + printf("Enter a string (100 chars max):\n"); + char inputA[101]; + scanf(" %100[^\n]", inputA); + + printf("Enter another string (100 chars max):\n"); + char inputB[101]; + scanf(" %100[^\n]", inputB); + + // TODO: 2. Determine whether strings are equal (two strings are equal if they + // have the same length and characters in both strings on a given index are + // identical). + int is = 1; + for (int i = 0; i < 101; ++i) { + if (inputA[i] == '\0' && inputB[i] == '\0') { + break; + } + if (inputA[i] != inputB[i] || (inputA[i] == '\0') || (inputB[i] == '\0')) { + is = 0; + break; + } + } + printf("%s", (is == 1) ? "The two strings ARE equal" + : "The two strings are NOT equal"); + return; +} + +#ifndef TEST_BUILD + +int main(int argc, char *argv[]) { + printf("-- read_print_string --\n"); + read_print_string(); + printf("\n-- read_print_string_length --\n"); + read_print_string_length(); + printf("\n-- read_print_string_alphnum --\n"); + read_print_string_alphnum(); + printf("\n-- read_print_string_lower --\n"); + read_print_string_lower(); + printf("\n-- read_print_string_replace --\n"); + read_print_string_replace(); + printf("\n-- read_print_string_compare --\n"); + read_print_string_compare(); + return 0; +} + +#endif diff --git a/Cviko3_1/types.h b/Cviko3_1/types.h new file mode 100644 index 0000000..6262ef2 --- /dev/null +++ b/Cviko3_1/types.h @@ -0,0 +1,18 @@ +/** + * Hlavičkový soubor types.h + * + * OBSAH V TOMTO SOUBROU NEUPRAVUJTE! + */ + +#ifndef TYPES_H +#define TYPES_H + +// DEKLAROVANÉ HLAVIČKY FUNKCÍ NIJAK NEMĚŇTE +void read_print_string(); +void read_print_string_length(); +void read_print_string_alphnum(); +void read_print_string_lower(); +void read_print_string_replace(); +void read_print_string_compare(); + +#endif diff --git a/Cviko3_2/Makefile b/Cviko3_2/Makefile new file mode 100644 index 0000000..798f733 --- /dev/null +++ b/Cviko3_2/Makefile @@ -0,0 +1,27 @@ +all: main + +CC = gcc +override CFLAGS += -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-unused-result -Wno-unknown-pragmas -pedantic -lm + +.PHONY: run test submit clean + +main: main.c + $(CC) $(CFLAGS) -O1 ./*.c -o "$@" + +main-debug: main.c + $(CC) $(CFLAGS) -O1 -g ./*.c -o "$@" + +main-test: main.c + $(CC) $(CFLAGS) -DTEST_BUILD=1 ./*.c -o "$@" + +main-solution: main.c + $(CC) $(CFLAGS) -DTEST_BUILD=1 -DSOLUTION_BUILD=1 test.c solution/*.c -o "$@" + +test: + @./.tests/scripts/run_tests.sh + +submit: + @PROJECT_SUBMIT_MODE=sources ECHO_QUIET=1 ./.tests/scripts/run_tests.sh + +clean: + rm -f main main-* diff --git a/Cviko3_2/main.c b/Cviko3_2/main.c new file mode 100644 index 0000000..2ce4d56 --- /dev/null +++ b/Cviko3_2/main.c @@ -0,0 +1,114 @@ +#include "types.h" +#include +#include +#include + +/** + * Safely print first argument provided to the program. Determine its meaning. + * + * @param argc total number of arguments provided to the program + * + * @param argv array/vector of arguments + */ +void print_first_argument(int argc, char *argv[]) { + if (argc < 1) { + return; + } + printf("%s\n", argv[0]); + return; +} + +/** + * Safely print first 4 arguments provided to the program while converting them + * to their corresponding data type. + * + * ./main + * + * To convert to , use function atoi() + * (https://cplusplus.com/reference/cstdlib/atoi/) + * + * To convert to , use function atof() + * (https://cplusplus.com/reference/cstdlib/atof/) + * + * Use the following commmand to execute your program with set of arguments: + * + * ./main 42 13.37 hello_program + * + * @param argc total number of arguments provided to the program + * + * @param argv array/vector of arguments + */ +void print_parse_arguments(int argc, char *argv[]) { + int a; + double b; + int s = (argc<4)?argc:4; + + for (int i = 0; i < s; ++i) { + switch (i) { + case 1: + a = atoi(argv[i]); + printf("%d\n",a); + break; + case 2: + b = atof(argv[i]); + printf("%.3f\n",b); + break; + default: + printf("%s\n",argv[i]); + break; + } + } + return; +} + +/** + * Safely print ALL arguments provided to the program without any conversion. + * + * ./main + * + * @param argc total number of arguments provided to the program + * + * @param argv array/vector of arguments + */ +void print_all_arguments(int argc, char *argv[]) { + for (int i = 0; i < argc; ++i) { + printf("%s\n", argv[i]); + } + return; +} + +/** + * Safely print the largest argument + * + * ./main + * + * @param argc total number of arguments provided to the program + * + * @param argv array/vector of arguments + */ +void print_largest_argument(int argc, char *argv[]) { + char *c = ""; + for (int i = 0; i < argc; ++i) { + if(strlen(argv[i]) > strlen(c)){ + c = argv[i]; + } + } + printf("Largest argument: %s",c); + return; +} + +#ifndef TEST_BUILD + +int main(int argc, char *argv[]) { + printf("\n-- print_first_argument --\n"); + print_first_argument(argc, argv); + printf("\n-- print_parse_arguments --\n"); + print_parse_arguments(argc, argv); + printf("\n-- print_all_arguments --\n"); + print_all_arguments(argc, argv); + printf("\n-- print_largest_argument --\n"); + print_largest_argument(argc, argv); + return 0; +} + +#endif diff --git a/Cviko3_2/types.h b/Cviko3_2/types.h new file mode 100644 index 0000000..0897b98 --- /dev/null +++ b/Cviko3_2/types.h @@ -0,0 +1,14 @@ +/** + * Hlavičkový soubor types.h + * + * OBSAH V TOMTO SOUBROU NEUPRAVUJTE! + */ + +#ifndef TYPES_H +#define TYPES_H + +// DEKLAROVANÉ HLAVIČKY FUNKCÍ NIJAK NEMĚŇTE +void print_first_argument(int argc, char *argv[]); +void print_parse_arguments(int argc, char *argv[]); +void print_all_arguments(int argc, char *argv[]); +#endif