Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit174852de7b331be9ba32c485ffe97d928778b9cc (patch)
Author
 angeld55 <angel_dimitriev@abv.bg>
2021-11-03 18:25:21 +0200
Committer
 angeld55 <angel_dimitriev@abv.bg>
2021-11-03 18:25:21 +0200
Filesc23441569c9c907e959088fa8bd8e76f70d6be0e
Introduction_to_programming_FMI-174852de7b331be9ba32c485ffe97d928778b9cc.tar
Introduction_to_programming_FMI-174852de7b331be9ba32c485ffe97d928778b9cc.tar.gz
Introduction_to_programming_FMI-174852de7b331be9ba32c485ffe97d928778b9cc.zip
Parent90d1e842ee65040064207e53bf204d4d9b32d547 (diff)
sem5
2 files changed, 235 insertions, 0 deletions
-rw-r--r--Sem. 05_kn/BullsAndCows.cpp116
-rw-r--r--Sem. 05_kn/refferences.cpp119
diff --git a/Sem. 05_kn/BullsAndCows.cpp b/Sem. 05_kn/BullsAndCows.cpp
new file mode 100644
index 0000000..1ab2289
--- /dev/null
+++ b/Sem. 05_kn/BullsAndCows.cpp
@@ -0,0 +1,116 @@
+#include <iostream>
+using namespace std;
+// Играта - бикове и крави.
+
+
+const int DIGITS_COUNT = 3;
+
+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/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();
+
+
+}