2015-04-07 38 views
0

氣泡排序應該基於工資數組。在工資按升序排序後,員工編號,工資率和工時應該改變。我能夠對付費率進行排序,但我的員工ID信息保持不變。我需要另外做while循環嗎?C++:如何使用氣泡排序來重新排列我的數據

#include "stdafx.h" 
#include <iostream> 
#include <iomanip> 
using namespace std; 

// Constant for the array size. 
const int ARRAY_SIZE = 4; 

// Function Prototypes 
void getEmployeeInfo(long [], int [], double [], double [], int); 

void bubbleSort(long empId[],int hours[],double payRate[],double wages[],int size); 

void displayWages(long empId[], double wages[], int size); 

int main() 
{ 
    // Array of employee ID numbers 
    long empId[ARRAY_SIZE] = { 5658845, 4520125, 7895122, 
           8777541}; 

    // Array to hold the hours worked for each employee 
    int hours[ARRAY_SIZE] = {0}; 

    // Array to hold the hourly pay rate for each employee 
    double payRate[ARRAY_SIZE] = {0}; 

    // Array to hold the gross wages for each employee 
    double wages[ARRAY_SIZE] = {0}; 

    // Get the employee payroll information and store 
    // it in the arrays. 
    getEmployeeInfo(empId, hours, payRate, wages, ARRAY_SIZE); 

    // Display the payroll information. 
    displayWages(empId, wages, ARRAY_SIZE); 

    // Sort the payroll information in ascending order with a bubble sort. 
    bubbleSort(empId, hours, payRate, wages, ARRAY_SIZE); 

    // Display the payroll information again. 
    displayWages (empId, wages, ARRAY_SIZE); 

    system("PAUSE"); 

    return 0; 
} 

// ******************************************************** 
// The getEmployeeInfo function receives four parallel * 
// arrays as arguments. The 1st array contains employee * 
// IDs to be displayed in prompts. It asks for input and * 
// stores hours worked and pay rate information in the * 
// 2nd and 3rd arrays. This information is used to  * 
// calculate gross pay, which it stores in the 4th array. * 
// ******************************************************** 
void getEmployeeInfo(long emp[], int hrs[], double rate[], 
        double pay[], int size) 
{ 
    cout << "Enter the requested information " 
     << "for each employee.\n"; 

    // Get the information for each employee. 
    for (int count = 0; count < size; count++) 
    { 
     cout << "\nEmployee #: " << emp[count] << "\t"; 

     // Get this employee's hours worked. 
     cout << "Hours worked: "; 
     cin >> hrs[count]; 

     // Validate hours worked. 
     while (hrs < 0) 
     { 
      cout << "\nHours worked must be 0 or more. " 
       << "Please re-enter: "; 
      cin >> hrs[count]; 
     } 

     // Get this employee's pay rate. 
     cout << "\tPay rate: $"; 
     cin >> rate[count]; 

     // Validate the pay rate. 
     while (rate[count] < 6.00) 
     { 
      cout << "\nPay rate must be 6.00 or more. " 
       << "Please re-enter: $"; 
      cin >> rate[count]; 
     } 

     // Calculate this employee's gross pay. 
     pay[count] = hrs[count]*rate[count]; 

     // ADD statement to calculate wages by multiplying 
       // hours with rate of pay; 
    } 
} 

// ******************************************************** 
// The bubbleSort function sorts the information based on * 
// the wages array.          * 
// ******************************************************** 
void bubbleSort(long empId[],int hours[],double payRate[],double wages[],int size) 
{ 
    bool swap; 
    int index; 

    do 
    { 
     swap = false; 
     for (int count = 0; count < (size - 1); count++) 
     { 
      if (wages[count] > wages [count + 1]) 
      { 
       index = wages[count]; 
       wages[count] = wages[count + 1]; 
       wages[count + 1] = index; 
       swap = true; 
      } 
     } 

    } while (swap); 
} 

// ******************************************************** 
// The displayWages function displays employee ID numbers * 
// and their wages.          * 
// ******************************************************** 
void displayWages(long empId[], double wages[], int size) 
{ 
    // Set up the numeric output formatting. 
    cout << fixed << showpoint << setprecision(2) << endl; 

    // Display the header. 
    cout << "----------------------------\n"; 
    cout << "Employee    Wages\n"; 
    cout << "----------------------------\n\n"; 

    // Display each employee's pay. 
    for (int count = 0; count < ARRAY_SIZE; count++) 
    { 
     cout << "Employee #" << empId[count] << " $"; 
     cout << setw(7) << wages[count] << endl << endl; 
    } 

} 
+0

裏面,你換了工資陣列的兩個元素的冒泡功能,您還需要交換EMPID,時間的相應元素和payRate陣列 – samgak

+0

那麼這是否意味着我應該再添加2個if語句,但它是否符合empId和hours? – Duck

+0

不,因爲你是根據工資排序的,所以if語句只應該比較工資。我會添加一個答案來顯示我的意思 – samgak

回答

0

裏面,你換了工資陣列的兩個元素的冒泡功能,您還需要交換EMPID,時間和payRate陣列的相應元素。此外,您需要爲臨時交換變量使用與交換值相同的類型,否則轉換爲int時會失去準確性。

void bubbleSort(long empId[],int hours[],double payRate[],double wages[],int size) 
{ 
    bool swap; 
    double temp1; 
    int temp2; 
    long temp3; 

    do 
    { 
     swap = false; 
     for (int count = 0; count < (size - 1); count++) 
     { 
      // compare the wages, because that is what you are sorting on 
      if (wages[count] > wages [count + 1]) 
      { 
       // swap all the data between the two array positions: 
       temp1 = wages[count]; 
       wages[count] = wages[count + 1]; 
       wages[count + 1] = temp1; 

       temp1 = payRate[count]; 
       payRate[count] = payRate[count + 1]; 
       payRate[count + 1] = temp1; 

       temp2 = hours[count]; 
       hours[count] = hours[count + 1]; 
       hours[count + 1] = temp2; 

       temp3 = empId[count]; 
       empId[count] = empId[count + 1]; 
       empId[count + 1] = temp3; 

       swap = true; 
      } 
     } 

    } while (swap); 
} 
+0

好吧。謝謝。這很簡單。我沒有想到只是在循環中添加語句,並且認爲要爲循環添加更多內容。 – Duck

+0

不客氣。請記住,將員工ID與特定工資關聯的唯一方法是,他們在兩個數組中處於相同位置。所以如果你改變了一個而不是另一個,他們將會失去同步並且不再匹配。由於這個原因,把所有的信息(id,wage等)放到一個結構或類中,並且有一個結構數組而不是4個獨立的數組可能會更好。 – samgak

0

創建一個輔助函數來交換

void swaphelper(long (&emp)[] , doubles(&wages)[] ,doubles(&payrate)[] , int(&hours)[], int fromIndex , int toIndex) 
{ 

    emp[fromIndex] = emp[fromIndex]^emp[toIndex]; 
    emp[toIndex] = emp[toIndex]^emp[fromIndex]; 

    wages[fromIndex] = wages[fromIndex]^wages[toIndex]; 
    wages[toIndex] = wages[toIndex]^wages[fromIndex]; 

    payrate[fromIndex] = payrate[fromIndex]^payrate[toIndex]; 
    payrate[toIndex] = payrate[toIndex]^payrate[fromIndex]; 

    hours[fromIndex] = hours[fromIndex]^hours[toIndex]; 
    hours[toIndex] = hours[toIndex]^hours[fromIndex]; 

} 


void bubbleSort(long empId[],int hours[],double payRate[],double wages[],int size) 
{ 
bool swap; 
int index; 

do 
{ 
    swap = false; 
    for (int count = 0; count < (size - 1); count++) 
    { 
     if (wages[count] > wages [count + 1]) 
     { 
      swaphelper(emp , wages , pirate , hours , count , count+1); // can be abstracted to a struct 
      swap = true; 
     } 
    } 

} while (swap); 
}