Advertisement
chrlwrd

Programming Challenge 2

Feb 23rd, 2012
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <conio.h>
  4. #include <cstdlib>
  5. #include <ctime>
  6. using namespace std;
  7.  
  8. bool isFlomb(char *);
  9. void getStatistics(char *);
  10. char * generateFlomb(void );
  11.  
  12. char flomb[20];
  13. int eyes, arms, ears, mouth;
  14.  
  15. int main()
  16. {
  17.     srand(time(NULL));
  18.     cout << "Press 1 to enter your own Flomb, 2 to generate a Flomb or 3 to get statistics" << endl;
  19.     cout << "Option: ";
  20.     char option = _getch();
  21.     cout << endl << endl;
  22.  
  23.     if(option == '1') // user inputs flomb
  24.     {
  25.         cout << "Enter flomb: ";
  26.         cin.getline(flomb, 20);
  27.  
  28.         if(isFlomb(flomb) == 1)
  29.         {
  30.             cout << endl << "Your Flomb is valid!" << endl << endl;
  31.         }
  32.         else
  33.         {
  34.             cout << endl << "Your Flomb is not valid." << endl << endl;
  35.         }
  36.     }
  37.     else if(option == '2') // generate flomb
  38.     {
  39.         generateFlomb();
  40.         cout << "Generated Flomb: " << flomb << endl << endl;
  41.     }
  42.     else if(option == '3') // get statistics
  43.     {
  44.         getStatistics(flomb);
  45.     }
  46.     else // invalid input
  47.     {
  48.         cout << "Please try again." << endl;
  49.     }
  50.  
  51.     main();
  52.  
  53.     return 0;
  54. }
  55.  
  56. bool isFlomb(char * input)
  57. {
  58.     int length = strlen(input);
  59.     arms = 0, ears = 0, eyes = 0, mouth = 0;
  60.  
  61.     for(int i = 0; i < length; i++)
  62.     {
  63.         switch(input[i])
  64.         {
  65.             case '\\':
  66.             case '/':
  67.                 arms++;
  68.                 break;
  69.             case '_':
  70.                 if(mouth > 0)
  71.                 {
  72.                     if(input[i-1] != '_')
  73.                     {
  74.                         mouth++; // more than one mouth
  75.                     }
  76.                     else
  77.                     {
  78.                         // just a long mouth
  79.                     }
  80.                 }
  81.                 else
  82.                 {
  83.                     mouth++; // first mouth
  84.                 }
  85.                 break;
  86.             case 'O':
  87.             case 'o':
  88.                 eyes++;
  89.                 break;
  90.             case '(':
  91.             case ')':
  92.                 ears++;
  93.         }
  94.     }
  95.  
  96.     if(arms > 2)
  97.     {
  98.         return 0;
  99.     }
  100.     else if(eyes > 2 || eyes < 1)
  101.     {
  102.         return 0;
  103.     }
  104.     else if(ears > 3 || ears < 2)
  105.     {
  106.         return 0;
  107.     }
  108.     else if(mouth > 1 || mouth < 1)
  109.     {
  110.         return 0;
  111.     }
  112.     else
  113.     {
  114.         return 1;
  115.     }
  116. }
  117.  
  118. void getStatistics(char * input)
  119. {
  120.     int length = strlen(input);
  121.     arms = 0, ears = 0, eyes = 0, mouth = 0;
  122.  
  123.     for(int i = 0; i < length; i++)
  124.     {
  125.         switch(input[i])
  126.         {
  127.             case '\\':
  128.             case '/':
  129.                 arms++;
  130.                 break;
  131.             case '_':
  132.                 if(mouth > 0)
  133.                 {
  134.                     if(input[i-1] != '_')
  135.                     {
  136.                         mouth++; // more than one mouth
  137.                     }
  138.                     else
  139.                     {
  140.                         // just a long mouth
  141.                     }
  142.                 }
  143.                 else
  144.                 {
  145.                     mouth++; // first mouth
  146.                 }
  147.                 break;
  148.             case 'O':
  149.             case 'o':
  150.                 eyes++;
  151.                 break;
  152.             case '(':
  153.             case ')':
  154.                 ears++;
  155.         }
  156.     }
  157.    
  158.     cout << "Flomb has " << arms << " arms, " << ears << " ears, " << eyes << " eyes and " << mouth << " mouths" << endl << endl;
  159. }
  160.  
  161. char * generateFlomb(void )
  162. {
  163.     char temp[10];
  164.    
  165.     while(isFlomb(temp) == 0)
  166.     {
  167.         arms = 0, ears = 0, eyes = 0, mouth = 0;
  168.         memset(temp, '\0', 10);
  169.         int a = 0, b = 0, c = 0;
  170.         a = rand() % 8;
  171.    
  172.         for(int i = 0; i < a; i++)
  173.         {
  174.             b = rand() % 4;
  175.             c = rand() % 2;
  176.    
  177.             switch(b)
  178.             {
  179.             case 0: //arm
  180.                 if(arms == 2)
  181.                 {
  182.                     // no more arms
  183.                 }
  184.                 else
  185.                 {
  186.                     if(c == 0)
  187.                         temp[i] = '/';
  188.                     else
  189.                         temp[i] = '\\';
  190.                     arms++;
  191.                 }
  192.                 break;
  193.             case 1: //ear
  194.                 if(ears == 3)
  195.                 {
  196.                     // no more ears
  197.                 }
  198.                 else
  199.                 {
  200.                      if(c == 0)
  201.                         temp[i] = '(';
  202.                     else
  203.                         temp[i] = ')';
  204.                     ears++;
  205.                 }
  206.                 break;
  207.             case 2: //eye
  208.                 if(eyes == 2)
  209.                 {
  210.                     // no more eyes
  211.                 }
  212.                 else
  213.                 {
  214.                     temp[i] = 'O';
  215.                     eyes++;
  216.                 }
  217.                 break;
  218.             case 3: //mouth
  219.                 if(mouth > 0)
  220.                 {
  221.                     if(temp[i-1] != '_')
  222.                     {
  223.                         // no more mouths
  224.                     }
  225.                     else
  226.                     {
  227.                         temp[i] = '_'; // longer mouth
  228.                     }
  229.                 }
  230.                 else
  231.                 {
  232.                     temp[i] = '_';
  233.                     mouth++;
  234.                 }
  235.                 break;
  236.             }
  237.         }
  238.     }
  239.  
  240.     for(int i = 0; i < 10; i++)
  241.     {
  242.         flomb[i] = temp[i];
  243.     }
  244.  
  245.     return 0;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement