2016-10-15 8 views
0

こんにちは、このコードは、私が取り組んでいるものの単なる例です。私は私の検索機能からすべてをコピーし、それを私の編集機能にダンプすることによって解決策を得ました。貼り付けをコピーするだけでなく、より良い解決策がありますか?検索と編集機能はコピーを貼り付けませんか?

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

structには、float整数といくつかの文字が含まれます。

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? 


    } 
+0

'min'と' '構造体LOC {INT miloc、INT MAXLOC}言うmax'の有効な位置を記憶構造体を有し、' minとmaxの位置を取得するには、この 'struct loc'を返す関数を作成し、この関数を呼び出して、配列の範囲を反復することによって、位置と検索/印刷/編集値を取得します。センス? – Recker

+0

@Recker私はそれを手に入れましたが、100%は簡単なサンプルコードを作ることができませんか? – xxFlashxx

答えて

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 ... 
    } 
} 
関連する問題