• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            朗朗空間

            我知道并不是 所有鳥兒都飛翔

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              16 Posts :: 0 Stories :: 6 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團(tuán)隊

            搜索

            •  

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            書上的一個練習(xí)!

            // date 2008.5.25
            // author fanghua.nie
            // just do a exercise in the C How To Program
             
            #include <windows.h>
            #include <stdio.h>
            #include <stdlib.h>
            #include <time.h>
             
            #define START_POS 1
            #define RACE_DISTANCE  70  // the distances of the race
             
            #define TORTOISE 'T'    // the symbol that is printed  in the screen as a tortoise
            #define RABBIT 'R'      // the symbol that is printed in the screen as a rabbit
            #define ROAD '.'        // the symbol that is printed in the screen as a mile road.
            #define COMPLETE_ROAD '+'
             
            // define the console color
            #define LIGHT_GREEN FOREGROUND_GREEN | FOREGROUND_INTENSITY
            #define LIGHT_YELLOW FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY
            #define LIGHT_RED FOREGROUND_RED | FOREGROUND_INTENSITY
            // define BOOL datatype
            typedef int BOOL;
            #define TRUE 1   
            #define FALSE 0
             
             
             
            //get the speed of the tortoise or the rabbit
            int GetTortoiseSpeed ( int percent, int * speed );
            int GetRabbitSpeed ( int percent, int * speed );
             
            //output the result in console interface
            void PrintRaceResult ( int tortoisePosition, int rabbitPosition );
             
            int main (void)
            {
                
            int percent; // save the percent as a integer that less 10
                            
            // 1  10%,   2 20%, 9 90%, 10 100% etc.
                
            int speed; // save the speed of the rabbit or the tortoise
                
                
            int tortoisePosition = START_POS;
                
            int rabbitPosition = START_POS;
             
                
            srand ( time(NULL) );
             
                
            printf ("     The game began:\n");
                
            //show the tortoise and the rabbit
                
            PrintRaceResult ( tortoisePosition, rabbitPosition );
             
                
            while ( rabbitPosition < RACE_DISTANCE && tortoisePosition < RACE_DISTANCE )
                
            {
                    
            //compute next speed of the tortoise
                    
            percent = rand()%10 + 1;
             
                    
            if ( !GetTortoiseSpeed ( percent, &speed ) ) //get the speed of the tortoise
                    
            {
                        
            puts ("Error!\n");
                        
            exit (ERROR);
                    
            }
                    
                    
            // move the tortoise to next position
                    
            tortoisePosition = tortoisePosition + speed;
             
                    
            if ( tortoisePosition < 0 )
                        
            tortoisePosition = START_POS;
                    
            else if ( tortoisePosition > RACE_DISTANCE )
                        
            tortoisePosition = RACE_DISTANCE;
                    
                    
            //compute next rate of rabbit speed
                    
            percent = rand()%10 + 1; //get the percent
             
                    
            if ( !GetRabbitSpeed (percent, &speed) ) //get the speed of rabbit
                    
            {
                        
            puts ("Error!\n");
                        
            exit (ERROR);
                    
            }
                    
                    
            //move the rabbit
                    
            rabbitPosition = rabbitPosition + speed;
                    
                    
            //check the position whether is right or not, then correct it.
                    
            if ( rabbitPosition < 0 )
                        
            rabbitPosition = START_POS;
                    
            else if ( rabbitPosition > RACE_DISTANCE )
                        
            rabbitPosition = RACE_DISTANCE;
                    
                    
            //show the tortoise and the rabbit
                    
            PrintRaceResult ( tortoisePosition, rabbitPosition );
             
                
            }
                
                
            //print the race result
                
            if ( rabbitPosition > tortoisePosition )
                
            {
                    
            printf ("Rabbit Wins!\n");
                
            }
                
            else if ( rabbitPosition == tortoisePosition )
                
            {
                    
            printf ("It's a tie\n");
                
            }
                
            else
                
            {
                    
            printf ("Tortoise Wins!\n");
                
            }
             
                
            return 0;
            }
             
            BOOL GetTortoiseSpeed ( int percent, int * speed )
            {
                
            if ( percent >= 1 && percent <= 5 )
                
            {
                    
            (*speed) = 3;
                    
            return TRUE;
                
            }
                
            else if ( percent >= 6 && percent <= 7 )
                
            {
                    
            (*speed) = -6;
                    
            return TRUE;
                
            }
                
            else if ( percent >= 8 && percent <= 10 )
                
            {
                    
            (*speed) = 1;
                    
            return TRUE;
                
            }
                
            else
                
            {
                    
            return FALSE;
                
            }
            }
             
            BOOL GetRabbitSpeed ( int percent, int * speed )
            {
                
            if ( percent >= 1 && percent <= 2 )
                
            {
                    
            (*speed) = 0;
                    
            return TRUE;
                
            }
                
            else if ( percent >= 3 && percent <= 4 )
                
            {
                    
            (*speed) = 9;
                    
            return TRUE;
                
            }
                
            else if ( percent == 5 )
                
            {
                    
            (*speed) = -12;
                    
            return TRUE;
                
            }
                
            else if ( percent >= 6 && percent <= 8 )
                
            {
                    
            (*speed) = 1;
                    
            return TRUE;
                
            }
                
            else if ( percent >= 9 && percent <= 10 )
                
            {
                    
            (*speed) = -2;
                    
            return TRUE;
                
            }
                
            else
                
            {
                    
            return FALSE;
                
            }
            }
             
            void PrintRaceResult ( int tortoisePosition, int rabbitPosition )
            {
                
            int tcount;
                
            int rcount;
             
                
            HANDLE hOut;
                
            hOut = GetStdHandle(STD_OUTPUT_HANDLE);
                
            printf ("\n     ");
                
            for ( tcount = START_POS; tcount <= RACE_DISTANCE; tcount++ )
                
            {
             
                    
            if ( tcount == tortoisePosition )
                    
            {
                        
            SetConsoleTextAttribute (hOut, LIGHT_GREEN );
                        
            putchar (TORTOISE);
                    
            }
                    
            else if ( tcount < tortoisePosition )
                    
            {
                        
            SetConsoleTextAttribute (hOut, LIGHT_GREEN );
                        
            putchar (COMPLETE_ROAD);
                    
            }
                    
            else
                    
            {
                        
            SetConsoleTextAttribute (hOut, LIGHT_RED );
                        
            putchar (ROAD);
                    
            }
                
            }
                
            printf ("\n     ");
             
                
            for ( rcount = START_POS; rcount <= RACE_DISTANCE; rcount++ )
                
            {
                    
            if ( rcount == rabbitPosition )
                    
            {
                        
            SetConsoleTextAttribute ( hOut, LIGHT_GREEN );
                        
            putchar (RABBIT);
                    
            }
                    
            else if ( rcount < rabbitPosition )
                    
            {
                        
            SetConsoleTextAttribute (hOut, LIGHT_GREEN );
                        
            putchar (COMPLETE_ROAD);
                    
            }
                    
            else
                    
            {
                        
            SetConsoleTextAttribute ( hOut, LIGHT_YELLOW );
                        
            putchar (ROAD);
                    
            }
                
            }
                
            printf ("\n\n");
             
            }
            posted on 2008-05-25 13:56 聶元朗 閱讀(489) 評論(1)  編輯 收藏 引用 所屬分類: C語言學(xué)習(xí)筆記

            Feedback

            # re: 書上的練習(xí)-隨機數(shù)-龜兔賽跑[未登錄] 2008-05-27 13:54 冷月
            太強了!  回復(fù)  更多評論
              

            久久国产成人午夜aⅴ影院| 无遮挡粉嫩小泬久久久久久久| 九九精品99久久久香蕉| 国产精品久久亚洲不卡动漫| 亚洲一本综合久久| 人妻无码αv中文字幕久久琪琪布| 国产成人久久精品一区二区三区| 久久婷婷五月综合97色一本一本| 精品久久久久中文字| 亚洲国产另类久久久精品| 欧美亚洲另类久久综合| 国产精品久久久久久五月尺| 久久免费视频网站| A级毛片无码久久精品免费 | 久久91这里精品国产2020| 久久人做人爽一区二区三区| 色综合久久88色综合天天| 伊人久久精品无码二区麻豆| 久久综合久久性久99毛片| 精品久久久无码人妻中文字幕豆芽 | 久久久久婷婷| 青青热久久综合网伊人| 一本色综合网久久| 亚洲午夜精品久久久久久浪潮 | 精品永久久福利一区二区| 久久99精品久久久大学生| 久久精品中文字幕第23页| 97久久精品人人澡人人爽| 丰满少妇高潮惨叫久久久| 午夜久久久久久禁播电影| 久久人与动人物a级毛片| 久久中文字幕人妻熟av女| 深夜久久AAAAA级毛片免费看| 国产国产成人久久精品| 久久国产成人精品麻豆| 久久婷婷综合中文字幕| 97久久天天综合色天天综合色hd| 2022年国产精品久久久久| 伊人久久免费视频| 久久久久国产亚洲AV麻豆| 久久久亚洲精品蜜桃臀|