我想寫一個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;
}
我已經包含了我編寫的用於調試程序的語句。我在這裏做錯了什麼?
乍一看:請在'main'開始時只調用'srand'一次。反覆調用它不會使它「更隨機」。相反,使用一秒粒度,您將重複將種子重置爲相同的值。 –
[請參閱此討論,爲什麼不在C中運行malloc()和系列的返回值..](https://stackoverflow.com/q/605845/2173917) –
在哪個系統/ OS上運行程序?你是否嘗試減少printf調用並增加循環時間? – user3336433