【问题标题】:c++ sorting array functionc++排序数组函数
【发布时间】:2016-05-01 03:56:47
【问题描述】:
      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;
    }

似乎无法弄清楚将数组按从低到高的顺序排序的代码,例如 销售额从低到高排序:

周一的总销售额为 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;
     }
  }

} 同时(交换); }

【问题讨论】:

    标签: c++ arrays sorting


    【解决方案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);
    

    【讨论】:

    • 应该在我的 c++ 书中添加 im 仍处于初学者阶段,我正在学习线性搜索中的 im,你能在冒泡排序中编写相同的代码吗?谢谢
    • 不,但我相信一旦你有了结构,你就可以实现自己的冒泡排序。向量中的结构可帮助您保持数据井井有条,这样您就不会在对哪一天支付的金额进行排序时忘记跟踪。
    • 我在我的书中添加了一个排序功能,但我仍然没有得到它。谢谢
    • 按从低到高的顺序显示销售额:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    相关资源
    最近更新 更多