第七章:函数——C++的编程模块
一、复习题
1. 第一,声明函数原型;第二,定义函数;第三,调用函数
2. a. void igor();
b. float tofu(int);
c. double mpg(double,double);
d. long summation(long [],int);
e. double doctor(const char*);
f. void ofcourse(boss);
g. char* plot(map*);
3. void func(int arr[],int len,int n){
for(int i = 0; i < len; i++){
arr[i] = n;
}
}
4. void func(int* begin,int* end,int n){
for(;begin != end;begin++){
*begin = n;
}
}
5. double func(const double arr[],int len){
int max = arr[0];
for(int i = 1;i<len;i++){
max = max < arr[i] ? arr[i] : max;
}
return max;
}
6. 因为函数参数为基本类型时,函数中形参的修改并不会影响实参,所以const在此时并无太大意义。
7. char str[10] ; char * ;
8. { int i = 0;
int count = 0;
while(str[i] != '\0'){
if(str[i] == c1){
str[i] = c2;
count++;
}
}
return count;
}
9. 字符p;一个存放两个字符指针的数组
10.(struct glitz);(struct* glitz);传址的效率更高,但同时会带来修改原结构的风险。
11.int judge(int (*)(const char*));
12.a. void func(applicant a){
cout << a.name << endl;
for(int i = 0;i < 3;i++){
cout << a.credit_ratings[i] << endl;
}
}
b. void func(applicant* a){
cout << a->name << endl;
for(int i = 0;i < 3;i++){
cout << a->credit_ratings[i] << endl;
}
}
13.typedef void (*p)(applicant*) p_f1;
typedef const char* (*p)(const applicant*,const applicant*) p_f2;
p_f1 p1 = f1;
p_f2 p2 = f2;
p_f1 ap[5];
p_f2 (*pa)[10];
二、编程练习
1.
#include <iostream>
using namespace std;
double avg(double, double);
int main() {
cout << "Enter 2 nums (0 to quit)" << endl;
double x, y;
while (cin >> x >> y) {
if (x != 0 && y != 0) {
cout << "the avg is " << avg(x, y) << endl;
} else {
break;
}
cout << "Enter 2 nums (0 to quit)" << endl;
}
return 0;
}
double avg(double x, double y) {
return 2 * x * y / (x + y);
}
2.
#include <iostream>
using namespace std;
const int Size = 10;
int enter(int arr[]) {
int i = 0;
cout << "Enter #1: ";
while (cin >> arr[i] && i < Size) {
cout << "Enter #" << (++i + 1) << ": ";
}
return i;
}
void show(int arr[], int len) {
for (int i = 0; i < len; i++) {
cout << arr[i] << endl;
}
}
double avg(int arr[], int len) {
double avg;
for (int i = 0; i < len; i++) {
avg += (double) arr[i] / len;
}
return avg;
}
int main() {
int score[Size];
int len = enter(score);
show(score, len);
cout << avg(score, len) << endl;
return 0;
}
3.
#include <iostream>
using namespace std;
struct box {
char maker[40];
float height;
float width;
float length;
float volume;
};
void show(box b) {
cout << b.maker << endl;
cout << b.height << endl;
cout << b.width << endl;
cout << b.length << endl;
cout << b.volume << endl;
}
void set(box *pb) {
pb->volume = pb->length * pb->width * pb->height;
}
int main() {
box b = {"David", 10, 20, 30};
show(b);
set(&b);
show(b);
return 0;
}
4.
#include <iostream>
using namespace std;
long double cal(unsigned num, unsigned pick) {
long double res = 1.0;
long double n = num;
unsigned p = pick;
for (; p > 0; p--, n--) {
res *= n / p;
}
return res;
}
int main() {
double total, choices;
cout << "Enter the total number of the 1st time and the num you pick";
while (cin >> total >> choices && choices <= total) {
cout << "the probability is " << cal(total, choices) << endl;
cout << "Enter the total number of the 2nd time";
if (cin >> total) {
cout << "the probability is " << cal(total, 1) << endl;
}
cout << "Enter the total number of the 1st time and the num you pick";
}
return 0;
}
5.
#include <iostream>
using namespace std;
int fac(int num) {
if (num > 1) {
return num * fac(num - 1);
}
return 1;
}
int main() {
cout << "Enter a num plz : ";
int num;
cin >> num;
cout << num << "! = " << fac(num) << endl;
}
6.
#include <iostream>
using namespace std;
const int Size = 10;
int Fill_array(double arr[], int len) {
int i = 0;
cout << "Enter #1: ";
while (cin >> arr[i] && i < len) {
cout << "Enter #" << (++i + 1) << ": ";
}
return i;
}
void Show_array(double arr[], int len) {
for (int i = 0; i < len; i++) {
cout << arr[i] << endl;
}
}
void Reverse_array(double arr[], int len) {
if (len % 2 == 0) {
for (int i = 1; i < len / 2; i++) {
double temp = arr[i];
arr[i] = arr[len - i - 1];
arr[len - i - 1] = temp;
}
} else {
for (int i = 1; i < (len - 1) / 2; i++) {
double temp = arr[i];
arr[i] = arr[len - i - 1];
arr[len - i - 1] = temp;
}
}
}
int main() {
double arr[Size];
int len = Fill_array(arr, Size);
Show_array(arr, len);
Reverse_array(arr, len);
Show_array(arr, len);
return 0;
}
7.
#include <iostream>
using namespace std;
const int Size = 10;
double *Fill_array(double arr[], double *len) {
int i = 0;
cout << "Enter #1: ";
while (cin >> arr[i] && arr + i != len) {
cout << "Enter #" << (++i + 1) << ": ";
}
return arr + i;
}
void Show_array(double arr[], double *len) {
for (int i = 0; arr + i != len; i++) {
cout << arr[i] << endl;
}
}
void Reverse_array(double arr[], double *len) {
int length = len - arr;
if (length % 2 == 0) {
for (int i = 1; i < length / 2; i++) {
double temp = arr[i];
arr[i] = arr[length - i - 1];
arr[length - i - 1] = temp;
}
} else {
for (int i = 1; i < (length - 1) / 2; i++) {
double temp = arr[i];
arr[i] = arr[length - i - 1];
arr[length - i - 1] = temp;
}
}
}
int main() {
double arr[Size];
double *len = Fill_array(arr, &arr[Size - 1]);
Show_array(arr, len);
Reverse_array(arr, len);
Show_array(arr, len);
return 0;
}
8.
#include <iostream>
using namespace std;
const int Seasons = 4;
const char *Snames[4] = {"Spring", "Summer", "Fall", "Winter"};
double expenses[4]{};
void fill(double expense[]) {
for (int i = 0; i < Seasons; i++) {
cout << "Enter " << Snames[i] << " expenses: ";
cin >> expense[i];
}
}
void show(double expense[]) {
double total = 0.0;
for (int i = 0; i < Seasons; i++) {
cout << Snames[i] << " expenses: " << expense[i] << endl;
total += expense[i];
}
cout << "total : " << total;
}
int main() {
fill(expenses);
show(expenses);
return 0;
}
#include <iostream>
using namespace std;
const int Seasons = 4;
const char *Snames[4] = {"Spring", "Summer", "Fall", "Winter"};
struct expense{
double expenses[4]{};
};
expense pense;
void fill(double expense[]) {
for (int i = 0; i < Seasons; i++) {
cout << "Enter " << Snames[i] << " expenses: ";
cin >> expense[i];
}
}
void show(double expense[]) {
double total = 0.0;
for (int i = 0; i < Seasons; i++) {
cout << Snames[i] << " expenses: " << expense[i] << endl;
total += expense[i];
}
cout << "total : " << total;
}
int main() {
fill(pense.expenses);
show(pense.expenses);
return 0;
}
9.
#include <iostream>
#include <string>
using namespace std;
const int SLEN = 30;
struct student {
char fullnmae[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() {
cout << "Enter class size: ";
int class_size;
cin >> class_size;
while (cin.get() != '\n')
continue;
auto *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;
return 0;
}
int getinfo(student pa[], int n) {
int i = 0;
cout << "Enter the name: " << endl;
cout << "Enter the hobby: " << endl;
cout << "Enter the level: " << endl;
while (cin >> pa[i].fullnmae &&
cin >> pa[i].hobby &&
cin >> pa[i].ooplevel) {
i++;
cout << "Enter the name: " << endl;
cout << "Enter the hobby: " << endl;
cout << "Enter the level: " << endl;
}
return i + 1;
}
void display1(student st) {
cout << st.fullnmae << endl;
cout << st.hobby << endl;
cout << st.ooplevel << endl;
}
void display2(const student *st) {
cout << st->fullnmae << endl;
cout << st->hobby << endl;
cout << st->ooplevel << endl;
}
void display3(const student pa[], int n) {
for (int i = 0; i < n; i++) {
cout << pa[i].fullnmae << endl;
cout << pa[i].hobby << endl;
cout << pa[i].ooplevel << endl;
}
}
10.
#include <iostream>
using namespace std;
double add(double x, double y) {
return x + y;
}
double sub(double x, double y) {
return x - y;
}
double calculate(double x, double y, double (*p)(double, double)) {
return p(x, y);
}
int main() {
double a = 10.2;
double b = 20.1;
double (*p[2])(double, double) = {add, sub};
for (int i = 0; i < 2; i++) {
cout << p[i](a, b) << endl;
}
return 0;
}