gpt4 book ai didi

c - 搜索和编辑功能没有复制粘贴?

转载 作者:行者123 更新时间:2023-11-30 16:56:06 25 4
gpt4 key购买 nike

您好,这段代码只是我正在处理的示例。我通过复制搜索功能中的所有内容并将其转储到编辑功能中得到了解决方案。除了复制粘贴之外,还有更好的解决方案吗?

 struct inventory
{
float a,b,c,d;
char something[MAXSIZE];
};
typedef struct inventory Inventory;

结构将包含一些浮点整数和一些字符。

void search(const Inventory inv[], int np); // declare search function
void edit(struct inventory inventoryRegister[],int np);

int main(void)
{

int np=0;
struct inventory inventoryRegister[MAXSIZE];


// calling the functions search and edit

search(inventoryRegister, np);
edit(inventoryRegister, np);

return 0;
}

void search(const Inventory inv[], int np)
{
int i,
float min, max;

printf("Enter min max");
scanf("%f %f", &min, &max);

for (i = 0; i < np; i++)
if (inv[i].a >= low && inv[i].a <= high)
{
print..
}
//repeat for b,c,d and something
}

void edit(struct inventory inventoryRegister[],int np)
{
int a;
print("Enter new a");
scanf("%f", &a);

// Here i can copy and paste my entire search function and do a loop to replace the min & max with my new input a.
But is there any easier way to do it? say i call the search(); and somehow extract the elements between min & max and do a loop replacement with a?
Any suggestion?


}

最佳答案

免责声明:这是一种伪代码

struct MinMaxLocation
{
int minloc;
int maxloc;
};

struct MinMaxLocation getMinMaxLocation(struct inventory inventoryRegister[],float min,float max)
{
// your logic to find min and max locations here

struct MinMaxLocation loc;
loc.minloc = // min element location
loc.maxloc = // max element location
return loc;
}

void search(....)
{
struct MinMaxLocation loc_here_search = getMinMaxLocation(inventoryRegister,min,max);
for (i = loc_search_here.min; i < loc_search_here.max; i++)
{
print ...
}
}

void edit(....)
{
struct MinMaxLocation loc_here_search = getMinMaxLocation(inventoryRegister,min,max);
for (i = loc_search_here.min; i < loc_search_here.max; i++)
{
edit the values here ...
}
}

关于c - 搜索和编辑功能没有复制粘贴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40062332/

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