2016-05-01 84 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