2017-08-23 23 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欧姆。这工作。 –