| 潇峰's profile疯子的天空PhotosBlogLists | Help |
|
|
May 19 CString 与 CTime 数据转换CTime 转换成 CString:(yy-mm--dd)
TimeString.Format("%d-%d-%d",Date.GetYear(),Date.GetMonth(),Date.GetDay());
CString:(yy-mm--dd) 转换成 CTime: COleDateTime dt; dt.ParseDateTime(TimeString); SYSTEMTIME st; dt.GetAsSystemTime(st); CTime tmp(st); Date=tmp; 用CFile与CArchive实现文件读写例如用Dialog-Based应用程序来实现文件读写:
void CMyDialog::SaveFile()
{
}
void CMyDialog::OpenFile()
{
}
同时要将虚函数Serialize() 加上:
void CMyDialog::Serialize(CArchive& ar)
{
}
当然也可向下调用成员变量的Serialize()函数 那么需要先像这样在成员变量的Serialize函数中添加代码
而此成员变量须是从CObject继承而来,因为CObject才能Serialize February 14 日历 C 源码 (待注释)/* xiaofeng @2005.10.25 */
/* simple Calendar */ #include <conio.h> #include <stdio.h> #include <dos.h> #define ESC 0x11b
#define LEFT 0x4b00 #define RIGHT 0x4d00 #define UP 0x4800 #define DOWN 0x5000 #define SPACE 0x3920 int input(int x,int y,char *msg);
int inkey()
{ int key=0; if (bioskey(1)) key=bioskey(0); return key; } int main() { int key=0; unsigned int year; unsigned long int day; char month; char maxdate[12]={31,28,31,30,31,30,31,31,30,31,30,31}; char months[12][10]={ "January","February","March","April","May","June","July","August", "September","October","November","December" }; struct date now; int i,vyear=1,vmonth=1; getdate(&now); year=now.da_year; month=now.da_mon; clrscr(); textcolor(YELLOW); cprintf("Current Date:%2d/%2d/%4d",now.da_day,now.da_mon,now.da_year); printf("\t\t"); textcolor(GREEN);cprintf("Year:"); textcolor(RED);cprintf("Left | Right "); textcolor(GREEN);cprintf("Month:"); textcolor(RED);cprintf("Up | Down"); textcolor(LIGHTBLUE); gotoxy(20,2);cprintf("Monthly Magazine"); gotoxy(10,3);cprintf (" ********* *********"); gotoxy(10,11);cprintf(" *****************************"); gotoxy(10,4);cprintf(" Sun Mon Tue Wed Thu Fri Sat "); textcolor(WHITE); gotoxy(12,12);cprintf("Press SAPCE to Specify Year."); while(key != ESC ) { key=0; if ( !(year%4) && year%100 || !(year%400) ) maxdate[1]=29; else maxdate[1]=28; day=year-1+(year-1)/4-(year-1)/100+(year-1)/400+1; for(i=0;i<month-1;i++) day+=maxdate[i]; if (vyear) {gotoxy(8,2);textcolor(RED);cprintf(" %5u ",year);} if(vmonth) {gotoxy(21,3);textcolor(WHITE);cprintf("%-10s\n",months[month-1]);} if (vyear || vmonth) { textcolor(LIGHTBLUE); gotoxy(1,5);clreol(); gotoxy(1,10);clreol(); gotoxy(12+(int)(day-day/7*7)*4,5); for (i=1;i<=maxdate[month-1];i++) { cprintf("%3d ",i); if ( day-day/7*7==6) printf("\n\t "); day++; } clreol(); vyear=vmonth=0; } while( key!=ESC && key!=SPACE && key!=LEFT && key!=RIGHT && key!=UP && key!=DOWN) key=inkey(); switch(key) { case UP: if( month==1) {if (year>1) {vmonth=12;vyear=year-1;} } else vmonth=month-1;break; case DOWN:if(month==12) {if (year<65535){ vmonth=1;vyear=year+1; } } else vmonth=month+1;break; case LEFT:if (year>1) vyear=year-1;break; case RIGHT:if (year<65535) vyear=year+1;break; case SPACE: textcolor(GREEN); vyear=input(13,13,"Enter year:"); gotoxy(1,13);clreol(); if (vyear) year=vyear; else vyear=year; /* for refresh */ } if (vyear) year=vyear; if (vmonth) month=vmonth; } textcolor(LIGHTGRAY); clrscr(); return 1; } int input(int x,int y,char *msg) { char pc=0; unsigned int n=0; gotoxy(x,y);cprintf("%s",msg); while( (pc!=13 || n==0 ) && pc!=27 ) { do{ pc=getch(); }while( !(pc>=48 && pc<=57 || pc==8 || pc==13 || pc==27) ); if ( pc>=48 && pc<=57) { if (pc==48 && n==0 || (long)n*10+pc-48 > 65535 ) continue; cprintf("%c",pc); n=n*10+pc-48; pc=0; } if (pc==8 && n>0) { printf("\b \b"); n/=10; pc=0; } if (pc==27) n=0; } return n; } 俄罗斯方块 [C 源码]/***** 俄罗斯方块 *******/
/***** 作者:疯子 *******/
/***** 仅供研究讨论 请勿随意转载 ******/
#include <graphics.h>
#include <alloc.h> #include <time.h> /* 定义按键 */
#define ESC 0x11b #define LEFT 0x4b00 #define RIGHT 0x4d00 #define UP 0x4800 #define DOWN 0x5000 #define ENTER 0x1c0d #define SPACE 0x3920 #define KEY_P 0x1970 #define cursor(keypress) (keypress==LEFT || keypress==RIGHT||keypress==UP|| keypress==DOWN) /* 定义的颜色 与坐标 */ #define BOX_SIZE 15 /* 一个小方块的尺寸 */ #define BDTOP 90 /* 主界面框的 位置 */ #define BDLEFT 120 /* TOP=x,LETF=y */ #define STATUS_XPOS 400 /* 信息显示的 位置 */ #define STATUS_YPOS 150 #define SCREEN_color LIGHTBLUE /* 屏幕颜色 前景色 */ #define BACK_color BLACK /* 屏幕 背景色 */ #define Border_color WHITE /* 各框的边框色 */ /* 转换坐标的宏 */
#define XPOS(x) (BDLEFT+(x-1)*BOX_SIZE) #define YPOS(y) (BDTOP+(y-1)*BOX_SIZE) #define SEGtime 1.05 /* 计时器计时的时间段 second */ /**************定义 全局 变量******************************************/
int background[20][10]={0}; /**** 直观的方块图形数据 ****/ int boxes[7][4][4]={
1,0,0,0, 1,1,0,0, 0,1,0,0, 0,0,0,0,
0,1,0,0,
1,1,0,0, 1,0,0,0, 0,0,0,0, 1,1,0,0,
1,1,0,0, 0,0,0,0, 0,0,0,0,
1,1,0,0,
1,0,0,0, 1,0,0,0, 0,0,0,0,
1,1,0,0,
0,1,0,0, 0,1,0,0, 0,0,0,0, 0,1,0,0,
1,1,1,0, 0,0,0,0, 0,0,0,0, 1,1,1,1,
0,0,0,0, 0,0,0,0, 0,0,0,0 }; int box[4][4]={0}; int pretype,precolor; int disable_rotate; /*** 当形状是正方形时,用来禁止旋转的参数 (否则旋转时出现闪烁) ***/ void *firstline,*boxpic; /* 用来储存图象 */ unsigned int score; unsigned int level; unsigned int RNDseed; int x,y; int xmax,ymax; /* 函数参数 */ enum showplace{gameview,preview}; /** for void_draw_one_boxe 指明绘图地点是游戏框还是预览框**/ enum timerpara{tm_reset,tm_continue,tm_pause,tm_count}; /** for int_timer() 重设(及初始化),继续,暂停,计时**/ enum rotatepara{preprocess=1,run=2}; /** for void_rotate (生成预览时的)后台旋转处理,或游戏中的实时旋转处理**/ /******************* 声明 函数 ****************************************/ void initialize(); /** 初始化图形界面 **/ void gminit(); /** 游戏的初始化 **/ int inkey(); /** 接受按键 **/ int newboxes(); /** 产生新的形状,返回参数:1-成功 0-失败 **/ void draw_one_box(int x,int y,int place); void clear_one_box(int x,int y); void drawboxes(int x,int y,int boxptr[4][4]); void clearboxes(int x,int y,int boxptr[4][4]); void boxpreview(); /** 下一个形状的预览 **/ int random(int maximum); /** 随机数生成函数 **/ void rotate(int thebox[4][4],int rotatepara); /** 图形旋转函数 **/ int judge(int thebox[4][4]); /** 判断移动或旋转是否可行的函数 1-成功 0-失败 **/ void puton(); /** 不能下落时的放置函数 **/ void expunction(); /** 消去行的函数 **/ int control(); /** 控制接口函数 返回值为按键值 **/ int timer(int timerpara); /** 计时器函数 返回值勤为计时信息 1-可继续计时 0-本段计时终了 **/ void showstatus(); /** 显视分数,难度级别等信息的函数 **/ void pause(); /** 游戏暂停 **/ /***** 主函数 ********************************************/ void main() { int move=0,next; static int bx,by; registerbgidriver(EGAVGA_driver); initialize(); gminit(); while( move!=ESC) { next=0; if (!newboxes()) move=ESC; /** 游戏结束 **/ while(move!=ESC && !next) { move=0; move=control(); bx=x;by=y; /* bx,by 为原图形坐标的备份 **/ /** 根据 control() 返回的信息来改变x,y 然后进行判断-judge() 如果判断此步不可行,恢复原来的x,y数据 ***/ switch(move) { case KEY_P: pause();break; case LEFT: if (x>1) {x--;move=1;} else move=2; break; case RIGHT: if (x+xmax<10) {x++;move=1;} else move=2; break; case UP: rotate(box,run);move=2;break; case SPACE: while( judge(box) ) y++; y--; clearboxes(bx,by,box); drawboxes(x,y,box); move=0; case DOWN: y++; if( judge(box) ) move=1; else { /** 如果不能继续下落,进行放置和消去 **/ y--; puton(); expunction(); next=1; /* 下一个图形的开始 */ move=0; } } if (move==1 && judge(box) ) { clearboxes(bx,by,box); /** 消掉原来的图形 **/ drawboxes(x,y,box); /** 绘出新的图形 **/ } else {x=bx;y=by;} } } closegraph(); free(boxpic); free(firstline); /** 已在void_initialize()中分配内存 **/ } /**************************************************************/
void initialize() { int gdriver=DETECT,gmode; initgraph(&gdriver,&gmode,""); setbkcolor(BACK_color); setfillstyle(SOLID_FILL,SCREEN_color); bar3d(0,0,639,479,0,0); firstline=malloc(imagesize(0,0,BOX_SIZE*10,BOX_SIZE)); /** 保存最顶一行的画面 **/ boxpic=malloc(imagesize(0,0,BOX_SIZE,BOX_SIZE)); } void gminit()
{ setcolor(Border_color); setfillstyle(SOLID_FILL,BACK_color); bar3d(BDLEFT-1,BDTOP-1,BDLEFT+10*BOX_SIZE+1+9,BDTOP+20*BOX_SIZE+1+19,0,0); getimage(XPOS(1),YPOS(1),XPOS(11),YPOS(2),firstline); bar3d(STATUS_XPOS-1,STATUS_YPOS-1,STATUS_XPOS+BOX_SIZE*4+1+3,STATUS_YPOS+BOX_SIZE*4+1+3,0,0); bar3d(STATUS_XPOS-20,STATUS_YPOS-1+BOX_SIZE*4+20,STATUS_XPOS+BOX_SIZE*4+1+3+20,STATUS_YPOS+BOX_SIZE*5+1+3+20,0,0); bar3d(STATUS_XPOS-20,STATUS_YPOS-1+BOX_SIZE*6+20,STATUS_XPOS+BOX_SIZE*4+1+3+20,STATUS_YPOS+BOX_SIZE*7+1+3+20,0,0); outtextxy(STATUS_XPOS-30,STATUS_YPOS+BOX_SIZE*7+32,"SPACE Key: Quick Down"); outtextxy(STATUS_XPOS-30,STATUS_YPOS+BOX_SIZE*8+32,"The P Key: Pause/Resume"); RNDseed=biostime(0,0); /** 初始化游戏数据 **/ pretype=random(7)-1; /**第一次产生的形状的类型 **/ precolor=random(14); /**第一次产生的形状的颜色 **/ /* 此后上面这两个数据在 void_boxpreview() 中产生 */ level=0; score=0; timer(tm_reset); } int inkey() { int key=0; if (bioskey(1)) key=bioskey(0); return key; } int newboxes()
{ int i,(*ptr)[4][4],color; xmax=ymax=0; x=4;y=1; color=precolor; ptr=boxes+pretype; if (pretype==2) disable_rotate=1; else disable_rotate=0; for(i=0;i<16;i++) { /**** 对于一个4*4 的方块数据, y=i/4,x=i%4 ****/ /**** xmax,ymax 为图形的最大坐标 ****/ box[i/4][i%4]=(*ptr)[i/4][i%4]; if ((*ptr)[i/4][i%4]==1) { if (xmax<i%4) xmax=i%4; if (ymax<i/4) ymax=i/4; } } if( ! judge(box) ) return 0; /*** 位置占满,不能绘出新图形 ***/ /** 产生下一个形状的预览,并刷新信息 **/ boxpreview(); showstatus(); setfillstyle(SOLID_FILL,color); drawboxes(x,y,box); return 1; } void draw_one_box(int x,int y,int place)
/**** 利用 bar3d() 画出一个小方块 ****/ { if (place==preview) bar3d(STATUS_XPOS+(x-1)*BOX_SIZE+x-1,STATUS_YPOS+(y-1)*BOX_SIZE+y-1, STATUS_XPOS+x*BOX_SIZE+x-1,STATUS_YPOS+y*BOX_SIZE+y-1,0,0); else if(place==gameview) bar3d(XPOS(x)+x-1,YPOS(y)+y-1,XPOS(x+1)+x-1,YPOS(y+1)+y-1,0,0); } void clear_one_box(int x,int y)
/**** 利用putimage() 的XOR_PUT 将原图消去 ****/ /**** 游戏主框的背景色(其实是bar3d的颜色)要和setbkcolor() 中所设的颜背色一致 ***/ /** 因为XOR运算后的颜色是屏幕的背景色 **/ { getimage(XPOS(x)+x-1,YPOS(y)+y-1,XPOS(x+1)+x-1,YPOS(y+1)+y-1,boxpic); putimage(XPOS(x)+x-1,YPOS(y)+y-1,boxpic,XOR_PUT); } void drawboxes(int x,int y,int boxptr[4][4])
{ int i; for(i=0;i<16;i++) if ( boxptr[i/4][i%4]==1) draw_one_box(x+i%4,y+i/4,gameview); } void clearboxes(int x,int y,int boxptr[4][4])
{ int i; for(i=0;i<16;i++) if ( boxptr[i/4][i%4]==1 ) clear_one_box(x+i%4,y+i/4); } void boxpreview()
/*** 图形的数据只有7种,但可以预旋转,从而产生各种形状 ***/ { int i,*pt; int rndrotate; /** 预旋转的次数 **/ rndrotate=random(4)-1; pretype=random(7)-1; precolor=random(14); pt=(int*)(*(boxes+pretype)); setfillstyle(SOLID_FILL,BACK_color);
bar3d(STATUS_XPOS-1,STATUS_YPOS-1,STATUS_XPOS+BOX_SIZE*4+1+3,STATUS_YPOS+BOX_SIZE*4+1+3,0,0); setfillstyle(SOLID_FILL,precolor); if( pretype!=2) /** 正方形时,旋转就免了吧 **/
{ for (i=0;i<rndrotate;i++) rotate( *(boxes+pretype),preprocess ); } for (i=0;i<16;i++) if (*(pt+i)==1) draw_one_box(1+i%4,1+i/4,preview); } int random(int maximum)
{ RNDseed=RNDseed*RNDseed*17/97+RNDseed+23; return RNDseed%maximum+1; } void rotate(int thebox[4][4],int rotatepara)
/**** 旋转的算法: curbox[x][ymax-y]=oribox[y][x] (ymax>y) 当ymax<y 时,y替代负数yamx-y 且 curbox[x][y]=0 将预旋转和实时旋转分开写,是因为预旋转时不知道xmax,ymax...... ****/ { int prebox[4][4]={0}; int i,bymax,bxmax; if (rotatepara==run && disable_rotate ) return; if(rotatepara==preprocess ) /*** 预旋转处理 ***/ { bymax=0; for(i=0;i<16;i++) if ( (prebox[i/4][i%4]=thebox[i/4][i%4])==1) if ( bymax< i/4 ) bymax=i/4; for (i=0;i<16;i++) if( bymax>=i/4 ) thebox[i%4][bymax-i/4]=prebox[i/4][i%4]; else thebox[i%4][i/4]=0; return; } else if (rotatepara==run) { bymax=ymax;bxmax=xmax; xmax=ymax=0; for (i=0;i<16;i++) { if( thebox[i/4][i%4]==1) prebox[i%4][bymax-i/4]=thebox[i/4][i%4]; if (prebox[i%4][bymax-i/4]==1) { if (ymax<i%4) ymax=i%4; if (xmax< bymax-i/4 ) xmax=bymax-i/4; } }
if( judge(prebox) ) /** 如时旋转成功,刷新画面并更新原数据 **/ { clearboxes(x,y,box); drawboxes(x,y,prebox); for (i=0;i<16;i++) thebox[i/4][i%4]=prebox[i/4][i%4]; } else { xmax=bxmax;ymax=bymax;} /** 否则 还原数据 **/ } } int judge(int thebox[4][4])
/*** 如果图形中位置box[x][y] 与背景中对应的位置background[X][Y] 同时为1 (图形有重叠) ,则判断结果为假 (0) ***/ { int i,*backptr,*boxptr; int moveable=1; if ( y+ymax>20 || x+xmax>10 ) return 0; /* 越界 */ boxptr=(int*)thebox; for (i=0;i<16;i++) if ( *(boxptr+i)==1 && background[y-1+i/4][x-1+i%4]==1 ) { moveable=0; break; } return moveable; } void puton()
{ int i; for (i=0;i<16;i++) if( box[i/4][i%4]==1) *(*(background+y-1+i/4)+x-1+i%4)=1; } void expunction()
/** 消去方法是用getimage() ,putimage() **/ { int i,j,k; int size; static unsigned int lines=0; int tetris=0; void *ptr; for (i=y+ymax;i>=y;i--) { k=1; for(j=0;j<10;j++) if ( background[i-1][j]==0) {k=0; break;} if(k==1 ) { if( i>1 ) { for(j=i-1;j>0;j--) for(k=0;k<10;k++) background[j][k]=background[j-1][k]; size=imagesize(XPOS(1),YPOS(1),XPOS(11)+9,YPOS(i)+i-2);
ptr=malloc(size); getimage(XPOS(1),YPOS(1),XPOS(11)+9,YPOS(i)+i-2,ptr); putimage(XPOS(1),YPOS(2)+1,ptr,COPY_PUT); } for(k=0;k<10;k++) background[0][k]=0; /*** 每消去一行,最顶行必然是空的,这一空行firstline在void_gminit() 己经读取过 ***/ putimage(XPOS(1),YPOS(1),firstline,COPY_PUT);
lines++; i++; y++; tetris++; if (lines==100) {level++;lines=0;} free(ptr); } } if( tetris!=0 ) score+=3*tetris-2; } int control()
{ int key=0; while( !cursor(key) && key!=ESC && key!=SPACE && key!=KEY_P)
{ key=0; while(key==0) { key=inkey(); if ( ! timer(tm_count) ) /** 超过一定时间 则自动下落 **/ { key=DOWN; timer(tm_reset); /** 下落一行后重新开始计时 **/ } } } return key; } int timer(int timerpara)
{ static float oritime; static float curtime; static float elatime; switch (timerpara)
{ case tm_count: curtime=clock()/CLK_TCK; if (curtime-oritime>=SEGtime-(float)level/10) return 0; break; case tm_pause : curtime=clock()/CLK_TCK; elatime=curtime-oritime; /** 记下此次经历过的时间 **/ break; case tm_continue: curtime=clock()/CLK_TCK; oritime=curtime-elatime; /** 继续暂停时的计时 **/ break; case tm_reset: oritime=curtime=clock()/CLK_TCK; } return 1; } void showstatus()
{ unsigned int n=10000; char lv[2]="\0",sc[2]="\0"; int x=1; int m=score; *lv=level+48; setfillstyle(SOLID_FILL,BACK_color);
bar3d(STATUS_XPOS-20,STATUS_YPOS-1+BOX_SIZE*4+20,STATUS_XPOS+BOX_SIZE*4+1+3+20,STATUS_YPOS+BOX_SIZE*5+1+3+20,0,0); bar3d(STATUS_XPOS-20,STATUS_YPOS-1+BOX_SIZE*6+20,STATUS_XPOS+BOX_SIZE*4+1+3+20,STATUS_YPOS+BOX_SIZE*7+1+3+20,0,0); outtextxy(STATUS_XPOS-20+10,STATUS_YPOS-1+BOX_SIZE*4+20+7,"Level");
outtextxy(STATUS_XPOS-20+70,STATUS_YPOS-1+BOX_SIZE*4+20+7,lv); outtextxy(STATUS_XPOS-20+5,STATUS_YPOS-1+BOX_SIZE*6+20+7,"Score:");
while(n>0) /*** 将数据(int_score) 每一位转化为字符后再一一显示 ***/ { *sc=48+m/n; if ( m/n>0 ) m-=m/n*n; outtextxy(STATUS_XPOS-20+x*10+45,STATUS_YPOS-1+BOX_SIZE*6+20+7,sc); n/=10; x++; } } void pause() { int tkey=0; timer(tm_pause); while( tkey !=KEY_P ) tkey=inkey(); timer(tm_continue); } /** Turbo C.20 编译调试通过 @2005.9.10 **/ |
|
|