2013-01-31 66 views
-2

我編寫了這個程序來計算給定比薩(直徑)的這個尺寸的切片數量。但結果似乎有點過了......任何援助將不勝感激:)C++ - 計算比薩中切片的數量

例如: 如果我輸入18寸的披薩,它導致4.00344片... 如果我輸入22英寸比薩,它結果在4.8931片...

見下面的代碼:

#include <iostream> 
using namespace std; 
int main() 
{ 
    // Title of CMD Window 
    system("title How many Slices are in your Pizza?"); 
    // Declare variables  
    double diameter = 0.0,  // Diameter of the pizza 
      slices = 0.0,  // No. of slices in the pizza 
      area = 0.0,   // Area of the whole pizza 
      oneSlice = 14.125; // Area of one pizza slice 
    const double PI = 3.14159; 
    // Display prompt 
    cout << "What is the diameter of the pizza (inches):" << "\n"; 
    cin >> diameter; 
    // Calculate the area of the pizza 
    area = PI * diameter; 
    // Calculate number of slices for the size of pizza given 
    slices = area/oneSlice; 
    // Display results 
    cout << "\n\n" << "You have " << slices << " slice(s) in this pizza:" << "\n\n" 
     << "************************************" << "\n" 
     << "\tDiameter of pizza= " << diameter << "\n" 
     << "\tArea of pizza= " << area << "\n" 
     << "************************************" << "\n"; 
    system("pause"); 
    return 0; 
} 
// End of program 
+0

您會期望結果是什麼?爲什麼? – interjay

+4

面積= PI *直徑;這真的看起來不對 – lezebulon

+0

'area = PI * diameter; //或者嘗試area = PI * radius * radius;' - 你是否嘗試了評論的公式? –

回答

4

圓的面積不pi * diameter:這是周長。

您需要area = PI * (diameter/2.0) * (diameter/2.0);

+0

看起來像工作!謝謝馬克! – menormedia

+3

我想這是一個不好的跡象,當問問題的人回答「看起來像是有用」或類似的好答案時。這就像你想要的只是一個快速修復,你仍然不明白*爲什麼*你的方法是不正確的,你也不關心。 – us2012