gpt4 book ai didi

c - 基于C的抽象数据类型列表数组中的memcpy

转载 作者:行者123 更新时间:2023-11-30 21:00:57 25 4
gpt4 key购买 nike

我目前正在努力理解结构数组的工作原理。作为引用,这里是完整的代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 14
/******************************************************************
* Data Structure Definition *
******************************************************************/
typedef struct {
unsigned int prodID; /* product ID, uniquely identifies an element */
char prodDesc[50]; /* product description*/
float prodPrice; /* product price*/
int prodQty; /* product count or quantity */
}product, products[SIZE]; /* product record */

typedef struct cell {
products item; /* array of products */
int prodCtr; /* number of products in the list */
}*chocoList; /* Definition of the ADT List ver 2 */

typedef enum {
TRUE, FALSE
}boolean;

/******************************************************************
* Function Prototypes *
******************************************************************/
void initializeList(chocoList *L);
void populateSortedList(chocoList L);
void displayList(chocoList L, char * listName);
void displayProduct(product X);
int insertSorted(chocoList L, product X);
product deleteFirstOccurence(chocoList L, float prodPrice);

int main(void)
{
chocoList L; /* sorted List */
product deleted; /* container for the deleted record */

/*---------------------------------------------------------------------------------
* Problem #1 :: Initialize the choco list. Display the products in the list after*
* calling populateSortedList(). *
* printf("\n\n\nProblem #1:: "); *
*--------------------------------------------------------------------------------*/
printf("\n\n\nProblem #1:: ");
initializeList(&L);
populateSortedList(L);
displayList(L, "Problem 1");

/*---------------------------------------------------------------------------------
* Problem #2 :: Delete 1 product from the list by calling deleteFirstOccurence(). *
* Display the returned product record by calling displayProduct(). *
* printf("\n\n\nProblem #2:: "); *
*--------------------------------------------------------------------------------*/
printf("\n\n\nProblem #2:: ");
deleted = deleteFirstOccurence(L, 75.50);
displayProduct(deleted);

/*---------------------------------------------------------------------------------
* CHALLENGE :: Redo Problem #s 1 & 2 using either versions 3 or 4 *
* Keep in mind the changes when converting the ADT List Versions *
*--------------------------------------------------------------------------------*/

getch();
return 0;
}

/****************************************************************************************
* This function initializes the list for its first use. *
****************************************************************************************/
void initializeList(chocoList *L)
{
*L = (chocoList)malloc(sizeof(struct cell));
(*L)->prodCtr = 0;
}

/****************************************************************************************
* This function populates the list by calling insertSorted(). *
****************************************************************************************/
void populateSortedList(chocoList L)
{
int x, result = 1;

product data[] = { {1501, "Hersheys", 100.50, 10},
{1502, "Hersheys Almond", 100.50, 15},
{1503, "Hersheys Krackel", 100.50, 15},
{1701, "Toblerone", 150.75, 20},
{1702, "Toblerone Milk", 150.75, 40},
{1703, "Toblerone Honey", 150.75, 10},
{1550, "Cadbury", 200.00, 30},
{1201, "Kitkat", 97.75, 40},
{1450, "Ferrero", 150.50, 50},
{1601, "Meiji", 75.50, 60},
{1301, "Nestle", 124.50, 70},
{1525, "Lindt", 175.50, 80},
{1545, "Valor", 100.50, 90},
{1455, "Tango", 49.50, 100}
};
for(x = 0; x < 10 && result == 1; x++){
result = insertSorted(L, data[x]);
}
}

/****************************************************************************************
* This function display the details of all products in the list. *
****************************************************************************************/
void displayList(chocoList L, char * listName)
{
int x;
system("CLS"); /* clears the screen before displaying succeeding lines */

if(L->prodCtr != 0){
printf("\nElements of the Product List %s:", listName);
printf("\n\n%-10s%-15s%10s%10s","ID","Description","Price","Quantity");
printf("\n%-10s%-15s%10s%10s","--","-----------","-----","--------");

for(x = 0 ; x < L->prodCtr; x++){
printf("\n%-10d", L->item[x].prodID);
printf("%-15s", L->item[x].prodDesc);
printf("%10.2f", L->item[x].prodPrice);
printf("%10d", L->item[x].prodQty);
}
}else{
printf("\n\tChoco list is currently empty!\n");
}

printf("\n\n Press any key to continue . . . ");
getch();
}

/****************************************************************************************
* This function inserts a product X in an alphabetically sorted list according to its *
* product description. It returns a value of 1 for successful insertion. Otherwise, 0. *
* Use memcpy() in shifting the elements downward to provide space for the new product. *
***************************************************************************************/
int insertSorted(chocoList L, product X)
{
int ctr;
if(L->prodCtr<14){
for(ctr=0; ctr<L->prodCtr && strcmp(L->item[ctr].prodDesc, X.prodDesc)<0;ctr++){ }
memcpy(L->item + ctr + 1, L->item + ctr, sizeof(product) * (L->prodCtr - ctr));
L->item[ctr] = X;
L->prodCtr++;
ctr = 1;
}
else{
ctr = 0;
}
return ctr;
}

/****************************************************************************************
* This function deletes the first occurence of a product given the prodPrice.It returns*
* the product record ones it is found. Otherwise, it returns a dummy record containing *
* "XXX" for string values and 0 for integer and float values. Use memcpy() in shifting *
* the elements upward to avoid having empty indices within the array. *
***************************************************************************************/
product deleteFirstOccurence(chocoList L, float prodPrice)
{
int ctr;
product dummy;
strcpy(dummy.prodDesc , "XXX");
dummy.prodID = 0;
dummy.prodPrice = 0;
dummy.prodQty = 0;
for(ctr = 0; ctr<L->prodCtr && L->item[ctr].prodPrice != prodPrice; ctr++){ }
if(ctr!=L->prodCtr){
dummy = L->item[ctr];
memcpy(L->item + ctr, L->item + ctr + 1, sizeof(product) * (L->prodCtr - ctr));
L->prodCtr--;
}
return dummy;
}

/****************************************************************************************
* This function display the details of 1 product. *
***************************************************************************************/
void displayProduct(product X)
{
//system("CLS"); /* clears the screen before displaying succeeding lines */
printf("\n\nElements of Product %d:", X.prodID);
printf("\n\n%-10s%-15s%10s%10s","ID","Description","Price","Quantity");
printf("\n%-10s%-15s%10s%10s","--","-----------","-----","--------");

printf("\n%-10d", X.prodID);
printf("%-15s", X.prodDesc);
printf("%10.2f", X.prodPrice);
printf("%10d", X.prodQty);

printf("\n\n Press any key to continue . . . ");
getch();
}

我对这一部分特别有几个问题:

int insertSorted(chocoList L, product X)
{
int ctr;
if(L->prodCtr<14){
for(ctr=0; ctr<L->prodCtr && strcmp(L->item[ctr].prodDesc, X.prodDesc)<0;ctr++){ }
memcpy(L->item + ctr + 1, L->item + ctr, sizeof(product) * (L->prodCtr - ctr));
L->item[ctr] = X;
L->prodCtr++;
ctr = 1;
}
else{
ctr = 0;
}
return ctr;
}

首先,有人可以向我解释一下这句话吗?

memcpy(L->item + ctr + 1, L->item + ctr, sizeof(product) * (L->prodCtr - ctr));

第二,使用memcpy是不是效率更高?或memmove在这种情况下?

最佳答案

memcpy 正在尝试移动数组的一部分,以便为新项目腾出空间。从视觉上看,它正在这样做:

enter image description here

哪里

  • 绿色是数组中不需要移动的项
  • 蓝色是需要移动以为新项目腾出空间的项目
  • 白色是数组中未使用的项目
  • 黄色是新项目X

为了这个示例,我选择了以下值:

  • L->prodCtr 最初为 6
  • for 循环将 ctr 设置为 3

这就是 memcpy 的工作原理。第一个参数 (L->item + ctr + 1) 是目标地址,即之后第一个蓝色项目的地址移动。第二个参数L->item + ctr是源地址,即移动之前第一个蓝色项目的地址。第三个论点由两部分组成。 sizeof(product) 是每个项目的大小(以字节为单位)。 (L->prodCtr - ctr) 是需要移动的项目数。将它们相乘即可得到要移动的字节数。

<小时/>

这就是 memcpy 应该做的事情。但是,要回答你的第二个问题,你不能使用memcpy来做到这一点。原因是 memcpy 仅在源和目标不重叠时才起作用。另一方面,即使源和目标重叠,memmove 也能保证正常工作。所以这里没有任何选择,你必须使用memmove

关于c - 基于C的抽象数据类型列表数组中的memcpy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38025968/

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