1、 編寫一個程式,不斷要求使用者輸入兩個數,直到其中的一個為0.對于每兩個 數,程式将使用一個函數來計算它們的調和平均數,并将結果傳回給main(), 而後者将報告結果。調和平均數指的是倒數平均值的倒數,計算公式如下: 代碼
#include<iostream>
double avg(int x, int y);
int main()
{
using namespace std;
int a;
int b;
cout << "Enter two number:\n";
while((cin >> a >> b) && a != 0 && b != 0)
{
cout << "result: " << avg(a, b) << endl;
cout << "Enter next two number:\n";
}
cout << "Bey!\n";
return 0;
}
double avg(int x, int y)
{
double value = 2.0*x*y/(x+y);
return value;
}
運作結果
2、 編寫一個以,要求使用者輸入最多10個高爾夫成績, 并将其存儲在一個數組中。程式允許使用者提早結束輸入, 并在一行上顯示所有成績,然後報告平均成績。 請合3個數組處理函數來分别進行輸入,顯示和計算平均成績。 代碼
#include<iostream>
const int limit = 10;
int fill_array(double ar[], int limit);
double show_array(double ar[], int size);
void avg_array(double sum, int size);
int main()
{
using namespace std;
double ar[limit];
int size = fill_array(ar, limit);
double sum = show_array(ar, size);
avg_array(sum,size);
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:\n";
break;
} else if(temp < 0)
break;
ar[i] = temp;
}
return i;
}
double show_array(double ar[], int size)
{
using namespace std;
int i;
double sum;
cout << "所有成績: ";
for(i=0; i< size; i++)
{
sum += ar[i];
cout << ar[i] << " ";
}
return sum;
}
void avg_array(double sum, int size)
{
using namespace std;
cout << "平均值為: " << sum/size <<endl;
}
運作結果
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 show_box(box b);
void set_box(box *pb);
int main()
{
box b = {"Guugle Gu", 2, 3,5, 1};
show_box(b);
set_box(&b);
show_box(b);
return 0;
}
void show_box(box b)
{
using namespace std;
cout << "Box maker: " << b.maker <<endl;
cout << "Box height: " << b.height <<endl;
cout << "Box width: " << b.width <<endl;
cout << "Box length: " << b.length <<endl;
cout << "Box volume: " << b.volume <<endl;
}
void set_box(box *pb)
{
using namespace std;
pb->volume = pb->height * pb->width * pb->length;
}
運作結果
4、 許多州的彩票發行機構都使用如程式清單7.4所示的簡單彩票玩法的變體。 在這些玩法中,玩家從一組被稱為域号碼(field number)的号碼中選擇幾個。 例如,可以從域号碼1~47中選擇5個号碼:還可以從第二個間(如1~27) 中選擇一個号碼(稱為特選号碼)。要赢得頭等獎,必須正确猜中所有的号碼。 中頭獎的幾率是選中所有域号碼幾率與選中特選号碼幾率的乘積。 請修改程式清單7.4,以計算中得這種彩票頭獎的贓證。 代碼
#include<iostream>
const int n1=5;
const int num1 = 47;
const int n2=1;
const int num2 = 27;
long double odds(unsigned num, unsigned n);
int main()
{
using namespace std;
double odds1 = odds(num1, n1);
double odds2 = odds(num2, n2);
cout << "中頭獎的幾率為: " << odds1*odds2 <<endl;
return 0;
}
long double odds(unsigned num, unsigned n)
{
using namespace std;
long double res=1;
for(num, n; n>0; n--,num--)
res = res * n/num;
return res;
}
運作結果
5、 定義一個遞歸函數,接受一個整數參數,并傳回該參數的階乘。 在程式中對該函數進行測試,程式使用循環讓使用者輸入不同的值, 程式員将報告這些值的階乘。 代碼
#include<iostream>
int factorial(int n);
int main()
{
using namespace std;
int n;
cout << "Enter a number:";
cin >> n;
cout << factorial(n) <<endl ;
return 0;
}
int factorial(int n)
{
int res = 1;
if (n > 1)
res = n * factorial(n-1);
else
res = res * 1;
return res;
}
運作結果
6.編寫一個程式,它使用下列函數: Fill_array()将一個double數組的名稱和長度作為參數,它提示使用者輸入double值,并将這些值存儲到數組中。當數組被填滿或使用者輸入了非數字時,輸入将停止,并傳回實際輸入了多少個數字。 Show_array()将一個double 數組 的名稱和長度作為參數,并顯示該數組的内容。 Reverse-array() 将一個double數組的名稱和長度作為參數,并将存儲在數組中的值的順序 反轉。程式将使用這些函數來填充數組,然後顯示數組;反轉數組,然後 顯 示數組;反轉數組中除第一個和最後一個元素之外的所有元素,然後顯示數組。 代碼
#include<iostream>
int fill_array(double ar[], int limit);
void show_array(double ar[], int size);
void reverse_array(double ar[], int size);
using namespace std;
int main()
{
int n;
cout << "輸入數組的元素個數:";
cin >> n;
double ar[n];
int size = fill_array(ar, n);
show_array(ar, size);
reverse_array(ar, size);
cout << "---------反轉後結果----------\n";
show_array(ar, size);
return 0;
}
int fill_array(double ar[], int limit)
{
double temp;
int i;
for (i=0; i<limit; i++)
{
cout << "Enter value#" << (i+1) << " \n";
cin >> temp;
if (!cin)
{
cin.clear();
cout << "bad input:\n";
break;
}else
ar[i] = temp;
}
return i;
}
void show_array(double ar[], int size)
{
for(int i=0; i<size; i++)
cout << "ARR_RES" << (i+1) << ": " << ar[i] <<endl;;
}
void reverse_array(double ar[], int size)
{
int i,j;
for (i=1, j=size-i-1; i<j; i++, j--)
{
double temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
}
運作結果
9、 設計一個名為calculate()的函數,它接受兩個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()類似的數。 如果讀者愛冒險,可以嘗試建立一個指針數組,其中的指針指向add() 樣式的函數,并編寫一個循環,使用這些指針連續讓calculate()調用這些函數。 提示:下面是聲明這種指針數組的方式,其中包含3個指針: double (*pf[3]) (double,double); 可以采用數組初始化句法,并将函數名作為位址來初始化這樣的數組。 代碼
#include<iostream>
double add(double x, double y);
double sub(double x, double y);
double mean(double x, double y);
double calculate(double x, double y, double (*pf)(double, double));
int main()
{
using namespace std;
double (*pf[3])(double, double) = {add, sub, mean};
const char (*guugle[3]) = {"sum", "difference", "mean"};
double a,b;
cout << "Enter pairs of numbers (q to quit):";
int i;
while (cin >> a >> b)
{
cout << calculate(a, b, add) << "= sum\n";
cout << calculate(a, b, mean) << "= mean\n";
for(i=0; i<3 ; i++)
cout << calculate(a, b, pf[i]) << " = " << guugle[i] << "\n";
}
return 0;
}
double calculate(double x, double y, double (*pf)(double, double))
{
return (*pf)(x, y);
}
double add(double x, double y)
{
return x + y;
}
double sub(double x, double y)
{
return x - y;
}
double mean(double x, double y)
{
return (x+y)/2.0;
}
運作結果