2013-08-02 43 views
0

我嘗試創建一個類似於「迷失在遷移」的東西爲我的決賽,但具有隨機化和定時器你怎麼運行兩個同時獨立地用C

我如何運行兩個麻煩IM項目辦做而相互獨立

計時器不應該消失,而且必須一直運行,直到時間到了,必須獨立運行

隨機不得影響定時器

錯誤 歸咎於迪rectional鍵不顯示導致

而(定時器)一次,然後轉到做一段時間(隨機),並會繼續在「隨機」而不會去回,而(定時器)

而循環(定時器)受到影響BU的getch()和getche()暫停其

#include<stdio.h> 
#include<conio.h> 
#include<stdlib.h> 
#include<windows.h> 
#include<time.h> 

//******************************************// 
//  DEFAULT BUILT-IN CLOCK    // 
//******************************************// 

void Wait(int seconds) 
{ 
    clock_t end wait; 
    endwait = clock() + seconds * CLK_TCK; 
    while (clock() < end wait) {} 
} 

//***********************************************************// 
//     DIRECTIONAL KEYS      // 
//***********************************************************// 

#define LEFT 75 
#define RIGHT 77 
#define UP 72 
#define DOWN 80 

int rdtsc() 
{ 
as m volatile("rdtsc"); 
} 

int main() 
{ 
    char ans; 
    int image; 
    int loop=1;  
    int correct=0, total=0; 
    int sec=0, min=0; 

//***********************************************************// 
//      TIMER        // 
//***********************************************************// 
int x=1; 
while(1==1) 
{ 
    Wait(1); 
    sec++; 
    if(sec==46) 
    { 
      loop=0; 
    } 

    printf("%i:%i\n\n", min, sec); 

//***********************************************************// 
//    RANDOMIZED IMAGES       // 
//***********************************************************// 


fflush(st din); 
srand(rdtsc()); 
image=rand()%4; 

do 
{ 

    if(image==0) 
    { 
    printf(">>IMAGE 1 CORRECT LEFT<<");  
    ans=getche();      
        if(ans==LEFT) 
        {   
        printf("\n\ncorrect!");   
        } 
        else 
        if(ans!=LEFT) 
        { 
        printf("\n\nwrong!"); 
        } 
    } 
    else 
    if(image==1) 
    { 
    printf(">>IMAGE 2 CORRECT UP<<"); 
    ans=getche(); 
        if(ans==UP) 
        {   
        printf("\n\ncorrect!");   
        } 
        else 
        if(ans!=UP) 
        { 
        printf("\n\nwrong!"); 
        } 
    } 
    else 
    if(image==2) 
    { 
    printf(">>IMAGE 3 CORRECT DOWN<<"); 
    ans=getche(); 
        if(ans==DOWN) 
        { 
        printf("\n\ncorrect!");      
        } 
        else 
        if(ans!=DOWN) 
        { 
        printf("\n\nwrong!"); 
        }   
    } 
    else 
    if(image==3) 
    { 
    printf(">>IMAGE 4 CORRECT RIGHT<<"); 
    ans=getche(); 
        if(ans==RIGHT) 
        {  
        printf("\n\ncorrect!");   
        } 
        else 
        if(ans!=RIGHT) 
        { 
        printf("\n\nwrong!"); 
        }   
    }  
getch(); 
system("cls"); 
} 
while(loop==1); 

} 

getch(); 
} 

回答

0
  • 創建一個狀態機,你們每個人要完成兩個任務。
  • 使用switch語句編寫代碼來實現每個狀態機。
  • 將它們依次放置在外部循環內。

    的for(int i = 0;我< 100; i ++在) {

    開關(STATE1) { 情況下0: //做你需要任務1 }

    開關(狀態2) { 情況下0: //做任何你需要的任務2 }

    }