zoukankan      html  css  js  c++  java
  • TypeWriting

    头文件getputch.h

    /*
    * getputch.c
    */
    /* 用于getch/putch的通用头文件"getputch.h" */
    
    #ifndef __GETPUTCH
    
      #define __GETPUTCH
    
      #if defined(_MSC_VER) || (__TURBOC__) || (LSI_C)
    
        /* MS-Windows/MS-DOS(Visual C++, Borland C++, LSI-C 86 etc ...)*/
    
        #include <conio.h>
    
        static void init_getputch(void) { /**/ }
    
        static void term_getputch(void) { /**/ }
    
    
      #else
    
        /* 提供了Curses库的UNIX/Linux/OS X环境 */
    
        #include <curses.h>
    
        #undef putchar
        #undef puts
        #undef printf
        static char __buf[4096];
    
        /*--- _ _putchar:相当于putchar函数(用“换行符+回车符”代替换行符进行输出)---*/
        static int __putchar(int ch)
        {
            if (ch == '
    ')
                putchar('
    ');
            return putchar(ch);
        }
    
        /*--- putch:显示1个字符,清除缓冲区 ---*/
        static int putch(int ch)
        {
            int result = putchar(ch);
    
            fflush(stdout);
            return result;
        }
    
        /*--- _ _printf:相当于printf函数(用“换行符+回车符”代替换行符进行输出)---*/
        static int __printf(const char *format, ...)
        {
            va_list    ap;
            int     count;
    
            va_start(ap, format);
            vsprintf(__buf, format, ap);
            va_end(ap);
    
            for (count = 0; __buf[count]; count++) {
                putchar(__buf[count]);
                if (__buf[count] == '
    ')
                    putchar('
    ');
            }
            return count;
        }
    
        /*--- _ _puts:相当于puts函数(用“换行符+回车符”代替换行符进行输出)---*/
        static int __puts(const char *s)
        {
            int i, j;
    
            for (i = 0, j = 0; s[i]; i++) {
                __buf[j++] = s[i];
                if (s[i] == '
    ')
                    __buf[j++] = '
    ';
            }
            return puts(__buf);
        }
    
        /*--- 库初始处理 ---*/
        static void init_getputch(void)
        {
            initscr();
            cbreak();
            noecho();
            refresh();
        }
    
        /*--- 库终止处理 ---*/
        static void term_getputch(void)
        {
            endwin();
        }
    
        #define putchar    __putchar
        #define    printf    __printf
        #define puts    __puts
    
      #endif
    
    #endif

    消除一个字符串:

    /*
    *type_erase_string.c
    */
    #include<time.h>
    #include<ctype.h>
    #include<stdio.h>
    #include<string.h>
    #include"getputch.h"
    
    int main()
    {
        int i;
        char *str = "How do you do?";
        int len = strlen(str);
        clock_t start, end;
        init_getputch();
        printf("please input as same
    ");
        printf("%s
    ", str);
    
        fflush(stdout);
    
        start = clock();
        for (i = 0; i < len; i++)
        {
            int ch;
            do
            {
                ch = getch();    //从键盘读取信息,getputch.h函数
                if (isprint(ch))
                {
                    putch(ch);
                    if (ch != str[i])
                    {
                        putch('');    //光标退一格
                    }
                }
            }while (ch != str[i]);
        }
    
        end = clock();
        printf("
    spend time %1.fs. 
    ", (double)(end - start) / CLOCKS_PER_SEC);    //ubuntu14显示不出来,应该可以用localtime来显示
        term_getputch();
        return 0;
    }

    消除已经输出的字符

    /*
    *type_erase_string_put.c
    */
    #include<stdio.h>
    #include<time.h>
    #include<stdio.h>
    #include<string.h>
    #include"getputch.h"
    
    int main()
    {
        int i;
        char *str = "How do you do?";
        int len = strlen(str);
        clock_t start, end;
        init_getputch();
        printf("please input as following
    ");
        start = clock();
        for (i = 0; i < len; i++)
        {
            //显示str[i]以后的字符并把光标返回到开头 
     回车符
            printf("%s 
    ", &str[i]);    
            fflush(stdout);
            while(getch() != str[i])
                ;
        }
    
        end = clock();
        printf("spending time %.1f s 
    ", (double)(end-start)/CLOCKS_PER_SEC);
        term_getputch();
        return 0;
    }

    输入多个字符串

    /*
    *type_put_more.c
    */
    #include<stdio.h>
    #include<time.h>
    #include<string.h>
    #include"getputch.h"
    
    #define QNO 12
    
    int main()
    {
        char *str[QNO] = {"book", "computer", "default", "comfort", "monday",
                        "power", "light", "music", "programming",
                        "dog", "video", "include"};
    
        int i, stage;
        clock_t start, end;
    
        init_getputch();
        printf("Starting practice type. 
    ");
        printf("please enter space to start.
    ");
    
        while(getch() != ' ')
            ;
    
        start = clock();
    
        for (stage = 0; stage < QNO; stage++)
        {
            int len = strlen(str[stage]);
            for (i = 0; i < len; i++)
            {
                //显示str[stage][i]以后的字符并把光标返回到开头
                printf("%s 
    ", &str[stage][i]);
                fflush(stdout);
                while (getch() != str[stage][i])
                    ;
            }
        }
    
        end = clock();
    
        printf("
    Spending time %.1f second . 
    ", (double)(end - start)/CLOCKS_PER_SEC);
        term_getputch();
        return 0;
    }

    打乱出题顺序,使用数组来标识出题顺序

    /*
    *type_random.c
    */
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<time.h>
    #include"getputch.h"
    
    #define QNO 12
    #define swap(type, x, y) do{type t = x; x = y; y = t;}while(0)
    
    int main()
    {
    
        char *str[QNO] = {"book", "computer", "default", "comfort", "monday",
                        "power", "light", "music", "programming",
                        "dog", "video", "include"};
    
        int i, stage;
        int qno[QNO];    //出题顺序
        clock_t start, end;
        init_getputch();
        srand(time(NULL));    //设置随机数种子
    
        for (i = 0; i < QNO; i++)
        {
            qno[i] = i;
        }
    
        for (i = QNO - 1; i > 0; i--)    //打乱出题顺序
        {
            int j = rand()% (i + 1);
            if (i != j)
            {
                swap(int , qno[i], qno[j]);
            }
        }
    
        printf("Starting type practice.
    ");
        printf("Starting after entering space .
    ");
        while (getch() != ' ')
            ;
    
        start = clock();
        for (stage = 0; stage < QNO; stage++)
        {
            int len = strlen(str[qno[stage]]);
            for (i = 0; i < len; i++)
            {
                printf("%s 
    ", &str[qno[stage]][i]);
                fflush(stdout);
                while (getch() != str[qno[stage]][i])
                    ;
            }
        }
    
        end = clock();
        printf("
     Costing time : %.1f seconds. 
    ", (double)(end - start)/CLOCKS_PER_SEC);
    
        term_getputch();
        return 0;
    
    }

    交换指针打乱出题顺序:

    /*
    *type_random_2.c
    */
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<time.h>
    #include"getputch.h"
    
    
    #define QNO 12
    
    #define swap(type, x, y) do {type t = x; x = y; y = t;} while(0)
    
    int main()
    {
        
        char *str[QNO] = {"book", "computer", "default", "comfort", "monday",
                        "power", "light", "music", "programming",
                        "dog", "video", "include"};
        int i, stage;
        clock_t start, end;
    
        init_getputch();
        srand(time(NULL));
    
        for (i = QNO - 1; i > 0; i--)
        {
            int j = rand()%(i+1);
            if (i != j)
            {
                swap(char *, str[i], str[j]);
            }
        }
    
        printf("Starting type practice
    ");
        printf("Start after entering space. 
    ");
        while (getch() != ' ')
            ;
        start = clock();
    
        for (stage = 0; stage < QNO; stage++)
        {
            int len = strlen(str[stage]);
            for (i = 0; i < len; i++)
            {
                printf("%s 
    ", &str[stage][i]);
                fflush(stdout);
                while(getch() != str[stage][i])
                    ;
            }
        }
    
        end = clock();
        printf("
     Costing time : %.1f seconds 
    ", (double)(end-start)/CLOCKS_PER_SEC);
        term_getputch();
        return 0;
    
    }

    键盘联想字符:输出键盘一块区域的字符,隐藏一个,打出隐藏的这个字符

    /*
    *type_keyboard.c
    */
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<time.h>
    #include"getputch.h"
    
    #define NO 3
    #define KTYPE    16
    
    int main()
    {
        char *kstr[] = {"12345", "67890-=",
                        "!@#$%", "^&*()_+",
                        "qlert", "yuiop[]\",
                        "QWERT", "YUIOP{}|",
                        "asdfg", "hjkl;'",
                        "ASDFG", "HJKL:""
                        "zxcvb", "nm,./",
                        "ZXCVB", "NM<>?"};
    
        int i, stage;
    
        clock_t start, end;
    
        init_getputch();
        srand(time(NULL));
    
        printf("Starting practice type 
    ");
        printf("please use ? to hidden type. 
    ");
        printf("Start entering space
    ");
    
        fflush(stdout);
        while(getch() != ' ')
            ;
    
        start = clock();
    
        for (stage = 0; stage < NO; stage++)
        {
            int k, p, key;
            char temp[10];
    
            do 
            {
                k = rand() % KTYPE;
                p = rand() % strlen(kstr[k]);
                key = kstr[k][p];
            }while(key == ' ');
    
            strcpy(temp, kstr[k]);
            temp[p] = '?';
            printf("%s", temp);
            fflush(stdout);
            while(getch() != key)
                ;
    
            putchar('
    ');
        }
    
        end = clock();
        printf("Costing time : %.1f seconds. 
    ", (double)(end - start)/CLOCKS_PER_SEC);
    
        term_getputch();
        return 0;
    }
  • 相关阅读:
    微信支付的安全漏洞之XXE
    IP地址分类(A类 B类 C类 D类 E类)
    MySql新增表的字段,删除表字段
    Java基础之中间件的初识
    Java基础之IO框架
    微信H5支付坑一--手续费未结算
    设计模式之简单工厂模式
    nginx负载均衡的5种策略(转载)
    Mybatis注意点之#与$区别
    RSF 分布式 RPC 服务框架的分层设计
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/12011070.html
Copyright © 2011-2022 走看看