我的计算结果似乎有这个奇怪的问题。当它应该是 1.375 时,我似乎总是得到 -0.125 的结果。这可能是我放置计算的方式吗?
下面是参与计算的.cpp文件:
位置数据.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;
}
任务计划.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 的 int
时,
(sunTypePercent/100)
始终为零,因为您正在进行整数除法。
您可能需要 sunTypePercent
为 float
(或 double
)。
关于c++ - 计算结果出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19475112/