2013-01-31 26 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