Кажется, у меня есть эта странная проблема с моим результатом вычисления. Кажется, я всегда получаю результат -0.125, когда он должен быть 1.375. Может ли это быть моим вычислением?
Ниже приведены файлы.cpp, участвующие в вычислении:
LocationData.cpp
Это та часть, где выполняется вычисление.
float LocationData::computeCivIndex(string sunType,int planetNo,int moonNo,float particulatePercent,float plasmaPercent) {
float civIndex;
int sunTypePercent;
if (sunType == "O")
sunTypePercent = 30;
if (sunType == "B")
sunTypePercent = 45;
if (sunType == "A")
sunTypePercent = 60;
if (sunType == "F")
sunTypePercent = 75;
if (sunType == "G")
sunTypePercent = 90;
if (sunType == "K")
sunTypePercent = 80;
if (sunType == "M")
sunTypePercent = 70;
cout << sunTypePercent<< endl;
//calculate the civIndex
civIndex = ((sunTypePercent/100) - (particulatePercent+plasmaPercent)/200) * (planetNo+moonNo);
return civIndex;
}
MissionPlan.cpp
Это та часть, где вызывается метод вычисления
void MissionPlan::computeCivIndex() {
LocationData ld;
string type;
int planets,moons;
float particulate,plasma;
if (db.size() == 0)
cout << "No data! Please input statistical data before proceeding..." << endl;
else {
for (vector<PointTwoD>::iterator itr = db.begin(); itr != db.end(); ++itr) {
ld = itr->getLocationData();
type = ld.getSunType();
planets = ld.getNoOfEarthLikePlanets();
moons = ld.getNoOfEarthLikeMoons();
particulate = ld.getAveParticulateDensity();
plasma = ld.getAvePlasmaDensity();
//i print out the values to check that it is the values i inputted beforehand(manually)
cout << type << endl;
cout << planets << endl;
cout << moons << endl;
cout << particulate << endl;
cout << plasma << endl;
itr->setCivIndex(ld.computeCivIndex(type,planets,moons,particulate,plasma));
}
}
cout << "Computation complete! (" << db.size() << " records were updated)" << endl;
}
Любые идеи о том, как исправить эту странную проблему? Благодарю!
(sunTypePercent/100)
всегда равен нулю, когда sunTypePercent
является int
менее 100, потому что вы выполняете целочисленное деление.
Вероятно, sunTypePercent
должен быть float
(или double
).
+ Изменить
int sunTypePercent;
в
float sunTypePercent;
ИЛИ:
civIndex = ((sunTypePercent/100.0) - (particulatePercent+plasmaPercent)/200.0) * (planetNo+moonNo);