我有两个问题。
1.当我使用小数进行转换时,结果总是为0。
当我不使用小数时,结果是好的。
2.我想循环我的结果,这样如果我放入32 (start_temp)华氏度,我可以增加它,直到我的“结束温度”(end_temp) &为了也转换增加的温度,假设42如果增量是10,那么它将42华氏度转换为它的摄氏度。
我试着做了一个for循环,但是没有成功。
请帮帮忙?
int main()
{
int choice;
cout << "Please Select A Temperature Scale Conversion:\n"<< endl;
cout << "1. Convert F to C" << endl;
cout << "2. Convert C to F\n" << endl;
cout << "Your Choice: ";
cin >> choice;
cout << "Starting Temperature: ";
cin >> start_temp;
cout << "Ending Temperature: ";
cin >> end_temp;
cout << "Temperature Increase: ";
cin >> temp_incr;
if (choice == 1) {
cout << fixed << setprecision(2) << calcFahrenheit(start_temp) << endl;
} else if (choice == 2) {
cout << fixed << setprecision(2) << calcCelsius(start_temp) << endl;
} else {
cout << "You have entered an invalid option." << endl;
}
system("PAUSE");
return 0;
}
float calcFahrenheit(float start_temp) {
float f2c;
f2c = (5.0/9.0) * (start_temp - 32.0);
return f2c;
}
float calcCelsius(float start_temp) {
float c2f;
c2f = (9.0/5.0) * (start_temp + 32.0);
return c2f;
}发布于 2012-10-21 04:27:32
您可以这样做:在c2f中保留start_temp,然后在循环中使用该值再次计算c2f,直到end_temp,并使用temp_incr增加c2f
for (c2f=start_temp ; c2f<=end_temp ; c2f+=temp_incr)
{
// Do your calculation
}同样的道理也适用于另一个公式
https://stackoverflow.com/questions/12992308
复制相似问题