Advertisement
Nordicus

Password

Dec 14th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. // Increasing Passwords.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. void waitLine()
  11. {
  12.     cin.clear();
  13.     cin.ignore(255, '\n');
  14.     cin.get();
  15. }
  16.  
  17. //Logic for increasing a base 36 number
  18. void incrChar(string &Password, int curChar)
  19. {
  20.     curChar--;
  21.     if (Password[curChar] == 32)
  22.     {
  23.         Password[curChar] = char(65);
  24.     }
  25.     else if (((Password[curChar] > 64) && (Password[curChar] < 90)) || ((Password[curChar] > 47) && (Password[curChar] < 57)))
  26.     {
  27.         Password[curChar]++;
  28.     }
  29.     else if (Password[curChar] == 90)
  30.     {
  31.         Password[curChar] = 48;
  32.     }
  33.     else
  34.     {
  35.         Password[curChar] = 'A';
  36.         incrChar(Password, curChar);
  37.     }
  38.  
  39. }
  40.  
  41. //Check for alphebetisation and repeats
  42. bool isValid(string Password)
  43. {
  44.     bool r = true;
  45.     for (int i=0; i<36; i++)
  46.     {
  47.         if (!(Password[i]==char(32)))
  48.         {
  49.             for (int j=i+1; j<36; j++)
  50.             {
  51.                 if (Password[i]>char(64))
  52.                 {
  53.                     if (Password[j]<=Password[i] && Password[j]>64)
  54.                     {
  55.                         r = false;
  56.                     }
  57.                 }
  58.                 else
  59.                 {
  60.                     if (Password[j]<=Password[i] || Password[j]>64)
  61.                     {
  62.                         r = false;
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.     }
  68.     return r;
  69. }
  70.  
  71. string nthPassword(int n)
  72. {
  73.     string Password = "                                    ";
  74.     do
  75.     {
  76.         incrChar(Password, 36);
  77.         if (isValid(Password))
  78.         {
  79.             n--;
  80.         }
  81.     } while (n>0);
  82.     return Password;
  83. }
  84.  
  85.  
  86. int main()
  87. {
  88.     int n;
  89.     cin >> n;
  90.     cout << nthPassword(n);
  91.     waitLine();
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement