【发布时间】: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<windLog> Obj 来存储我的windlog 文件。然后我需要将它传递给void readFile(Vector<WindLog> &VecObj); 以获取一些读取文件并添加到我创建的向量类中。
之后程序将运行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的参数,它们的参数相同,这让我很困扰,我不知道它是否可以。
如果我的任何问题不清楚,请给我一些评论,以便我解决它。如果你们想就我的代码给我一些建议,我将不胜感激。
【问题讨论】:
-
你的标题说的是多个类,但问题是关于多个功能。你真正想问的是什么?
-
看起来您需要一个处理所有这些操作的类,并以 Vector 作为成员。
-
@Barmar 对不起标题,我已经改了。我关于option1-4参数的问题。如果可以的话,它需要相同的 4 参数和 idk,或者我可以以任何方式对其进行更多优化。
-
我觉得很好。
-
我唯一不同的是重新排序文件以消除对前向声明的需要。
标签: c++ class c++11 data-structures