经典编程900例(c语言)(第十一篇)

    科技2025-11-10  10

    例124:通过系统时间计算程序运行时间

    #include <stdio.h> #include <time.h> int add_fast(int a, int b) { return(a + b); } int add_slow(int a, int b) { return(a + b); } int main(int argc, char const *argv[]) { unsigned long int i, result; clock_t start_time, stop_time; printf("Processing...\n"); // clock()获取当前时间 start_time = clock(); // 循环调用2000000次函数和赋值操作 for (i = 0; i < 2000000L; i++) result = add_fast(i, -i); stop_time = clock(); printf("Processing time for fast call %d ticks\n", stop_time - start_time); // ----------------------------------------------------------------------- start_time = clock(); for (i = 0; i < 2000000L; i++) result = add_slow(i, -i); stop_time = clock(); printf("Processing time for normal call %d ticks\n", stop_time - start_time); return 0; }

    例125:打印目标文件夹下的文件夹名

    #include <stdio.h> // 这个头文件vs下没有 #include <dirent.h> #include <malloc.h> #include <string.h> #include <stdlib.h> int main(int argc, char const *argv[]) { DIR *directory_pointer; struct dirent *entry; struct FileList { char filename[64]; struct FileList *next; } start, *node; if ((directory_pointer = opendir(argv[1])) == NULL) printf("Error opening %s\n", argv[1]); else { start.next = NULL; node = &start; while (entry = readdir(directory_pointer)) { node->next = (struct FileList *) malloc(sizeof(struct FileList)); if (node == NULL) { printf("Insufficient memory to store list\n"); exit(1); } node = node->next; strcpy(node->filename, entry); node->next = NULL; } } closedir(directory_pointer); node = start.next; while (node) { printf("%s\n", node->filename); node = node->next; } return 0; }

    例126:dos.h…

    #include <stdio.h> #include <dos.h> #include <stdlib.h> void interrupt far (*original_handler)(); void interrupt far handler(void) { printf("Some event just happened\n"); _disable(); _dos_setvect(0xFF, original_handler); _enable(); exit(0); } void main(void) { int i = 0; original_handler = _dos_getvect(0xFF); _disable(); _dos_setvect(0xFF, handler); _enable(); while (i++ < 100) ; geninterrupt(0xFF); }

    例127:dos.h…

    #include <stdio.h> #include <dos.h> int main(int argc, char const *argv[]) { int i; for (i = 0; i <= 255; i++) printf("Interrupt: %x Vector: %lx\n", i, _dos_getvect(i)); return 0; }

    例128:获取程序的进程id

    #include <stdio.h> #include <process.h> int main(int argc, char const *argv[]) { // getpid()需要包含process.h printf("Process id: %X\n", getpid()); return 0; }

    例129:一段会逆向执行的代码

    #include <stdio.h> #include <setjmp.h> #include <stdlib.h> // 全局变量 jmp_buf location; void function(void) { printf("About to longjmp\n"); // void longjmp(jmp_buf env,int val); // longjmp的作用是使用setjmp保存在location中栈环境信息返回到setjmp的位置, // 也就是当执行longjmp时程序又回到setjmp处(恢复现场) // 第二个参数是具有非0值的val,它将成为从setjmp处返回的值 longjmp(location, 1); } int main(int argc, char const *argv[]) { // int setjmp(jmp_buf env); - 设置执行点(保护现场) // 返回值:若直接调用则返回0,若从longjmp调用返回则返回非0值的longjmp中的val值 if (setjmp(location) != 0) { printf("Returning from longjmp\n"); exit(1); } function(); return 0; }

    例130:又来了…

    #include <stdio.h> #include <dos.h> #include <conio.h> int count = 0; void interrupt handler(void) { count++; } int main(int argc, char const *argv[]) { void interrupt (*original_handler)(); int old_count = 0; original_handler = _dos_getvect(5); _disable(); // Turn off interrupts during _dos_setvect _dos_setvect(5, handler); _enable(); printf("Press Shift-PrtSc three times or any key to end\n"); while (count < 3) if (count != old_count) { printf("Shift-PrtSc pressed\n"); old_count = count; } _disable(); _dos_setvect(5, original_handler); _enable(); return 0; }

    例131:还有…(如果有条件的可以试试,反正我是找不到编译通过的方法了)

    #include <stdio.h> #include <dos.h> void main (void) { printf("Operating system version number %d.%d\n", _osmajor, _osminor); printf("Operating system version number %d.%d\n", _version & 255, _version >> 8); }

    例132:dos.h(该说的前面都说了)

    #include <stdio.h> #include <dos.h> void main (void) { printf("The Program Segment Prefix begins at %X\n", _psp); }

    例133:

    #include <stdio.h> #include <dos.h> #include <conio.h> int alphanum = 0; int counter = 0; void interrupt far handler(void) { if (++counter == 273) // 15 seconds { alphanum = !alphanum; // Toggle counter = 0; } } void main(void) { int i; void interrupt far (*original_handler)(); original_handler = _dos_getvect(0x1C); _disable(); _dos_setvect(0x1c, handler); _enable(); while (! kbhit()) if (alphanum) for (i = 'A'; i <= 'Z'; i++) printf("%c\n", i); else for (i = 0; i <= 100; i++) printf("%d\n", i); _disable(); _dos_setvect(0x1c, original_handler); _enable(); }
    Processed: 0.016, SQL: 8