练习代码为本人所写,欢迎大家学习交流
若有疑问请留言,我将尽快回复!!!!
题7.1:
#include <iostream>
#include <ctime>
using namespace std;
double Harmonic(double a, double b);
int main()
{
double x, y;
cout << "Please enter two double numble: ";
cin >> x >> y;
cout << "The Harmonic average of x and y: " << Harmonic(x, y);
//以下是时延和回车读取,防止看不到结果
clock_t delay = 10 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
double Harmonic(double a, double b)
{
double Harmonic_average = 2.0 * a * b / (a + b);
return Harmonic_average;
}
题7.2:
#include <iostream>
#include <ctime>
using namespace std;
const int MAX = 10;
int fill_score(double ar[], int limit);
void show(double ar[], int n);
int main()
{
double score_golf[MAX];
int size = fill_score(score_golf, MAX);
show(score_golf, size);
//以下是时延和回车读取,防止看不到结果
clock_t delay = 10 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
int fill_score(double ar[], int limit)
{
double temp;
int i = 0;
for (i = 0; i < limit; i++)
{
cout << "Enter the " << i + 1 << " score: ";
cin >> temp;
if (!cin || temp < 0)
break;
ar[i] = temp;
}
return i; //注意这里返回的就是个数
}
void show(double ar[], int n)
{
double sum = 0;
double average_score;
if (n == 0)
cout << "there is no score!!!!!";
else
{
for (int i = 0; i < n; i++)
{
cout << ar[i] << " ";
sum += ar[i];
}
average_score = sum / n;
cout << endl << "average of score: " << average_score;
}
}
题7.3:
#include <iostream>
#include <ctime>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volum;
};
void show(box t);
void set_volum(box * ps);
int main()
{
box box1={"Jiaqing Huang", 2, 3, 4};
show(box1); //记住直接用结构是不能改变内容的
set_volum(&box1); //用指针可以改变其中的结构
//以下是时延和回车读取,防止看不到结果
clock_t delay = 10 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
void show(box t)
{
cout << "maker: " << t.maker << " || height: " << t.height << " || width: " << t.width << " || length: " << t.length << endl;
}
void set_volum(box * ps)
{
ps->volum = ps->height * ps->width * ps->length;
cout << "volum: " << ps->volum;
}
题7.4:
#include <iostream>
#include <ctime>
using namespace std;
long double probability(int numb_normal_field, int pick, int numb_special_field);
int main()
{
int numb_normal_field_your, pick_your, numb_special_field_your;
cout << "Please enter the number of normal field, number picked, number of special field: ";
while (cin >> numb_normal_field_your >> pick_your >> numb_special_field_your)
{
cout << "You have ";
cout << probability(numb_normal_field_your, pick_your, numb_special_field_your) * 100 ;
cout << "% chance of winning!!";
}
//以下是时延和回车读取,防止看不到结果
clock_t delay = 10 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
long double probability(int numb_normal_field, int pick, int numb_special_field)
{
int n, p;
long double result = 1.0;
for (n = numb_normal_field, p = pick; p > 0; n--, p--)
result = result * p / n;
result = result / numb_special_field;
return result;
}
题7.5:
#include <iostream>
#include <ctime>
using namespace std;
int factorial(int n);
int main()
{
int numb;
cout << "Enter a number: ";
cin >> numb;
cout << "The factorial of " << numb << ": " << factorial(numb);
//以下是时延和回车读取,防止看不到结果
clock_t delay = 10 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
int factorial(int n)
{
if (n == 1 || n == 0)
return 1;
else
return n * factorial(n-1);
}
题7.6:
#include <iostream>
#include <ctime>
using namespace std;
const int MAX = 10;
int Fill_array(double ar[], int limit);
void Show_array(double ar[], int n);
void Reverse_array(double ar[], int n);
int main()
{
double numb[MAX];
cout << "Input:" << endl;
int size = Fill_array(numb, MAX);
cout << "Show:" << endl;
Show_array(numb, size);
cout << "Reverse and show:" << endl;
Reverse_array(numb, size);
Show_array(numb, size);
cout << "Reverse and show(In addition to the first and last number):" << endl;
Reverse_array(numb + 1, size - 2);
Show_array(numb, size);
//以下是时延和回车读取,防止看不到结果
clock_t delay = 50 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
int Fill_array(double ar[], int limit) //数组是可以直接改变的,不会使用副本(因为数组名原理上是指针)
{
int i;
for (i = 0; i < limit; i++)
{
double temp;
cout << "Enter the " << i + 1 << " double number: ";
cin >> temp;
if (!cin)
break;
ar[i] = temp;
}
return i;
}
void Show_array(double ar[], int n)
{
for (int i = 0; i < n; i++)
{
cout << ar[i] << " ";
}
cout << endl;
}
void Reverse_array(double ar[], int n)
{
double temp;
for (int i = 0; i < (n / 2); i++)
{
temp = ar[(n-1)-i];
ar[(n-1)-i] = ar[i];
ar[i] = temp;
}
}
题7.7:
#include <iostream>
#include <ctime>
using namespace std;
const int MAX = 10;
double * Fill_array(double ar[], int limit);
void Show_array(double ar[], double * pt);
void Reverse_array(double r, double ar[], double * pt);
int main()
{
double numb[MAX];
cout << "Input:" << endl;
double *p_end = Fill_array(numb, MAX);
cout << "Show:" << endl;
Show_array(numb, p_end);
cout << "* r and show:" << endl;
Reverse_array(0.5, numb, p_end);
Show_array(numb, p_end);
//以下是时延和回车读取,防止看不到结果
clock_t delay = 50 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
double * Fill_array(double ar[], int limit)
{
int i;
for (i = 0; i < limit; i++)
{
double temp;
cout << "Enter the " << i + 1 << " double number: ";
cin >> temp;
if (!cin)
break;
ar[i] = temp;
}
return &ar[i - 1];
}
void Show_array(double ar[], double * pt)
{
for (double * ps = &ar[0]; ps <= pt; ps++)
{
cout << *ps << " ";
}
cout << endl;
}
void Reverse_array(double r, double ar[], double * pt)
{
for (double * ps = &ar[0]; ps <= pt; ps++)
{
*ps *= r;
}
}
题7.8:
#include <iostream>
#include <ctime>
using namespace std;
const int Seasons = 4;
const char *Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" }; //相当于是把四 个字符串的首地址传给了sname
struct family
{
double expenses_b[Seasons];
};
void fill_a(double *pa);
void show_a(double *pa);
void fill_b(family *pb);
void show_b(family *pb);
int main()
{
cout << "练习a: " << endl;
double expenses_a[Seasons] = {0};
fill_a(expenses_a);
show_a(expenses_a);
cout << "---------------------------------------------------" << endl;
cout << "练习b: " << endl;
family my_family;
fill_b(&my_family); //记住用指针才可以改变结构变量,直接用结构名是不行的
show_b(&my_family);
//以下是时延和回车读取,防止看不到结果
clock_t delay = 50 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
void fill_a(double *pa)
{
for (int i = 0; i < Seasons; i++)
{
cout << "Enter the expenses of " << Snames[i] << " : ";
cin >> pa[i];
}
}
void show_a(double *pa)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++)
{
cout << Snames[i] << ": $" << pa[i] << endl;
total += pa[i];
}
cout << "Total Expenses: $" << total << endl;
}
void fill_b(family *pb)
{
for (int i = 0; i < Seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> pb->expenses_b[i];
}
}
void show_b(family *pb)
{
double total = 0.0;
cout << "\nEXPENSE\n";
for (int i = 0; i < Seasons; i++)
{
cout << Snames[i] << ": $" << pb->expenses_b[i] << endl;
total += pb->expenses_b[i];
}
cout << "Total Expense: $" << total << endl;
}
题7.9:
#include <iostream>
#include <ctime>
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(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;
}
student * ptr_stu = new student[class_size];
int entered = getinfo(ptr_stu, class_size);
for (int i = 0; i < entered; i++)
{
cout << "Student " << i + 1 << " :-------------------------------------------" << endl;
display1(ptr_stu[i]);
display2(&ptr_stu[i]);
}
display3(ptr_stu, entered);
delete [] ptr_stu;
cout << "DONE" << endl;
//以下是时延和回车读取,防止看不到结果
clock_t delay = 50 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
int getinfo(student pa[], int n)
{
int i;
for (i = 0; i < n; i++)
{
cout << "Enter the information of the " << i + 1 << "student!" << endl;
cout << "Fullname: ";
cin.getline(pa[i].fullname, SLEN);
cout << "Hobby: ";
cin.getline(pa[i].hobby, SLEN);
cout << "Ooplevel: ";
cin >> pa[i].ooplevel;
cin.get(); //记住在输入数字之后一定要把回车给读掉才能下一步读入一行
}
return i;
}
void display1(student st)
{
cout << "Display1--------------" << endl;
cout << "Fullname: " << st.fullname << endl;
cout << "Hobby: " << st.hobby << endl;
cout << "Ooplevel: " << st.ooplevel << endl;
}
void display2(student * ps)
{
cout << "Display2--------------" << endl;
cout << "Fullname: " << ps->fullname << endl;
cout << "Hobby: " << ps->hobby << endl;
cout << "Ooplevel: " << ps->ooplevel << endl;
}
void display3(const student pa[], int n)
{
cout << "Display3********************************************************" << endl;
for (int i = 0; i < n; i++)
{
cout << "Student " << i + 1 << " :----------------" << endl;
cout << "Fullname: " << pa[i].fullname << endl;
cout << "Hobby: " << pa[i].hobby << endl;
cout << "Ooplevel: " << pa[i].ooplevel << endl;
}
}
题7.10:
#include <iostream>
#include <ctime>
using namespace std;
double add(double a, double b);
double sub(double a, double b);
double mul(double a, double b);
double div(double a, double b);
double calculate(double a, double b, double (*fun)(double, double));
int main()
{
double x, y;
char function;
cout << "Enter two numbers and the function of calculate:" << endl;
cout << "A:add B:sub C:mul D:div" << endl;
while (cin >> x >> y >> function)
{
switch (function)
{
case 'A': cout << "Calculation results: " << calculate(x, y, add) << endl;
break;
case 'B': cout << "Calculation results: " << calculate(x, y, sub) << endl;
break;
case 'C': cout << "Calculation results: " << calculate(x, y, mul) << endl;
break;
case 'D': cout << "Calculation results: " << calculate(x, y, div) << endl;
break;
default : cout << "This is not a choice! Enter again" << endl;
}
}
//以下是时延和回车读取,防止看不到结果
clock_t delay = 50 * CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start <delay)
;
cin.get();
cin.get();
return 0;
}
double add(double a, double b)
{
return a + b;
}
double sub(double a, double b)
{
return a - b;
}
double mul(double a, double b)
{
return a * b;
}
double div(double a, double b)
{
return a / b;
}
double calculate(double a, double b, double (*fun)(double, double))
{
return fun(a, b);
}