-1

我编写了这个程序来计算给定大小的比萨饼(直径)的切片数。但结果似乎有点偏离......任何帮助将不胜感激:)

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

1 回答 1

5

圆的面积不是pi * diameter:那是周长。

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

于 2013-01-31T16:36:03.390 回答