第一题
要求:不断输入成对数字,直到其中一个为0,得到调和平均数,并报告结果。
//7.1
#include<iostream>
using namespace std;
double m_average(double m1, double m2);
int main()
{
double m1, m2;
double average;
while ((cin >> m1 >> m2) && (m1 != 0 && m2 != 0))
{
average = m_average(m1, m2);
cout << "the two numbers' average is " << average << endl;
}
cout << "Done\n";
return 0;
}
double m_average(double m1, double m2)
{
return 2.0 * m1 * m2 / (m1 + m2);
}
第二题
要求:最多输入10个高尔夫成绩,显示所有成绩,报告平均成绩,三个函数输入,显示,计算平均值。
//7.2
#include<iostream>
using namespace std;
const int Size = 10;
int grade_number(double grade[],int size);
void show_grade(const double grade[], int size);
double average(const double grade[], int size);
int main()
{
cout << "Enter the golf grade: \n";
double Grade[Size];//定义初始数组
int count=grade_number(Grade,Size);//得到新数组内的成员个数
if (count == 0)
cout << "Enter Warning.\n";
else
{
show_grade(Grade, count);
double Average = average(Grade, count);
}
cout << "Bye.\n";
return 0;
}
int grade_number(double grade[],int size)
{
double p;
int i;
for (i = 0; i < size; i++)
{
cin >> p;
if (!cin)
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Enter end.\n";
break;
}
else if (p< 0)
break;
grade[i] = p;
}
return i;
}
void show_grade(const double grade[], int size)
{
cout << "Show the golf grade :\n";
for (int i = 0; i < size; i++)
cout << grade[i] << endl;
}
double average(const double grade[], int size)
{
double total = 0;
cout << "The average is ";
for (int i = 0; i < size; i++)
total += grade[i];
cout << total / size << endl;
return total / size;
}
第三题
要求:显示结构内容,按值传递结构;传递结构地址,改变结构成员内容;
//7.3
#include<iostream>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void enter_stru(box* m, int n);
void show_stru(const box* m,int n);
void change_v(box* m,int n);
int main()
{
cout << "Enter the box number : \n";
int n;
cin >> n;
box* pr = new box[n];
enter_stru(pr, n);
show_stru(pr,n);
change_v(pr,n);
cout << "\n\nAfter the correction : ";
show_stru(pr, n);
delete[]pr;
return 0;
}
void enter_stru(box* m, int n)
{
int i;
for (i = 0; i < n; i++)
{
cout << "Please enter the box #" << i + 1
<< " massage :\n"<<"maker: ";
cin >> m[i].maker;
cout << "\nheight: "; cin >> m[i].height;
cout << "\nwidth: "; cin >> m[i].width;
cout << "\nlength: "; cin >> m[i].length;
cout << "\nvolume: "; cin >> m[i].volume;
}
}
void show_stru(const box *m,int n)
{
for (int i = 0; i < n; i++)
{
cout << "\nthe box #" << i + 1 << " massage :\n"
<< "maker: " << m[i].maker
<< "\nheight: " << m[i].height
<< "\nwidth: " << m[i].width
<< "\nlength: " << m[i].length
<< "\nvolume: " << m[i].volume;
}
}
void change_v(box* m, int n)
{
for (int i = 0; i < n; i++)
{
m[i].volume = m[i].height * m[i].length * m[i].width;
}
}
第四题
要求:分别用函数计算两个几率的大小,然后两者相乘。
//7.4
#include<iostream>
using namespace std;
double m_a(unsigned a, unsigned b);
int main()
{
cout << "enter two number :\n";
unsigned a, b;
double c_1, c_2;
if ((cin >> a >> b) && (a > b))
{
c_1 = m_a(a, b);
cout << "enter another number:\n";
if ((cin >> a >> b) && (a > b))
{
c_2 = m_a(a, b);
cout << "probability : 1/" << c_1 * c_2 << endl;
}
else
cout << "The enter is woring!\n";
}
else
cout << "The enter is woring!\n";
cout << "Done!";
return 0;
}
double m_a(unsigned a, unsigned b)
{
double m=1;
unsigned a1, a2;
for (a1 = a, a2 = b; a2 > 0; a1--, a2--)
m = m * a1 / a2;
return m;
}
第五题
要求:用递归算法,算出输入数字的阶乘,并输出。
//7.5
#include<iostream>
using namespace std;
long m_a(int a);
long j_a(int a);
int main()
{
cout << "enter the factorial number : \n";
int m;
long fa;
while (cin >> m&&(m>=0))
{
fa = m_a(m);
long sum = j_a(m);
cout << m << "! = " << fa << endl;
cout << m << " sum = " << sum << endl;
cout << "enter next number :\n(or quit ,enter character or negative)\n";
}
cout << "Done\n";
return 0;
}
long m_a(int a)
{
long fa=a;
if (a > 1)
fa = fa * m_a(a - 1);
else
return fa=1;
}
long j_a(int a)
{
int fa = a;
if (a > 0)
fa = fa + j_a(a - 1);
else
return fa;
}
第六题
要求:用三个函数输入,显示,以及转换数组内容顺序。
//7.6
#include<iostream>
using namespace std;
const int Size = 10;
int Fill_array(double * pr, int size);
void Show_array(const double* pr, int size);
void Reverse_array(double* pr, int size);
int main()
{
double arr_d[Size];
int m = Fill_array(arr_d, Size);
Show_array(arr_d, m);
cout << "前后顺序变化\n";
Reverse_array(arr_d, m);
Show_array(arr_d, m);
cout << "首尾不变" << endl;
Reverse_array(arr_d +1, m-2 );
Show_array(arr_d, m);
return 0;
}
int Fill_array(double* pr, int size)
{
cout << "Enter the double number:\n";
double m;
int i;
for (i = 0; i < size; i++)
{
cin >> m;
if (!cin)
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "enter end.\n";
break;
}
pr[i] = m;
}
return i;
}
void Show_array(const double* pr, int size)
{
for (int i = 0; i < size; i++)
cout << &pr[i] << ": " << pr[i] << endl;
}
void Reverse_array(double* pr, int size)
{
double a;
for (int i = 0; i < size; size--,i++)
{
a = pr[i];
pr[i] = pr[size-1];
pr[size-1] = a;
}
}
第七题
要求:用“超尾”法来重写程序7.7;
//7.7
#include<iostream>
using namespace std;
const int Size = 5;
double* Full_array(double ar[], int Size);
void Show_array(double ar[], double*pr);
void revalue(double r,double ar[], double* pr);
int main()
{
double ar[Size];
double* pe = Full_array(ar, Size);
Show_array(ar, pe);
double r;
cout << "Enter the revalue\n";
cin >> r;
revalue(r,ar, pe);
Show_array(ar, pe);
return 0;
}
double* Full_array(double ar[], int Size)
{
double* p;
double d;
int i = 0;
for ( i = 0; i < Size; i++)
{
cin >> d;
if (!cin)
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Enter end.\n";
break;
}
else if (d < 0)
break;
ar[i] = d;
}
p = &ar[i];//这其实是i=Size;即末尾数加一
return p;
}
void Show_array(double ar[], double* pe)
{
for (int i = 0; &ar[i] != pe; i++)
{
cout << i + 1 << "# money :$ " << ar[i]<<endl;
}
}
void revalue(double r,double ar[], double* pe)
{
for (int i = 0; &ar[i] != pe; i++)
ar[i] = r * ar[i];
}
第八题
要求:使用数组以及结构的形式对程序进行两次重写。
//7.8
#include<iostream>
#include<string>
const char* Sname[4] =
{ "Spring","Summer","Fall","Winter" };
void fill( double []);
void show(double []);
using namespace std;
struct Expenses
{
double searon_P[4];
};
//结构函数原型
void fill_s(Expenses*);
void show_s(Expenses*);
int main()
{
double expenses[4];
fill(expenses);
show(expenses);
//
Expenses *ps =new Expenses;
fill_s(ps);
show_s(ps);
delete ps;
return 0;
}
void fill(double expenses[])
{
for (int i = 0; i < 4; i++)
{
cout << "reason: " << Sname[i] << endl
<< "expenses: ";
cin >> expenses[i];
}
}
void show(double expenses[])
{
double total = 0;
for (int i = 0; i < 4; i++)
{
total += expenses[i];
}
cout << "all expenses : " << total<<endl;
}
//结构函数
void fill_s(Expenses *ep)
{
for (int i = 0; i < 4; i++)
{
cout << "reason: " << Sname[i] << endl
<< "expenses: ";
cin >>ep->searon_P[i];
}
}
void show_s(Expenses *ep)
{
double total = 0;
for (int i = 0; i < 4; i++)
{
total += ep->searon_P[i];
}
cout << "all expenses : " << total << endl;
}
第九题
要求:按照已有程序框架,将四个函数内容补充完整。
//7.9
#include<iostream>
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(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;
student* 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;
cout << "Done\n";
return 0;
}
int getinfo(student pa[], int n)
{
int i = 0;
for (i = 0; i < n; i++)
{
cout << "Student #" << i + 1 << endl
<<"Name\tooplevel\n";
cin >> pa[i].fullname >> pa[i].ooplevel;
if (!cin )
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Enter end!\n";
break;
}
cout << "Hobby:\n";
cin >> pa[i].hobby;
}
return (i-1);
}
void display1(student ptr)
{
cout << "\nname: " << ptr.fullname
<< "\nhobby: " << ptr.hobby
<< "\nooplevel: " << ptr.ooplevel << endl;
}
void display2(const student* ptr)
{
cout << "name: " << ptr->fullname
<< " hobby: " << ptr->hobby
<< " ooplevel: " << ptr->ooplevel << endl;
}
void display3(const student ptr[], int n)
{
for (int i = 0; i <n; i++)
{
cout << "\nname: " << ptr[i].fullname
<< "\nhobby: " << ptr[i].hobby
<< "\nooplevel: " << ptr[i].ooplevel << endl;
}
}
第十题
要求:用函数指针来调用函数并且声明一个指向函数的指针数组。
//7.10
#include<iostream>
using namespace std;
double add(double, double);
double pdd(double, double);
double calculate(double, double, double(*)(double,double));
int main()
{
double a, b;
while(cin >> a >> b)
{
cout << "a*b: \n";
double pm=calculate(a, b, pdd);
cout <<pm<< "\na+b: \n";
double am=calculate(a, b, add);
cout << am << endl;
}
int i;
double(*ap)(double, double) = add;
double(*ap1)(double, double) = pdd;
double(*ap2)(double, double, double (*) (double, double)) = calculate;
double(*app[2])(double,double) = { add,pdd };
for (i = 0; i < 2; i++)
{
cout << (&(*app[i]))(3,4) <<" = "<< (*(*app[i]))(3,4) << endl;
}
}
double add(double a, double b)
{
return a + b;
}
double pdd(double a, double b)
{
return a * b;
}
double calculate(double a, double b, double abb(double,double))
{
return abb(a, b);
}