C ++ - 计算披萨中的切片数量

时间:2013-01-31 16:33:02

标签: c++ visual-studio-2010

我写了这个程序来计算给定的这种比萨饼(直径)的切片数量。但结果似乎有点偏......任何帮助都会非常感激:)

例如: 如果我输入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

1 个答案:

答案 0 :(得分:4)

圆圈区域不是pi * diameter:这就是圆周。

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