VC++ C++ C# Algorithm

C++博客 首页 新随笔 联系 聚合 管理
  21 Posts :: 3 Stories :: 31 Comments :: 0 Trackbacks

2007年3月15日 #

 

void  move( int  number, char  from, char  rely, char  to)
{
    
if (number  ==   1 )
    
{
        printf(
" Move a disk from %c to %c\n " ,from,to);
        
return ;
    }

    move(number
- 1 ,from,to,rely);
    move(
1 ,from,rely,to);
    move(number
- 1 ,rely,from,to);
}


int  _tmain( int  argc, _TCHAR *  argv[])
{
    move(
3 , ' A ' , ' B ' , ' C ' );
    ::system(
" pause " );
    
return   0 ;
}


 

 

posted @ 2007-03-15 16:46 大熊猫 阅读(277) | 评论 (0)编辑 收藏

 1 int  A[ 10 ] = { 1 , 2 , 5 , 7 , 3 , 14 , 78 , 25 , 11 , 14 } ;
 2 void  InsertSort( int  a[], int  size)
 3 {
 4      for ( int  i = 1 ; i < size; i ++ )
 5      {
 6          int  key = a[i];
 7          int  j = i - 1 ;
 8          while (a[j] > key  &&  j >= 0 )
 9          {
10             a[j + 1 ] = a[j];
11             j -- ;
12         }

13         a[j + 1 ] = key;
14     }

15 }

16
17 int  _tmain( int  argc, _TCHAR *  argv[])
18 {
19
20     InsertSort(A, sizeof (A) / sizeof (A[ 0 ]));
21      for ( int  i = 0 ; i < 10 ; i ++ )
22      {
23         printf( " %d\n " ,A[i]);
24     }

25     ::system( " pause " );
26      return   0 ;
27 }

28
posted @ 2007-03-15 16:32 大熊猫 阅读(276) | 评论 (0)编辑 收藏

2007年3月12日 #

很多游戏都是用鼠标控制的,所以说处理鼠标事件也是非常重要的,鼠标事件和键盘事件处理方式差的不太多,所以我就直接给出了一个小程序,该程序把窗口一分为二,当在左半部分移动时,左面部分就变绿色,右面部分变黑色,在右半部分移动时,该部分就变蓝色,左半部分就变成了黑色,当鼠标左击时,对应的部分将会变红色。

#include  " SDL.h "
#include 
" SDL_ttf.h "
SDL_Surface 
* screen = NULL;

TTF_Font 
* font =
NULL;
// screen to show on window

const   int  SCREEN_BPP = 32 ;



int  main(  int  argc,  char *
 args[] )
{
    
// Start SDL

     bool  quit = false ;
    SDL_Rect rectLeft;
    SDL_Rect rectRight;
    rectLeft.x
= 0
;
    rectLeft.y
= 0
;
    rectLeft.w
= 320
;
    rectLeft.h
= 480
;
    rectRight.x
= 320
;
    rectRight.y
= 0
;
    rectRight.w
= 640
;
    rectRight.h
= 480
;
    SDL_Init( SDL_INIT_EVERYTHING );
    
if (TTF_Init() ==- 1
)
        
return   false
;
    
    screen 
=  SDL_SetVideoMode(  600 480
, SCREEN_BPP, SDL_SWSURFACE );
    
if (screen ==
NULL)
        
return   false
;

    Uint32 colorBlue
= SDL_MapRGB(screen -> format, 0 , 0 , 255
);
    Uint32 colorGreen
= SDL_MapRGB(screen -> format, 0 , 255 , 0
);
    Uint32 colorRed
= SDL_MapRGB(screen -> format, 255 , 0 , 0
);
    Uint32 colorBlack
= SDL_MapRGB(screen -> format, 0 , 0 , 0
);
    SDL_Event 
event
;
    
while ( !
quit)
    
{
        
if (SDL_PollEvent( & event
))
        
{
            
if ( event .type  ==
 SDL_MOUSEMOTION)
            
{
                Uint16 x
= event
.motion.x;
                Uint16 y
= event
.motion.y;


                
if (x > 0   &&  x < 320   &&  y > 0   &&  y < 480
 )
                
{
                    SDL_FillRect(screen,
&
rectLeft,colorBlue);
                    SDL_FillRect(screen,
&
rectRight,colorBlack);
                }

                
if (x > 320   &&  x < 640   &&  y > 0   &&  y < 480  )
                
{
                    SDL_FillRect(screen,
&
rectRight,colorGreen);
                    SDL_FillRect(screen,
&
rectLeft,colorBlack);
                }

            }

            
if ( event .type  == SDL_MOUSEBUTTONDOWN)
            
{
                Uint16 x
= event
.motion.x;
                Uint16 y
= event
.motion.y;
                
if ( event .button.button  ==
 SDL_BUTTON_LEFT)
                
{
                    
if (x > 0   &&  x < 320   &&  y > 0   &&  y < 480
 )
                    
{
                        SDL_FillRect(screen,
&
rectLeft,colorRed);
                    }

                    
if (x > 320   &&  x < 640   &&  y > 0   &&  y < 480  )
                    
{
                        SDL_FillRect(screen,
&
rectRight,colorRed);
                    }

                }

            }

            
if ( event .type  ==  SDL_QUIT)
                quit
= true
;
        }

        
if (SDL_Flip(screen)  ==   - 1 )
        
{
            
return   false
;
        }

    }

    SDL_FreeSurface(screen);
    SDL_Quit();

    
return   0 ;    
}

mousedown.jpg
posted @ 2007-03-12 20:10 大熊猫 阅读(1721) | 评论 (3)编辑 收藏

这次我们看看如何在SDL中处理键盘事件,每次事件发生以后,有关事件的所有信息都存储在SDL_Event类型的变量当中,查查手册我们可以知道,实际SDL_Event是一个联合体。
typedef union{
  Uint8 type;
  SDL_ActiveEvent active;
  SDL_KeyboardEvent key;
  SDL_MouseMotionEvent motion;
  SDL_MouseButtonEvent button;
  SDL_JoyAxisEvent jaxis;
  SDL_JoyBallEvent jball;
  SDL_JoyHatEvent jhat;
  SDL_JoyButtonEvent jbutton;
  SDL_ResizeEvent resize;
  SDL_ExposeEvent expose;
  SDL_QuitEvent quit;
  SDL_UserEvent user;
  SDL_SywWMEvent syswm;
} SDL_Event;
当发生键盘事件时,key变量就是有效的。
typedef struct{
  Uint8 type;
  Uint8 state;
  SDL_keysym keysym;
} SDL_KeyboardEvent;
key变量是一个结构体,其中keysym成员包含了按键信息。
typedef struct{
  Uint8 scancode;
  SDLKey sym;
  SDLMod mod;
  Uint16 unicode;
} SDL_keysym;
在keysym的成员当中,sym记录按键所对应的虚拟键。
比如说向上就是SDLK_UP;向下就是SDLK_DOWN;大家可以自己去查手册。

下面就看一个处理键盘消息的实例吧。
该例子中,我们一旦发现有上下左右四个键被按下,就马上在屏幕上显示相对应的消息,这里用到了扩展类库SDL_TTF,如有不清楚的,参考一下前面的几篇文章。
#include "SDL.h"
#include 
"SDL_ttf.h"
SDL_Surface 
*screen=NULL;
SDL_Surface 
*up=
NULL;
SDL_Surface 
*down=
NULL;
SDL_Surface 
*left=
NULL;
SDL_Surface 
*right=
NULL;
SDL_Surface 
*message=
NULL;
TTF_Font 
*font=
NULL;
//screen to show on window

const int SCREEN_BPP=32;
SDL_Color textColor
={255,255,255}
;


int main( int argc, char*
 args[] )
{
    
//Start SDL

    bool quit=false;
    SDL_Init( SDL_INIT_EVERYTHING );
    
if(TTF_Init()==-1
)
        
return false
;

    screen 
= SDL_SetVideoMode( 600480
, SCREEN_BPP, SDL_SWSURFACE );
    
if(screen==
NULL)
        
return false
;
    font
=TTF_OpenFont("tahoma.ttf",28
);
    up 
= TTF_RenderText_Solid( font, "Up was pressed."
, textColor );
    down 
= TTF_RenderText_Solid( font, "Down was pressed."
, textColor );
    left 
= TTF_RenderText_Solid( font, "Left was pressed"
, textColor );
    right 
= TTF_RenderText_Solid( font, "Right was pressed"
, textColor );
    SDL_Event 
event
;
    
while(!
quit)
    
{
        
if(SDL_PollEvent(&event
))
        
{
            
if(event.type ==
 SDL_KEYDOWN)
            
{
                
switch(event
.key.keysym.sym)
                
{
                    
case SDLK_UP: message=up;break
;
                    
case SDLK_DOWN: message=down;break
;
                    
case SDLK_LEFT: message=left;break
;
                    
case SDLK_RIGHT: message=right;break
;
                }

            }

            
if(event.type == SDL_QUIT)
                quit
=true
;
        }

        
if(message != NULL)
        
{
            Uint32 colorVal
=SDL_MapRGB(screen->format,0,0,0
);
            SDL_FillRect(screen, 
&screen->
clip_rect,colorVal);
            SDL_BlitSurface(message,NULL,screen,NULL);

            message
=
NULL;
        }

        
if(SDL_Flip(screen) == -1)
        
{
            
return false
;
        }

    }

    
//Quit SDL
    SDL_Quit();

    
return 0
;    
}

a.jpg
posted @ 2007-03-12 19:27 大熊猫 阅读(1203) | 评论 (0)编辑 收藏

2007年3月10日 #

 

int  _tmain( int  argc, _TCHAR *  argv[])
{
 
char  str1[]        =   " abc " ;
 
char  str2[]        =   " abc " ;
 
const   char  str3[]  =   " abc "
 
const   char  str4[]  =   " abc "
 
const   char *  str5   =   " abc " ;
 
const   char *  str6   =   " abc " ;

 cout
<< (str1 == str2) << " \n " ;
 cout
<< (str3 == str4) << " \n " ;
 cout
<< (str5 == str6) << " \n " ;
 ::system(
" pause " );
 
return   0 ;
}

以上会输出什么呢,答案是0,0,1。
前面四个变量都在栈上分配了内存,故内存地址大家都不同,而str5和str6所指字符串在常量区,大家有者一样的地址。
posted @ 2007-03-10 22:09 大熊猫 阅读(1053) | 评论 (0)编辑 收藏

仅列出标题  下一页