1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include "stdafx.h" // Microsoft Visual Studio
#include <vector>
#include <iostream>
#include <ctime>
#include <windows.h>
#include <conio.h>
#include <algorithm>
char getKey(void) {
DWORD mode;
HANDLE hstdin;
INPUT_RECORD inrec;
DWORD count;
char result = '\0';
/* Set the console mode to no-echo, raw input, */
/* and no window or mouse events. */
hstdin = GetStdHandle(STD_INPUT_HANDLE);
if (hstdin == INVALID_HANDLE_VALUE
|| !GetConsoleMode(hstdin, &mode)
|| !SetConsoleMode(hstdin, 0))
return result;
FlushConsoleInputBuffer(hstdin);
/* Wait for and get a single key PRESS */
do ReadConsoleInput(hstdin, &inrec, 1, &count);
while ((inrec.EventType != KEY_EVENT) || !inrec.Event.KeyEvent.bKeyDown);
/* Remember which key the user pressed */
result = inrec.Event.KeyEvent.uChar.AsciiChar;
/* Wait for and get a single key RELEASE */
do ReadConsoleInput(hstdin, &inrec, 1, &count);
while ((inrec.EventType != KEY_EVENT) || inrec.Event.KeyEvent.bKeyDown);
/* Restore the original console mode */
SetConsoleMode(hstdin, mode);
return result;
}
// get key strokes until return-key
void getLine(std::string &strInput) {
for (char c = getKey(); c != 13; c = getKey()) {
std::cout << c;
strInput += c;
}
}
int GetRandom(int iMax)
{
int iRandom;
srand(static_cast<unsigned int>(time(NULL) * clock()));
iRandom = (rand() / (RAND_MAX / iMax + 1));
return iRandom;
}
int main()
{
std::cout << "Input: ";
std::string strInput;
getLine(strInput);
std::cout << std::endl;
int iLen = strInput.size();
std::vector<int> vNum; // unique random numbers
while (vNum.size() < iLen) {
int iRandom = GetRandom(iLen);
if (std::find(vNum.begin(), vNum.end(), iRandom) == vNum.end())
vNum.push_back(iRandom);
}
std::cout << "Output: ";
for (std::vector<int>::iterator it = vNum.begin(); it != vNum.end(); ++it)
std::cout << strInput[*it];
std::cout << std::endl;
std::cout << "press any key";
getKey();
return 0;
}
|