易截截图软件、单文件、免安装、纯绿色、仅160KB

C Snippet #8(规定时间输入,否则默认跳转的实现)

http://www.ddj.com/cpp/221600722
Q: HOW DO I... put timers with default actions in my C code?
A: Many times, we need to write programs that will only wait a certain specified amount of time for a user to do something. After that time, we need to assume that the user isn't going to do anything and move on to some specified default action. This Snippet works exactly like the get_ch() function in most PC C compiler libraries, except that it will only wait as long as you tell it for the user to hit a key. If no key is pressed within the time limit, the function returns with an EOF. As usual, a test harness has been added to allow for stand-alone testing. To compile the stand-alone test, simply define the TEST macro.
/*
** TIMEGETC.C - waits for a given number of seconds for the user to press
** a key. Returns the key pressed, or EOF if time expires
**
** Public domain by Bob Jarvis
** Test harness added by Bob Stout
**
** Note: This relies on the non-standard kbhit() function, which is available
** in all Windows PC compilers - including MinGW32 (gcc).
*/
#include <stdio.h>
#include <time.h>
#include <conio.h>
int timed_getch(int n_seconds)
{
time_t start, now;
start = time(NULL);
now = start;
while(difftime(now, start) < (double)n_seconds && !kbhit())
{
now = time(NULL);
}
if(kbhit())
return getch();
else return EOF;
}
#ifdef TEST
main()
{
int c;
const char *ctrlchars[] =
{
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
"BS_", "HT_", "LF_", "VT_", "FF_", "CR_", "SO_", "SI_",
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
"CAN", "EM_", "SUB", "ESC", "FS_", "GS_", "RS_", "US_"
};

printf("Starting a 5 second delay...\n");
c = timed_getch(5);
if(c == EOF)
printf("Timer expired\n


相关文档:

Facts of C Programming Language

Facts of C Programming Language
C的一些掌故
(英文原文:http://www.programmingfacts.com/2009/12/01/facts-of-c-programming-language/)
C programming language was developed in 1972 by Dennis Ritchie and Brian Kernighan at the Bell Telephone Laboratories (AT&T Bell Laboratories) for use with the Un ......

C#与C/C++的区别的地方


编译:C程序直接编译成标准的二进制可执行的代码,但C#的源程序并不是被编译成二进制可执行的形式,而是一种中间语言(MSIL),类似JAVA中的字节码
结构体:C#的结构体与C++的结构体相似。但是C#的结构体与类是不同的,而且不支持继承。
预编译:C#中存在预编译的指令支持条件编译,警告,错误报告和编译行控制。其指令 ......

c调用api打开文件对话框

#include <windows.h>
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd )
{
OPENFILENAME ofn;
//在内存中开辟一块空间,存放用户选取的文件名
char szFile[MAX_PATH];//MAX_PATH ......

纯C实现c++类

//#include "stdafx.h"
/*
描述:纯c模拟类,纯c编写c++类,纯c实现c++类的简单范例,结构模拟类,struct 编写class.
c编写类是实现纯c编写com组件的基础。
*/
#include <stdio.h>
typedef struct _Vtbl
{
    void (*AddRef)(struct CObject* obj,int);//所有的函数的第一个参数类似class的隐匿的 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号