2016-05-01 89 views
0
 double gross_pay(double pay[7]){ 
int i; 
double add; 

add = 0; 
for(i = 0; i < 7; i++){ 
    add += pay[i]; 
} 

return add; 
} 

     int find_lowest(double pay[7]){ 
double lowest; 
int index, i; 

index = 0; 
lowest = pay[0]; 

for(i = 1; i < 7; i++){ 
    if(pay[i] < lowest){ 
     index = i; 
     lowest = pay[i]; 
    } 
} 

return index; 

} 

int find_highest(double pay[7]){ 
double highest; 
int index, i; 

index = 0; 
highest = pay[0]; 

for(i = 1; i < 7; i++){ 
    if(pay[i] > highest){ 
     index = i; 
     highest = pay[i]; 
    } 
} 

return index; 

} 



    int main() 
{ 

string dayweek[7]; 
double dailypay[7]; 
int i; 
double pay, add; 
int lowest, highest; 

dayweek[0] = "Monday"; 
dayweek[1] = "Tuesday"; 
dayweek[2] = "Wednesday"; 
dayweek[3] = "Thursday"; 
dayweek[4] = "Friday"; 
dayweek[5] = "Saturday"; 
dayweek[6] = "Sunday"; 


for(i = 0; i < 7; i++){ 
    cout << "Please enter the gross sales ecieved on" << endl; 
    cout << dayweek[i] << ": "; 
    cin >> pay; 
    dailypay[i] = pay; 
    cout << endl << endl; 
} 

add = gross_pay(dailypay); 
lowest = find_lowest(dailypay); 
highest = find_highest(dailypay); 


cout << endl; 
cout << "Total sales for the week: " << "$" << add << endl << endl; 
cout << "Total taxes withheld: $" << add*.0975 << "\n\n"; 
cout << "Net profit: $" << add*.9025 <<"\n\n"; 
cout << "Day with the highest sales amount was" << endl; 
cout << dayweek[highest].c_str() <<" with a sales total of $" << dailypay[highest]; 
cout << "\n\nDay with the lowest sales amount was" << endl; 
cout << dayweek[lowest].c_str() << " with a sales total of $" << dailypay[lowest]; 
    return 0; 
    } 

不能似乎弄清楚,將陣列爲了 銷售排序從低到高排序,從最低到最高例如代碼:C++排序陣列功能

週一的銷售總額爲$ 101.00

週二的銷售總額爲$ 135.64

週日的銷售總額爲$ 245.55

週三的銷售總額爲$ 533.44

週四的銷售總額爲$ 922.42

週六的銷售總額爲$ 1555.22

週五的銷售總額爲$ 2242.63

這些也爲銷售總額的輸入值,

這是我第一次在這個網站上問一個問題,如果我做錯了,請讓我知道,謝謝你的幫助!

你能寫一個使用此

 void sortArray(double arySales[], int size, string aryDays[]) 
    { 
    bool swap; 
    double tempSales; 
    string tempDays; 

    do 
    { 
    swap = false; 
    for (int count = 0; count < (size - 1); count++) 
    { 
    if (arySales[count] > arySales[count + 1]) 
    { 
     tempSales = arySales[count]; 
     arySales[count] = arySales[count + 1]; 
     arySales[count + 1] = tempSales; 

     tempDays = aryDays[count]; 
     aryDays[count] = aryDays[count + 1]; 
     aryDays[count + 1] = tempDays; 

     swap = true; 
    } 
    } 

}而(交換); }

回答

1

您應該將星期幾索引(0到6)加上當天的值加入結構中,然後填充這些結構的std::vector。然後你可以很容易地對它們進行排序:

struct PayForDay { 
    int day; // 0..6 
    double pay; 
}; 

bool PayLess(const PayForDay& lhs, const PayForDay& rhs) { 
    return lhs.pay < rhs.pay; 
} 

std::vector<PayForDay> payments; 

std::sort(payments.begin(), payments.end(), PayLess); 
+0

應該在初學者階段添加im,在我的C++書籍中,我學習了線性搜索的im,你可以在泡沫排序中編寫相同的代碼嗎?謝謝 – Alpha

+0

不,但我確定你可以實現你自己的冒泡排序,一旦你有結構的地方。矢量中的結構可以幫助您保持數據的有序組織,因此在排序哪個付費金額與哪一天相關時不會丟失跟蹤。 –

+0

我在書中添加了一個排序函數,但我仍然沒有開始。謝謝 – Alpha