diff options
sem 5
5 files changed, 246 insertions, 119 deletions
| -rw-r--r-- | Sem. 05/BullsAndColls.cpp | 132 | ||||
| -rw-r--r-- | Sem. 05/references.cpp | 56 | ||||
| -rw-r--r-- | Sem. 05/task15_sem3.cpp (renamed from Sem. 05_kn/draw) | 0 | ||||
| -rw-r--r-- | Sem. 05/task16_sem3.cpp | 58 | ||||
| -rw-r--r-- | Sem. 05_kn/refferences.cpp | 119 |
diff --git a/Sem. 05/BullsAndColls.cpp b/Sem. 05/BullsAndColls.cpp new file mode 100644 index 0000000..93594b6 --- /dev/null +++ b/Sem. 05/BullsAndColls.cpp @@ -0,0 +1,132 @@ +#include <iostream> +using namespace std; + +// Задача: +// Играта бикове и крави. +// Въвежда се число от потребителя. (скриваме го) + +// Приемаме заявки от вида : +// x +// 2 бика и 2 крави. +const int DIGITS_COUNT = 5; + + +int getNumberLength(int n) +{ + int count = 0; + + if (n == 0) + return 1; + + while (n != 0) + { + n /= 10; + count++; + } + return count; +} + +bool containsDigit(int n, int searchedDigit) +{ + while (n != 0) + { + int lastDigit = n % 10; + + if (lastDigit == searchedDigit) + return true; + + n /= 10; + } + return false; +} + +bool onlyDiffrentDigits(int n) +{ + while (n != 0) + { + int lastDigit = n % 10; + n /= 10; + + if (containsDigit(n, lastDigit)) + return false; + } + + return true; +} + +bool isValid(int n) +{ + return getNumberLength(n) == DIGITS_COUNT && onlyDiffrentDigits(n); +} + +void userInput(int& input) +{ + do + { + cin >> input; + } while (!isValid(input)); +} + +int getBulls(int toGuess, int myGuess) +{ + int count = 0; + + while (toGuess != 0) + { + int toGuessLastDigit = toGuess % 10; + int myGuessLastDigit = myGuess % 10; + + if (toGuessLastDigit == myGuessLastDigit) + count++; + + toGuess /= 10; + myGuess /= 10; + } + return count; +} + +int getSumOfBullsAndCows(int toGuess, int myGuess) +{ + int count = 0; + while (myGuess != 0) + { + int lastDigit = myGuess % 10; + + if (containsDigit(toGuess, lastDigit)) + count++; + + myGuess /= 10; + } + + return count; +} + +void getBullsAndCows(int toGuess, int myGuess, int& bulls, int& cows) +{ + bulls = getBulls(toGuess, myGuess); + cows = getSumOfBullsAndCows(toGuess, myGuess) - bulls; +} + + +int main() +{ + int toGuess; + userInput(toGuess); + + cout << "Success! Try to guess the number:" << endl; + + int bulls = 0; + int cows = 0; + + while (bulls != DIGITS_COUNT) + { + int myGuess; + userInput(myGuess); + getBullsAndCows(toGuess, myGuess, bulls, cows); + + cout << "Bulls: " << bulls << " " << "Cows: " << cows << endl; + } + cout << "Success!" << endl; + +} + diff --git a/Sem. 05/references.cpp b/Sem. 05/references.cpp new file mode 100644 index 0000000..fc89cb2 --- /dev/null +++ b/Sem. 05/references.cpp @@ -0,0 +1,56 @@ +#include <iostream> +using namespace std; + +// lvalue ("връщане на променлива") +// rvalue ("връщане на константа") + +//Референция - друго име за някоя променлива + +//Пример: +//Напишете функция, която приема цяло число и връща най-голямата и най-малката цифра. + +void getMaxAndMinDigit(int n, int& maxDigit, int& minDigit) +{ + int max = 0; + int min = 9; + + while (n != 0) + { + int lastDigit = n % 10; + + if (lastDigit > max) + max = lastDigit; + + if (lastDigit < min) + min = lastDigit; + + n /= 10; + } + maxDigit = max; + minDigit = min; + +} + + +int f1(int& n) //-връщат константа (rvalue) +{ + n++; + return n; +} +int& f2(int& n) //--връщат променлива (lvalue). +{ + n++; + return n; +} + +///!!!!!!!!!!! error +int& f3() +{ + int a = 10; + return a; +} + +int main() +{ + //cout << ++f(); +} diff --git a/Sem. 05_kn/draw b/Sem. 05/task15_sem3.cpp index 652a201..652a201 100644 --- a/Sem. 05_kn/draw +++ b/Sem. 05/task15_sem3.cpp diff --git a/Sem. 05/task16_sem3.cpp b/Sem. 05/task16_sem3.cpp new file mode 100644 index 0000000..652a201 --- /dev/null +++ b/Sem. 05/task16_sem3.cpp @@ -0,0 +1,58 @@ +#include <iostream> +using namespace std; + +int main() +{ + int n; + cin >> n; + + int spacesCount = 2 * (n - 1) + n + 2 * (n - 1); + int toGuess; + for (int i = 1; i <= n - 1; i++) + { + for (int j = 1; j <= i; j++) + cout << j << " "; + + for (int j = 0; j < spacesCount; j++) + cout << " "; + + for (int j = i; j >= 1; j--) + cout << " " << j; + + spacesCount -= 4; + + cout << endl; + + } + + //mid + for (int i = 1; i <= n; i++) + cout << i << " "; + for (int i = 0; i < n; i++) + cout << '-'; + for (int i = n; i >= 1; i--) + cout << " " << i; + cout << endl; + + + //down + spacesCount += 4; + for (int i = n - 1; i >= 0; i--) + { + for (int j = 1; j <= i; j++) + cout << j << " "; + + for (int j = 0; j < spacesCount; j++) + cout << " "; + + for (int j = i; j >= 1; j--) + cout << " " << j; + + spacesCount += 4; + + cout << endl; + + } + + +} diff --git a/Sem. 05_kn/refferences.cpp b/Sem. 05_kn/refferences.cpp deleted file mode 100644 index 47e4fba..0000000 --- a/Sem. 05_kn/refferences.cpp +++ /dev/null @@ -1,119 +0,0 @@ -#include <iostream> -using namespace std; - - -int f(int n) -{ - n++; - return n; -} - -// lvalue ("връщане на променлива") -// rvalue ("връщане на константа") - -//Референция - друго име за някоя променлива - -// Пример: -// Напишете функция, която приема цяло число и връща най-голямата и най-малката цифра. - -void getMaxAndMinDigit(int n, int& maxDigit, int& minDigit) -{ - int max = 0; - int min = 9; - - while (n != 0) - { - int lastDigit = n % 10; - - if (lastDigit > max) - max = lastDigit; - - if (lastDigit < min) - min = lastDigit; - - n /= 10; - } - maxDigit = max; - minDigit = min; - -} - -//! - -int& f(int& n) //--няма да върне копие. -{ - n++; - return n; -} - -int main() -{ - int n; - cin >> n; - - int max, min; - - getMaxAndMinDigit(n, max, min); - cout << "Min digit " << min << " " << "max digit " << max << endl; -} - - - - - -#include <iostream> -using namespace std; - -// lvalue ("връщане на променлива") -// rvalue ("връщане на константа") - -//Референция - друго име за някоя променлива - -//Пример: -// Напишете функция, която приема цяло число и връща най-голямата и най-малката цифра. - -void getMaxAndMinDigit(int n, int& maxDigit, int& minDigit) -{ - int max = 0; - int min = 9; - - while (n != 0) - { - int lastDigit = n % 10; - - if (lastDigit > max) - max = lastDigit; - - if (lastDigit < min) - min = lastDigit; - - n /= 10; - } - maxDigit = max; - minDigit = min; - -} - - -int f1(int& n) //-връщат константа (rvalue) -{ - n++; - return n; -} -int& f2(int& n) //--връщат променлива (lvalue). -{ - n++; - return n; -} -///!!!!!!!!!!! error -int& f3() -{ - int a = 10; - return a; -} -int main() -{ - cout << ++f(); - - -} |
