CLK_TCK,CLK_TCK未定义
关于C函数里,biostime的说明
我没使用过~——。-·给你参考下吧
函数名: biostime
功 能: 读取或设置BIOS时间
用 法: long biostime(int cmd, long newtime);
程序例:
#include stdio.h
#include bios.h
#include time.h
#include conio.h
int main(void)
{
long bios_time;
clrscr();
cprintf("The number of clock ticks since midnight is:\r\n");
cprintf("The number of seconds since midnight is:\r\n");
cprintf("The number of minutes since midnight is:\r\n");
cprintf("The number of hours since midnight is:\r\n");
cprintf("\r\nPress any key to quit:");
while(!kbhit())
{
bios_time = biostime(0, 0L);
gotoxy(50, 1);
cprintf("%lu", bios_time);
gotoxy(50, 2);
cprintf("%.4f", bios_time / CLK_TCK);
gotoxy(50, 3);
cprintf("%.4f", bios_time / CLK_TCK / 60);
gotoxy(50, 4);
cprintf("%.4f", bios_time / CLK_TCK / 3600);
}
return 0;
}
内部排序算法比较
按平均时间将排序分为四类:
(1)平方阶(O(n2))排序 一般称为简单排序,例如直接插入、直接选择和冒泡排序;
(2)线性对数阶(O(nlgn))排序 如快速、堆和归并排序;
(3)O(n1+£)阶排序 £是介于0和1之间的常数,即0£1,如希尔排序;
(4)线性阶(O(n))排序 如桶、箱和基数排序。
各种排序方法比较 简单排序中直接插入最好,快速排序最快,当文件为正序时,直接插入和冒泡均最佳。 影响排序效果的因素 因为不同的排序方法适应不同的应用环境和要求,所以选择合适的排序方法应综合考虑下列因素:
①待排序的记录数目n;
②记录的大小(规模);
③关键字的结构及其初始状态;
④对稳定性的要求;
⑤语言工具的条件;
⑥存储结构;
⑦时间和辅助空间复杂度等。
不同条件下,排序方法的选择
(1)若n较小(如n≤50),可采用直接插入或直接选择排序。 当记录规模较小时,直接插入排序较好;否则因为直接选择移动的记录数少于直接插人,应选直接选择排序为宜。
(2)若文件初始状态基本有序(指正序),则应选用直接插人、冒泡或随机的快速排序为宜;
(3)若n较大,则应采用时间复杂度为O(nlgn)的排序方法:快速排序、堆排序或归并排序。 快速排序是目前基于比较的内部排序中被认为是最好的方法,当待排序的关键字是随机分布时,快速排序的平均时间最短; 堆排序所需的辅助空间少于快速排序,并且不会出现快速排序可能出现的最坏情况。这两种排序都是不稳定的。 若要求排序稳定,则可选用归并排序。但本章介绍的从单个记录起进行两两归并的 排序算法并不值得提倡,通常可以将它和直接插入排序结合在一起使用。先利用直接插入排序求得较长的有序子文件,然后再两两归并之。因为直接插入排序是稳定的,所以改进后的归并排序仍是稳定的。
(4)在基于比较的排序方法中,每次比较两个关键字的大小之后,仅仅出现两种可能的转移,因此可以用一棵二叉树来描述比较判定过程。 当文件的n个关键字随机分布时,任何借助于"比较"的排序算法,至少需要O(nlgn)的时间。
百度文库里也有说明,详见:
下面是一段测试程序:
用系统计时器算时间复杂度。
#includestdio.h
#includestdlib.h
#includestring.h
#includetime.h
#define LIST_INIT_SIZE 50000
int bj1,yd1,n;
clock_t start_t,end_t;
typedef struct
{
int key;
}ElemType;
typedef struct
{
ElemType *elem;
int length;
}SqList;
void addlist(SqList L)
{
int i;
a: printf("请输入你要输入的个数:");
scanf("%d",n);
if(n50000)
{
printf("超出范围重新输入!!!\n");
goto a;
}
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem)exit(0);
L.length=0;
for(i=1;in+1;i++)
{
b: L.elem[i].key=rand();
if(L.elem[i].key30000)goto b;
++L.length;
}
}
void SelectSort(SqList L)//选择
{
start_t=clock();
int i,j,k,bj=0,yd=0;
for(i=1;iL.length;i++)
{
k=i;
for(j=i+1;jL.length;j++)
{
bj++;
if(L.elem[j].keyL.elem[k].key)k=j;
}
if(i!=k)
{
L.elem[0].key=L.elem[i].key;
L.elem[i].key=L.elem[k].key;
L.elem[k].key=L.elem[0].key;
yd+=3;
}
}
end_t=clock();
printf("比较次数为 %d移动次数为 %d\n",bj,yd);
printf("排序用时为 %f\n",float(end_t-start_t)/CLK_TCK);
}
void qipao(SqList L)//起泡
{
start_t=clock();
int i=1,j,bj=0,yd=0;
while(iL.length)
{
for(j=1;jL.length;j++)
{
bj++;
if(L.elem[j].keyL.elem[j+1].key)
{
L.elem[0].key=L.elem[j].key;
L.elem[j].key=L.elem[j+1].key;
L.elem[j+1].key=L.elem[0].key;
yd+=3;
}
}
i++;
}
end_t=clock();
printf("比较次数为 %d移动次数为 %d\n",bj,yd);
printf("排序用时为 %f\n",float(end_t-start_t)/CLK_TCK);
}
void InsertSort(SqList L)//直接插入
{
start_t=clock();
int i,j,yd=0,bj=0;
for(i=2;i=L.length;i++)
{
if(L.elem[i].keyL.elem[i-1].key)
{
L.elem[0].key=L.elem[i].key;
yd++;
j=i-1;
bj++;
while(L.elem[0].keyL.elem[j].key)
{
L.elem[j+1].key=L.elem[j].key;
j--;
yd++;
bj++;
}
L.elem[j+1].key=L.elem[0].key;
yd++;
}
}
end_t=clock();
printf("比较次数为 %d移动次数为 %d\n",bj,yd);
printf("排序用时为 %f\n",float(end_t-start_t)/CLK_TCK);
}
void xier(SqList L)//希尔
{
start_t=clock();
int i,d=L.length/2,j,w=0,k,yd=0,bj=0;
while(wd)
{
w=1;
for(i=w;iL.length;i=i+d)
{
k=i;
for(j=i+d;jL.length;j=j+d)
{
if(L.elem[i].keyL.elem[j].key)
{
k=j;
bj++;
}
}
if(i!=k)
{
L.elem[0].key=L.elem[i].key;
L.elem[i].key=L.elem[k].key;
L.elem[k].key=L.elem[0].key;
yd+=3;
}
w++;
}
d=d/2;
w=1;
}
end_t=clock();
printf("比较次数为 %d移动次数为 %d\n",bj,yd);
printf("排序用时为 %f\n",float(end_t-start_t)/CLK_TCK);
}
void BeforeSort()
{
yd1=0,bj1=0;
}
void display(int m,int n)
{
printf("比较次数为 %d移动次数为 %d\n",m,n);
}
int Partition(SqList L,int low,int high)//快速排序
{
int pivotkey;
L.elem[0]=L.elem[low];
yd1++;
pivotkey=L.elem[low].key;
while (lowhigh)
{
yd1++;
while(lowhighL.elem[high].key=pivotkey)
--high;
L.elem[low]=L.elem[high];
bj1++;
yd1++;
while (lowhighL.elem[low].key=pivotkey)
++low;
L.elem[high]=L.elem[low];
bj1++;
yd1++;
}
L.elem[low]=L.elem[0];
yd1++;
return low;
}
void QSort(SqList L,int low,int high)
{
int pivotloc;
if(lowhigh)
{
pivotloc=Partition(L,low,high);
QSort(L,low,pivotloc-1);
QSort(L,pivotloc+1,high);
}
}
void QuickSort(SqList L)
{
start_t=clock();
BeforeSort();
QSort(L,1,L.length);
display(yd1,bj1);
end_t=clock();
printf("排序用时为 %f\n",float(end_t-start_t)/CLK_TCK);
}
void Merge(ElemType R[],ElemType R1[],int low,int m,int high)//归并
{
int i=low, j=m+1, k=low;
while(i=mj=high)
{
if(R[i].key=R[j].key)
{
bj1++;
R1[k]=R[i];
yd1++;
i++;
k++;
}
else
{
bj1++;
R1[k]=R[j];
yd1++;
j++;
k++;
}
}
while(i=m)
{
R1[k]=R[i];
yd1++;
i++;
k++;
}
while(j=high)
{
R1[k]=R[j];
yd1++;
j++;
k++;
}
}
void MergePass(ElemType R[],ElemType R1[],int length, int n)
{
int i=0,j;
while(i+2*length-1n)
{
Merge(R,R1,i,i+length-1,i+2*length-1);
i=i+2*length;
}
if(i+length-1n-1)
Merge(R,R1,i,i+length-1,n-1);
else
for(j=i;jn;j++)
R1[j]=R[j];
}
void MSort(ElemType R[],ElemType R1[],int n)
{
int length=1;
while (lengthn)
{
MergePass(R,R1,length,n);
length=2*length;
MergePass(R1,R,length,n);
length=2*length;
}
}
void MergeSort(SqList L)
{
start_t=clock();
BeforeSort();
MSort(L.elem,L.elem,L.length);
display(yd1,bj1);
end_t=clock();
printf("排序用时为 %f\n",float(end_t-start_t)/CLK_TCK);
}
void main()
{
SqList L;
addlist(L);
printf("起泡排序: \n");
qipao(L);
addlist(L);
printf("直插排序: \n");
InsertSort(L);
addlist(L);
printf("选择排序: \n");
SelectSort(L);
addlist(L);
printf("希尔排序: \n");
xier(L);
addlist(L);
printf("快速排续: \n");
QuickSort(L);
addlist(L);
printf("归并排序: \n");
MergeSort(L);
}
求一个80行C语言程序
这是我从前写的键盘模拟程序。具体功能见里面的英文说明。
下面主函数 调用 只是 一个模拟 例子, 执行了抓取频幕,
并存放为 keyb_tmp.bmp
其它例子 注解掉了。去掉 // 就可执行。
编译器 MS VC++ 6.0
完整含说明,详细程序。(哦,字数超出,不让贴,不得不删去 一点说明)
/* ================================================*
* simu_keyboard.c
* Ji Zhang Jun-27-2006
* ---------------------------------------------------
* Function Syntax
* ---------------------------------------------------
* something can be done by such tech
* Send Screen_Capture, Window_Capture to clipboard.
* GetDIBits() can chose begin and end row number
* get mouse location on Desk Top ?
* ---------------------------------------------------
* Short-Cut key simulations:
* minimize all windows "windows + D" or "windows + M"
* cancel minimize windows "shift + window + M
* Alt+Backspace or CTRL+Z cancel previous op
* Alt+Shift+Backspace re-do the canceled op
* Alt+ESC active next window
* ===============================
void keybd_event(BYTE bVirtualKey, BYTE bScanCode,
DWORD dwFlags, DWORD dwExtraInfo);
bVirtualKey:
VK_NUMPAD7 0x67 VK_BACK 0x08
VK_NUMPAD8 0x68 VK_TAB 0x09
VK_NUMPAD9 0x69 VK_RETURN 0x0D
VK_MULTIPLY 0x6A VK_SHIFT 0x10
VK_ADD 0x6B VK_CONTROL 0x11
VK_SEPARATOR 0x6C VK_MENU 0x12
VK_SUBTRACT 0x6D VK_PAUSE 0x13
VK_DECIMAL 0x6E VK_CAPITAL 0x14
VK_DIVIDE 0x6F VK_ESCAPE 0x1B
VK_F1 0x70 VK_SPACE 0x20
VK_F2 0x71 VK_END 0x23
VK_F3 0x72 VK_HOME 0x24
VK_F4 0x73 VK_LEFT 0x25
VK_F5 0x74 VK_UP 0x26
VK_F6 0x75 VK_RIGHT 0x27
VK_F7 0x76 VK_DOWN 0x28
VK_F8 0x77 VK_PRINT 0x2A
VK_F9 0x78 VK_SNAPSHOT 0x2C
VK_F10 0x79 VK_INSERT 0x2D
VK_F11 0x7A VK_DELETE 0x2E
VK_F12 0x7B VK_LWIN 0x5B
VK_NUMLOCK 0x90 VK_RWIN 0x5C
VK_SCROLL 0x91 VK_NUMPAD0 0x60
VK_LSHIFT 0xA0 VK_NUMPAD1 0x61
VK_RSHIFT 0xA1 VK_NUMPAD2 0x62
VK_LCONTROL 0xA2 VK_NUMPAD3 0x63
VK_RCONTROL 0xA3 VK_NUMPAD4 0x64
VK_LMENU 0xA4 VK_NUMPAD5 0x65
VK_RMENU 0xA5 VK_NUMPAD6 0x66
================
bScanCode (use break code):
// Simulating a Alt+Tab keystroke
keybd_event(VK_MENU,0xb8,0 , 0); //Alt Press
keybd_event(VK_TAB,0x8f,0 , 0); // Tab Press
keybd_event(VK_TAB,0x8f, KEYEVENTF_KEYUP,0); // Tab Release
keybd_event(VK_MENU,0xb8,KEYEVENTF_KEYUP,0); // Alt Release
* ---------------------------------------------------
* SendInput() may do the same work
* ==============================================*/
#include Windows.h
#include Winuser.h
#include stdio.h
#include stdlib.h
#include memory.h
#include string.h
#include time.h
#define DEBUG 1
#pragma comment (lib, "User32.lib")
#pragma comment (lib, "Gdi32.lib")
void snapscreen_2_clipboard();
void snapwin_2_clipboard();
int get_mousebt_status();
void wait ( int m_seconds);
void simu_notepad();
void do_action(unsigned char action);
// dib functions
int GetBytesPerPixel(int depth);
int GetBytesPerRow(int width, int depth);
int GetBitmapBytes(int width, int height, int depth);
void save_clipboard_img_to_bmp();
FILE *fout;
main(int argc, char * argv[])
{
int i=0,j=0;
int px,py;
time_t now;
unsigned char action;
char buf[256];
// (void) do_action(0x44);
// following 3 calls for screen capture and save to bmp
(void) snapscreen_2_clipboard();
(void) wait(1000);
(void) save_clipboard_img_to_bmp();
// show exploer: (void) do_action(0x45); // works
// show file finder: (void) do_action(0x46); //works
// minimize all windows: (void) do_action(0x4d); // good
// show run command: (void) do_action(0x52); // Start-Run
// show start menu: (void) do_action(0x5B); // left mouse press Start
// show windows help: (void) do_action(0x70); // display "help center"
// Go on Standby: (void) do_action(0x5e); // do nothing
// simu_notepad();
/* -------------------------------------------------------
test mouse
press and release a BT, return the location and BT id
1 = Left 2=Right 3=Mid, [px,py] screen coords
* -------------------------------------------------------*/
/*---
while (i 40 ) {
j = get_mousebt_status(px,py);
if (j 0) while (get_mousebt_status(px,py) !=0 ) {};
if ( j 0) {
printf("%d: x=%d y=%d at ",j,px,py );
time(now);
printf("%s",ctime(now));
};
i = i + j;
};
*---*/
exit(0);
}
/* ------------------------------------------------ *
* void do_action(unsigned char action)
* action can be 0x45 0x46 0x4d 0x52 0x5b 0x70 0x5e
* -------------------------------------------------*/
void do_action(unsigned char action){
keybd_event(VK_LWIN,0,0,0);
keybd_event(action,0,0,0);
keybd_event(VK_LWIN,0,KEYEVENTF_KEYUP,0);
}
/* -------------------------------
* similar to system("notepad");
* typing letters
* ------------------------------*/
void simu_notepad()
{
/*
keybd_event(VkKeyScan('N'),1,0,0); keybd_event(VkKeyScan('N'),1,KEYEVENTF_KEYUP,0);
keybd_event(VkKeyScan('O'),0x98,0,0); keybd_event(VkKeyScan('O'),0x98,KEYEVENTF_KEYUP,0);
keybd_event(VkKeyScan('T'),0x94,0,0); keybd_event(VkKeyScan('T'),0x94,KEYEVENTF_KEYUP,0);
keybd_event(VkKeyScan('E'),0x92,0,0); keybd_event(VkKeyScan('E'),0x92,KEYEVENTF_KEYUP,0);
keybd_event(VkKeyScan('P'),0x99,0,0); keybd_event(VkKeyScan('P'),0x99,KEYEVENTF_KEYUP,0);
keybd_event(VkKeyScan('A'),0x9E,0,0); keybd_event(VkKeyScan('A'),0x9E,KEYEVENTF_KEYUP,0);
keybd_event(VkKeyScan('D'),0xA0,0,0); keybd_event(VkKeyScan('D'),0xA0,KEYEVENTF_KEYUP,0);
keybd_event(VK_RETURN,1,0,0);
keybd_event(VK_RETURN,1,KEYEVENTF_KEYUP,0);
*/
// keybd_event(VkKeyScan('C'),1,0,0); keybd_event(VkKeyScan('C'),1,KEYEVENTF_KEYUP,0);
// keybd_event(VkKeyScan('M'),1,0,0); keybd_event(VkKeyScan('M'),1,KEYEVENTF_KEYUP,0);
// keybd_event(VkKeyScan('D'),1,0,0); keybd_event(VkKeyScan('D'),1,KEYEVENTF_KEYUP,0);
// keybd_event(VK_RETURN,1,0,0);
// keybd_event(VK_RETURN,1,KEYEVENTF_KEYUP,0);
}
/* ------------------------------------------
* get mouse left and right button status
* if left down = 1
* if right down = 2
* else 0 -- up
* if GetAsyncKeyState(VK_RBUTTON) == 0 then UP
* if GetAsyncKeyState(VK_RBUTTON) != 0 then down
* down: ffff8000
* cmd catchs the left button !
* this can catch mouse outside of current window
BOOL GetCursorPos( LPPOINT lpPoint // cursor position );
POINT pt; GetCursorPos(pt);
return mouse BT status:
Left-down=1,right-down=2,mid-down=3,up=0;
return mouse position on Desk Top: [x,y]
* ------------------------------------------*/
int get_mousebt_status(int *x, int *y ){
int st;
short int b1,b2,b3;
POINT pt;
GetCursorPos(pt); // always to get mouse position
*x = pt.x; *y = pt.y; // return mouse position
st = b1 = b2 = b3 = 0;
b1 = 0x8000 GetAsyncKeyState(VK_LBUTTON);
b2 = 0x8000 GetAsyncKeyState(VK_RBUTTON);
b3 = 0x8000 GetAsyncKeyState(VK_MBUTTON);
if ( b1 != 0 ) st = 1; // if left button down, return 1
if ( b2 != 0 ) st = 2; // if right button down, return 2
if ( b3 != 0 ) st = 3; // if mid button down, return 3
return st;
}
void wait ( int m_seconds )
{
clock_t endwait;
endwait = clock () + m_seconds ; // * CLK_TCK
while (clock() endwait) {}
}
/* ----------------------------------
simulation of Alt-PrintScreen
to get current Window image in clipboard
Ji Zhang 2006-May-23
* -----------------------------------*/
void snapwin_2_clipboard()
{
keybd_event(VK_LMENU,0xA4,0,0);
keybd_event(VK_SNAPSHOT,0x2C,0,0);
keybd_event(VK_SNAPSHOT,0x2C,KEYEVENTF_KEYUP,0);
keybd_event(VK_LMENU,0xA4,KEYEVENTF_KEYUP,0);
}
/* ----------------------------------
simulation of PrintScreen
to get current Desk Top image in clipboard
Ji Zhang 2006-May-23
* -----------------------------------*/
void snapscreen_2_clipboard()
{
keybd_event(VK_SNAPSHOT,0x2C,0,0);
keybd_event(VK_SNAPSHOT,0x2C,KEYEVENTF_KEYUP,0);
}
/* --------------------------------------------------------------
* dib
int GetBytesPerPixel(int depth);
int GetBytesPerRow(int width, int depth);
int GetBitmapBytes(int width, int height, int depth);
* --------------------------------------------------------------*/
int GetBytesPerPixel(int depth)
{ return (depth==32 ? 4 : 3);
}
int GetBytesPerRow(int width, int depth)
{
int bytesPerPixel = GetBytesPerPixel(depth);
int bytesPerRow = ((width * bytesPerPixel + 3) ~3);
return bytesPerRow;
}
// bmi.bmiHeader.biWidth, bmi.bmiHeader.biHeight, bmi.bmiHeader.biBitCount
int GetBitmapBytes(int width, int height, int depth)
{
return height * GetBytesPerRow(width, depth);
}
void save_clipboard_img_to_bmp()
{
char nameout[80];
HANDLE h_bitmap,h_dib;
BITMAPINFO bmi;
HDC hDC;
int imageBytes;
BITMAPFILEHEADER hdr;
int scanLineCount;
unsigned char *img;
if (!OpenClipboard(NULL)) {
printf("Can not open clipboard\n");
exit(0);
};
if (DEBUG ==1) printf("pass open clipboard\n");
// HANDLE GetClipboardData(UINT uFormat);
h_bitmap = GetClipboardData(CF_BITMAP);
h_dib = GetClipboardData(CF_DIB);
if (h_bitmap ==NULL || h_dib ==NULL){
printf("I got NULL bitmap: ");
} else { printf("I got bitmap: ");};
memcpy(bmi,h_dib,sizeof(bmi));
printf("%d x %d \n",bmi.bmiHeader.biWidth, bmi.bmiHeader.biHeight);
hDC = CreateCompatibleDC(NULL); // Gdi32.lib.
CloseClipboard();
bmi.bmiHeader.biCompression = BI_RGB;
// possible to use part of imgage with img_w,img_h
imageBytes = GetBitmapBytes(bmi.bmiHeader.biWidth, bmi.bmiHeader.biHeight, bmi.bmiHeader.biBitCount);
printf("pass GetBitmapBytes=%d \n",imageBytes);
img = (char *) malloc(imageBytes);
if (!img) {
printf("No enought memory for img !\n"); exit(0);
}
// BITMAPFILEHEADER hdr;
hdr.bfType = ((WORD) ('M' 8) | 'B'); // is always "BM"
hdr.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
+ (bmi.bmiHeader.biClrUsed * sizeof(RGBQUAD)) + bmi.bmiHeader.biSizeImage;
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
+ (bmi.bmiHeader.biClrUsed * sizeof(RGBQUAD));
scanLineCount = GetDIBits(hDC,h_bitmap,0,bmi.bmiHeader.biHeight, img, bmi, DIB_RGB_COLORS);
strcpy(nameout,"keyb_tmp.bmp");
if ( (fout = fopen(nameout,"wb") ) == NULL ) {
printf("\007Cann't open output file: %s ", nameout);exit(1);
};
fwrite( hdr, sizeof(BITMAPFILEHEADER ), 1, fout );
fwrite( bmi, sizeof(BITMAPINFO), 1, fout );
fwrite( img, sizeof(unsigned char),imageBytes, fout );
fclose(fout);
printf("Output in %s\n",nameout);
}
/* ----------------------------end dib and bmp -------------------- */