// page152.cpp : 定义控制台应用程序的入口点。
//C++面向对象程序设计
//判断两种披萨哪种最适合购买
#include "stdafx.h"
#include <iostream>
using namespace std ;
double unitprice (int diameter ,double price);
//返回披萨每平方英寸的价格
//形参diameter是以英寸为单位的披萨直径,形参price是以披萨的价格
int _tmain(int argc, _TCHAR* argv[])
{
int diameter_small , diameter_large ;
double price_small ,unitprice_small,
price_large ,unitprice_large;
cout << " Welcome to the Pizza Consumers Union .\n";
cout << " Enter diameter of a small pizza (in inches): ";
cin >> diameter_small;
cout << " Enter the price of a small pizza : $ ";
cin >> price_small ;
cout << " Enter diameter of a large pizza (in inches): ";
cin >> diameter_large;
cout << " Enter the price of a large pizza : $ ";
cin >> price_large;
unitprice_small = unitprice (diameter_small , price_small);
unitprice_large = unitprice( diameter_small ,price_large);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << " Small pizza:\n"
<< " Diameter = " << diameter_small << " inches\n"
<< " Price = $" << price_small
<< " Per square inch = $ " << unitprice_small << endl
<< " Large pizza :\n"
<< " Diameter = " << diameter_large << " inches\n"
<< " Price = $ " << price_large
<< " Per square inch = $ " << unitprice_large << endl ;
if(unitprice_large < unitprice_small)
{
cout << " The large one is the better buy .\n";
}
else
cout << " The small one is the better buy .\n";
cout << " Buon Appetio!\n";//意大利语:祝你有个好口胃
cin >> diameter_small ;
return 0 ;
}
//--------自定义函数------
double unitprice (int diameter ,double price)
{
const double PI = 3.14159;
double radius ,area ;
radius = diameter / static_cast<double>(2);//强制转换将2从int转换double
area = PI *radius * radius ;
return ( price / area);
}