2017-08-23 54 views
0

我想寫一個C程序,我有一個10個字符串的數組,其中每個字符串表示停放在現場i的汽車的車牌號碼。隨機選取一個地點,如果空置,則生成一個隨機車牌號碼並將其分配到該地點,如果該地點被佔用,則該地點騰空並且車牌號碼被刪除。但是,該程序正在進入一個無限循環,這正是我想要的,但它不會打印我編寫的用於調試程序的任何語句。代碼如下:C程序停車模擬不給出輸出

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
#include <unistd.h> 
#include <time.h> 
#include <stdint.h> 

char * generateLicense() 
{ 
    srand((unsigned)time(NULL)); 
    char const *code[] = {"AN","AP","AR","AS","BR","CG","CH","DD","DL","DN","GA","GJ","HR","HP","JH","JK","KA","KL","LD","MH","ML","MP","MN","MZ","NL","OD","PB","PY","RJ","SK","TN","TR","TS","UK","UP","WB"}; 
    char const *alphabets[] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; 
    char const *numbers[] = {"0","1","2","3","4","5","6","7","8","9"}; 
    char *licensePlate = (char *)malloc(100*sizeof(char)); 
    strcpy(licensePlate,code[rand()%36]); 
    strcat(licensePlate,"-"); 
    strcat(licensePlate,numbers[rand()%10]); 
    strcat(licensePlate,numbers[rand()%10]); 
    strcat(licensePlate,"-"); 
    strcat(licensePlate,alphabets[rand()%26]); 
    strcat(licensePlate,alphabets[rand()%26]); 
    strcat(licensePlate,"-"); 
    strcat(licensePlate,numbers[rand()%10]); 
    strcat(licensePlate,numbers[rand()%10]); 
    strcat(licensePlate,numbers[rand()%10]); 
    strcat(licensePlate,numbers[rand()%10]); 
    return licensePlate;  
} 

int main() 
{ 
    char *messagebody = (char *)malloc(100*sizeof(char)); 
    char *licensePlate = (char *)malloc(100*sizeof(char)); 
    char *currentSpot = (char *)malloc(10*sizeof(char)); 
    char *by = ", by: "; 
    char *client = "From client 1, "; 
    char *spots[] = {"00-00-00-0000","00-00-00-0000","00-00-00-0000","00-00-00-0000","00-00-00-0000","00-00-00-0000","00-00-00-0000","00-00-00-0000","00-00-00-0000","00-00-00-0000"}; 
    int spot; 
    printf("variables declared\n"); 
    srand((unsigned)time(NULL)); 
    while(1) 
    { 
     printf("in while loop\n"); 
     //messagebody = ""; 
     //licensePlate = ""; 
     spot = rand()%10; 
     //currentSpot = ""; 
     sprintf(currentSpot, "%d", spot); 
     printf("%s",currentSpot); 
     strcpy(messagebody,client); 
     printf("%s",messagebody); 
     if(spots[spot] == "00-00-00-0000") 
     { 
      printf("%s",messagebody); 
      strcpy(licensePlate, generateLicense()); 
      printf("%s",licensePlate); 
      strcpy(spots[spot], licensePlate); 
      strcat(messagebody,"spot occupied: "); 
      printf("%s",messagebody); 
      strcat(messagebody,currentSpot); 
      printf("%s",messagebody); 
      strcat(messagebody,by); 
      printf("%s",messagebody); 
      strcat(messagebody,licensePlate); 
      printf("%s",messagebody); 
     } 
     else 
     { 
      printf("%s",messagebody); 
      strcpy(licensePlate, spots[spot]); 
      strcpy(spots[spot],"00-00-00-0000"); 
      strcat(messagebody,"spot vacated: "); 
      printf("%s",messagebody); 
      strcat(messagebody,currentSpot); 
      printf("%s",messagebody); 
      strcat(messagebody,by); 
      printf("%s",messagebody); 
      strcat(messagebody,licensePlate); 
      printf("%s",messagebody); 
     } 
     printf("%s",messagebody); 
     sleep(5); 
    } 
    return 0; 
} 

我已經包含了我編寫的用於調試程序的語句。我在這裏做錯了什麼?

+1

乍一看:請在'main'開始時只調用'srand'一次。反覆調用它不會使它「更隨機」。相反,使用一秒粒度,您將重複將種子重置爲相同的值。 –

+1

[請參閱此討論,爲什麼不在C中運行malloc()和系列的返回值..](https://stackoverflow.com/q/605845/2173917) –

+0

在哪個系統/ OS上運行程序?你是否嘗試減少printf調用並增加循環時間? – user3336433

回答

4

你的程序有一個訪問衝突:spots爲十個字符串常量數組:

char *spots[] = { 
    "00-00-00-0000", 
    "00-00-00-0000", 
    "00-00-00-0000", 
    ... 
}; 

這些文字是不可變的,它是一個arror去改變他們。

相反,定義一個十個字符數組的數組,可以容納你的車牌。你需要空間,爲您模式2-2-2-4加上一個字符爲空終止:

char spots[10][14] = {""}; 

現在spots是最大十個空字符串。長度13.可以testwhether已覆蓋他們已經與:

if (*spots[spot] == '\0') ... // string is empty 

還有更多的問題,您的代碼:

  • 動態內存分配是這樣的一個小程序,實際上是不必要的和使其變得複雜。您有10個帶13個字母牌照的插槽,可以在自動存儲器中輕鬆創建。
  • 不要爲車牌分配內存,然後strcpy。通過將14個字符的緩衝區傳遞給一個填充它的函數來直接創建車牌。
  • 長長的strcat序列非常笨拙。考慮使用snprintf,這將在近一步創建一個車牌。

這裏是一個簡潔的方式完成您的問題被限制在30停車動作:

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

void make_license(char str[]) 
{ 
    static const char *code[] = { 
     "AN", "AP", "AR", "AS", "BR", "CG", "CH", "DD", "DL", 
     "DN", "GA", "GJ", "HR", "HP", "JH", "JK", "KA", "KL", 
     "LD", "MH", "ML", "MP", "MN", "MZ", "NL", "OD", "PB", 
     "PY", "RJ", "SK", "TN", "TR", "TS", "UK", "UP", "WB" 
    }; 

    snprintf(str, 14, "%s-%02d-%c%c-%04d", 
     code[rand() % 36], rand() % 100, 
     'A' + rand() % 26, 'A' + rand() % 26, 
     rand() % 10000);  
} 

int main() 
{ 
    char spots[10][14] = {""}; 
    int n = 30; 

    srand(time(NULL)); 

    while (n--) { 
     int spot = rand() % 10; 

     if (*spots[spot]) { 
      printf("Car %s leaves spot %d.\n", spots[spot], spot + 1); 

      *spots[spot] = '\0';   // remove licence plate 
     } else { 
      make_license(spots[spot]);  // create licence plate 

      printf("Car %s arrives at spot %d.\n", spots[spot], spot + 1); 
     } 
    } 

    puts(""); 
    puts("Final arrangement"); 

    for (n = 0; n < 10; n++) { 
     printf("%4d %s\n", n + 1, spots[n]); 
    } 

    return 0; 
} 

如果要使用動態分配(也許這是分配的要求),你應該使許可證牌指針指向字符串。它們初始化到NULL,釋放他們,如果你從列表中刪除他們,同時也大功告成後一定要釋放任何剩餘的字符串:

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

char *make_license(void) 
{ 
    static const char *code[] = { 
     "AN", "AP", "AR", "AS", "BR", "CG", "CH", "DD", "DL", 
     "DN", "GA", "GJ", "HR", "HP", "JH", "JK", "KA", "KL", 
     "LD", "MH", "ML", "MP", "MN", "MZ", "NL", "OD", "PB", 
     "PY", "RJ", "SK", "TN", "TR", "TS", "UK", "UP", "WB" 
    }; 

    char *str = malloc(14); 

    snprintf(str, 14, "%s-%02d-%c%c-%04d", 
     code[rand() % 36], rand() % 100, 
     'A' + rand() % 26, 'A' + rand() % 26, 
     rand() % 10000); 

    return str; 
} 

int main() 
{ 
    char *spots[10] = {NULL}; 
    int n = 30; 

    srand(time(NULL)); 

    while (n--) { 
     int spot = rand() % 10; 

     if (spots[spot]) { 
      printf("Car %s leaves spot %d.\n", spots[spot], spot + 1); 

      free(spots[spot]); 
      spots[spot] = NULL;    // remove licence plate 
     } else { 
      spots[spot] = make_license(); // create licence plate 

      printf("Car %s arrives at spot %d.\n", spots[spot], spot + 1); 
     } 
    } 

    puts(""); 
    puts("Final arrangement"); 

    for (n = 0; n < 10; n++) { 
     printf("%4d %s\n", n + 1, spots[n] ? spots[n] : "--"); 
     free(spots[n]); 
    } 

    return 0; 
} 

但是,你應該清楚地決定哪種方法你拿。您的程序介於兩者之間:它分配內存,然後嘗試圍繞數據嘗試使用strcpy,就像使用自動內存緩衝區一樣。

+0

謝謝M歐姆。這工作。 –