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

时间:2016-10-15 17:50:17

标签: c function search struct

嗨,这段代码只是我工作的一个例子。我通过从搜索功能中复制所有内容并将其转储到我的编辑功能中来获得解决方案。有没有更好的解决方案,而不仅仅是复制粘贴?

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

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

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?


    }

1 个答案:

答案 0 :(得分:0)

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

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 ... 
    }
}