// page168.cpp : 定义控制台应用程序的入口点。
//C++面向对象程序设计
// 判断圆形皮萨和矩形皮萨,哪一种更适合购买
#include "stdafx.h"
#include <iostream>
double unitprice(int diameter , double price );
//返回圆形皮萨每平方英寸的价格。形参的啊meter是以英寸为单位的皮萨直径,形参price是皮萨的价格
double unitprice(int length , int width , double price );
//返回矩形皮萨每平方英寸的价格
//皮萨的长度和宽度分别是length英寸和width英寸
//形参price是皮萨的价格
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std ;
int diameter, length ,width;
double price_round ,unit_price_round,
price_rectangular , unitprice_rectangular ;
cout << " Welcome to the Pizza Consumers Union.\n";
cout << " Enter the diameter in inches"
<< " of a round pizza : ";
cin >> diameter;
cout << " Enter the price of a round Pizza :$";
cin >> price_round;
cout << " Enter length and width in inches\n"
<< " of a rectangual pizza :";
cin >> length >> width ;
cout << " Enter the price of a rectangular pizza :$ ";
cin >> price_rectangular;
unitprice_rectangular = unitprice( length , width , price_rectangular );
unit_price_round = unitprice( diameter , price_round );
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << endl
<< " Round pizzza : length = "
<< diameter << " inches\n"
<< " Price = $ " << price_round
<< " per squre inch = $" << unit_price_round
<< endl
<< " Rectanguarl pizza : length = "
<< length << " inches\n"
<< " Rectanguarl pizza : width = "
<< width << " inches\n"
<< " price = $ " << price_rectangular
<< " per square inch = $" << unitprice_rectangular
<< " per square inch = $ " << unitprice_rectangular
<< endl;
if (unit_price_round < unitprice_rectangular)
{
cout << " The round one is the better buy .\n";
}
else
cout << " The rectangular one is the better buy .\n";
cout << " Buon Appetito!\n";
cin >> diameter;
return 0;
}
double unitprice(int diameter , double price )
{
const double PI = 3.1415;
double radius , area;
radius = diameter / static_cast<double>(2);
area = PI * radius * radius ;
return (price / area);
}
double unitprice(int length , int width , double price )
{
double area = length * width ;
return (price / area);
}