我编写了这个程序来计算给定比萨(直径)的这个尺寸的切片数量。但结果似乎有点过了......任何援助将不胜感激:)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
您会期望结果是什么?为什么? – interjay
面积= PI *直径;这真的看起来不对 – lezebulon
'area = PI * diameter; //或者尝试area = PI * radius * radius;' - 你是否尝试了评论的公式? –