1
编写一个程序,不断要求用户输入两个数,直到其中的一个为0。对于每两个数,程序将使用一个函数来计算它们的调和平均数,并将结果返回给main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下:
调和平均数 = 2.0 * x * y / (x + y)
程序:
#include<iostream>
double th(double x, double y);
int main(void)
{
using namespace std;
double x, y;
cout << "Enter two numbers: ";
while (cin >> x >> y && x && y)
{
cout << "调和平均数为:" << th(x, y) << endl;
cout << "Enter two numbers: ";
}
cout << "Ok, bye!\n";
return 0;
}
double th(double x, double y)
{
return 2.0 * x * y / (x + y);
}
2
编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许用户提早结束输入,并在一行上显示所有成绩,然后报告平均成绩。请使用3个数组处理函数来分别进行输入、显示和计算平均成绩。
程序:
#include<iostream>
int in(double scores[]);
void out(const double scores[], int n);
double average(const double scores[], int n);
int main(void)
{
using namespace std;
double scores[10];
int n = in(scores);
out(scores, n);
double ave = average(scores, n);
cout << "Average is " << ave <<"."<< endl;
return 0;
}
int in(double scores[])
{
using namespace std;
int i = 0;
cout << "Enter score #" << i+1 << ": ";
while (cin >> scores[i] && i < 10)
{
i++;
cout << "Enter score #" << i+1 << ": ";
}
return i;
}
void out(const double scores[], int n)
{
using namespace std;
for (int i = 0; i < n; i++)
cout << scores[i] << " ";
cout << endl;
}
double average(const double scores[], int n)
{
double sum = 0;
for (int i = 0; i < n; i++)
sum += scores[i];
return sum / n;
}
3
下面是一个结构声明:
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
a.编写一个函数,按值传递box结构,并显示每个成员的值。
b.编写一个函数,传递box结构的地址,并将volume成员设置为其他三位长度的乘积。
c.编写一个使用这两个函数的简单程序。
程序:
#include<iostream>
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void showbox(box x);
void calv(box* x);
int main(void)
{
using namespace std;
box x;
cout << "Enter the maker: ";
cin.getline(x.maker, 40);
cout << "Enter the height: ";
cin >> x.height;
cout << "Enter the width: ";
cin >> x.width;
cout << "Enter the length: ";
cin >> x.length;
calv(&x);
showbox(x);
return 0;
}
void showbox(box x)
{
using namespace std;
cout << "maker: " << x.maker << endl;
cout << "height: " << x.height << endl;
cout << "width: " << x.width << endl;
cout << "length: " << x.length << endl;
cout << "volume: " << x.volume << endl;
}
void calv(box* x)
{
x->volume = x->height * x->length * x->width;
}
4
许多州的彩票发行机构都使用如程序清单7.4所示的简单彩票玩法的变体。在这些玩法中,玩家从一组被称为域号码(field number)的号码中选择几个。例如,可以从域号码1-47中选择5个号码;还可以从第二个区间(如1-27)中选择一个号码(称为特选号码)。要赢得头奖,必须正确猜中所有的号码。中头奖的几率是选中所有域号码的几率与选中特选号码几率的乘积。例如,在这个例子中,中头奖的几率是从47个号码中正确选取5个号码的几率与从27个号码中正确选择1个号码的几率的乘积。请修改程序清单7.4,以计算中得这种彩票头奖的几率。
程序清单7.4:
#include<iostream>
long double probability(unsigned numbers, unsigned picks);
int main(void)
{
using namespace std;
double total, choices;
cout << "Enter the total number of choices on the game card and\n"
"the number of picks allowed:\n";
while ((cin >> total >> choices) && choices <= total)
{
cout << "You have one chance in ";
cout << probability(total, choices);
cout << " of winning.\n";
cout << "Next two numbers (q to quit): ";
}
cout << "Bye!\n";
return 0;
}
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;
}
程序:
#include<iostream>
long double probability(unsigned numbers, unsigned picks);
int main(void)
{
using namespace std;
cout << "You have one chance in ";
cout << probability(47, 5) * probability(27, 1);
cout << " of winning.\n";
cout << "Bye!\n";
return 0;
}
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;
}
5
定义一个递归函数,接受一个整数参数,并返回该参数的阶乘。前面讲过,3的阶乘写作3!,等于3 * 2!,依此类推;而0!被定义为1。通用的计算公式是,如果n大于零,则n! = n * (n - 1)!。在程序中对该函数进行测试,程序使用循环让用户输入不同的值,程序将报告这些值的阶乘。
程序:
#include<iostream>
long factorial(int n);
int main(void)
{
using namespace std;
int n;
cout << "Enter a number: ";
while (cin >> n)
{
if (n < 0)
cout << "Enable, enter another number: ";
else if (n == 0)
cout << "1\nEnter a number: ";
else
cout << factorial(n) << endl << "Enter a number: ";
}
cout << "Ok, bye!\n";
return 0;
}
long factorial(int n)
{
if (n > 1)
{
return n * factorial(n - 1);
}
else
return 1;
}
6
编写一个程序,它使用下列函数:
Fill_array()将一个double数组的名称和长度作为参数。它提示用户输入double值,并将这些值存储到数组中。当数组被填满或用户输入了非数字时,输入将停止,并返回实际输入了多少个数字。
Show_array()将一个double数组的名称和长度作为参数,并显示该数组的内容。
Reverse_array()将一个double数组的名称和长度作为参数,并将存储在数组中的值的顺序反转。
程序将使用这些函数来填充数组,然后显示数组;反转数组,然后显示数组;反转数组中除第一个和最后一个元素之外的所有元素,然后显示数组。
程序:
#include<iostream>
int Fill_array(double arr[], int length);
void Show_array(const double arr[], int length);
void Reverse_array(double arr[], int length);
int main(void)
{
using namespace std;
int length;
double arr[10];
length = Fill_array(arr, 10);
cout << "Show array:\n";
Show_array(arr, length);
cout << "Reverse array:\n";
Reverse_array(arr, length);
Show_array(arr, length);
return 0;
}
int Fill_array(double arr[], int length)
{
using namespace std;
int i = 0;
cout << "Enter number #" << i + 1 << ": ";
while (cin >> arr[i] && i < length)
{
i++;
cout << "Enter number #" << i + 1 << ": ";
}
return i;
}
void Show_array(const double arr[], int length)
{
using namespace std;
for (int i = 0; i < length; i++)
cout << arr[i] << endl;
}
void Reverse_array(double arr[], int length)
{
double temp;
for (int i = 0; i < (length/2); i++)
{
temp = arr[length - 1 - i];
arr[length - 1 - i] = arr[i];
arr[i] = temp;
}
}
7
修改程序清单7.7中的3个数组处理函数,使之使用两个指针参数来表示区间。fill_array()函数不返回实际读取了多少个数字,而是返回一个指针,该指针指向最后被填充的位置;其他的函数可以将该指针作为第二个参数,以标识数据结尾。
程序清单7.7:
#include<iostream>
const int Max = 5;
int fill_array(double ar[], int limit);
void show_array(const double ar[], int n);
void revalue(double r, double ar[], int n);
int main(void)
{
using namespace std;
double properties[Max];
int size = fill_array(properties, Max);
show_array(properties, size);
cout << "Enter revalueation factor: ";
double factor;
cin >> factor;
revalue(factor, properties, size);
show_array(properties, size);
cout << "Done.\n";
return 0;
}
int fill_array(double ar[], int limit)
{
using namespace std;
double temp;
int i;
for (i = 0; i < limit; i++)
{
cout << "Enter value #" << (i + 1) << ": ";
cin >> temp;
if (!cin)
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input: input process terminated.\n";
break;
}
else if (temp < 0)
break;
ar[i] = temp;
}
return i;
}
void show_array(const double ar[], int n)
{
using namespace std;
for (int i = 0; i < n; i++)
{
cout << "Property #" << (i + 1) << ": $";
cout << ar[i] << endl;
}
}
void revalue(double r, double ar[], int n)
{
for (int i = 0; i < n; i++)
ar[i] *= r;
}
程序:
#include<iostream>
const int Max = 5;
double* fill_array(double *begin, int limit);
void show_array(const double* begin, const double* end);
void revalue(double r, double* begin, double* end);
int main(void)
{
using namespace std;
double properties[Max];
double* end;
end = fill_array(properties, Max);
show_array(properties, end);
cout << "Enter revalueation factor: ";
double factor;
cin >> factor;
revalue(factor, properties, end);
show_array(properties, end);
cout << "Done.\n";
return 0;
}
double* fill_array(double *begin, int limit)
{
using namespace std;
double tem;
double * temp;
int i;
for (temp = begin,i = 0; i < limit; i++,temp++)
{
cout << "Enter value #" << (i + 1) << ": ";
cin >> tem;
if (!cin)
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input: input process terminated.\n";
break;
}
else if (tem < 0)
break;
temp[i] = tem;
}
return temp;
}
void show_array(const double* begin, const double* end)
{
using namespace std;
const double* temp;
int i;
for (temp = begin, i = 0; temp != end; temp++, i++)
{
cout << "Property #" << (i + 1) << ": $";
cout << temp[i] << endl;
}
}
void revalue(double r, double* begin, double* end)
{
double* temp;
int i;
for (temp = begin, i = 0; temp != end; i++, temp++)
temp[i] *= r;
}
8
这个练习让您编写处理数组和结构的函数。下面是程序的框架,请提供其中描述的函数,以完成该程序。
程序框架:
#include<iostream>
using namespace std;
const int SLEN = 30;
struct student {
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
// getinfo() has two arguments: a pointer to the first element of
// an array of student structures and an int repesenting the
// number of the array. The function solicits and
// stores data about students.It terminates input upon filling
// the array or upon encountering a blank line for the student
// name. The function returns the actual number of array elements
// filled.
int getinfo(student pa[], int n);
// display1() takes a student structure as an argument
// and displays its contents
void display1(student st);
// display2() takes the address of student structure as an
// argument and displays the structures's contents
void display2(const student* ps);
// display3() takes the address of the first element of an array
// of student structures and the number of array elements as
// arguments and displays the contents of the structures
void display3(const student* pa, int n);
int main(void)
{
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;
}
程序:
#include<iostream>
using namespace std;
const int SLEN = 30;
struct student {
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
int getinfo(student pa[], int n);
void display1(student st);
void display2(const student* ps);
void display3(const student* pa, int n);
int main(void)
{
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;
}
int getinfo(student pa[], int n)
{
int i;
for (i = 0; i < n; i++)
{
cout << "Student #" << (i + 1) << ": " << endl;
cout << "fullname: ";
cin.getline(pa[i].fullname, 30);
cout << "hobby: ";
cin.getline(pa[i].hobby, 30);
cout << "ooplevel: ";
cin >> pa[i].ooplevel;
cin.get();
}
return i;
}
void display1(student st)
{
cout << st.fullname << ", " << st.hobby << ", " << st.ooplevel << endl;
}
void display2(const student* ps)
{
cout << ps->fullname << ", " << ps->hobby << ", " << ps->ooplevel << endl;
}
void display3(const student* pa, int n)
{
for (int i = 0; i < n; i++)
cout << pa[i].fullname << ", " << pa[i].hobby << ", " << pa[i].ooplevel << endl;
}
9
设计一个名为clculate()的函数,它接受两个double值和一个指向函数的指针,而被指向的函数接受两个double参数,并返回一个double值。calculate()函数的类型也是double,并返回被指向的函数使用calculate()的两个double参数计算得到的值。例如,假设add()函数的定义如下:
double add(double x, double y)
{
return x + y;
}
则下述代码的函数调用:
double q = calculate(2.5, 10.4, add);
将导致calculate()把2.5和10.4传递给add()函数,并返回add()的返回值(12.9)。
请编写一个程序,它调用上述两个函数和至少另一个与add()类似的函数。该程序使用循环来让用户成对地输入数字。对于每对数字,程序都使用calculate()来调用add()和至少一个其他的函数。如果读者爱冒险,可以尝试创建一个指针数组,其中的指针指向add()样式的函数,并编写一个循环,使用这些指针连续让calculate()调用这些函数。提示:下面是声明这种指针数组的方式,其中包含3个指针:
double(*pf[3])(double, double);
可以采用数组初始化句法,并将函数名作为地址来初始化这样的数组。
程序:
#include<iostream>
double add(double x, double y);
double multiply(double x, double y);
double subtract(double x, double y);
double calculate(double x, double y, double(*pf)(double, double));
int main(void)
{
using namespace std;
double x, y;
cout << "Enter two numbers: ";
while (cin >> x >> y)
{
cout << "x + y = " << calculate(x, y, add) << endl;
cout << "x - y = " << calculate(x, y, subtract) << endl;
cout << "x * y = " << calculate(x, y, multiply) << endl;
cout << "Enter another two numbers: ";
}
return 0;
}
double add(double x, double y)
{
return x + y;
}
double multiply(double x, double y)
{
return x * y;
}
double subtract(double x, double y)
{
return x - y;
}
double calculate(double x, double y, double(*pf)(double, double))
{
return pf(x, y);
}
程序(使用指针数组存储函数名):
#include<iostream>
double add(double x, double y);
double multiply(double x, double y);
double subtract(double x, double y);
double calculate(double x, double y, double(*pf)(double, double));
int main(void)
{
using namespace std;
double x, y;
double(*pf[3])(double, double) = { add,subtract,multiply };
int i;
cout << "Enter two numbers: ";
while (cin >> x >> y)
{
for (i = 0; i < 3; i++)
{
cout << calculate(x, y, pf[i]) << endl;
}
cout << "\n";
cout << "Enter another two numbers: ";
}
return 0;
}
double add(double x, double y)
{
return x + y;
}
double multiply(double x, double y)
{
return x * y;
}
double subtract(double x, double y)
{
return x - y;
}
double calculate(double x, double y, double(*pf)(double, double))
{
return pf(x, y);
}