page152

// 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);
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值