嘿,這是我在我的cs類中做家庭作業的一個類,現在我正在玩python。我想學習如何在Python中設計類,並希望實現這個。這是一個相對直接的課程。幫助將不勝感激。下面的代碼:在python中實現類
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <vector>
#include <cmath>
using namespace std;
int num_flights=0 ;
const int Columns_Total = 14; /*the total columns are 14 for each flight*/
class Flight
{
public:
Flight();
void major_calc(string& filename,ifstream& is,string& line, ofstream& os);
void print (ofstream& os);
private:
vector<vector <double> > store;
int num_lines;
double avg_wind_speed;
double avg_height;
double avg_tempr;
double std_dev_speed;
double std_dev_height;
double std_dev_tempr;
};
Flight::Flight()
{
}
void Flight::major_calc(string& filename,ifstream& is, string& line, ofstream& os)
{ num_lines=0;
vector <double> single_line; /* Vector to one test line of a flight*/
vector <double> flight_height; /* Vector stores the heights associated with all the test lines for a particular flight*/
vector <double> flight_wind_speed; /* Vector stores the wind speeds associated with all the test lines for a particular flight*/
vector <double> flight_tempr; /* Vector stores the temperatures associated with all the test lines for a particular flight*/
while(getline(is,line))
{
if (line.substr(0,3) == "ACM") /* Checks a new flight */
{
num_flights++;
os << line << endl;
return ;
}
else
{
istringstream iss(line,istringstream::in); /* Reading the string*/
double FieldInfo;
num_lines++; /* updating the counter*/
for (int n=0; n<14; n++)
{
iss >> FieldInfo;
single_line.push_back(FieldInfo);
}
store.push_back(single_line);
flight_height.push_back(single_line[4]); /* Updates the height vector with the value from a particular test line*/
flight_wind_speed.push_back(single_line[3]); /* Updates the wind speed vector with the value from a particular test line*/
flight_tempr.push_back(single_line[8]);/* Updates the temperature vector with the value from a particular test line*/
}
single_line.clear(); /* Empties the single line vector so it can be used again*/
}
double temp1=0; /* temporary variables used to calculate the average height, average speed and average temperature*/
double temp2=0;
double temp3=0;
for (int i=0; i< flight_height.size();i++)
{
temp1+= flight_height[i];
temp2+= flight_wind_speed[i];
temp3+= flight_tempr[i];
}
avg_height= temp1/(flight_height.size());
avg_wind_speed= temp2/(flight_wind_speed.size());
avg_tempr= temp3/(flight_tempr.size());
double sqr=2.0; /* Temporary variables used to calculate std deviation associate with the speed, height and the temperature*/
double temp4=0;
double temp5=0;
double temp6=0;
for (int i=0; i< flight_height.size();i++)
{
temp4+= pow((flight_height[i]-avg_height),sqr);
temp5+= pow((flight_wind_speed[i]-avg_wind_speed),sqr);
temp6+= pow((flight_tempr[i]-avg_tempr),sqr);
}
std_dev_height= sqrt(temp4/flight_height.size());
std_dev_speed= sqrt(temp5/flight_wind_speed.size());
std_dev_tempr= sqrt(temp6/flight_tempr.size());
}
void Flight::print (ofstream& os)
{
os<<"printing the results out"<<endl; /* Printing out all the values*/
os<<"avg speed: "<<avg_wind_speed<<"knots"<<endl;
os<<"avg height: "<<avg_height<<"feet"<<endl;
os<<"avg tempr: "<<avg_tempr<<"celcius"<<endl;
os<<"std dev in height: "<<std_dev_height<<"knots"<<endl;
os<<"std dev in speed: "<<std_dev_speed<<"feet"<<endl;
os<<"std dev in temperature: "<< std_dev_tempr<<"celcius"<<endl;
os<<"The number of lines "<< num_lines<<endl;
}
int main()
{
string filename;
string line;
cout<<"Enter the filename"<<endl;
cin>>filename;
ifstream is;
is.open(filename.c_str()); /*open the file*/
bool FileDoesntExist = is.fail();
if(FileDoesntExist) /*checks if the file exists*/
{
cout << "File doesn't exist" << endl;
exit(1);
}
string fileout = filename.substr(filename.rfind(".")+1,filename.length()) + ".log"; /*making the ouput file */
ofstream os (fileout.c_str());
vector <Flight> complete;
while(!is.eof())
{
Flight test;
while(getline(is,line))
{
test.major_calc(filename,is,line,os);
test.print(os);
}
complete.push_back(test);
}
return 0;
}
由於這是一個「相對簡單的類」或許你可以嘗試在Python自己寫的,並當您遇到特定問題時提出問題。我認爲期望有人用Python爲你重寫整個東西有點多。 – 2010-12-06 16:13:04
我從來沒有說過要重寫整個東西......我只是想提示如何改變東西,所以我可以在Python中實現它。對不起,如果你得到了錯誤的信息 – dawnoflife 2010-12-06 16:16:41