私は10文字列の配列を持つCプログラムを書こうとしています。各文字列はスポット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'を一度だけ呼び出してください。それを繰り返し呼び出すことによって、よりランダムになることはありません。逆に、1秒の細かさでは、同じ値にシードを繰り返しリセットします。 –
[malloc()の戻り値とC ..のファミリをキャストしない理由についてのこのディスカッションを参照してください](https://stackoverflow.com/q/605845/2173917) –
プログラム? printf呼び出しを減らし、ループ時間を短縮しようとしましたか? – user3336433