Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 05_kn
diff options
context:
space:
mode:
Commitbf2b3c1b31b22fc3d035c2e116d0c6a471629ba1 (patch)
Author
 Justsvetoslavov <justsvetoslavov@gmail.com>
2021-11-05 13:36:01 +0200
Committer
 Justsvetoslavov <justsvetoslavov@gmail.com>
2021-11-05 13:36:01 +0200
Files4ad08515fd48bf90d95666c0a7cb5045013882a4 /Sem. 05_kn
Introduction_to_programming_FMI-bf2b3c1b31b22fc3d035c2e116d0c6a471629ba1.tar
Introduction_to_programming_FMI-bf2b3c1b31b22fc3d035c2e116d0c6a471629ba1.tar.gz
Introduction_to_programming_FMI-bf2b3c1b31b22fc3d035c2e116d0c6a471629ba1.zip
Parent627534742b0fcc94132d4bb13d6a4cc0b51c3d88 (diff)
Parent2d690dadaf0dc606c59ae688011bac2af48d5d69 (diff)
Merge branch 'main' of https://github.com/Angeld55/Introduction_to_programming_FMI
3 files changed, 292 insertions, 0 deletions
-rw-r--r--Sem. 05_kn/BAndC.cpp115
-rw-r--r--Sem. 05_kn/draw58
-rw-r--r--Sem. 05_kn/refferences.cpp119
diff --git a/Sem. 05_kn/BAndC.cpp b/Sem. 05_kn/BAndC.cpp
new file mode 100644
index 0000000..30f8a3d
--- /dev/null
+++ b/Sem. 05_kn/BAndC.cpp
@@ -0,0 +1,115 @@
+#include <iostream>
+using namespace std;
+
+
+const int DIGITS_COUNT = 4;
+
+int getNumLen(int n)
+{
+ int count = 0;
+ while (n != 0)
+ {
+ count++;
+ n /= 10;
+ }
+ return count;
+}
+
+bool containsDigit(int n, int digit)
+{
+ while (n != 0)
+ {
+ int lastDigit = n % 10;
+
+ if (digit == lastDigit)
+ return true;
+
+ n /= 10;
+ }
+ return false;
+}
+bool containsDiffrentDigits(int n)
+{
+ while (n >= 10)
+ {
+ int lastDigit = n % 10;
+ n /= 10;
+
+ if (containsDigit(n, lastDigit))
+ return false;
+ }
+ return true;
+}
+
+bool isValid(int n)
+{
+ return getNumLen(n) == DIGITS_COUNT && containsDiffrentDigits(n);
+}
+
+void userInput(int& toGuess)
+{
+ do
+ {
+ cin >> toGuess;
+ } while (!isValid(toGuess));
+}
+
+int getBulls(int toGuess, int myGuess)
+{
+ int bulls = 0;
+ while (toGuess != 0)
+ {
+ int toGuessLastDigit = toGuess % 10;
+ int myGuessLastDigit = myGuess % 10;
+
+ if (toGuessLastDigit == myGuessLastDigit)
+ bulls++;
+
+ toGuess /= 10;
+ myGuess /= 10;
+ }
+ return bulls;
+}
+
+int getCows(int toGuess, int myGuess, int bulls)
+{
+ int cows = 0;
+ while (myGuess != 0)
+ {
+ int lastDigit = myGuess % 10;
+
+ if (containsDigit(toGuess, lastDigit))
+ cows++;
+
+ myGuess /= 10;
+ }
+ return cows - bulls;
+}
+
+void getBullsAndCows(int toGuess, int myGuess, int& bulls, int& cows)
+{
+ bulls = getBulls(toGuess, myGuess);
+ cows = getCows(toGuess, myGuess, bulls);
+}
+
+int main()
+{
+ int toGuess;
+ userInput(toGuess);
+
+ cout << "Try to guess:" << 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_kn/draw b/Sem. 05_kn/draw
new file mode 100644
index 0000000..652a201
--- /dev/null
+++ b/Sem. 05_kn/draw
@@ -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
new file mode 100644
index 0000000..47e4fba
--- /dev/null
+++ b/Sem. 05_kn/refferences.cpp
@@ -0,0 +1,119 @@
+#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();
+
+
+}