add cviko 3
This commit is contained in:
parent
6c320c8763
commit
03a90c10be
6 changed files with 328 additions and 0 deletions
10
Cviko3_1/Makefile
Normal file
10
Cviko3_1/Makefile
Normal file
|
@ -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-*
|
145
Cviko3_1/main.c
Normal file
145
Cviko3_1/main.c
Normal file
|
@ -0,0 +1,145 @@
|
|||
#include "types.h"
|
||||
#include <stdio.h>
|
||||
|
||||
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
|
18
Cviko3_1/types.h
Normal file
18
Cviko3_1/types.h
Normal file
|
@ -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
|
27
Cviko3_2/Makefile
Normal file
27
Cviko3_2/Makefile
Normal file
|
@ -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-*
|
114
Cviko3_2/main.c
Normal file
114
Cviko3_2/main.c
Normal file
|
@ -0,0 +1,114 @@
|
|||
#include "types.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* 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 <int> <double> <string>
|
||||
*
|
||||
* To convert <string> to <int>, use function atoi()
|
||||
* (https://cplusplus.com/reference/cstdlib/atoi/)
|
||||
*
|
||||
* To convert <string> to <double>, 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 <int> <double> <string>
|
||||
*
|
||||
* @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 <int> <double> <string>
|
||||
*
|
||||
* @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
|
14
Cviko3_2/types.h
Normal file
14
Cviko3_2/types.h
Normal file
|
@ -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
|
Loading…
Reference in a new issue