为结构增加价值,在C

时间:2019-02-19 18:21:06

标签: c

我是编程的新手,所以我试图编写一个小程序,既可以显示汽车信息,又可以将汽车添加到“库”中 现在,我要参加1.Show汽车的外观如下:

  ID          BRAND         PICS 
  bbb188     BMW    1   2   3
  AAA-999     VOLVO    4   5   6
  CCC-999     CITROEN    1   2   3

但是添加新车后,PICS不会显示。 因此,如果我要添加AAA-111 VOLVO 1,结果如下:

 bbb188     BMW    1   2   3
 AAA-999     VOLVO    4   5   6
 CCC-999     CITROEN    1   2   3
 AAA-111     VOLVO    -398253632   3   3

我只得到图片的随机数,总是3个值。 谁能帮我这个忙,请告诉我怎么做。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define MAX 1000
#define IDSIZE 20
#define BRANDSIZE 50
#define PICSIZE 10

typedef struct{
  char id[IDSIZE+1];
  char brand[BRANDSIZE+1];
  int *pic;
} Car;


void printCar(Car *pCar,int imagecount)
{
  printf(" %s ",pCar->id);
  printf("    %s ",pCar->brand);
  for(int i=0; i<imagecount; i++){
    printf("   %d",pCar->pic[i]);
  }
  printf("\n");
}

Car initCar(char itsId[],char itsBrand[],int *itsPic, int imagecount)
{
  Car newCar;
  strcpy(newCar.id, itsId);
  strcpy(newCar.brand, itsBrand);
  newCar.pic = itsPic;

  return newCar;
}

void PrintList(Car aLista[],int imagecount, int carcount)
{
  for(int i = 0; i<imagecount; i++)
    printCar(&aLista[i],carcount);
}

void AddCar(Car aList[], int *pAt, Car theCar) 
{
  aList[(*pAt)++]=theCar;    
}

Car NewCar(Car minapatienter[], int patientCount)
{
  Car newCar;

  gets(newCar.id);
  printf("type in  ID \n"); 
  gets(newCar.id); 
  printf("type in brand\n");
  gets(newCar.brand);

  bool imageInputDone = false; 
  int imageCount=0;
  while(imageInputDone == false)
  {
    printf("type in image reference \n");
    int newImage; 
    scanf("%d",&newImage);

    newCar.pic = &newImage; 
    imageCount++;
    printf("vill du \n1.Add another image reference \n2.exit\n");
    int input;
    scanf("%d", &input);
    printf("input: %i\n",input);
    switch(input)
    {
        case 1: 
            printf("Adding one more image\n");
            break;
        case 2: 
            printf("Leaving loop\n");
            imageInputDone = true; 
            break;
        default:
            while (input<1 || input<2)
              ;
            printf("Input correct number\n");
            break;
    }

    return newCar; 
  }
}

int main(void)
{
  int carCount=0;
  int imagecount=0;
  Car myCar[MAX]; 
  int input;

  int test[3]={1,2,3};
  int test2[3]={4,5,6};

  myCar[0]= initCar("bbb188","BMW", test, 3);
  myCar[1] = initCar("AAA-999","VOLVO", test2, 3);
  myCar[2] = initCar("CCC-999", "CITROEN", test,3);

  carCount=3;
  imagecount=3;

  do {
    printf("1. Show cars \n2. Add car \n");
    scanf("%d",&input);
    switch(input)
    {
      case 1:
        printf("ID          BRAND         PICS \n");
        PrintList(myCar,carCount, imagecount);
        break; 
      case 2: 
        AddCar(myCar,&carCount,NewCar(myCar,carCount));
        printf("ID          BRAND         PICS \n");
        PrintList(myCar,carCount, imagecount);
    }    //break; 
  } while (input < '1'|| input < '2');

  return 0;
}

2 个答案:

答案 0 :(得分:0)

您的NewCar函数存在一些问题。 newImage在堆栈存储器中。当您进行分配newCar.pic = &newImage;时,newCar.pic将指向未定义的内存区域,因为newImage不在其范围内。更好的方法是,我们仅使用其值,而不在此处使用地址运算符。还有newCar.pic是一个指针(int数组)。因此,您需要在使用前分配它。添加更多图像项目时,需要重新分配它。并将pic初始化为NULL指针。

这是我对您的NewCar函数的修改:

Car NewCar(Car minapatienter[], int patientCount)
{
    Car newCar;

    gets(newCar.id);
    printf("type in  ID \n"); 
    gets(newCar.id); 
    printf("type in brand\n");
    gets(newCar.brand);
    newCar.pic = NULL;

    bool imageInputDone = false; 
    int imageCount=0;
    while(imageInputDone == false)
    {
      printf("type in image reference \n");
      int newImage; 
      scanf("%d",&newImage);

      // Rellocation
      newCar.pic = realloc(newCar.pic, (imageCount+1)*sizeof(int));
      newCar.pic[imageCount] = newImage; 
      imageCount++;

      printf("vill du \n1.Add another image reference \n2.exit\n");
      int input;
      scanf("%d", &input);
      printf("input: %i\n",input);
      switch(input)
      {
          case 1: 
              printf("Adding one more image\n");
              break;
          case 2: 
              printf("Leaving loop\n");
              imageInputDone = true; 
              break;
          default:
              while (input<1 || input<2)
                ;
              printf("Input correct number\n");
              break;
      }

      return newCar; 
    }
}

答案 1 :(得分:0)

由于只有一个全局计数器,因此每辆车打印的图像数量相同。每个图片都需要一个计数器:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define MAX       1000
#define IDSIZE    20
#define BRANDSIZE 50
#define PICSIZE   10

typedef struct Car
{
  char  id[IDSIZE+1];
  char  brand[BRANDSIZE+1];
  int  *pic;
  int   imagecount;
} Car;

有了此更改,就无需传递打印计数:

void printCar(Car *pCar)
{
  printf(" %s ", pCar->id);
  printf("    %s ", pCar->brand);
  for(int i=0; i<pCar->imagecount; i++)
  {
    printf("   %d",pCar->pic[i]);
  }
  printf("\n");
}

计数器需要在初始化期间存储:

Car initCar(char itsId[], char itsBrand[], int *itsPic, int imagecount)
{
  Car newCar;
  strcpy(newCar.id, itsId);
  strcpy(newCar.brand, itsBrand);
  newCar.pic = itsPic;
  newCar.imagecount = imagecount;

  return newCar;
}

打印列表时,您将图像数量和汽车数量混合在一起:

void PrintList(Car aLista[], int imagecount, int carcount)
{
  for(int i = 0; i<imagecount; i++)
    printCar(&aLista[i],carcount);
}

这必须是:

void PrintList(Car aLista[], int carcount)
{
  for (int i = 0; i < carcount; i++)
    printCar(&aLista[i]);
}

将汽车添加到阵列中基本上可以,但是您可以检查是否达到MAX辆汽车。

void AddCar(Car aList[], int *pAt, Car theCar) 
{
  aList[(*pAt)++]=theCar;    
}

现在最大的问题。该函数在内存使用和怪异循环方面存在问题。

Car NewCar(void)
{
  Car newCar = {0};  // initialze with empty strings and NULL pointers.

  // TODO: Replace gets with fgets!
  // gets(newCar.id); // WHY read before you prompt??
  printf("type in  ID \n"); 
  gets(newCar.id); 
  printf("type in brand\n");
  gets(newCar.brand);

  bool imageInputDone = false; 
  int imageCount=0;

  while(imageInputDone == false)
  {
    printf("type in image reference \n");
    int newImage; 
    scanf("%d",&newImage);

    imageCount++;
    int *newpics = realloc(newCar.pic, (imageCount) * sizeof(int));
    newpics[imageCount-1] = newImage; 
    newCar.pic = newpics;
    // TODO: Check for NULL

    printf("vill du \n1.Add another image reference \n2.exit\n");
    int input;
    scanf("%d", &input);
    printf("input: %i\n",input);
    while (input < 1 || input > 2)
    switch(input)
    {
        case 1: 
            printf("Adding one more image\n");
            break;
        case 2: 
            printf("Leaving loop\n");
            imageInputDone = true; 
            break;
        default:
            printf("Input correct number\n");
            break;
    }

    newCar.imagecount = imageCount;
    return newCar; 
  }
}

最后...

int main(void)
{
  int carCount=0;
  Car myCar[MAX]; 
  int input;

  int test[3]  = {1,2,3};
  int test2[3] = {4,5,6};

  myCar[0] = initCar("bbb188", "BMW", test, 3);
  myCar[1] = initCar("AAA-999", "VOLVO", test2, 3);
  myCar[2] = initCar("CCC-999", "CITROEN", test, 3);

  carCount=3;

  do
  {
    printf("1. Show cars \n2. Add car \n");
    scanf("%d", &input);

    switch(input)
    {
      case 1:
        printf("ID          BRAND         PICS \n");
        PrintList(myCar, carCount);
        break; 
      case 2: 
        AddCar(myCar, &carCount, NewCar());
        printf("ID          BRAND         PICS \n");
        PrintList(myCar, carCount);
    }    //break; 
  } while (input < 1 || input > 2); // compare as integers, not characters. Don't use < operator

  return 0;
}

该代码未经测试。剩下的错误留给运动。 ;)