Advertisement
boky8

Pogodi broj

Feb 24th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. void zamijeni(char& a, char&b) {
  8.     char c = a;
  9.     a = b;
  10.     b = c;
  11. }
  12.  
  13. string nasumicni() {
  14.     string znamenke = "0123456789";
  15.     int a, b;
  16.  
  17.     srand(time(NULL));
  18.  
  19.     for (int i = 0; i < 1000; i++) {
  20.         a = rand() % 10;
  21.         b = rand() % 10;
  22.         zamijeni(znamenke[a], znamenke[b]);
  23.     }
  24.  
  25.     return znamenke.substr(0, 4);
  26. }
  27.  
  28. int t(string pogodi, string pokusaj) {
  29.     int brojac = 0;
  30.  
  31.     for (int i = 0; i < 4; i++) {
  32.         if (pogodi[i] == pokusaj[i])
  33.             brojac++;
  34.     }
  35.  
  36.     return brojac;
  37. }
  38.  
  39. int v(string pogodi, string pokusaj) {
  40.     int brojac = 0;
  41.  
  42.     for (int i = 0; i < 4; i++) {
  43.         for (int j = 0; j < 4; j++) {
  44.             if (pogodi[i] == pokusaj[j] && pogodi[i] != pokusaj[i]) {
  45.                 brojac++;
  46.                 break;
  47.             }
  48.         }
  49.     }
  50.  
  51.     return brojac;
  52. }
  53.  
  54. bool jesu4(string pokusaj) {
  55.     if (pokusaj.length() == 4)
  56.         return true;
  57.  
  58.     return false;
  59. }
  60.  
  61. bool jesuZnamenke(string pokusaj) {
  62.     string znamenke = "0123456789";
  63.     bool znamenka;
  64.  
  65.     for (int i = 0; i < 4; i++) {
  66.         znamenka = false;
  67.  
  68.         for (int j = 0; j < 10; j++) {
  69.             if (pokusaj[i] == znamenke[j]) {
  70.                 znamenka = true;
  71.                 break;
  72.             }
  73.         }
  74.  
  75.         if (!znamenka)
  76.             return false;
  77.     }
  78.  
  79.     return true;
  80. }
  81.  
  82. bool ponavljanje(string pokusaj) {
  83.     for (int i = 0; i < 4; i++) {
  84.         for (int j = i+1; j < 4; j++) {
  85.             if (pokusaj[i] == pokusaj[j])
  86.                 return true;
  87.         }
  88.     }
  89.  
  90.     return false;
  91. }
  92.  
  93. bool pogodak(string pogodi, string pokusaj) {
  94.     if (pogodi == pokusaj)
  95.         return true;
  96.  
  97.     return false;
  98. }
  99.  
  100. int main() {
  101.     string pogodi = nasumicni(), pokusaj;
  102.  
  103.     cout << "Zamislio sam cetveroznamenkasti broj!\nPokusaj ga pogoditi!\n";
  104.  
  105.      do {
  106.         cout << "Unos: ";
  107.         getline(cin, pokusaj);
  108.  
  109.         if (jesu4(pokusaj)) {
  110.             if (jesuZnamenke(pokusaj)) {
  111.                 if (!ponavljanje(pokusaj)) {
  112.                     if (v(pogodi, pokusaj))
  113.                         cout << v(pogodi, pokusaj) << "V  ";
  114.                     if (t(pogodi, pokusaj))
  115.                         cout << t(pogodi, pokusaj) << "T";
  116.                 }
  117.                 else {
  118.                     cout << "Znamenke se ne smiju ponavljati!\n";
  119.                 }
  120.             }
  121.             else {
  122.                 cout << "Moras unjeti znamenke!\n";
  123.             }
  124.         }
  125.         else {
  126.             cout << "Moras unjeti 4 znamenke!\n";
  127.         }
  128.  
  129.         cout << "\n\n";
  130.     } while (!pogodak(pogodi, pokusaj));
  131.  
  132.     cout << "Bravo broj je pogoden!\n";
  133.  
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement