2015-11-01 79 views
0

我做的是应该从以英里和科学记数法从完成到子程序主程序测算返回太阳距离的显示值的函数。生成的程序会正确构建并运行,但它会返回应该获得的错误值。 下面是输出应该是这样的基础上,增加了函数之前构建的代码:[在这里输入的形象描述] [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  
     123456789... 
     --------------------------------- 

     @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; 
    } 
+0

'(3 * 2)*(pow(2,(n-2))'看起来这个公式要求将2 ^(n-2)'乘以3,而不是乘以6. –

回答

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

+0

好的,谢谢so多为你的帮助!它现在,决定修改它通过以下方式(除了在主程序即N + 1的改变值n的正确值,N + 2,N + 3)和子程序现在读取:{ \t \t N = N - 2; \t \t双estimateByBode =(4 + 3 *(POW(2,N)))/(10); \t \t返回estimateByBode; \t} – marihikari

+0

@marihikari不要忘记接受答案,如果这解决了你的问题 – user463035818