2017-01-15 114 views
-3
#include "stdio.h" 
#include "stdlib.h" 
#include "time.h" 

int main() 
{ 
    printf("\n1"); 

    srand ((unsigned)time(NULL)); 

    printf("\n1"); 

    struct node { 
     int Digi; 
     struct node *Prev; 
     struct node *Next; 
    }; 

    printf("\n1"); 

    struct node Num1, Num2, Num3, Num4, Num5, Num6, Num7, Num8, Num9, Num10,NumAdd; 

    Num1.Digi = rand() % 100; 
    Num1.Prev = NULL; 
    do { 
     Num2.Digi = rand() % 100; 
    } while ((Num2.Digi < Num1.Digi) || (Num2.Digi == Num1.Digi)); 

    Num2.Prev = &Num1; 
    Num1.Next = &Num2; 

    do 
     Num3.Digi = rand() % 100; 
    while ((Num3.Digi < Num2.Digi) || (Num3.Digi == Num2.Digi)); 

    Num3.Prev = &Num2; 
    Num2.Next = &Num3; 

    do 
     Num4.Digi = rand() % 100; 
    while ((Num4.Digi < Num3.Digi) || (Num4.Digi == Num3.Digi)); 

    Num3.Prev = &Num4; 
    Num3.Next = &Num3; 

    do 
     Num5.Digi = rand() % 100; 
    while ((Num5.Digi < Num4.Digi) || (Num5.Digi == Num4.Digi)); 

    Num5.Prev = &Num4; 
    Num4.Next = &Num5; 

    do 
     Num6.Digi = rand() % 100; 
    while ((Num6.Digi < Num5.Digi) || (Num6.Digi == Num5.Digi)); 

    Num6.Prev = &Num5; 
    Num5.Next = &Num6; 

    do 
     Num7.Digi = rand() % 100; 
    while ((Num7.Digi < Num6.Digi) || (Num7.Digi == Num6.Digi)); 

    Num7.Prev = &Num6; 
    Num6.Next = &Num7; 

    do 
     Num8.Digi = rand() % 100; 
    while ((Num8.Digi < Num7.Digi) || (Num8.Digi == Num7.Digi)); 

    Num8.Prev = &Num7; 
    Num7.Next = &Num8; 

    do 
     Num9.Digi = rand() % 100; 
    while ((Num9.Digi < Num8.Digi) || (Num9.Digi == Num8.Digi)); 

    Num8.Prev = &Num9; 
    Num9.Next = &Num8; 

    do 
     Num10.Digi = rand() % 100; 
    while ((Num10.Digi < Num9.Digi) || (Num10.Digi == Num9.Digi)); 

    Num10.Prev = &Num9; 
    Num9.Next = &Num10; 
    Num10.Next = NULL; 
    printf("\n1"); 
    printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", 
      Num1.Digi, Num2.Digi, Num3.Digi, Num4.Digi, Num5.Digi, 
      Num6.Digi, Num7.Digi, Num8.Digi, Num9.Digi, Num10.Digi); 
} 

該程序很少運行到最後。但打印出2「1」後,它總是卡住。有時它會正確運行並顯示10個數字。但它最有可能被卡住了。我知道有一些循環,但它們不會導致程序停留很長時間。我們現在都有快速的電腦。那麼有誰知道它爲什麼會卡住?簡單的C程序卡住了

+3

你的頭腦創造了[___MCVE___(http://stackoverflow.com/help/mcve)? –

+0

你是否嘗試過使用調試器或在其中放置幾個​​'printf'來查看控制流的位置? –

+0

爲什麼不在比較中使用'<='? –

回答

1

你有結構的循環,如果最後一個之前的任何接受的隨機數是99,其餘都不環路可以終止,但由於新的可以接受的數字不能小於99或等於99的方式,或大於99

生成一個填充了唯一隨機數的數組,排序然後將它們讀入您的struct s可能會更簡單。請注意0​​函數傳遞了已添加到列表中的隨機數的計數,以便僅搜索這些數字。

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <stdbool.h> 

bool in_list(int num, int arr[], size_t list_sz) 
{ 
    size_t i; 
    for (i = 0; i < list_sz; i++) { 
     if (arr[i] == num) { 
      break; 
     } 
    } 

    return i < list_sz ? true : false; 
} 

int compare(const void *x, const void *y) { 
    const int *cx = (const int *) x; 
    const int *cy = (const int *) y; 

    return (*cx > *cy) - (*cx < *cy); 
} 

int main(void) 
{ 
    int random_numbers[10]; 
    int num; 

    srand(time(NULL)); 

    for (size_t i = 0; i < 10; i++) { 
     do { 
      num = rand() % 100; 
     }while (in_list(num, random_numbers, i)); 
     random_numbers[i] = num; 
    } 

    qsort(random_numbers, 10, sizeof(int), compare); 

    for (size_t i = 0; i < 10; i++) { 
     printf("%d\n", random_numbers[i]); 
    } 

    return 0; 
} 

輸出示例:

0 
19 
20 
29 
30 
56 
58 
72 
82 
86 
+1

OMG非常感謝你!!!!!! –

+1

對不起,新的蜜蜂要疊加 –