簡體   English   中英

多功能需要相同的參數(如何優化),

[英]Multiple Function Need Same Parameter (how to optimize),

您好,這個問題可能有些愚蠢(問題標題可能不會變得無關緊要,直到找到一個完美的標題為止),我花了幾個小時才弄清楚這一點。

我會盡力解釋。 首先,我將首先給出我的代碼。

文件“ 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<windLog> Obj來存儲我的windlog文件。 然后我需要將其傳遞給void readFile(Vector<WindLog> &VecObj); 對於一些讀取文件,並添加到我創建的Vector類中。

之后,該程序將void showMenu(Vector<WindLog> &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的參數,它們具有相同的參數,這讓我感到困擾,我不知道它是否還可以。

如果我的問題尚不清楚,請給我一些評論,以便我解決。 如果你們想給我一些有關我的代碼的建議,我將不勝感激。

如果所有選項都將始終使用相同的參數,則可以將它們放在向量中並使用該選項對其進行索引。

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

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

只要在函數中具有不同的名稱(它們的功能相同),在不同的函數中具有相同的參數類型就可以了。 如果確實困擾您,您可以編寫一個函數並傳遞一個附加參數(例如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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM