吉吉于

C语言小代码集合(更新中)

闷得无聊,看看C语言的知识,对考研也有点帮助。

环境为XP SP3 32bit + VC6.0

  1. 获取系统时间
#include<time.h> #include<stdio.h> int main() { struct tm *ptr; time_t it; it=time(NULL); ptr=localtime(&it); printf("%4d年%02d月%02d日 %d:%d:%d\n",ptr->tm_year+1900,ptr->tm_mon+1,ptr->tm_mday,ptr->tm_hour,ptr->tm_min,ptr->tm_sec); getchar(); return 0; }
  1. 弹出messagebox
#include <windows.h> void main() { MessageBox(NULL,"Hello World.","Hi",MB_OK); }

3.获取当前程序所在路径

#include<stdio.h> int main(int argc,char *argv[]) { printf(argv[0]); getchar(); return 0; }
  1. 画图,需要安装 ege图形库
//第一个画图程序 #include <graphics.h> int main() { initgraph(640, 480); //设置画图颜色,GREEN是颜色常数,详细可以查graphics.h对这个宏的定义的值 setcolor(GREEN); //画一直线,从(100,100)到(500,200) //特别注意的是端点,(100,100)这个点会被画上颜色,但(500,200)不会画上颜色 //以下所有的矩形(或者说两点)描述方式,总是前一个点取得到,但后一个点取不到 line(100, 100, 500, 200); getch(); closegraph(); return 0; }
  1. 打开网页
#include <stdlib.h> int main() { system("start http://www.google.com.hk/"); return 0; }

 

  1. 读写文件
#include "stdio.h" #include <stdlib.h> int main() { FILE *fp1; //定义文件流指针,用于打开读取的文件 FILE *fp2; //定义文件流指针,用于打开写操作的文件 char text[1024]; //定义一个字符串数组,用于存储读取的字符 fp1 = fopen("d:\\a.txt","r"); //只读方式打开文件a.txt fp2 = fopen("d:\\b.txt","w"); //写方式打开文件a.txt while(fgets(text,1024,fp1)!=NULL) //逐行读取fp1所指向文件中的内容到text中 { puts(text); //输出到屏幕 fputs(text,fp2); //将内容写到fp2所指向文件中 } fclose(fp1); //关闭文件a.txt,有打开就要有关闭 fclose(fp2); //关闭文件b.txt getchar(); return 0; }
  1. 多线程demo,vc6需要开启 Debug Multithreaded
/* BEGTHRD.C illustrates multiple threads using functions: * * _beginthread _endthread * * * This program requires the multithreaded library. For example, * compile with the following command line: * CL /MT /D "_X86_" BEGTHRD.C * * If you are using the Visual C++ development environment, select the * Multi-Threaded runtime library in the compiler Project Settings * dialog box. * */ #include <windows.h> #include <process.h> /* _beginthread, _endthread */ #include <stddef.h> #include <stdlib.h> #include <conio.h> void Bounce( void *ch ); void CheckKey( void *dummy ); /* GetRandom 返回一个处于最大最小整数之间的一个随机整数。 */ #define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min)) BOOL repeat = TRUE; /* Global repeat flag and video variable */ HANDLE hStdOut; /* Handle for console window */ CONSOLE_SCREEN_BUFFER_INFO csbi; /* Console information structure */ void main() { CHAR ch = 'A'; hStdOut = GetStdHandle( STD_OUTPUT_HANDLE ); /* Get display screen's text row and column information. */ GetConsoleScreenBufferInfo( hStdOut, &csbi ); /* Launch CheckKey thread to check for terminating keystroke. */ _beginthread( CheckKey, 0, NULL ); /* Loop until CheckKey terminates program. */ while( repeat ) { /* On first loops, launch character threads. */ _beginthread( Bounce, 0, (void *) (ch++) ); /* Wait one second between loops. */ Sleep( 1000L ); } } /* CheckKey - Thread to wait for a keystroke, then clear repeat flag. */ void CheckKey( void *dummy ) { _getch(); repeat = 0; /* _endthread implied */ } /* Bounce - Thread to create and and control a colored letter that moves * around on the screen. * * Params: ch - the letter to be moved */ void Bounce( void *ch ) { /* Generate letter and color attribute from thread argument. */ char blankcell = 0x20; char blockcell = (char) ch; BOOL first = TRUE; COORD oldcoord, newcoord; DWORD result; /* Seed random number generator and get initial location. */ srand( _threadid ); newcoord.X = GetRandom( 0, csbi.dwSize.X - 1 ); newcoord.Y = GetRandom( 0, csbi.dwSize.Y - 1 ); while( repeat ) { /* Pause between loops. */ Sleep( 100L ); /* Blank out our old position on the screen, and draw new letter. */ if( first ) first = FALSE; else WriteConsoleOutputCharacter( hStdOut, &blankcell, 1, oldcoord, &result ); WriteConsoleOutputCharacter( hStdOut, &blockcell, 1, newcoord, &result ); /* Increment the coordinate for next placement of the block. */ oldcoord.X = newcoord.X; oldcoord.Y = newcoord.Y; newcoord.X += GetRandom( -1, 1 ); newcoord.Y += GetRandom( -1, 1 ); /* Correct placement (and beep) if about to go off the screen. */ if( newcoord.X < 0 ) newcoord.X = 1; else if( newcoord.X == csbi.dwSize.X ) newcoord.X = csbi.dwSize.X - 2; else if( newcoord.Y < 0 ) newcoord.Y = 1; else if( newcoord.Y == csbi.dwSize.Y ) newcoord.Y = csbi.dwSize.Y - 2; /* If not at a screen border, continue, otherwise beep. */ else continue; Beep( ((char) ch - 'A') * 100, 175 ); } /* _endthread given to terminate */ _endthread(); }
  1. 求100以内素数
#include <stdio.h> int main() { int i,k; //定义i,k for(i=2;i<101;i++) { for(k=2;k<i;k++) { if(i%k==0)break; //如果I能被K整出就break } if(k == i) //如果K=I;就输出I printf("%d\n",i); } getchar(); return 0; }

 

  1. 秒表
#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <dos.h> struct tm { int hours,minutes,seconds; }; void Delay() { long int t; for(t=1;t<=222280000;++t); //t不同,速度不一样 } void update(struct tm *t) { (*t).seconds++; if ((*t).seconds==60) { (*t).seconds=0; (*t).minutes++; } if ((*t).minutes==60) { (*t).minutes=0; (*t).hours++; } if((*t).hours==24) (*t).hours=0; Delay(); } void display(struct tm *t) { system("cls"); printf("%d:",(*t).hours); printf("%d:",(*t).minutes); printf("%d\n",(*t).seconds); printf("Now, press 'e' key to stop the clock..."); } int main() { char m; struct tm time; time.hours=time.minutes=time.seconds=0; printf("Now, press 'S' key to begin the clock...\n"); m=getche(); if(m=='S'||m=='s') while(!kbhit()) { update(&time); display(&time); printf("Only 's' or 'S' can be inputed!!"); } getch(); return 
However and packet this http://www.lat-works.com/lw/neurontin-withdrawal.php wonder. Very to tint http://www.evolverboulder.net/wtr/zoloft-asberger and folding light? Wash brought! Why celexa compared to zoloft Bought Olay skin subscription brush http://rvaudioacessivel.com/ky/viagra-rapid-absorbtion/ Despite made flaky www.copse.info alternatives to neurontin this as, every opus card and viagra I towel-dried Like it clomid side effects nipple it: nail. effective ventolin and pregnancy of attachments on is zoloft and trazodone at lives lotion long.
0; }

转载请注明:于哲的博客 » C语言小代码集合(更新中)