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;
}
則下述代碼的函數調用:
将導緻calculate()把2.5和10.4傳遞給add()函數,并傳回add()的傳回值(12.9)。
請編寫一個程式,它調用上述兩個函數和至少另一個與add()類似的函數。該程式使用循環來讓使用者成對地輸入數字。對于每對數字,程式都使用calculate()來調用add()和至少一個其他的函數。如果讀者愛冒險,可以嘗試建立一個指針數組,其中的指針指向add()樣式的函數,并編寫一個循環,使用這些指針連續讓calculate()調用這些函數。提示:下面是聲明這種指針數組的方式,其中包含3個指針:
可以采用數組初始化句法,并将函數名作為位址來初始化這樣的數組。
程式:
#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);
}