【问题标题】:Multiple Function Need Same Parameter (how to optimize),多个函数需要相同的参数(如何优化),
【发布时间】:2017-10-26 22:09:55
【问题描述】:

您好,这个问题可能有点愚蠢(在找到一个完美的标题之前,问题标题可能不会改变),我花了几个小时才弄清楚这一点。

我会尽量详细解释。 首先,我将首先给出我的代码。

文件'main.cpp'

using namespace std;

void readFile(Vector<WindLog> &VecObj);
void showMenu(Vector<WindLog> &VecObj);
void option1(Vector<WindLog> &VecObj);
void option2(Vector<WindLog> &VecObj);
void option3(Vector<WindLog> &VecObj);
void option4(Vector<WindLog> &VecObj);
void option5();
int getUserChoice();
int getMonthInput();
int getYearInput();

int main()
{

    Vector<WindLog> Obj;
    readFile(Obj);
    showMenu(Obj);

    return 0;
}

void readFile(Vector<WindLog> &VecObj){

    Date date;
    Time time;
    float s, sr;
    WindLog wLog;
    string token, filename, line;

    ifstream myFile("testfile.csv");
    getline(myFile, line); //to skip header information ;

    if(myFile.is_open()){
        while(getline(myFile, line))
        {
            stringstream ss(line); // <-- pas line to stream

            getline(ss, token,'/'); // <--Token for day;
            date.setDay(atoi(token.c_str()));

            getline(ss, token,'/'); // <--Token for month;
            date.setMonth(atoi(token.c_str()));

            getline(ss, token,' '); // <--Token for year;
            date.setYear(atoi(token.c_str()));

            getline(ss, token,':'); // <-- Token for Hour;
            time.setHour(atoi(token.c_str()));

            getline(ss, token,','); // <-- Token for Minute;
            time.setMinute(atoi(token.c_str()));

            for(int i = 0; i<9; i++)
                getline(ss, token, ',');

            getline(ss, token,','); // <-- Token for WindSpeed
            s = atof(token.c_str());

            getline(ss,token,','); // <-- Token for Solar Radiation
            sr = atof(token.c_str());

            wLog = WindLog(date, time, s, sr);
            VecObj.add(wLog);
        }
    }
    else{cout<<"No File Opened"<<endl;}



}
void showMenu(Vector<WindLog> &VecObj){
    int choice;

    while(true){
        cout << "\t\tOptions Menu" << endl;
        cout << "<------------------------------------------->" <<endl;
        cout << "1) Show Maximum Wind Speed for Specified Month and Year." << endl;
        cout << "2) Show Average Wind Speed of Specified Year." << endl;
        cout << "3) Show Total Radiation in kWh/m^2 for each Month of Specified Year." << endl;
        cout << "4) Print Average Wind Speed and Total Solar Radiation for each month of Specified Year, to File('WindandSolar.csv')." << endl;
        cout << "5) Exit Program." << endl;

        choice = getUserChoice();

        switch(choice)
        {
            case 1:option1(VecObj); break;
            case 2:option2(VecObj); break;
            case 3:option3(VecObj); break;
            case 4:option4(VecObj); break;
            case 5:option5(); break;
            default:
                cout<<"Invalid Choice, Try Again"<<endl;
        }
    }
}
int getUserChoice(){
    int temp;
    cout << "Enter Your Choice : ";
    cin >> temp;
    while(cin.fail()){
        cout << "Error, Try Again. " << endl;
        cout << "Enter Your Choice : ";
        cin.clear();cin.ignore(256,'\n');

        cin >> temp;
    }
    return temp;
}
int getMonthInput(){
    int tempMonth;
    bool verify = false;

    while(verify==false)
    {
        cout << "Enter Month : ";
        cin >> tempMonth;

        if(cin.fail())
        {
            cout<<"Input Error, Try Again."<<endl;
            cin.clear();cin.ignore(256,'\n');

        }
        else if(tempMonth < 1 || tempMonth > 12)
        {cout<<"Invalid Month, Try Again."<<endl;}
        else
        {verify = true;}
    }
    return tempMonth;
}
int getYearInput(){
    int tempYear;
    bool verify = false;

    while(verify==false)
    {
        cout << "Enter Year : ";
        cin >> tempYear;

        if(cin.fail())
        {
            cin.clear();cout<<"Input Error, Try Again."<<endl;

            cin.ignore(256,'\n');
        }
        else if(tempYear<=0)
        {cout<<"Invalid Year, Try Again."<<endl;}
        else
        {verify = true;}
    }
    return tempYear;
}


void option1(Vector<WindLog> &VecObj){}
void option2(Vector<WindLog> &VecObj){}
void option3(Vector<WindLog> &VecObj){}
void option4(Vector<WindLog> &VecObj){}
void option5(){exit(0);}

让我解释一下(如果有不清楚的地方,请发表评论,我会添加更具体的细节,只在 C++ 中使用大约 1 个月)。

我主要声明Vector&lt;windLog&gt; Obj 来存储我的windlog 文件。然后我需要将它传递给void readFile(Vector&lt;WindLog&gt; &amp;VecObj); 以获取一些读取文件并添加到我创建的向量类中。

之后程序将运行void showMenu(Vector&lt;WindLog&gt; &amp;VecObj); 并显示菜单选项和User 输入的选项,从 1 到 5;

此功能之一将根据用户选择执行。

void option1(Vector<WindLog> &VecObj); void option2(Vector<WindLog> &VecObj); void option3(Vector<WindLog> &VecObj); void option4(Vector<WindLog> &VecObj);

我的问题是option1-option4的参数,它们的参数相同,这让我很困扰,我不知道它是否可以。

如果我的任何问题不清楚,请给我一些评论,以便我解决它。如果你们想就我的代码给我一些建议,我将不胜感激。

【问题讨论】:

  • 你的标题说的是多个类,但问题是关于多个功能。你真正想问的是什么?
  • 看起来您需要一个处理所有这些操作的类,并以 Vector 作为成员。
  • @Barmar 对不起标题,我已经改了。我关于option1-4参数的问题。如果可以的话,它需要相同的 4 参数和 idk,或者我可以以任何方式对其进行更多优化。
  • 我觉得很好。
  • 我唯一不同的是重新排序文件以消除对前向声明的需要。

标签: c++ class c++11 data-structures


【解决方案1】:

如果所有选项总是采用相同的参数,您可以将它们放在一个向量中并使用选项对其进行索引。

typedef void (*option_fun_t)(Vector<WindLog> &VecObj);
option_fun_t option_fun[] = {option1, option2, option3, option4};

choice = getUserChoice();
option_fun[choice-1](VecObj);

【讨论】:

    【解决方案2】:

    在不同的函数中使用相同的参数类型是非常好的,只要函数具有不同的名称(它们确实如此)。如果实在困扰你,你可以编写 one 函数并传递一个附加参数(如int option),然后根据该选项进行处理,如下所示:

    void option(Vector<WindLog>& VecObj, int option)
    {
        switch(option)
        {
        case 1:
            {
                // Processing in case of option 1
                break;
            }
    
        case 2:
            {
                // Processing in case of option 2
                break;
            }
    
        case 3:
            {
                // Processing in case of option 3
                break;
            }
    
        case 4:
            {
                // Processing in case of option 4
                break;
            }
    
        default:
            break;
        }
    }
    

    【讨论】:

    • 感谢您的输入,我会期待这个重新评估我的代码。
    • 我显然更喜欢多个(较小的)功能而不是一个大开关盒。请注意,OP 已经有一个 switch case。
    • @Jarod42 我通常同意有单独的函数,但是,我正在解决他对具有相同参数的多个函数的担忧。此外,如果在Vector&lt;WindLog&gt; 上针对所有选项执行一些冗长(不是双关语)的初步处理步骤,则可以将多个函数聚合为一个。在这种情况下,您可能会从 main() 调用处理函数,然后可能会进行简单的最终计算并使用 switch 语句打印结果。诚然,这些都是很多毫无根据的假设,但并非不切实际。
    猜你喜欢
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多