#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;
}
{
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;
}
#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;
}
#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;
}
#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;
}
}
{
return x + y;
}
則下述代碼中的函數調用:
double q = calculate(2.5,10.4,add);
將導致calculate()把2.5和10.4傳遞給add()函數,並返回add()的返回值(12.9).請編寫一個程序,它調用上述兩個函數和至少另一個與add()類似的數。如果讀者愛冒險,可以嘗試創建一個指針數組,其中的指針指向add()樣式的函數,並編寫一個循環,使用這些指針連續讓calculate()調用這些函數。
#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;
}