我做的是應該從以英里和科學記數法從完成到子程序主程序測算返回太陽距離的顯示值的函數。生成的程序會正確構建並運行,但它會返回應該獲得的錯誤值。 下面是輸出應該是這樣的基礎上,增加了函數之前構建的代碼:[在這裏輸入的形象描述] [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;
}
'(3 * 2)*(pow(2,(n-2))'看起來這個公式要求將2 ^(n-2)'乘以3,而不是乘以6. –