1.编写一个程序,不断要求用户输入两个数,直到其中一个为0。对于每两个数,程序将使用一个函数来计算它的调和平均数,并将结果返回给main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下:
调和平均数 = 2.0 * x * y / (x + y)
#include
double count_ave(double a, double b)
{
double s;
s = 2.0 * a * b / (a + b);
return s;
}
int main()
{
using namespace std;
double x, y;
cin >> x >> y;
while (x && y)
{
double ave = count_ave(x, y);
cout << "The harmonic mean is: " << ave << endl;
cin >> x >> y;
}
return 0;
}
2.编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存入到一个数组中。程序允许用户提早结束输入,并在一行上显示所有成绩,然后报告平均成绩。请使用3个数组处理函数来分别进行输入、显示和计算平均成绩。
#include
int main()
{
using namespace std;
double input[10];
double average[10];
double display[10];
int i = 0;
double sum, ave;
sum = 0;
while (cin >> input[i] && i < 10)
{
i++;
}
for (int j = 0; j < i; j++)
average[j] = display[j] = input[j];
for (int j = 0; j < i; j++)
cout << average[j] << "、";
cout << endl;
for (int j = 0; j < i; j++)
sum += display[j];
cout << "Those numbers' average is: " << sum / i << endl;
return 0;
}
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
a. 编写一个函数,按值传递box结构,并显示每个成员的值。
b. 编写一个函数,传递box结构的地址,并将volume成员设置为其他三维长度的乘积。
c. 编写一个使用这两个函数的简单程序。
#include
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void display(box b)
{
using namespace std;
cout << "maker: " << b.maker << endl;
cout << "height: " << b.height << endl;
cout << "width: " << b.width << endl;
cout << "length: " << b.length << endl;
cout << "volume: " << b.volume << endl;
}
void change(box* b)
{
b->volume = b->height * b->width * b->length;
}
int main()
{
using namespace std;
box bo = { "information",1,10,100,1000 };
display(bo);
change(&bo);
display(bo);
return 0;
}
在这些玩法中,玩家从一组被称为域号码(field number)的号码中选择几个。例如,可以从域号码147中选择5个号码;还可以从第二个区间(如127)选择一个号码(称为特选号码)。要赢得头奖,必须正确猜中所有的号码。中头奖的几率是选中所有域号码的几率与选中特选号码几率的乘积。例如,在这个例子中,中头奖的几率是从47个号码中正确选取5个号码的几率与从27个号码中正确选择1个号码的几率的乘积。请修改程序清单7.4,以计算中得这种彩票头奖的几率。
#include
long double probability(unsigned numbers, unsigned picks)
{
long double result = 1.0;
long double n;
unsigned p;
for (n = numbers, p = picks; p > 0; n--, p--)
{
result = result * n / p;
}
return result;
}
int main()
{
using namespace std;
cout << probability(47, 5) * probability(27, 1) << endl;
return 0;
}
前面讲过,3的阶乘写作3!,等于32!,依此类推;而0!被定义为1。通用的计算公式是,如果n大于零,则n!=n(n-1)!。在程序中对该函数进行测试,程序使用循环让用户输入不同的值,程序将报告这些值的阶乘。
#include
int Calculate_Factorial(int a)
{
if (a == 1)
return 1;
else
return a * Calculate_Factorial(a - 1);
}
int main()
{
using namespace std;
int n;
cin >> n;
int count = Calculate_Factorial(n);
cout << count << endl;
return 0;
}
Fill_array()将一个double数组的名称和长度作为参数。它提示用户输入double值,并将这些值存储到数组中。当数组被填满或用户输入了非数字时,输入将停止,并返回实际输入了多少个数字。
Show_array()将一个double数组的名称和长度作为参数,并显示该数组的内容。
Reverse_array()将一个double数组的名称和长度作为参数,并将存储在数组中的值的顺序反转。
程序将使用这些函数来填充数组,然后显示数组;反转数组,然后显示数组;反转数组中除第一个和最后一个元素之外的所有元素,然后显示数组。
#include
const int SIZE = 10;
using namespace std;
int Fill_array(int* ar, int l)
{
int count = 0;
while (cin >> ar[count] && count < l)
{
count++;
}
return count;
}
void Show_array(int* ar, int l)
{
for (int i = 0; i < l; i++)
{
cout << ar[i] << " ";
}
cout << endl;
}
void Reverse_array(int* ar, int l)
{
const int* a = ar;//复制一份const 数组作为备用
for (int i = 0; i < l; i++)
{
ar[i] = a[l - i - 1];
}
}
int main()
{
int arr[10] = { 0 };
int lenth = Fill_array(arr, SIZE);//返回值lenth重新作为数组的长度
Show_array(arr, lenth);
Reverse_array(arr, lenth);
Show_array(arr, lenth);
return 0;
}
fill_array()函数不返回实际读取了多少个数字,而是返回一个指针,该指针指向最后被填充的位置;其他的函数可以将该指针作为第二个参数,以标识数据结尾。
#include
using namespace std;
const int Max = 5;
double* fill_array(double* pp,double* ppl)
{
double* LP = NULL;//指针指向空指针,避免指针未初始化造成野指针
int i = 0;
for (LP = pp; LP <= ppl; LP++)
{
cout << "Enter value #" << (i + 1) << ": ";
if (!(cin >> *LP))
{
cin.clear();
while (*LP != '\n')
continue;
cout << "Bad input: input process terminated. \n";
break;
}
i++;
}
return LP - 1;
}
void show_array(double* pp, double* ppl)
{
double* LP;
int i = 0;
for (LP = pp; LP <= ppl; LP++)
{
cout << "Property #" << (i + 1) << ": $";
cout << *LP << endl;
i++;
}
}
void revalue(double r, double* pp, double* ppl)
{
double* LP;
for (LP = pp; LP <= ppl; LP++)
{
(*LP) *= r;
}
}
int main()
{
double properties[Max];
double* Last_Point = fill_array(properties, properties + Max - 1);
show_array(properties, Last_Point);
cout << "Enter revaluation factor:";
double factor;
while (!(cin >> factor))
{
cin.clear();
while (cin.get() != '\n')
{
continue;
}
cout << "Bad input: input process terminated. \n";
}
revalue(factor,properties, Last_Point);
show_array(properties, Last_Point);
}
a. 使用const char*数组存储表示季度名称的字符串,并使用double数组存储开支。
#include
#include
using namespace std;
const int Season = 4;
const char* Snames[Season] = { "Spring","Summer","Fall","Winter" };
void fill(double* pa)
{
for (int i = 0; i < Season; i++)
{
cout << "Enter " << Snames[i] << " expense ";
cin >> pa[i];
}
}
void show(double da[], int n)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Season; i++)
{
cout << Snames[i] << ": $" << da[i] << endl;
total += da[i];
}
cout << "Total Expenses: $" << total << endl;
}
int main()
{
double expenses[Season];
fill(expenses);//传递地址
show(expenses,Season);//按值传递
return 0;
}
b. 使用const char*数组存储表示季度名称的字符串,并使用一个结构,该结构只有一个成员——一个用于存储开支的double数组。这种设计与使用array类的基本设计类似。
#include
#include
using namespace std;
const int Season = 4;
const char* Snames[Season] = { "Spring","Summer","Fall","Winter" };
struct Expenses
{
double expenses[Season];
};
void fill(Expenses* pa)
{
for (int i = 0; i < Season; i++)
{
cout << "Enter " << Snames[i] << " expense ";
cin >> pa->expenses[i];
}
}
void show(Expenses da)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Season; i++)
{
cout << Snames[i] << ": $" << da.expenses[i] << endl;
total += da.expenses[i];
}
cout << "Total Expenses: $" << total << endl;
}
int main()
{
Expenses expenses;;
fill(&expenses);//传递地址
show(expenses);//按值传递
return 0;
}
#include
using namespace std;
const int SLEN = 30;
struct student
{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
int getinfo(student pa[], int n)
{
int result = 0;
for (int i = 1; i <= n; i++)
{
cout << "student #" << i << ": " << endl;
cout << "fullname: ";
cin.getline(pa[i].fullname, SLEN);
if (pa[i].fullname[0] == '\0')
break;
cout << "hobby: ";
cin.getline(pa[i].hobby, SLEN);
cout << "ooplevel: ";
(cin >> pa[i].ooplevel).get();
++result;
}
cout << "Enter End.\n";
return result;
}
void display1(student st)
{
cout << "display1:" << endl;
cout << "fullname:" << st.fullname << " hobby:" << st.hobby << " ooplevel:" << st.ooplevel << endl;
}//按值传递,使用 . 运算符
void display2(const student* ps)
{
cout << "display2:" << endl;
cout << "fullname:" << ps->fullname << " hobby:" << ps->hobby << " ooplevel:" << ps->ooplevel << endl;
}//传递地址,使用 -> 运算符
void display3(const student pa[], int n)
{
cout << "display3:" << endl;
for (int i = 0; i < n; i++)
{
cout << "fullname:" << pa[i].fullname << " hobby:" << pa[i].hobby << " ooplevel:" << pa[i].ooplevel << endl;
}
}
int main()
{
cout << "ENter class size: ";
int class_size;
cin >> class_size;
while (cin.get() != '\n')
continue;
student* ptr_stu = new student[class_size];
int entered = getinfo(ptr_stu, class_size);
for (int i = 0; i < entered; i++)
{
display1(ptr_stu[i]);
display2(&ptr_stu[i]);
}
display3(ptr_stu, entered);
delete[] ptr_stu;
cout << "Done\n";
return 0;
}
例如,假设add()函数的定义如下:
double add(double x, double y)
{
return x + y;
}
则下述代码中的函数调用将导致calculate()把2.5和10.4传递给add()函数,并返回add()的返回值(12.9);
double q = calculate(2.5, 10.4, add);
请编写一个程序,它调用上述两个函数和至少另一个与add()类似的函数。该程序使用循环来让用户成对地输入数字。对于每对数字,程序都使用calculate()来调用add()和至少一个其他的函数。如果读者爱冒险,可以尝试创建一个指针数组,其中的指针指向add()样式的函数,并编写一个循环,使用这些指针连续让calculate()调用这些函数。提示:下面是声明这种指针数组的方式,其中包含三个指针:
double (pf[3])(double, double);
可以采用数组初始化语法,并将函数名作为地址来初始化这样的数组。
#include
using namespace std;
double add(double, double);
double multiply(double, double);
double calculate(double, double, double (*pf)(double, double));
const int SIZE = 2;
int main()
{
double (*pf[SIZE])(double, double) = { add, multiply };
double x, y;
cout << "Please enter two numbers : "
<< endl;
while (cin >> x >> y)
{
for (int i = 0; i < SIZE; i++)
{
double q = calculate(x, y, pf[i]);
cout << q << " ";
}
cout << "\nPlease enter two numbers : "
<< endl;
}
return 0;
}
double calculate(double x, double y, double (*pf)(double, double))
{
double result;
result = pf(x, y);
return result;
}
double add(double x, double y)
{
cout << "x + y = ";
return x + y;
}
double multiply(double x, double y)
{
cout <<"\n"<< "x * y = ";
return x * y;
}