[英]display printed out text as if it was written by an human (C)
如果可能的話,我正在嘗試做idk,但是由於我的老師說他會提高我的定性,所以我會嘗試一下...
我有很多字符
char myArray[100]
要做的是一步一步地打印所有的數組,就像一個人在寫它,一個字符,一個延遲,另一個,延遲等。
我該怎么辦?
循環打印myArray[i]
並執行某些操作? 我可以想到做一個循環,在循環中斷之前要運行很多次,但這並不是一個優雅的解決方案...
提前
如果您使用的是Windows,請使用sleep
功能。 如果您使用的是Linux,請使用usleep
功能。
將此交給您的老師以獲得最高分。 警告:可能會要求您解釋一下。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
void my_delay (int millisec)
{
clock_t start = clock(), end = start + (millisec*CLOCKS_PER_SEC/1000);
while (clock() < end) ;
}
void stutter_write (char *string)
{
int i, typos;
while (*string)
{
// random delay
my_delay (1+(rand() & 255));
// randomly inject typos
typos = 1+(rand() % 20);
if (typos < 4)
{
for (i=0; i<typos; i++)
{
// wrong digit
if (isdigit(*string))
putchar ((rand() % 10)+'0');
else
// random letter
putchar ((rand() % 26)+'a'+32*(rand() & 31 < 10));
fflush (stdout);
// wait for it ..
my_delay (200+(rand() & 511));
}
// oh blast. backspace:
while (typos--)
{
putchar (8);
fflush (stdout);
my_delay (100+(rand() & 127));
}
}
// space, lowercase or number: fast
if (*string == ' ' || islower(*string) || isdigit(*string))
{
my_delay (250);
} else
// uppercase: slightly slower
if (isupper(*string))
{
my_delay (350);
} else
// some common symbols on keyboard: unshifted
if (strchr("[]=-';.,/", *string))
{
my_delay (400);
} else
// all others are shifted and/or hard to find or something like that
my_delay (600);
putchar (*string);
fflush (stdout);
string++;
}
}
int main (int argc, char **argv)
{
int i,j;
srand(0);
for (i=1; i<argc; i++)
{
if (i > 1) stutter_write (" ");
stutter_write (argv[i]);
}
printf ("\n");
return 0;
}
用法:(假設您的程序名稱是“口吃”
stutter Here is a long string, which will be output as typed by a human. No, really.
..並且該字符串將出現在控制台中,就像由自動尋字打字機鍵入的一樣(調整延遲率以模擬更高級的打字員)。
模仿更多的“自然”錯別字更加困難。 常見的錯別字(在我寫的時候補上)是:打幾個字,仔細考慮一下,全部退格並輸入其他內容; 換位(“ instaed”而不是“ instead”),忘記Shift(“ 0”而不是“(”),錯誤的鍵盤行(“ wbat”而不是“ what”),在鍵盤上上移一個字符(“ ehat”而不是“ what”),而忘記了一個字符(“ wat”而不是“ what”)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.