7.12 复习题
1.先声明函数,再进行函数的定义,最后是调用函数使用
2.a.void igor();
b.float tofu(int b);
c.double mpg(double c1, double c2);
d.long summation(long d[], int length);
e.double doctor(const string e);
f.void ofcourse(boss f);
g.string plot(map *g);
3.
void seam(int arr[], int length, int value) {
for (int i = 0; i < length; i++) {
arr[i] = value;
}
}
4.
void set_val(int* first, int* last, int value) {
for (int* p = first; p != last; p++) {
*p = value;
}
}
5.
int get_max(double arr[], int length) {
int max = arr[0];
for (int i = 1; i < length; i++) {
if (max < arr[i]) {
max = arr[i];
}
}
return max;
}
6.因为基本类型的函数参数是以值的形式传递,在调用函数的过程中不会修改原数据的值,但是指针代表的是地址传递,需要const来进行保护,防止原数据被修改。
8.
int replace(char* str, char c1, char c2) {
int flag = 0;
while (*str) {
if (*str == c1) {
*str = c2;
flag++;
}
str++;
}
return flag;
}
9.a.*"pizza"="p";
b."taco"[2]="c";
11.
int judge(int (*back)(const char *ch)) {
}
12.
void print_app(applicant app) {
cout << app.name << endl;
for (int i = 0; i < 3; i++) {
cout << app.credit_rating[i] << endl;
}
}
void print_appl(applicant* app) {
cout << app->name << endl;
for (int i = 0; i < 3; i++) {
cout << app->credit_rating[i] << endl;
}
}
13.
void f1(applicant * a);
const char* f2(const applicant * a1, const applicant * a2);
typedef void (*p_f1)(applicant*);
typedef const char* (*p_f2)(const applicant*, const applicant*);
p_f1 p1 = f1;
p_f2 p2 = f2;
p_f1 ap[5];
p_f2(*pa)[10];
7.13 编程练习
1.
#include<iostream>
using namespace std;
int main() {
//1.
double average(double x, double y);
double x, y;
while (1) {
cout << "Please enter two num:";
cin >> x;
cin >> y;
if (x == 0 || y == 0) break;
cout << average(x, y) << endl;
}
system("pause");
return 0;
}
double average(double x, double y) {
double avrg;
avrg = 2 * x * y / (x + y);
return avrg;
}
2.
#include<iostream>
using namespace std;
const int Size = 10;
int main() {
//2.
int enter_s(double arr[], int size);
void print_s(double arr[], int size);
double average_s(double arr[], int size);
double arr[Size];
int size = enter_s(arr, Size);
print_s(arr, size);
average_s(arr, size);
system("pause");
return 0;
}
int enter_s(double arr[], int size) {
int i = 0;
while(i<size) {
cout << "Please enter the score: ";
cin >> arr[i++];
cout << "if you want to be the end,please enter a 's':";
cin.get();
if (cin.get() == 's') {
cout << "End!" << endl;
break;
}
}
return i;
}
void print_s(double arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
double average_s(double arr[], int size) {
double average = 0;
for (int i = 0; i < size; i++) {
average += arr[i];
}
cout << average / size << endl;
return average;
}
3.
#include<iostream>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
int main() {
//3.
void print_all_box(box demo3);
void print_box(box * d3);
box demo3 = { "sdhj",10,10,5 };
print_all_box(demo3);
print_box(&demo3);
system("pause");
return 0;
}
void print_all_box(box demo3) {
cout << demo3.maker << endl;
cout << demo3.height << endl;
cout << demo3.width << endl;
cout << demo3.length << endl;
cout << demo3.volume << endl;
}
void print_box(box* d3) {
cout << d3->maker << endl;
cout << d3->height << endl;
cout << d3->width << endl;
cout << d3->length << endl;
d3->volume = d3->height * d3->length * d3->width;
cout << d3->volume << endl;
}
4.
#include<iostream>
using namespace std;
long double probability(unsigned field_number, unsigned special_number, unsigned picks);
int main() {
//4.
double field_number, special_number, choices;
cout << "Enter the total number of choices on the game card and\n"
"the number of picks allowed:\n";
while ((cin >> field_number >> special_number >> choices) && choices <= field_number) {
cout << "You have one choice in ";
cout << probability(field_number, special_number, choices);
cout << "of winning.\n";
cout << "Next three numbers(q to quit): ";
}
cout << "bye!" << endl;
system("pause");
return 0;
}
long double probability(unsigned field_number, unsigned special_number, unsigned picks) {
long double result = 1.0;
long double fn, sn;
unsigned p;
for (fn = field_number, sn = special_number, p = picks; p > 0; fn--, p--) {
result = result * fn / p;
}
result = result / sn;
return result;
}
5.
#include<iostream>
using namespace std;
int main() {
int Factorial(int number);
int num;
cout << "Please enter a number:";
cin >> num;
cout << "The " << num << "! = ";
cout << Factorial(num) << endl;
system("pause");
return 0;
}
int Factorial(int number) {
int count = 1;
while (number != 0) {
count = count * (number--);
}
return count;
}
6.
#include<iostream>
using namespace std;
double num[] = { 0 };
int main() {
//6.(有bug)
int Fill_array(double* num, int length);
void Show_array(double* num, int length);
int length = 5;
cout << "all: " << Fill_array(num, length) << endl;
Show_array(num, length);
system("pause");
return 0;
}
int Fill_array(double* num, int length) {
int flag = 0;
double number;
cout << "Please enter a number: ";
while ((cin >> number) && flag < length) {
*num = number;
num++;
cout << "Enter the next number(q or quit): ";
}
return flag;
}
void Show_array(double* num, int length) {
for (int i = 0; i < length; i++) {
cout << *num << endl;
num++;
}
}
7.
#include<iostream>
using namespace std;
const int Max = 5;
double* fill_array(double* first, double* end);
void show_array(double* first, double* end);
void revalue(double r, double* first, double* end);
int main() {
//7.
double properties[Max];
double* size = fill_array(properties, properties + Max);
show_array(properties, size);
if (size - properties > 0) {
cout << "Enter revalution factor: ";
double factor;
while (!(cin >> factor)) {
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input process terminated." << endl;
}
revalue(factor, properties, size);
show_array(properties, size);
}
cout << "Done." << endl;
cin.get();
cin.get();
system("pause");
return 0;
}
double* fill_array(double* first, double* end) {
double temp;
double* i;
int j = 0;
for (i = first; i < end; i++,j++) {
cout << "Enter value #" << (j + 1) << ":";
cin >> temp;
if (!cin) {
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input process terminated." << endl;
break;;
}
else if (temp < 0)
break;
*i = temp;
}
return i;
}
void show_array(double* first, double* end) {
double* i;
int j = 0;
for (i = first; i < end; i++,j++) {
cout << "Property #" << (j + 1) << ": $";
cout << *i++ << endl;
}
}
void revalue(double r, double* first, double* end) {
double* i;
for (i = first; i < end; i++) {
*i *= r;
}
}
8.
#include<iostream>
using namespace std;
const int Seasons = 4;
const char* Snames[] = {
"Spring","Summer","Fall","Winter"
};
void fill(double* pa);
void show(double da);
int main() {
//8.
double expenses;
fill(&expenses);
show(expenses);
system("pause");
return 0;
}
void fill(double* pa) {
for (int i = 0; i < Seasons; i++) {
cout << "Enter " << Snames[i] << " expenses: ";
cin >> *pa++;
}
}
void show(double da[]) {
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++) {
cout << Snames[i] << ": $" << da[i] << endl;
total += da[i];
}
cout << "Total Expenses: $" << total << endl;
}