有利用周末时间复习了一遍第七章,做一下课后习题
1.编写一个程序,不断要求用户输入两个数,知道其中一个为0,对于每两个数将使用一个函数来计算他们的调和平均数,将结果返回给main(),而后者报告结果。
#include <iostream>
using namespace std;
double average(double,double);
int main()
{
double num1,num2;
cout<<"请输入两个数字"<<endl;
while(cin>>num1>>num2&&num1!=0&&num2!=0)
{
cout<<"调和平均数为:"<<endl;
cout<<average(num1,num2)<<endl;
cout<<"请输入两个数字:"<<endl;
}
return 0;
}
double average(double x,double y)
{
return 2.0*x*y/(x+y);
实验结果:
2.编写一个程序,要求用户输入最多10个高尔夫成绩,并将其储存在一个数组中。允许用户提早结束输入,并在一行上显示所有成绩,报告平均成绩。使用三个数组分别处理输入,显示,和计算平均成绩
#include <iostream>
using namespace std;
int input();
int display();
double average();
double arr[10];
int k=0;
int sum=0;
int main()
{
input();
display();
average();
return 0;
}
int input()
{
cout<<"输入-1则自动停止输入"<<endl;
for(int i=0;i<10;i++)
{
cin>>arr[i]; //使用数组的不是拷贝而是直接使用原数组
if(arr[i]==-1)
break;
k=k+1;
}
return k;
}
int display()
{
for(int i=0;i<10;i++)
{
cout<<arr[i]<<" ";
sum+=arr[i];
}
return sum;
}
double average()
{
double ave;
ave = sum / (k-1);
cout<<"平均成绩为:"<<ave<<endl;
}
实验结果:
3.编写一个函数,按值传递box结构,并显示每一个成员的值,使用传递Box结构的地址计算其乘积,编写一个使用上述两个函数的简单程序
#include <iostream>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
/**按值传递*/
void PrintBox(box a)
{
cout << "Box maker: " <<a.maker << endl;
cout << "Box height: " <<a.height << endl;
cout << "Box width: " <<a.width << endl;
cout << "Box length: " <<a.length << endl;
cout << "Box volume: " <<a.volume << endl;
}
/**运用指针*/
double CalBoxVolume(box *pbox)
{
pbox->volume = pbox->height * pbox->width * pbox->length;
cout<<"三维长度的乘积为:"<<pbox->volume<<endl;
}
int main()
{
struct box mbox = { "haha", 1.25, 3.0, 4.0, 0.0 };
PrintBox(mbox);
cout<<endl;
CalBoxVolume(&mbox);
return 0;
}
实验结果:
4.中奖几率是从47个号码汇总正确选取5个号码的几率与从27个号码中正确选取一个号码的几率的乘积。计算这种彩票头奖的几率。
#include <iostream>
using namespace std;
long double win_rate(unsigned number,unsigned pick);
int main()
{
long double a=win_rate(45,5);
long double b=win_rate(27,1);
cout<<"几率为"<<1/(a*b)<<endl;
return 0;
}
long double win_rate(unsigned number,unsigned pick)
{
long double result=1.0;
unsigned n,p;
for(n=number,p=pick;p>0;n--,p--)
result=result*n/p;
return result;
}
实验结果:
5.定义一个递归函数,接受一个整数参数,并返回该参数的阶乘。
#include<iostream>
using namespace std;
long int factorial(int n);//函数声明
int main()
{
int x;
long int ans;
cout << "Please enter the number: (q to quit)" << endl;
while(cin >> x)
cout << "The factorial is: "<< factorial(x) << endl;
cout << "Over." << endl;
return 0;
}
long int factorial(int n)
{
long int ans;
if (n >0)
ans = n * factorial(n-1); //使用递归
if (n == 0)
ans = 1;
return ans;
}
实验结果:
6.编写一个程序,使用书中所列的函数
#include <iostream>
using namespace std;
//函数声明
int Fill_array(double * p_arr, int n);
void Show_array(double * p_arr, int n);
void Reverse_array(double * p_arr, int bagin, int end, int n);
int main()
{
int n, size;
cout << "Please enter the number of array: " << endl;
cin >> size;
double * p_arr = new double[size];
n = Fill_array(p_arr, size);
Show_array(p_arr, n);
Reverse_array(p_arr, 0, n-1, n);
Show_array(p_arr, n);
Reverse_array(p_arr, 1, n-2, n);//除首尾元素外进行交换
Show_array(p_arr, n);
cout << "Over.\n";
return 0;
}
//填充函数
int Fill_array(double * p_arr, int n)
{
int count = 0;
cout << "Please enter the array: " << endl;
for(int i = 0; i < n && cin >> p_arr[i]; i++)//注意输入条件
{
count++;
}
return count;
}
//显示函数
void Show_array(double * p_arr, int n)
{
cout << "Now, the array is: " << endl;
for(int i = 0; i < n; i++)
{
cout << p_arr[i] << " ";
}
cout << endl;
}
//反转函数
void Reverse_array(double * p_arr, int begin, int end, int n)//注意参数起止
{
double t;//原来这里是int类型,经提醒后修改
for(int i = begin; i < n/2; i++)
{
t = p_arr[i];
p_arr[i] = p_arr[begin + end - i];//注意参数起止对交换的影响
p_arr[begin + end - i] = t;
}
}
实验结果:
Tips 对于数组来说最好使用new随机分配,double *p=arr[SIZE];
7.修改程序清单中的3个数组处理函数,使之使用两个两个指针参数来表示区间.
#include <iostream>
using namespace std;
const int Max = 5;
double * fill_array(double * p_begin, double * p_end);//如何用两个指针表示区间
void show_array(double * p_begin, double * p_end);//注意这里的类型要前后一致
void revalue(double r, double * p_begin, double * p_end);
int main()
{
double properties[Max];
double * pt = fill_array(properties, properties + Max);
show_array(properties, pt);
if (properties != pt)//当首尾地址不同,即存在区间时
{
cout << "Enter revaluation factor: ";
double factor;
while(!(cin >> factor))//输入错误时返回false值,取反执行该操作
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input: Please enter a number: ";
}
revalue(factor, properties, pt);
show_array(properties, pt);
}
cout << "Done.\n";
return 0;
}
//填充数组
double * fill_array(double * p_begin, double * p_end)
{
int i = 1;
double * pt;
for (pt = p_begin; pt != p_end; pt++, i++)
{
cout << "Enter value #" << i << ": ";
cin >> *pt;
//-----------输入非数字和负数的情况------------------------
if (!cin)
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input process terminated.\n";
break;
}
else if (*pt < 0)
break;
}
//-----------传递指针,注意这里传递的是地址,要用&----------
return &(*pt);
}
//显示数组
void show_array(double * p_begin, double * p_end)
{
int i = 1;
double * pt;
for (pt = p_begin; pt != p_end; pt++, i++)//注意这里用指针传递
{
cout << "Property #" << i << ": $";
cout << *pt << endl;
}
}
//修改数组
void revalue(double r, double * p_begin, double * p_end)
{
for (double * pt = p_begin; pt != p_end; pt++)
*pt *= r;
}
实验结果:
8.在不使用array类的情况下重写程序清单7.15所做的工作,编写两个版本
8.a
#include <iostream>
using namespace std;
const int Seasons = 4;
const char * p_season[Seasons] = {"Spring", "Summer", "Fall", "Winter"};
void fill(double * pa);
void show(double * da);
int main()
{
double expenses[Seasons];
fill(&expenses[Seasons]);//注意传递地址
show(&expenses[Seasons]);
return 0;
}
void fill(double * pa)
{
for(int i = 0; i < Seasons; i++)
{
cout << "Enter " << *(p_season + i) << " expenses: ";
cin >> pa[i];//注意这里pa[i]等价于*(pa+i),即数组名实际为一个指针
}
}
void show(double * da)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for(int i = 0; i < Seasons; i++)
{
cout << *(p_season + i) << ": $" << da[i]<< endl;
total += da[i];//解释同上
}
cout << "Total Expenses: $" << total << endl;
}
8. b
#include <iostream>
struct expenses
{
double money;
};//定义结构
using namespace std;
const int Seasons = 4;
const char * p_season[Seasons] = {"Spring", "Summer", "Fall", "Winter"};
void fill(double * pa);
void show(double * da);
int main()
{
expenses use[Seasons];//声明结构
fill(&use[Seasons].money);//注意传递地址
show(&use[Seasons].money);
return 0;
}
//------------调用函数不需要修改---------------
void fill(double * pa)
{
for(int i = 0; i < Seasons; i++)
{
cout << "Enter " << *(p_season + i) << " expenses: ";
cin >> *(pa+i);//注意这里pa[i]等价于*(pa+i),即数组名实际为一个指针
}
}
void show(double * da)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for(int i = 0; i < Seasons; i++)
{
cout << *(p_season + i) << ": $" << da[i]<< endl;
total += da[i];//解释同上
}
cout << "Total Expenses: $" << total << endl;
}
9.回头补上