函数构造,定义变量,求解答。

img

#include <iostream>
using namespace std;


class Trader
{
public:
    int id;
    float mathPrice;
    float engPrice;
    float chnPrice;
    Trader() {}
    Trader(int id,float m,float e,float c) {this->id = id;mathPrice = m;engPrice = e;chnPrice = c;}
    float getMinPrice()
    {
        if(mathPrice < engPrice && mathPrice < chnPrice)
            return mathPrice;
        if(engPrice < mathPrice && engPrice < chnPrice)
            return engPrice;
        return chnPrice;
    }
    void print() {cout<<id<<","<<chnPrice<<","<<mathPrice<<","<<engPrice<<endl;}

};

int getMinPrice(Trader *t,int n)
{
    int nIdx = 0;
    for(int i=1;i<5;i++)
    {
        switch(n)
        {
        case 0:
            if(t[i].chnPrice < t[nIdx].chnPrice)
                nIdx = i;
            break;
        case 1:
            if(t[i].mathPrice < t[nIdx].mathPrice)
                nIdx = i;
            break;
        case 2:
            if(t[i].engPrice < t[nIdx].engPrice)
                nIdx = i;
            break;
        }
    }
    return nIdx;
}
int main()
{
    Trader t[5];
    for(int i=0;i<5;i++)
        cin>>t[i].id>>t[i].mathPrice>>t[i].engPrice>>t[i].chnPrice;
    int k = getMinPrice(t,0);
    printf("语文价格最低的书商id:%d,价格:%.1f\n",t[k].id,t[k].chnPrice);
    k = getMinPrice(t,1);
    printf("数学价格最低的书商id:%d,价格:%.1f\n",t[k].id,t[k].chnPrice);
    k = getMinPrice(t,2);
    printf("英语价格最低的书商id:%d,价格:%.1f\n",t[k].id,t[k].chnPrice);
    return 0;
 
}