求两个温度之间的差

Finding the difference between two temperature

本文关键字:温度 之间 两个      更新时间:2023-10-16

我有一个函数,应该找到两个温度之间的差异。首先,我打印出摄氏温度和大概华氏温度,然后我找到并打印出它们之间的差异。我的问题是,当我运行这个程序时,所有输出的差值都是58。

它应该打印出如下内容:

C----AF----Diff
1----32----31
2----34----32

等。我代码:

void calDiff(int& cel, int& appFar, int diff){
while(cel!= 101){
    diff = appFar - cel;
    cout << diff << endl;
    cel++;
    appFar++;
}
}
  1. 你需要一个函数把摄氏度转换成华氏度。
  2. 如果您不想更改celappFar,则删除参考&

int cel2far(int cel)
{
     // convert cel to far and return approx. far
}
void calDiff(int cel, int appFar, int diff)
{
    while(cel!= 101){
        diff = appFar - cel;
        cout << diff << endl;
        cel++;
        appFar = cel2far(cel);
    }
}

每次循环都将摄氏度和华氏温度分别增加1,因此每次的差值都是相同的。仅仅因为你通过参考传递温度并不意味着当你改变温度时它会重新计算华氏温度。您应该将摄氏度加1,重新计算华氏度,然后计算差值。