您的位置:首页 > 编程语言

过程抽象和返回一个值的函数(例子代码)

2010-12-04 00:13 435 查看
//3.1函数调用
//r根据用户的预算,计算可以购买多大的狗舍
//
#include<iostream>
#include<cmath>
#include<stdlib.h>
using namespace std;

int main()
{
const double COST_PER_SQ_FT = 10.50;
double budget,area,length_side;

cout << "Enter the amout budgeted for your dog house $";
cin >> budget;

area=budget/COST_PER_SQ_FT;
length_side=sqrt(area);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout<<"For a price of $"<<budget<<endl
<<"I can build you a luxurious square dog house/n"
<<"that is "<<length_side
<<" feet on each sibe./n";
system("pause");

return 0;
}


前些天一直不晓得cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); 是干什么用的,今天终于查到了:setf()是追加标志字的函数,而flags()是设置标志字;fixed标志是以定点形式显示浮点数;showpoint标志是强制显示小数点;precision就是精度,表示输出多少小数位。

——2010.12.4

// 3.3函数的定义
//

#include<iostream>
#include<stdlib.h>
using namespace std;

double total_cost(int number_par,double price_par);
//函数声明,计算以单价price_par购买number_par件商品的总价,含5%税销售

int main()
{
double price,bill;
int number;

cout<<"Enter the number of items purchased: ";
cin>>number;
cout<<"Enter the price per item $";
cin>>price;

bill=total_cost(number,price);		//函数调用

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout<<number<<" items at "
<<"$"<<price<<" each./n"
<<"Final bill,including tax,is $"<<bill
<<endl;

system("pause");

return 0;
}

double total_cost(int number_par,double price_par)
{
const double TAX_RATE=0.05;//5%税额
double subtotal;

subtotal=price_par*number_par;
return(subtotal+subtotal*TAX_RATE);
}


——2010.12.5

//3.5实参顺序有错
//判断用户的成绩,要么为Pass(及格),要么为Fail(不及格)

#include<iostream>
#include<stdlib.h>
using namespace std;

char grade(int received_par,int min_score_par);
//如果received_par大于或等于min_score_par,返回'P'表示及格;
//否则返回'F'表示不及格

int main()
{
int score,need_to_pass;
char letter_grade;

cout<<"Enter your score and the minimum needed to pass:/n";
cin>>score>>need_to_pass;
letter_grade=grade(need_to_pass,score);//注意形参顺序
cout<<"You received a score of "<<score<<endl
<<"Minimum to pass is "<<need_to_pass<<endl;
if(letter_grade=='P')
{
cout<<"You passed. Congratulation!/n";
}
else
{
cout<<"Sorry.You failed./n";
}
cout<<letter_grade
<<" will be entered in your record./n";

system("pause");
return 0;
}

char grade(int received_par,int min_score_par)
{
if(received_par>=min_score_par)
{
return 'P';
}
else
{
return 'F';
}
}


——2010.12.6

//3.9购买比萨
//判断两种比萨哪一种最适合购买
#include<iostream>
#include<stdlib.h>
using namespace std;

double unitprice(int diameter,double price);
//返回比萨每平方英寸的价格
//形参diameter是以英寸为单位的比萨直径,形参price是比萨的价格

int main()
{
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 pizz: $";
cin>>price_small;
cout<<"Enter diameter of a large pizza (in inches):";
cin>>diameter_large;
cout<<"Enter the price of a large pizz: $";
cin>>price_large;

unitprice_small=unitprice(diameter_small,price_small);
unitprice_large=unitprice(diameter_large,price_large);

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout<<"Small pizza:/n"
<<"Diameter= "<<diameter_small<<endl
<<"Price= $"<<price_small
<<" Per square inch= $"<<unitprice_small<<endl
<<"Large pizza:/n"
<<"Diameter= "<<diameter_large<<endl
<<"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 Appetito!/n";

system("pause");
return 0;
}

double unitprice(int diameter,double price)
{
const double PI=3.14159;
double radius,area;

radius=diameter/static_cast<double>(2); //强制类型转换保证结果精度和正确性
area=PI*radius*radius;

return(price/area);
}


//3.10局部变量
//计算一块豌豆试验田的平均收成
#include<iostream>
#include<stdlib.h>
using namespace std;

double est_total(int min_peas,int max_peas,int pod_count);
//返回预计收获的豌豆总数
//形参pod_count是豆荚数
//形参min_peas和max_peas是每个豆荚中的最少和最多豌豆数

int main()
{
int max_count,min_count,pod_count;
double average_pea,yield;
//这里average_pea为main的局部变量

cout<<"Enter minimum and maximum number of peas in a pod: ";
cin>>min_count>>max_count;
cout<<"Enter the number of pods: ";
cin>>pod_count;
cout<<"Enter the weight of an average pea (in ounces): ";
cin>>average_pea;

yield=est_total(min_count,max_count,pod_count)*average_pea;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout<<"Min number of peas per pod= "<<min_count<<endl
<<"Max number of peas per pod= "<<max_count<<endl
<<"Pod count = "<<pod_count<<endl
<<"Average pea weight= "
<<average_pea<<" ounces"<<endl
<<"Estimated average yield= "<<yield<<" ounces"<<endl;

system("pause");
return 0;
}

double est_total(int min_peas,int max_peas,int pod_count)
{
double average_pea;
//这里average_pea为est_total的局部变量

average_pea=(max_peas+min_peas)/2.0;
return(pod_count*average_pea);
}


——2010.12.8

//3.11一个全局命名常量
//计算一个圆的面积和一个球体的体积.
//两个计算使用同样的半径
#include<stdlib.h>
#include<iostream>
#include<cmath>
using namespace std;

const double PI=3.14159;

double area(double raqius);
//返回具有指定半径的一个圆的面积

double volume(double radius);
//返回具有指定半径的一个球的体积

int main()
{
double radius_of_both,area_of_circle,volume_of_sphere;

cout<<"Enter a radius to use for both a circle and a sphere(in inches): /n";
cin>>radius_of_both;

area_of_circle=area(radius_of_both);
volume_of_sphere=volume(radius_of_both);

cout<<"Radius= "<<radius_of_both<<" inches/n"
<<"Area of circle= "<<area_of_circle
<<" square inches/n" //圆的面积单位是平方英寸
<<"Volume of sphere= "<<volume_of_sphere
<<" cubic inches/n";//球体的体积单位是立方英寸

system("pause");
return 0;
}

double area(double radius)
{
return(PI*pow(radius,2));		//PI全局常量
}

double volume(double radius)
{
return((4.0/3.0)*PI*pow(radius,3));			//PI全局常量
}


——2010.12.9

//3.12形参作用局部变量
//律师事务所计费程序
#include<iostream>
#include<stdlib.h>
using namespace std;

const double RATE=150.00; //每刻钟收费

double fee(int hours_worked,int minutes_worked);
//返回hours_worked小时又minutes_worked分钟的律师费

int main()
{
int hours,minutes;
double bill;

cout<<"Welcome to the offices of Dewey,Cheatham,and Howe./n"
<<"The law office with a heart./n"
<<"Enter the hours and minutes of your consultation:/n";
cin>>hours>>minutes;

bill=fee(hours,minutes);

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout<<"For "<<hours<<" hours and "<<minutes//minutes的值没有被fee函数调用更改
<<" minutes,your bill is $"<<bill<<endl;

system("pause");
return 0;
}

double fee(int hours_worked,int minutes_worked)//minutes_worked被初始化为minutes的值
{
int quarter_hours;

minutes_worked=hours_worked*60+minutes_worked;
quarter_hours=minutes_worked/15;

return(quarter_hours*RATE);
}


//3.13使用命名空间
//计算一个圆的面积和一个球体的体积
//两个计算使用相同的半径
#include<iostream>
#include<stdlib.h>
#include<cmath>

const double PI=3.14159;

double area(double radius);
//返回具有指定半径的一个圆的面积

double volume(double radius);
//返回具有指定半径的一个球的体积

int main()
{
using namespace std;//局部使用

double radius_of_both,area_of_circle,volume_of_sphere;

cout<<"Enter a radius to use for both a circle and a sphere(in inches): /n";
cin>>radius_of_both;

area_of_circle=area(radius_of_both);
volume_of_sphere=volume(radius_of_both);

cout<<"Radius= "<<radius_of_both<<" inches /n"
<<"Area of circle= "<<area_of_circle<<" square inches/n"
<<"Volume of sphere= "<<volume_of_sphere<<" cubic inches/n";

system("pause");
return 0;

}

double area(double radius)
{
using namespace std;//局部使用

return(PI*pow(radius,2));
}

double volume(double radius)
{
using namespace std;//局部使用

return((4.0/3.0)*PI*pow(radius,3));
}


//3.15重载一个函数名称
//演示对函数名称ave的重载
#include<iostream>
#include<stdlib.h>

double ave(double n1,double n2);
//返回两个数字n1和n2的平均值

double ave(double n1,double n2,double n3);
//返回三个数字n1,n2,n3的平均值

int main()
{
using namespace std;
cout<<"The average of 2.0,2.5 and 3.0 is "
<<ave(2.0,2.5,3.0)<<endl;//这的ave()函数带3个参数
cout<<"The average of 4.5 and 5.5 is "
<<ave(4.5,5.5)<<endl;//这的ave()函数带2个参数

system("pause");
return 0;
}

double ave(double n1,double n2)//这的ave()函数带2个参数
{
return((n1+n2)/2.0);
}

double ave(double n1,double n2,double n3)//这的ave()函数带3个参数
{
return((n1+n2+n3)/3.0);
}


//3.16重载函数名称
//判断圆形比萨和矩形比萨,哪一种更适合购买
#include<iostream>
#include<stdlib.h>

double unitprice(int diameter,double price);
//返回圆形比萨每平方英寸的价格,形参diameter是以英寸为单位
//的比萨直径,形参price是比萨的价格

double unitprice(int length,int width,double price);
//返回矩形比萨每平方英寸的价格
//比萨的长度和宽度分别是length英寸和width英寸
//形参price是比萨的价格

int main()
{
using namespace std;
int diameter ,length,width;
double price_round,unit_price_round,price_rectangular,
unitprice_rectangular;

cout<<"Wlecome 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 of a rectangular pizza: ";
cin>>length>>width;
cout<<"Enter the price of a rectangular pizza: $";
cin>>price_rectangular;

//重载unitprice函数的使用
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 pizza: Diameter= "
<<diameter<<" inches/n"
<<"Price= $"<<price_round
<<" Per square inch= $"<<unit_price_round
<<endl
<<"Rectangular pizza: Length= "<<length<<" inches/n"
<<"Rectangular pizza: Width= "<<width<<" inches/n"
<<"Price= $"<<price_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";

system("pause");
return 0;

}
double unitprice(int diameter,double price)
{
const double PI=3.14159;
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);
}


——2010.1210

over this chaper!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐