C++项目:将整数转换为双精度浮点数。

huangapple go评论177阅读模式
英文:

C++ Project: converting a interger into a double

问题

你好,这是你的翻译:

"Hi to all I am new to programming with C++, and I have been assigned a project where I have to build a program that calculates the average birthrate & death rate percentage of a population. I am currently stuck on this step where I need to convert an integer value into a double value to get the correct output. For example, a function for finding the birthrate would be (births/population) and since dividing integers it causes the floating point number to be truncated giving me a result of 0 instead of getting (10/100 which would equal 0.1). I understand that I have declared integers, but that is the requirement. I have thought about typecasting but had no luck converting the value. I apologize for the long explanation I hope it makes sense. Thank you in advance!"

这是你的代码,我已经过滤掉不需要翻译的部分:

//  Population class declaration
class Population
{
private:
    int population, births, deaths;

public:
    // default constructor: with no parameters
    Population()
    {
        population = 2;
        births = 0;
        deaths = 0;
    }

    // constructor
    Population(int p, int b, int d)
    {
        population = p;
        births = b;
        deaths = d;
    }

    // birthrate member function
    int getBirthrate();
    // deathrate member function
    int getDeathrate();

    void setPopulation(int);
    void setBirth(int);
    void setDeath(int);
    int getPopulation();
    int getBirth(int);
    int getDeaths(int);
};

// member functions
void Population::setPopulation(int p)
{
    if (population < 2)
    {
        population = 2;
        cout << "Error: Invalid population input!" << endl;
    }
    else
    {
        population = p;
    }
}

void Population::setBirth(int b)
{
    if (births < 0)
    {
        births = 0;
        cout << "Error: Invalid births input!" << endl;
    }
    else
    {
        births = b;
    }
}

void Population::setDeath(int d)
{
    if (deaths < 0)
    {
        deaths = 0;
        cout << "Error: Invalid deaths input!" << endl;
    }
    else
    {
        deaths = d;
    }
}

int Population::getPopulation()
{
    return population;
}

int Population::getBirth(int)
{
    return births;
}

int Population::getDeaths(int)
{
    return deaths;
}

// calculating functions
int Population::getBirthrate()
{
    return births / population;
}

int Population::getDeathrate()
{
    return deaths / population;
}

// main function
int main()
{
    // instantiation of a population object
    Population x;

    int xBirths, xDeaths, xPopulation;

    // getting the values
    cout << "What is the population: " << endl;
    cin >> xPopulation;

    cout << "What is the number of births: " << endl;
    cin >> xBirths;

    cout << "What are the number of deaths: " << endl;
    cin >> xDeaths;

    // set functions
    x.setPopulation(xPopulation);
    x.setBirth(xBirths);
    x.setDeath(xDeaths);

    // calculating the values
    cout << "The population is: " << x.getPopulation() << endl;
    cout << "The birthrate is: " << x.getBirthrate() << endl;
    cout << "The deathrate is: " << x.getDeathrate() << endl;

    return 0;
}

希望对你有所帮助!如果有任何问题,请随时提出。

英文:

Hi to all I am new to programming with C++, and I have been assigned a project where I have to build a program that calculates the average birthrate & death rate percentage of a population. I am currently stuck on this step where I need to convert an integer value into a double value to get the correct output. For example, a function for finding the birthrate would be (births/population) and since dividing integers it causes the floating point number to be truncated giving me a result of 0 instead of getting (10/100 which would equal 0.1). I understand that I have declared integers, but that is the requirement. I have thought about typecasting but had no luck converting the value. I apologize for the long explanation I hope it makes sense. Thank you in advance!

This is the following criteria:

  1. private variables: have to be integers (population, births, deaths)

  2. public functions:

  • default constructor with no parameters, initialize (population = 2), (births & deaths = 0)
  • constructor with three parameters. The constructor should pass the population, the number of births, and deaths for the Population object.
  1. "Set" member function for each of the three-member variables. If a population figure is less than 2 and is passed to the class, use a default value of 2. If a birth or a death figure less than 0 is passed, use a default value of 0.

  2. getBirthrate member function. calculates the birth rate based on the following formula: (Birthrate = Number of births/population)

  3. getDeathrate member function. calculates the death rate based on the following formula: (Deathrate = Number of deaths/ population)

    #include &lt;iostream&gt;
    #include &lt;string&gt;
    #include &lt;iomanip&gt;
    using namespace std;
    //  Population class declaration
    class Population
    {
    private:
    int population, births, deaths;
    public:
    // default constructor: with no parameters
    Population()
    {
    population = 2;
    births = 0;
    deaths = 0;
    }
    // constructor
    Population(int p, int b, int d)
    {
    population = p;
    births = b;
    deaths = d;
    }
    // birthrate member function
    int getBirthrate();
    // deathrate member function
    int getDeathrate();
    void setPopulation(int);
    void setBirth(int);
    void setDeath(int);
    int getPopulation();
    int getBirth(int);
    int getDeaths(int);
    };
    // member functions
    void Population::setPopulation(int p)
    {
    if (population &lt; 2)
    {
    population = 2;
    cout &lt;&lt; &quot;Error: Invalid population input!&quot; &lt;&lt; endl;
    }
    else
    {
    population = p;
    }
    }
    void Population::setBirth(int b)
    {
    if (births &lt; 0)
    {
    births = 0;
    cout &lt;&lt; &quot;Error: Invalid births input!&quot; &lt;&lt; endl;
    }
    else
    {
    births = b;
    }
    }
    void Population::setDeath(int d)
    {
    if (deaths &lt; 0)
    {
    deaths = 0;
    cout &lt;&lt; &quot;Error: Invalid deaths input!&quot; &lt;&lt; endl;
    }
    else
    {
    deaths = d;
    }
    }
    int Population::getPopulation()
    {
    return population;
    }
    int Population::getBirth(int)
    {
    return births;
    }
    int Population::getDeaths(int)
    {
    return deaths;
    }
    // calculating functions
    int Population::getBirthrate()
    {
    return births / population;
    }
    int Population::getDeathrate()
    {
    return deaths / population;
    }
    // main function
    int main()
    {
    // instantiation of a population object
    Population x;
    int xBirths, xDeaths, xPopulation;
    // getting the values
    cout &lt;&lt; &quot;What is the population: &quot; &lt;&lt; endl;
    cin &gt;&gt; xPopulation;
    cout &lt;&lt; &quot;What is the number of births: &quot; &lt;&lt; endl;
    cin &gt;&gt; xBirths;
    cout &lt;&lt; &quot;What are the number of deaths: &quot; &lt;&lt; endl;
    cin &gt;&gt; xDeaths;
    // set functions
    x.setPopulation(xPopulation);
    x.setBirth(xBirths);
    x.setDeath(xDeaths);
    // calculating the values
    cout &lt;&lt; &quot;The population is: &quot; &lt;&lt; x.getPopulation() &lt;&lt; endl;
    cout &lt;&lt; &quot;The birthrate is: &quot; &lt;&lt; x.getBirthrate() &lt;&lt; endl;
    cout &lt;&lt; &quot;The deathrate is: &quot; &lt;&lt; x.getDeathrate() &lt;&lt; endl;
    return 0;
    }
    

</details>
# 答案1
**得分**: 0
替代这个:
```cpp
int Population::getBirthrate()
{
return births / population;
}

使用这个:

int Population::getBirthrate()
{
   return (births * 1.0) / population;
}

将一个整数与1.0相乘将其转换为双精度浮点数。

英文:

instead of this:

int Population::getBirthrate()
{
return births / population;
}

do this:

int Population::getBirthrate()
{
return (births * 1.0) / population;
}

multiplying an integer with 1.0 convert them into double.

答案2

得分: 0

我猜你有两个问题

int Population::getDeathrate()
{
    return deaths / population;
}

第一个问题就是,正如你已经指出的,你在进行整数除法。你可以通过使用类型转换解决这个问题。第二个问题是,你的函数返回一个整数,而从上面的问题看来,你想要一个浮点数的结果,例如 10/100 = 0.1。解决这个问题的方法是将返回类型更改为 double

因此,将这两个问题合并在一起:

double Population::getDeathrate()
{
    return (double)deaths / (double)population;
}
英文:

I would guess that you have two problems

int Population::getDeathrate()
{
return deaths / population;
}

The first is, as you've identified, you are doing integer division. You can solve that with a type cast. The second problem is that your function returns an integer, when it seems from the question above that you want a floating point result, e.g. 10/100 = 0.1. The solution to that problem is to change the return type to double.

So, putting both of those together

double Population::getDeathrate()
{
return (double)deaths / (double)population;
}

huangapple
  • 本文由 发表于 2023年3月7日 13:50:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定