将小数作为变量

时间:2015-02-18 18:37:22

标签: c++

所以我有一种代码告诉我所需的盒子和包的数量。因此,对于实例E =引擎数,B =框,P =包。可以说我需要67个引擎,我有B = E(67)/ 24这应该给我2.7因为67/24。我必须让电脑说2个盒子和7个引擎,但我不知道该怎么做。

谢谢!

    // Include Section
     #include <iostream>
     #include <iomanip>
     #include <cstdlib>
     using namespace std;

     // Main Program
     int main( )
     {
        int E;     // number of engines needed
        double CB; // The cost of the boxes containing 24 engines
        double CP; // The cost of the packages containing 3 engines
        int B;     // The number of boxes needed to get the amount                  
        int P;     // The number of packages needed to get the amount of engines
        double C;  // The total cost of all the engines desired.

    // Output Identification
     system("CLS");
     cout << "Take Home #5 by Yousef Saqer - "
         << "Calculate Engine Purchase\n\n";

    cout << " Enter the number of engines needed: ";
    cin >> E;
    cout << " Enter the cost of a box of 24 engines: ";
    cin >> CB;
    cout << " Enter the cost of a package of 3 engines: ";
    cin  >> CP;



   if(E < 24)
   {
    B = E / 24;
    P = E / 3;
    C = CB * B + CP * P;

    if(E > 3)
    {
        P++;
        cout << " You will need to purchase " << P 
        << " packages of engines at a total cost of $"<< C << endl;
    }

    else
    {
        cout << " You will need to purchase " << 1
        << " package of engines at a total cost of $"<< CP << endl;
    }


   }

   else
   {
    B = E / 24;
    P = E/24.0;
    C = CB * B + CP * P;

    if( B != 1)
    {
        cout << " You will need to purchase " << B << " boxes and "
         << P << " packages of engines at a total cost of $"<< C<< endl;
    }

    else
    {
        cout << " You will need to purchase " << B << " box and " 
        << P << " packages of engines at a total of $" << C << endl;
    }

}
cout << "\n\nEnd Program.\n";

cin.get();
cin.get();

return 0;

}

1 个答案:

答案 0 :(得分:0)

int main (){
    double param, fractpart, intpart;

    param = 2.7;
    fractpart = modf (param , &intpart);

    return 0;
}

这种方式param=2intpart=0.7

这是整个代码 http://www.cplusplus.com/reference/cmath/modf/