#Linux中的GCC规划# Some useful small function C code

1. Enter and identify the up, down, left, and right on the keyboard

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//分配内存的大小
#define     SIZE    10
//定义按键们的宏
#define     ESC     "\033"
#define     UP      "\033[A"
#define     DOWN    "\033[B"
#define     LEFT    "\033[D"
#define     RIGHT   "\033[C"

int main()
{
    char *get = (char*)malloc(SIZE);

    for ( ; ; ) 
    {   
        fgets(get, SIZE, stdin);
        /*    用fgets()函数从stdin中读取字符串时,会自动在字符串末尾追加"\n",这里将末尾字符改为"\0"    */
        get[strlen(get) - 1] = '\0';

        if (!strcmp(get, ESC))
            printf("This is \"ESC\" button!\n");
        if (!strcmp(get, UP))
            printf("This is \"UP\" button!\n");
        if (!strcmp(get, DOWN))
            printf("This is \"DOWN\" button!\n");
        if (!strcmp(get, LEFT))
            printf("This is \"LEFT\" button!\n");
        if (!strcmp(get, RIGHT))
            printf("This is \"RIGHT\" button!\n");
    }   

    return 0;
}

Operation result: Each time you press an arrow key, you need to press Enter again (used to refresh the cache).

kshine@kshine-virtual-machine:~/桌面/GCC$ gcc udlr1.c  -o main -Wall
kshine@kshine-virtual-machine:~/桌面/GCC$ ./main
^[[A
This is "UP" button!
^[[D
This is "LEFT" button!
^[[B
This is "DOWN" button!
^[[C
This is "RIGHT" button!

2. For the above test code, we can introduce system("stty -icanon"); as shown in the following code

#include <stdio.h>
#include <stdlib.h>
int main()
{
        char ch;
        system("stty -icanon");//关闭缓冲区,输入字符无需回车直接接受
        ch = getchar();
        if(ch == 'a')
                printf("OK\n");
        return 0;
}

3. printf(), highlight

#include"stdio.h"
int main()
{
	printf("\033[40;39m  底色40黑色,字色39无无  \033[0m\n");//\033[;m  ……  \033[0m
	printf("\033[41;38m  底色41深红,字色38无无  \033[0m\n");//\033[;m  ……  \033[0m
	printf("\033[42;37m  底色42绿色,字色37白色  \033[0m\n");//\033[;m  ……  \033[0m
	printf("\033[43;36m  底色43黄色,字色36深绿  \033[0m\n");//\033[;m  ……  \033[0m
	printf("\033[44;35m  底色44蓝色,字色35紫色  \033[0m\n");//\033[;m  ……  \033[0m
	printf("\033[45;34m  底色45紫色,字色34蓝色  \033[0m\n");//\033[;m  ……  \033[0m
	printf("\033[46;33m  底色46深绿,字色33黄色  \033[0m\n");//\033[;m  ……  \033[0m
	printf("\033[47;32m  底色47白色,字色32绿色  \033[0m\n");//\033[;m  ……  \033[0m
	printf("\033[48;31m  底色48无无,字色31红色  \033[0m\n");//\033[;m  ……  \033[0m
	printf("\033[49;30m  底色49无无,字色30黑色  \033[0m\n");//\033[;m  ……  \033[0m
	return 0;
}

4. Realize strcmp+ read-only formal parameters

5. Implement strcpy+ assertion function

6. Implement strlen

7. Pointer constants and constant pointers

8. The priority of pointers and self-added symbols

9. Special functions

9.1 Generate random numbers

9.2 Generate a random verification code and enter it for verification

9.3 Output system current time

10. Definition and use of function pointers and function pointer arrays

10.1 Definition and use of function pointer

10.2 Definition and use of function pointer array

11. Bubbling sort of integer arrays

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/Kshine2017/article/details/85327236