-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
931e9fe
commit 692ca7b
Showing
11 changed files
with
603 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/**************************************************************************** | ||
* This file is part of the "Introduction to programming" course. FMI 2019/20 | ||
*****************************************************************************/ | ||
|
||
/** | ||
* @file crossword.cpp | ||
* @author Ivan Filipov | ||
* @author Kristian Krastev | ||
* @author Vasilena Peycheva | ||
* @date 01.2020 | ||
* @brief Solution for advanced_string tasks from practice 10. | ||
*/ | ||
|
||
#include <iostream> | ||
|
||
const int MAX = 16; | ||
|
||
// fill the crossword puzzle with words | ||
void fill(char cross[][MAX], int size) { | ||
|
||
for (size_t i = 0; i < size - 1 ; i++) | ||
std::cin >> cross[i]; | ||
} | ||
|
||
void print(char cross[][MAX], int size) { | ||
|
||
for (size_t i = 0; i < size - 1; i++) { | ||
for (size_t j = 0; j < size; j++) | ||
std::cout << cross[i][j]; | ||
|
||
std::cout << '\n'; | ||
} | ||
} | ||
|
||
// check if given row and column are filled with the same elements | ||
bool check_word(char cross[][MAX], int size, int row, int col) { | ||
|
||
for (size_t i = 0; i < size -1; i++) | ||
if (cross[row][i] != cross[i][col]) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
// check how many rows and columns match | ||
int check_cross(char cross[][MAX], int size) { | ||
|
||
int result = 0; | ||
for (size_t i = 0; i < size - 1; i++) { | ||
for (size_t j = 0; j < size - 1; j++) | ||
if (check_word(cross, size, i, j)) | ||
result++; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
int main() { | ||
|
||
char cross[MAX][MAX]; | ||
int size; | ||
std::cin >> size; | ||
fill(cross, size); | ||
|
||
std::cout << "\nThe cross looks: \n"; | ||
print(cross, size); | ||
std::cout << "\nYou have: " << check_cross(cross, size) << " matching words! \n"; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/**************************************************************************** | ||
* This file is part of the "Introduction to programming" course. FMI 2019/20 | ||
*****************************************************************************/ | ||
|
||
/** | ||
* @file dictionary.cpp | ||
* @author Ivan Filipov | ||
* @author Kristian Krastev | ||
* @author Vasilena Peycheva | ||
* @date 01.2020 | ||
* @brief Solution for advanced_string tasks from practice 10. | ||
*/ | ||
|
||
#include <iostream> // cin, cout | ||
#include <string.h> // strlen, strcat | ||
|
||
const unsigned int WORD_CNT = 3; | ||
const unsigned int MAX_INPUT = 1024; | ||
|
||
bool check_dic(const char* dic[], const char* word) { | ||
for (size_t i = 0; i < WORD_CNT; i++) | ||
if (strcmp(word, dic[i]) == 0) // if the word on position i | ||
return true; //in the dicionary is as same as the given one | ||
|
||
return false; | ||
} | ||
|
||
int main() { | ||
|
||
const char* dic[] = { "cats", "dogs", "animals" }; | ||
|
||
char input[MAX_INPUT]; // "it is raining cats and dogs"; | ||
|
||
std::cin.getline(input, MAX_INPUT); //reading the whole input | ||
|
||
//for each word form it, check into the dictionary | ||
|
||
//takes the first word | ||
char* curr_word = strtok(input, " "); | ||
unsigned int how_many = 0; | ||
while (curr_word != nullptr) { | ||
|
||
if (check_dic(dic, curr_word)) | ||
how_many++; | ||
|
||
// takes the next word form input string | ||
curr_word = strtok(nullptr, " "); | ||
} | ||
|
||
std::cout << how_many << '\n'; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/**************************************************************************** | ||
* This file is part of the "Introduction to programming" course. FMI 2019/20 | ||
*****************************************************************************/ | ||
|
||
/** | ||
* @file sequence.cpp | ||
* @author Ivan Filipov | ||
* @author Kristian Krastev | ||
* @author Vasilena Peycheva | ||
* @date 01.2020 | ||
* @brief Solution for advanced_string tasks from practice 10. | ||
*/ | ||
|
||
#include <iostream> | ||
#include <string.h> | ||
|
||
size_t get_same_symb_cnt(const char* str) { | ||
|
||
size_t cnt = 1; | ||
while (*str != '\0' && *str == *(str + 1)) { | ||
++str; | ||
++cnt; | ||
} | ||
|
||
return cnt; | ||
} | ||
|
||
void resize(char*& str, const char* next_symbols) { | ||
|
||
if (str == nullptr) { | ||
str = new char[strlen(next_symbols) + 1]; | ||
strcpy(str, next_symbols); | ||
return; | ||
} | ||
|
||
size_t len = strlen(str); | ||
size_t more_symb = strlen(next_symbols); | ||
char* new_str = new char[len + more_symb + 1]; | ||
strcpy(new_str, str); | ||
strcat(new_str, next_symbols); | ||
|
||
delete[] str; | ||
str = new_str; | ||
} | ||
|
||
char* get_sequence(size_t n) { | ||
|
||
char* result_str = new char[2]; | ||
strcpy(result_str, "1"); | ||
|
||
const char* temp_ptr = result_str; | ||
|
||
char* temp_str = nullptr; | ||
|
||
char temp[32]; | ||
|
||
for (size_t i = 1; i < n; i++) { | ||
|
||
while (*temp_ptr != '\0') { | ||
|
||
size_t symb_cnt = get_same_symb_cnt(temp_ptr); | ||
|
||
temp[0] = '0' + symb_cnt; | ||
temp[1] = *temp_ptr; | ||
temp[2] = '\0'; | ||
|
||
resize(temp_str, temp); | ||
temp_ptr += symb_cnt; | ||
} | ||
delete[] result_str; | ||
result_str = new char[strlen(temp_str) + 1]; | ||
strcpy(result_str, temp_str); | ||
delete[] temp_str; | ||
temp_str = nullptr; | ||
temp_ptr = result_str; | ||
} | ||
|
||
delete[] temp_str; | ||
return result_str; | ||
} | ||
|
||
int main() { | ||
|
||
char* sequence = get_sequence(5); | ||
|
||
delete[] sequence; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/**************************************************************************** | ||
* This file is part of the "Introduction to programming" course. FMI 2019/20 | ||
*****************************************************************************/ | ||
|
||
/** | ||
* @file yoda_talks.cpp | ||
* @author Ivan Filipov | ||
* @author Kristian Krastev | ||
* @author Vasilena Peycheva | ||
* @date 01.2020 | ||
* @brief Solution for advanced_string tasks from practice 10. | ||
*/ | ||
|
||
#include <iostream> | ||
#include <string.h> // strtok, strlen, strcpy | ||
#include <time.h> // time | ||
#include <stdlib.h> // rand | ||
|
||
const unsigned int MAX_INPUT = 1024; | ||
|
||
// how many spaces are out there + 1 | ||
size_t how_many_words(const char* str) { | ||
|
||
if (str == nullptr) | ||
return 0; | ||
|
||
size_t cnt = 1, i = 0; // at least one word | ||
|
||
while (str[i] != '\0') { | ||
|
||
if (str[i] == ' ') | ||
cnt++; | ||
|
||
i++; | ||
} | ||
|
||
return cnt; | ||
} | ||
|
||
void free_words(char**& words, size_t size) { | ||
|
||
for (size_t i = 0; i < size; i++) | ||
delete[] words[i]; | ||
|
||
delete words; | ||
|
||
words = nullptr; | ||
} | ||
|
||
void yoda_talks(char** words, size_t size) { | ||
|
||
// here we will check the taken words | ||
bool* is_taken_word = new (std::nothrow) bool[size]; | ||
|
||
if (is_taken_word == nullptr) | ||
return; | ||
|
||
// mark all as untaken | ||
for (size_t i = 0; i < size; i++) | ||
is_taken_word[i] = false; | ||
|
||
size_t num_taken = 0; | ||
size_t index; | ||
|
||
// while there is untaken word | ||
while (num_taken < size) { | ||
|
||
index = rand() % size; // trying to take this word | ||
|
||
while (is_taken_word[index]) | ||
index = (index + 1) % size; // try the next one, if outsize array, start from the beg | ||
|
||
// mark it as taken | ||
is_taken_word[index] = true; | ||
num_taken++; | ||
|
||
// print it | ||
std::cout << words[index]; | ||
|
||
if(num_taken != size) // no space after the last word | ||
std::cout << ' '; | ||
} | ||
|
||
delete[] is_taken_word; | ||
} | ||
|
||
int main() { | ||
|
||
char buff[MAX_INPUT]; | ||
|
||
std::cin.getline(buff, MAX_INPUT); | ||
|
||
size_t words_cnt = how_many_words(buff); | ||
|
||
// allocating pointer for each string ( word ) | ||
char** words = new char*[words_cnt]; | ||
|
||
// taking the first word from the buff | ||
char* word = strtok(buff, " "); | ||
|
||
int i = 0; | ||
while (word != nullptr) { | ||
|
||
// allocating memory for the current word | ||
words[i] = new (std::nothrow) char[strlen(word) + 1]; | ||
|
||
if (words[i] == nullptr) { | ||
|
||
std::cerr << "can't allocate memory :( !\n"; | ||
free_words(words, i - 1); | ||
return -1; | ||
} | ||
|
||
// copying its content | ||
strcpy(words[i], word); | ||
|
||
i++; | ||
|
||
// taking the next word | ||
word = strtok(nullptr, " "); | ||
} | ||
|
||
// makes us sure that all the memory is successfully allocated and copied | ||
// for (int j = 0; j < i; j++) | ||
// std::cout << words[j] << std::endl; | ||
|
||
srand(time(nullptr)); | ||
|
||
yoda_talks(words, words_cnt); | ||
|
||
free_words(words, words_cnt); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.