2017-08-23 13 views
0

私は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; 
} 

私はプログラムをデバッグするために書いた文も含めました。私はここで間違って何をしていますか?

+1

一見: 'main'の先頭で' srand'を一度だけ呼び出してください。それを繰り返し呼び出すことによって、よりランダムになることはありません。逆に、1秒の細かさでは、同じ値にシードを繰り返しリセットします。 –

+1

[malloc()の戻り値とC ..のファミリをキャストしない理由についてのこのディスカッションを参照してください](https://stackoverflow.com/q/605845/2173917) –

+0

プログラム? printf呼び出しを減らし、ループ時間を短縮しようとしましたか? – user3336433

答えて

4

あなたのプログラムは、アクセス違反があります。spotsは10個の文字列リテラルの配列です:

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

これらのリテラルは不変であり、それらを変更しようとするarrorです。

代わりに、番号プレートを保持できる10個のchar配列の配列を定義します。あなたはヌル終端のためのあなたのパターン2-2-2-4プラス1文字のためのスペースが必要です。

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

spotsを最大の10個の空の文字列です。長さ13.あなたがあなたが既にそれらを上書きしているtestwhetherすることができます:あなたのコードに多くの問題があります

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

  • は、動的なメモリ割り当ては、このような小さなプログラムのために、本当に不要であり、複雑にする。自動メモリに簡単に作成できる13文字のナンバープレート付きのスロットが10個あります。
  • ナンバープレートのメモリを割り当てないで、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 Oehmありがとうございます。それはうまくいった。 –

関連する問題