gpt4 book ai didi

parking 模拟的C程序没有给出输出

转载 作者:太空宇宙 更新时间:2023-11-04 06:15:51 24 4
gpt4 key购买 nike

我想编写一个 C 程序,其中有一个包含 10 个字符串的数组,其中每个字符串表示停在第 i 处的汽车的车牌号。随机选择一个点,如果空出,则生成一个随机车牌号并分配给该点,如果被占用,则腾出该点并删除车牌号。然而,程序进入了一个无限循环,这正是我想要的,但它没有打印我为调试程序而编写的任何语句。代码如下:

#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;
}

我也包含了我为调试程序而编写的语句。我在这里做错了什么?

最佳答案

您的程序存在访问冲突:spots 是一个包含十个字符串文字的数组:

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

这些文字是不可变的,试图更改它们是一种错误。

相反,定义一个包含十个字符数组的数组来存放您的车牌。您的模式 2-2-2-4 需要空间,空终止符需要一个字符:

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

现在 spots 是十个最大的空字符串。长度为 13。你可以测试你是否已经覆盖了它们:

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

您的代码还有更多问题:

  • 对于这么小的程序来说,动态内存分配确实是不必要的,并且使它变得复杂。您有 10 个带有 13 个字母车牌的插槽,可以在自动内存中轻松创建。
  • 不要为车牌分配内存,然后是strcpy。通过将 14 个字符的缓冲区传递给填充它的函数来直接创建车牌。
  • 较长的 strcat 序列非常笨拙。考虑使用 snprintf,它几乎可以一次性创建车牌。

这是您的问题的简明实现,仅限于 30 个 parking Action :

#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,就好像您在使用自动内存缓冲区一样。

关于 parking 模拟的C程序没有给出输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45831559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com