有关仅正确使用设置器和变量的问题 [英] Question regarding using only setters and variables correctly

查看:76
本文介绍了有关仅正确使用设置器和变量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的编程工作遇到问题。我觉得我似乎已经很接近正确了,但是有些问题了。我知道我必须做一些不同的事情才能使程序正确运行,因为它目前无法正常工作,但是我不确定它是什么。

I am having an issue with my current programming assignment. I feel as though I am very close to having it correct, but something is off. I know that I have to do something different in order to make the program function correctly, as it doesn't work right now, but I am not sure what it is.

我在如何使用单个私有变量同时产生两种温度方面苦苦挣扎。

I am struggling specifically with how to use a single private variable to make both kinds of temperatures.

这里是赋值:


设置温度类。该类应具有以华氏度设置温度的功能和以摄氏度为单位设置温度的功能。在专用部分中仅保留一个数据成员用于存储温度。创建一个获取华氏温度的函数和一个获取摄氏温度的函数。用驱动程序彻底测试每个功能。

Make a Temperature class. The class should have a function for setting the temperature in Fahrenheit and function for setting a temperature in Celsius. Keep only one data member in the private section for storing the temperature. Create a function for obtaining the in Fahrenheit and a function for obtaining the temperature in Celsius. Test each function thoroughly with a driver.

F =(9/5)C + 32,C =(5/9)(F-32)

F = (9/5)C + 32, C = (5/9)(F - 32)

当前代码:

#include<iostream>
using namespace std;

class Temperature
{
private:
    double temperature;

public:
    void set_fahrenheit(double f)
    {
        temperature = f;
    }

    void set_celsius(double c)
    {
        temperature = c;
    }

    double get_fahrenheit()
    {
        return temperature;
    }

    double get_celsius()
    {
        return temperature;
    }

    double converter(double temperature)
    {
        if (temperature = f)
        {
            return (9/5)*temperature + 32;
        }
        else if (temperature = c))
        {
            return (5/9)*(temperature - 32;
        }
    }    
};

int main()
{
    Temperature Temp1;
    double temperaturetemp;
    string response;

    cout << "Would you like to convert a Celsius temperature to Fahrenheit or convert a Fahrenheit temperature to Celsius? (Enter C2F or F2C respectively)" << endl;
    cin >> response;

    cout << "Please enter the temperature you would like to convert in degrees" << endl;
    cin >> temperaturetemp;

    if (response == "C2F"){Temp1.set_fahrenheit(temperaturetemp);}
    else if (response == "F2C"){Temp1.set_celsius(temperaturetemp);}

    cout << Temp1.converter(temperaturetemp);
 }


推荐答案

只需使用一个特定的单位内部存储温度,最好d是Kelvin 1 IMO(因为它是标准的SI物理单位)。

Just use one specific unit to store the temperature internally, best would be Kelvin1 IMO (as it's the standard SI physical unit).

在将温度设置为华氏度或

Do the necessary calculations when setting or getting the temperature as Fahrenheit or degrees Celsius.

也不要使用整数除法来表示分数:

(5/9)的结果为 0 整数除,它应为(5.0 / 9.0)才能获得有效的 double 值。

9/5 相同,因为整数除法将得到 1

Also don't use integer division to represent fractions:
(5/9) will result as 0 integer division, it should be (5.0/9.0) to get a valid double value.
Same with 9/5 as integer division will result as 1.

您的代码还有其他问题:

Further issues with your code:


  1. 在函数双转换器(双温度)中,尝试使用 f ,不在此范围内

  2. 在同一函数中,您有一个名为 temperature 的参数,您的同名成员变量

  1. In your function double converter(double temperature) you try to use f, which isn't in scope there
  2. In that same function you have a parameter named temperature, which shadows your member variable with the same name






1) 0K = -459,67F / 0K = -273.15°C


1)0K = -459,67F / 0K = -273.15°C

这篇关于有关仅正确使用设置器和变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆