2016-05-22 12 views
-3

"8.dat"ファイルからすべてのレコードをカウントします。個々のレコーディングを読み込むには、ダイナミックメモリキャプチャを実行します。選択ソートの仕方は?

異なるキーにレコードを並べ替え:

・品目数(昇順);

・コスト(降順)。

・在庫数(降順)。

使用選択ソート

合計ソートは、配列が元の状態にソートされるたびに12回行われます。

比較および置換のそれぞれのケース数について。

このコードは、挿入の並べ替えに使用されます。私は選択ソートを使用する必要があります。選択ソートの仕方は?

お願いします。

#include <iostream> 
#include <fstream> 
#include <conio.h> 

using namespace std; 

struct PRICE 
{ 
    int number; 
    char name[20]; 
    int cost; 
    int quantity; 
} *pm; 

int Menu(); 
void PrintPRICE(PRICE); 
void sort_cost(PRICE*, int); 
void sort_quantity(PRICE*, int); 

long file_size(const char*); 

int main() 
{ 
    int count = 0; 
    const char *fname = "D:\8.dat"; 

    FILE* file = fopen(fname, "r"); 
    if (file != NULL) 
    { 
     long size = file_size(fname); 
     count = size/sizeof PRICE; 
     pm = new PRICE[count]; 
     fread(pm, sizeof PRICE, count, file); 
     fclose(file); 
    } 

    for (int i=0; i<count; i++) 
    { 
     PrintPRICE(pm[i]); 
     cout << endl; 
    } 

    int ch = Menu(); 

    switch (ch) 
    { 
    case 1: 
     { 
      sort_cost(pm, count); 
      cout << endl; 
      cout << " Result: " << endl; 
      cout << "-----------------------" << endl; 
      for (int i=0; i<count; i++) 
      { 
       PrintPRICE(pm[i]); 
       cout << endl; 
      } 
      break; 
     } 
    case 2: 
     { 
      sort_quantity(pm, count); 
      cout << " Result: " << endl; 
      cout << "-----------------------" << endl; 
      for (int i=0; i<count; i++) 
      { 
       PrintPRICE(pm[i]); 
       cout << endl; 
      } 
      break; 
     } 
    default: break; 
    } 

    delete [] pm; 
    _getch(); 
} 

void PrintPRICE(PRICE price) 
{ 
    cout << " Product: " << price.name << endl; 
    cout << " Number of orden: " << price.number << endl; 
    cout << " Product cost: " << price.cost << endl; 
    cout << " Quantity in stock: " << price.quantity << endl; 
    cout << "-----------------------------------n" << endl; 
} 

long file_size(const char* filename) 
{ 
    FILE *Pfile = NULL; 
    Pfile = fopen(filename, "rb"); 
    fseek(Pfile, 0, SEEK_END); 
    long size = ftell(Pfile); 
    fclose(Pfile); 
    return size; 
} 

void sort_cost(PRICE* array, int count) 
{ 
    int change = 0; 
    int comparesion = 0; 
    for (int i=1; i<count; i++) 
    { 
     PRICE key = array[i]; 
     int j = i - 1; 
     comparesion++; 

     while (i>=0 && array[i].cost>key.cost) 
     { 
      array[j + 1] = array[j]; 
      j = j - 1; 
      change++; 
     } 
     array[j + 1] = key; 
    } 
    cout << "n Quantity change: " << change << endl; 
    cout << " Quantity comparesion: " << comparesion << endl; 
} 

void sort_quantity(PRICE* array, int count) 
{ 
    int change = 0; 
    int comparesion = 0; 
    for (int i=1; i<count; i++) 
    { 
     PRICE key = array[i]; 
     int j = i - 1; 
     comparesion++; 
     while (j>=0 && array[i].quantity>key.quantity) 
     { 
      array[j + 1] = array[j]; 
      j = j - 1; 
      change++; 
     } 
     array[j + 1] = array[j]; 
    } 
    cout << "n Quantity change: " << change << endl; 
    cout << " Quantity comparesion: " << comparesion << endl; 
} 

int Menu() 
{ 
    int n; 
    cout << " 1 - Sort by cost" << endl; 
    cout << " 2 - Sort by quantity" << endl; 
    cout << "n Your choice: "; cin >> n; 
    return n; 
} 
+0

'このコードが使用される挿入ソートのソースコード。私は選択ソートを使用する必要があります。どうすればいいですか? - >挿入ソートの代わりに選択ソートを使用します。 –

答えて

0

選択ソート
空隙selectSort(INT ARR []、int型N) {

int pos_min,temp; 

for (int i=0; i < n-1; i++) 
{ 
    pos_min = i;   
    for (int j=i+1; j < n; j++) 
    { 

    if (arr[j] < arr[pos_min]) 
       pos_min=j; 
    } 


     if (pos_min != i) 
     { 
      temp = arr[i]; 
      arr[i] = arr[pos_min]; 
      arr[pos_min] = temp; 
     } 
} 
} 
+0

構造体のレコードをソートする必要があります。 – n096

関連する問題