practice 1.
#include <iostream>
#include <string>
using namespace std;
double avg(int x, int y);
int main()
{
int a;
int b;
cout << "enter int x and y: ";
while (cin >> a, cin>> b)
{
if (a!=0&&b!=0)
{
cout <<"the avg is:"<< avg(a, b) << endl;
}
else
{
break;
}
}
cout << "done.";
return 0;
}
double avg(int x, int y)
{
double avg_num;
avg_num = 2.0 * x * y / (x + y);
return avg_num;
}
practice 2。 参见程序清单7.7 的三个函数
#include <iostream>
#include <string>
using namespace std;
int fill_array(double ar[], int limit);
void show_array(const double ar[], int n);
double avg(const double ar[], int n);
int main()
{
double scores[10];
double avg_num = 0;
int size = fill_array(scores, 10);
show_array(scores, size);
avg_num = avg(scores, size);
cout << "the avareage number is:" << avg_num << endl;
getchar();
return 0;
}
int fill_array(double ar[], int limit)
{
double temp;
int i;
for (i = 0; i < limit; i++)
{
cout << "enter value #" << (i + 1) << ": " << endl;
cin >> temp;
if (!cin)//错误输入跳出for循环
{
cin.clear();
while (cin.get()!='\n')//输入不为换行继续执行for循环
{
continue;
}
cout << "quit input." << endl;
break;
}
else if(temp < 0)//输入为负数跳出for循环
{
break;
}
ar[i] = temp;
}
return i;
}
void show_array(const double ar[], int n)
{
cout << "the array is: ";
for (int i = 0; i < n; i++)
{
cout << ar[i] <<" ";
}
}
double avg(const double ar[], int n)
{
int sum = 0;
double avange = 0;
for (int i = 0; i < n; i++)
{
sum = sum + ar[i];
}
avange = sum / n;
cout << "size=" << n << endl;
return avange;
}
practice3 注意指针的表述格式
#include <iostream>
#include <string>
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
using namespace std;
float vol(struct box *pbox);
void show_para(box B1);
int main()
{
box box1 = { "gengzhiyang", 5,5,5 };
show_para(box1);
vol(&box1);
cout << "the volume is " << box1.volume << endl;
getchar();
return 0;
}
void show_para(box B1)
{
cout << "maker " << B1.maker << endl;
cout << "height " << B1.height << endl;
cout << "width " << B1.width << endl;
cout << "length " << B1.length << endl;
}
float vol(struct box *pbox)
{
pbox->volume = pbox->height * pbox->length * pbox->width;
return pbox->volume;
}
practice 5
#include <iostream>
#include <string>
using namespace std;
double factorial(int n);
int main()
{
int F = 0;
int temp = 0;
cout << "enter an integer: ";
cin >> temp;
if (temp > 0)
F = factorial(temp);
cout << "the temp is: " << temp << endl;
cout << "the factorial is: " << F <<endl;
getchar();
getchar();
return 0;
}
double factorial(int n)
{
if (n == 0||n == 1)
{
return 1;
}
else
return (n * factorial(n - 1));
}
practice 6
#include <iostream>
using namespace std;
const int para_limit = 10;
int fill_array(double ar[], int limit);
void show_array(const double ar[], int n);
void reverse_array(double ar[], int n);
int main()
{
int num = 0;
double array[para_limit];
num = fill_array(array, para_limit);
show_array(array, num);
reverse_array(array, num);
show_array(array, num);
getchar();
getchar();
return 0;
}
int fill_array(double ar[], int limit)
{
double temp;
int i;
for (i = 0; i < limit; i++)
{
cout << "enter value #" << (i + 1) << ": " << endl;
cin >> temp;
if (!cin)//错误输入跳出for循环
{
cin.clear();
while (cin.get() != '\n')//输入不为换行继续执行for循环
{
continue;
}
cout << "quit input." << endl;
break;
}
else if (temp < 0)//输入为负数跳出for循环
{
break;
}
ar[i] = temp;
}
return i;
}
void show_array(const double ar[], int n)
{
cout << "the array is: ";
for (int i = 0; i < n; i++)
{
cout << ar[i] << " ";
}
}
void reverse_array(double ar[], int n)
{
double mid;
for (int i = 1; i < (n+1) / 2; i++)
{
mid = ar[i];
ar[i] = ar[n - i];
ar[n - i] = mid;
}
}
practice 9 重点!!!!!!
#include <iostream>
using namespace std;
double calculate(double x, double y, double(*pt)(double, double));
double add(double x, double y);
int main()
{
char input;
double n1 = 0, n2 = 0;
cout << "please enter tow double, alpha to quit:";
while (cin >> n1 >> n2)
{
cout << calculate(n1, n2, add);
}
return 0;
}
double add(double x, double y)
{
return (x + y);
}
double calculate(double x, double y, double (*pt)(double, double))
{
return pt(x, y);
}