C语言学习整理(非基础/非算法/非数据结构)

距离上一次C语言课已经有一年了(现在:2020-1-19),这一年里,在网上学习到了一些好玩的东西(有的受益至今)。

该语言现主要用于测试算法/写小工具。(可能会更新)

Part.1

1.1.多线程

  本人目前使用场景少,举个傻瓜例子:

  从10个txt文件中查找某条数据

    如果使用单线程(或许就是你的主线程)将要花费 0 - 10个txt的时间

    使用多线程花费 0 - 花费时间最长的txt的时间

1.2.正则表达式

  键盘输入时可能会用,输入时可以精确获取内容.

  必要场景:由于scanf会以制表符(tab 回车键 空格等) 当我们需要输入一个带有空格的字符串时需要使用正则表达式

    详细细节参考 https://blog.csdn.net/chuhe163/article/details/81048751

  在线测试:菜鸟教程工具 https://c.runoob.com/front-end/854

1.3.短路特性(逻辑运算)

  进行判断时对运行结果/运行效率有影响,

#include <stdio.h>
#include <stdlib.h> int main(){ int num1=1; int num2=1; if(num1 == 1 || num2 = 2){ printf("%d",num2);//输出1 } printf("%d%d",num1,num2);//输出11
  system("pause");
  return 0; }

  运行效率可以参考这篇博客  https://blog.csdn.net/huatian5/article/details/51083516

  短路特性详细可参考 https://blog.csdn.net/meetings/article/details/44260917

1.4.作用域

  这个似乎很基础. http://c.biancheng.net/cpp/html/3465.html

Part.2

2.1.Sleep函数

  超好用,用于实现运行 暂停 运行 这样的操作。

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main(){
    printf("开始倒计时!\n");
    for(int i = 5;i > 0;--i){
        printf("\t%d!\n",i);
        Sleep(990);
    }
    system("PAUSE");
    return 0;
}

  需要注意的是每个函数执行时都需要一定的时间

  当使用Sleep函数实现"实时更新"时若使用Sleep(1000)可能会出现

  11点01分01秒

  11点01分02秒

  11点01分04秒

  这样的问题 建议使用更高的频率来决定是否刷新 例如Sleep(100)

  为了更好的体验 可以尝试使用Sleep(800)来代替1秒

2.2.kbhit函数

  非阻塞型输入,它本身是一个bool类型的函数,有键盘输入?返回True:返回False;

  常与getch函数一起使用.

#include <stdio.h>
#include <conio.h>
 
int main(){
    char ch;
    while(true){
        if(kbhit()){
            ch = getch();
            printf("你下了ascii码为 %d 的字符\n",ch);
        }
    }
    return 0;
}

  对于二者的结合使用,可以参考这篇博客 https://blog.csdn.net/boreassoft/article/details/76728405

2.3.time_t + struct tm

  tm是用于获取时间的结构体,它的原型:

struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};

  time_t是一种数据类型 详情 https://www.cnblogs.com/dushikang/p/8575678.html

  使用示例:

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

int main(){
    time_t t;
    struct tm *lt;
    time(&t);
    lt = localtime(&t);
    printf("现在是%d年%d月%d日 %d点%d分%d秒.\n",lt->tm_year+1900
        ,lt->tm_mon,lt->tm_mday,lt->tm_hour,lt->tm_min,lt->tm_sec);
    system("PAUSE");
    return 0;
}

2.4.system

  好用,实质是dos命令.dos命令十分丰富.

  system("shutdown -p");//关机

  system("dir");//浏览当前程序同文件夹文件目录

  system("mode con cols=80 lines=24");//设置控制台长/宽

  感兴趣的朋友可以去学习一下.

2.5.键鼠模拟

  模拟键鼠操作,仅在此列举函数:

//按下esc键
keybd_event(VK_ESCAPE , 0x1b, 0, 0);
//弹起esc键
keybd_event(VK_ESCAPE , 0x1b,KEYEVENTF_KEYUP , 0);
//按下'e'键
keybd_event('E',0,0,0);
//弹起'e'键
keybd_event('E',0,KEYEVENTF_KEYUP,0);
//鼠标左键按下
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
//鼠标右键弹起
mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0);
int x,y; POINT pt = {0, 0}; LPPOINT xy = &pt; //获取鼠标坐标 GetCursorPos(xy); printf("%d\t%d",pt.x,pt.y);

本文为Dumb原创,请勿抄袭...

猜你喜欢

转载自www.cnblogs.com/drakeisdumb/p/12215415.html