Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Homeworks / homework1.cpp
diff options
context:
space:
mode:
1 files changed, 48 insertions, 31 deletions
-rw-r--r--Year 1/Programming introduction/Homeworks/homework1.cpp79
diff --git a/Year 1/Programming introduction/Homeworks/homework1.cpp b/Year 1/Programming introduction/Homeworks/homework1.cpp
index 1a3d069..c0542ec 100644
--- a/Year 1/Programming introduction/Homeworks/homework1.cpp
+++ b/Year 1/Programming introduction/Homeworks/homework1.cpp
@@ -122,25 +122,22 @@ void ex1()
* Exercise 2
*/
-void setMarmaladeType(int type, int *blueberryMarmalades, int *peachMarmalades, int *rosehipMarmalades)
+void setMarmaladeType(unsigned int type, unsigned int *blueberryMarmalades, unsigned int *peachMarmalades, unsigned int *rosehipMarmalades)
{
if (type % 7 == 0) *blueberryMarmalades += 1;
else if (type % 13 == 0) *peachMarmalades += 1;
else if (type % 17 == 0) *rosehipMarmalades += 1;
}
-void ex2()
+void readCodesAndCountMarmalades(unsigned int archiveLength, unsigned int *blueberryMarmalades, unsigned int *peachMarmalades, unsigned int *rosehipMarmalades)
{
- int archiveLength;
- cin >> archiveLength;
+ archiveLength -= archiveLength % 8; // The last code could be invalid (shorter than 8 digits)
- int blueberryMarmalades = 0, peachMarmalades = 0, rosehipMarmalades = 0;
-
- for (int i = 0, digit, countryDigits, typeDigits, type; i < archiveLength; i++)
+ for (unsigned int i = 0, digit, countryDigits, typeDigits, type; i < archiveLength; i++)
{
cin >> digit;
- if (i % 8 == 0) // Reset at start of ID number
+ if (i % 8 == 0) // Reset at start of every code
{
countryDigits = 1;
typeDigits = type = 0;
@@ -156,10 +153,19 @@ void ex2()
}
else if (typeDigits == 4)
{
- setMarmaladeType(type, &blueberryMarmalades, &peachMarmalades, &rosehipMarmalades);
+ setMarmaladeType(type, blueberryMarmalades, peachMarmalades, rosehipMarmalades);
}
}
+}
+
+void ex2()
+{
+ unsigned int archiveLength;
+ cin >> archiveLength;
+ unsigned int blueberryMarmalades = 0, peachMarmalades = 0, rosehipMarmalades = 0;
+ readCodesAndCountMarmalades(archiveLength, &blueberryMarmalades, &peachMarmalades, &rosehipMarmalades);
+
cout << "Мармалад от боровинки: " << blueberryMarmalades << endl;
cout << "Мармалад от праскови: " << peachMarmalades << endl;
cout << "Шипков мармалад: " << rosehipMarmalades << endl;
@@ -171,17 +177,12 @@ void ex2()
bool isInteresting(int n)
{
- int lastDigit;
-
- do
+ while (n > 9)
{
- lastDigit = n % 10;
- n /= 10;
-
- if ((lastDigit & 1) == ((n % 10) & 1))
+ if ( ((n % 10) & 1) == (((n / 10) % 100) & 1) )
return false;
+ n /= 10;
}
- while (n != 0);
return true;
}
@@ -229,9 +230,7 @@ int genSecretNum()
i++;
}
else if (rnd == 0)
- {
rnd = rand();
- }
rnd /= 10;
}
@@ -252,6 +251,25 @@ bool digitInsideNum(int digit, int num)
return false;
}
+bool isValidGuess(int guess)
+{
+ if (guess < 0) return false;
+
+ bool recordedDigits[10] = { 0 };
+ recordedDigits[0] = true; // We don't have zeroes in secret number
+
+ for (int i = 0; i < NUMBER_LENGTH; i++)
+ {
+ if (guess == 0 || recordedDigits[guess % 10])
+ return false;
+
+ recordedDigits[guess % 10] = true;
+ guess /= 10;
+ }
+
+ return guess == 0; // False if wider than NUMBER_LENGTH digits
+}
+
void countBullsAndCows(int secretNum, int guess, int *bulls, int *cows)
{
*bulls = *cows = 0;
@@ -262,17 +280,12 @@ void countBullsAndCows(int secretNum, int guess, int *bulls, int *cows)
for (int i = 0; i < NUMBER_LENGTH; i++)
{
if (guess % 10 == secretNum % 10)
- {
*bulls += 1;
- }
else
{
(secretCows *= 10) += secretNum % 10;
-
- // Getting unique cows prevents the later cow loop from incrementing cows
- // multiple times when we have duplicate digits in secretNum and in guessCows
- if (!digitInsideNum(guess % 10, guessCows))
- (guessCows *= 10) += guess % 10;
+ // If the guess (and secret) can have duplicate digits, this should be done only with the unique ones
+ (guessCows *= 10) += guess % 10;
}
secretNum /= 10;
@@ -296,19 +309,23 @@ void playBullsAndCows()
{
cin >> guess;
- if (guess == secNum)
+ if (!isValidGuess(guess))
+ {
+ cout << "Your guess is invalid!" << endl;
+ }
+ else if (guess == secNum)
{
- cout << "Позна!" << endl;
+ cout << "You win!" << endl;
return;
}
else
{
countBullsAndCows(secNum, guess, &bulls, &cows);
- cout << bulls << " бикове и " << cows << " крави" << endl;
+ cout << bulls << " bulls and " << cows << " cows" << endl;
}
}
- cout << "Не позна в определения брой ходове!" << endl;
+ cout << "You lose! The number was " << secNum << " and you couldn't guess it in " << N << "guesses!" << endl;
}
void ex4()
@@ -318,5 +335,5 @@ void ex4()
int main()
{
- ex0();
+ ex4();
}