功能和基本子程序距离计算

时间:2015-11-01 23:31:13

标签: c++ function prototype

我正在创建一个功能,该功能应该显示距离太阳的英里距离值,以及从完成子程序到主程序的计算返回的科学记数法。生成的程序正确构建和运行,但它返回应该获取的错误值。 这是输出应该是什么样的,基于在添加函数之前构造的代码:[在此处输入图像描述] [1]

  [1]: http://i.stack.imgur.com/hJiKj.png
And the output I get now is about 4 times as much as that. 
Here is the code: 
// ----------------------------------------------------------------------------
//
// -------------------------- Bode's Law Calculation --------------------------
//
// ----------------------------------------------------------------------------
//
// Name   : Bode's Law
// Version: 1.2b
// Purpose: Demonstration program to be used for 
//          examining output in scientific notation.
//          This program uses Bode's formula for 
//          estimating the distance between the Sun
//          and the planets in our solar system.
// Author : Jeffrey L. Popyack
// Date   : Jan. 20, 1998
// Modified: Mar. 3, 1999
//           Jan. 16, 2002 - names of constants changed
//           to agree with Horstmann style guidelines.
//           Feb. 2002, Feb. 2003 - reformatted internal layout
//
// ----------------------------------------------------------------------------

    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std; 
    double estimateByBode(int n, double dist);
// ----------------------------------------------------------------------------
//
// -------------------------------- Prototypes --------------------------------
//
// ----------------------------------------------------------------------------

//  Draw column guides up to NumColumns wide
    void columnGuides(int NumColumns) ;



// ----------------------------------------------------------------------------
//
// ------------------------------- Main Program -------------------------------
//
// ----------------------------------------------------------------------------

    int main(void)
    {
    // ------------------------------------------------------------------------
    // Bode's formula estimates the distance from planet n
    // to the Sun according to the following formula:
    //                   (n-2)
    //    dist = (4 + 3*2     )/10,
    // where dist is given in astronomical units, and
    // one astronomical unit equals 93,000,000 miles.

    // PLANET_2, PLANET_3, etc. are the names of the planets.
    // ------------------------------------------------------------------------

        const string PLANET_2 = "Venus", 
                     PLANET_3 = "Earth" ,
                     PLANET_4 = "Mars" ;

    // ------------------------------------------------------------------------
    //  dist2, dist 3, etc. are estimated distances of
    //  planets from the Sun, using Bode's Law:
    //  e.g., dist2 is the distance from PLANET_2 to the Sun.
    // ------------------------------------------------------------------------
        int n = 1;
        double dist = 0;
        dist = estimateByBode (n, dist);
        double dist2 = estimateByBode(n,dist);
        double dist3 = estimateByBode(n+1, dist);
        double dist4 = estimateByBode(n + 3,dist) ;

        columnGuides(40) ;
        cout << "Planet  Astro Units (est.)  Miles (est.)" << endl;
        cout << setw(6) << left << PLANET_2.c_str() 
             << fixed << right << setprecision(3) << setw(11) << dist2 
             << scientific << setprecision(2) << setw(22) << dist2*93000000 
             << endl ;

        cout << setw(6) << left << PLANET_3.c_str() 
             << fixed << right << setprecision(3) << setw(11) << dist3 
             << scientific << setprecision(2) << setw(22) << dist3*93000000 
             << endl ;

        cout << setw(6) << left << PLANET_4.c_str() 
             << fixed << right << setprecision(3) << setw(11) << dist4 
             << scientific << setprecision(2) << setw(22) << dist4*93000000 
             << endl ;

        return 0 ;
    }


// ----------------------------------------------------------------------------
//
// -------------------------- Subprogram Definitions --------------------------
//
// ----------------------------------------------------------------------------

    void columnGuides(int NumColumns)
    {
    // ------------------------------------------------------------------------
    /**
       This procedure draws column guides of the form

                  1         2         3    
         123456789012345678901234567890123 ...
         ---------------------------------

         @param NumColumns - the desired column width  

    */
    // ------------------------------------------------------------------------

        int i ;

        for(i=1; i<=NumColumns/10; i++)
            cout << setw(10) << i ;
        cout << endl ;

        for(i=1; i<=NumColumns; i++)
            cout << i%10 ;
        cout << endl ;

        cout << setfill('-') << setw(NumColumns) << right << "-" << endl ;
        cout << setfill(' ') << resetiosflags(ios::right) ;
    }
    double estimateByBode(int n, double dist)
    {


        double estimateByBode = (4 + (3 * 2) * (pow(2, (n - 2))) / 10);
        return estimateByBode; 
    }

1 个答案:

答案 0 :(得分:0)

Titus-Bode law

a = 0.4 + 0.3 * 2^m, for m = 0,1,2,....

可以改写为

a = (4 + 3 * 2^m)/10

您的索引可能会被一个关闭,而且代码实际上看起来有人已经在摆弄它(为什么2*3*2^(n-2) ??)。我建议你写一下

a = (4 + 3 * pow(2,n-offset);

并为offset尝试不同的值。