Advertisement
Nordicus

Password II

Dec 16th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. // Increasing Passwords II.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <array>
  8. using namespace std;
  9.  
  10. void waitLine()
  11. {
  12.     cin.clear();
  13.     cin.ignore(255, '\n');
  14.     cin.get();
  15. }
  16.  
  17. bool scanRight (int Password[], int curChar)
  18. {
  19.     bool success = true;
  20.     if (curChar<35)
  21.     {
  22.         if (curChar+1>=Password[curChar]) //Checks for viability of this start of a password.
  23.         {
  24.             if (Password[curChar+1]<=Password[curChar])
  25.             {
  26.                 Password[curChar+1]=Password[curChar]+1;
  27.                 success = scanRight(Password,curChar+1);
  28.             }
  29.         }
  30.         else
  31.         {
  32.             success = false;
  33.         }
  34.     }
  35.     return success;
  36. }
  37.  
  38. void incrChar (int Password[], int curChar)
  39. {
  40.     if (Password[curChar]<36)
  41.     {
  42.         Password[curChar]++;
  43.         bool valid = true;
  44.         valid = scanRight(Password, curChar);
  45.         //If this value of Password[curChar] isn't allowed, increase it again.
  46.         if (!valid)
  47.         {
  48.             incrChar(Password, curChar);
  49.         }
  50.     }
  51.     else
  52.     {
  53.         for (int i=curChar; i<37; i++)
  54.         {
  55.             Password[i]=1;
  56.         }
  57.         incrChar(Password, curChar-1);
  58.     }
  59. }
  60.  
  61. string makeString (int Password[])
  62. {
  63.     string strPassword;
  64.     for (int i=0; i<36; i++)
  65.     {
  66.         if (Password[i]>26)
  67.         {
  68.             strPassword = strPassword + char(Password[i]+21);
  69.         }
  70.         else if (Password[i]>0)
  71.         {
  72.             strPassword = strPassword + char(Password[i]+64);
  73.         }
  74.     }
  75.     return strPassword;
  76. }
  77.  
  78. string nthPassword(int n)
  79. {
  80.     //Declare and initialise our password array.
  81.     int Password[36];
  82.     for (int i=0; i<36; i++)
  83.     {
  84.         Password[i] = 0;
  85.     }
  86.  
  87.     //Find the nth by working through all values of the password.
  88.     for (; n>0; n--)
  89.     {
  90.         incrChar(Password, 35);
  91.     }
  92.     return makeString(Password);   
  93. }
  94.  
  95. //Find correct st, nd, rd, or th.
  96. string suffix (int n)
  97. {
  98.     string thisSuffix;
  99.     switch (n%10)
  100.     {
  101.         case 1:
  102.             thisSuffix = "st";
  103.             break;
  104.         case 2:
  105.             thisSuffix = "nd";
  106.             break;
  107.         case 3:
  108.             thisSuffix = "rd";
  109.             break;
  110.         default:
  111.             thisSuffix = "th";
  112.     }
  113.     return thisSuffix;
  114. }
  115.  
  116. int main()
  117. {
  118.     //Read in our value
  119.     int n;
  120.     cin >> n;
  121.        
  122.     //And output that password.
  123.     cout << "The " << n << suffix(n) << " password is: " << nthPassword(n);
  124.    
  125.     //Wait for user input.
  126.     waitLine();
  127.  
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement