-2

我对编程很陌生,而且我遇到了指针问题。我下面的代码可以例外,因为当我对它进行排序时,我的计数不跟随我的文章编号。我可能需要指针才能使其正常工作,但我不知道如何。

谁能帮我?

void printMenu(void)
{

  printf("\nMENU:\n");
  printf("(D)isplay the menu\n");
  printf("(G)enerate inventory\n");
  printf("(P)rint inventory\n");
  printf("(L)inear search article\n");
  printf("(B)inary search article\n");
  printf("(I)nsertion sort inventory\n");
  printf("B(u)bble sort inventory\n");
  printf("(M)erge sort inventory\n");
  printf("(Q)uit program\n");
}

void generateInventory(article inventory[], int noOfArticles,
    int minArticleNumber, int maxArticleNumber, int maxNoOfArticles)
{
  int i, j;
  int idCount[] =
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };

  for (i = 0; i < noOfArticles; i++)
  {
    inventory[i].id = rand() % (maxArticleNumber - minArticleNumber + 1) +

    minArticleNumber;
    idCount[inventory[i].id - 1] = idCount[inventory[i].id - 1] + 1;

    for (j = 0; j <= i; ++j)
    {
      if (idCount[inventory[i].id - 1] > 1)
      {
        inventory[i].id = rand() % (maxArticleNumber + minArticleNumber);
      }
    }
    inventory[i].counts = rand() % maxNoOfArticles;
  }
}

void printInventory(const article inventory[], int noOfArticles)
{

  int i;

  printf("\nINVENTORY\n");
  printf("%7s %8s\n", "Article", "Count");
  for (i = 0; i < noOfArticles; i++)
  {
    printf("%7d %8d\n", inventory[i].id, inventory[i].counts);
  }
}

int getArticleId()
{

  int id;

  printf("\nGive article id: ");
  scanf("%d", &id);

  return id;
}

void printSearchResult(const article inventory[], int index)
{

  if (index == -1)
  {
    printf("\nArticle not found\n");
  }
  else
  {
    printf("\nArticle id: %d\n", inventory[index].id);
    printf("Article counts: %d\n", inventory[index].counts);
  }
}

int linearSearchInventory(const article inventory[], int noOfArticles, int id)
{
  int i = 0;
  int index = -1;

  while (index == -1 && i < noOfArticles)
  {
    if (id == inventory[i].id)
    {
      index = i;
    }

    i++;
  }
}

int binarySearchInventory(const article inventory[], int noOfArticles, int id)
{

  int index = -1;
  int left = 0;
  int right = noOfArticles - 1;
  int middle;

  while (index == -1 && left <= right)
  {
    middle = (left + right) / 2;
    if (id == inventory[middle].id)
    {
      index = middle;
    }
    else if (id < inventory[middle].id)
    {
      right = middle - 1;
    }
    else
    {
      left = middle + 1;
    }
  }

  return index;
}

void insertionSortInventory(article inventory[], int noOfArticles)
{

  int i, j;
  int next;

  for (i = 1; i < noOfArticles; i++)
  {
    next = inventory[i].id;

    j = i - 1;
    while (j >= 0 && next < inventory[j].id)
    {
      inventory[j + 1].id = inventory[j].id;
      j = j - 1;
    }

    inventory[j + 1].id = next;
  }
}

void bubbleSortInventory(article inventory[], int noOfArticles)
{

  int c, d, t;

  for (c = 0; c < (noOfArticles - 1); c++)
  {

    for (d = 0; d < noOfArticles - c - 1; d++)
    {

      if (inventory[d].id > inventory[d + 1].id)
      {

        t = inventory[d].id;
        inventory[d].id = inventory[d + 1].id;
        inventory[d + 1].id = t;
      }
    }
  }
}

void mergeSortInventory(article inventory[], int noOfArticles)
{

  int temp[noOfArticles / 2];
  int nLeft, nRight;
  int i, iLeft, iRight;

  if (noOfArticles > 1)
  {

    nLeft = noOfArticles / 2;
    nRight = (int) ceil((double) noOfArticles / 2);

    mergeSortInventory(inventory, nLeft);
    mergeSortInventory(&inventory[noOfArticles / 2], nRight);

    for (i = 0; i < nLeft; i++)
    {
      temp[i] = inventory[i].id;
    }

    i = 0;
    iLeft = 0;
    iRight = 0;
    while (iLeft < nLeft && iRight < nRight)
    {
      if (temp[iLeft] < inventory[noOfArticles / 2 + iRight].id)
      {
        inventory[i].id = temp[iLeft];
        iLeft = iLeft + 1;
      }
      else
      {
        inventory[i].id = inventory[noOfArticles / 2 + iRight].id;
        iRight = iRight + 1;
      }
      i = i + 1;
    }

    while (iLeft < nLeft)
    {
      inventory[i].id = temp[iLeft];
      i = i + 1;
      iLeft = iLeft + 1;
    }

  }
}
4

2 回答 2

0

如果我的问题是正确的,那么您希望 idCount 数组与库存数组保持相关。我假设,由于您使用 article 作为类型,因此您要么将变量定义为文章,这将毫无意义,或者您更可能构建了 article 类型的结构,然后创建了一个数组这些结构,并称为数组库存。

如果是这种情况,那么保持它们相关的最可能的方法就是在文章结构中包含计数。

有一些方法可以在不这样做的情况下使数组具有关系,但它们毫无意义,因为一个简单的四行结构就可以解决问题,即使该结构是不同结构的包装器,或者是另一个结构的标头。

于 2013-10-23T15:20:00.943 回答
0

对记录进行排序时,您只需分配结构的 id 成员:

inventory[foo].id = inventory[bar].id;

您应该分配完整的结构:

inventory[foo] = inventory[bar];

请记住,临时对象必须是非类型articleint因此它们也可以被分配一个完整的结构,而不仅仅是一个 id 值

于 2013-10-23T15:37:21.837 回答