显示打印出的文本,好像它是由人写的(C)

时间:2014-02-13 21:39:05

标签: c printf

如果可能的话,我正试图做什么,但是因为我的老师说他会提高我的校准,我会试一试......

我有一系列字符

char myArray[100]

要做的就是逐步打印所有数组,就像人类正在编写它,角色,延迟,另一个,延迟等等

我应该怎么做?

在循环中打印myArray[i]并执行某些操作?我可以想到做一个在它打破之前运行很多次的循环,但它不像一个优雅的解决方案......

事先提前

2 个答案:

答案 0 :(得分:0)

如果您使用的是Windows,请使用sleep功能。如果您使用的是Linux,请使用usleep函数。

答案 1 :(得分:0)

将此交给您的老师以获得最高分。请注意:可能会要求您解释它。

#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”而不是“代替”),忘记Shift(“0”而不是“(”),错误的键盘行(“wbat”而不是“what”),在键盘上移动一个字符(“ehat”而不是“什么”),忘记了一个角色(“wat”而不是“what”)。