您的位置:首页 > 编程语言 > C语言/C++

C++Primer Plus(第六版)第七章编程练习

2019-03-02 11:30 274 查看

好几道题没做,返回指针的函数写之前的写过就没再写一次了

#include <iostream>
using namespace std;
/*
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};

double harmonic_mean(double a,double b);
int input(double *a);*/
int Fill_array(double *a,int size);
void Show_array(double *a, int size);
void Reverse_array(double *a,int size);
/*void display(double *a,int size);
double ave(double *a,int size);
void display(box a);
void do_the_math(box *a);
int factorial(int a);*/
int main()
{
/*cout << "No.1"<<endl;
double i,j;
while(1)
{
cout << "input two numbers not '0':"<<endl;
cin >> i;
cin >> j;
if(i==0 || j==0)
{
cout << "end the loop";
break;
}

cout << "The harmonic mean =" <<harmonic_mean(i,j)<<endl;
}
cout << "No.2"<<endl;
double golf[10];
int size;
size = input(golf);
display(golf,size);
cout << "No.3"<<endl;
box one =
{
"Bone",10,10,10,50
};
display(one);
do_the_math(&one);
display(one);
cout << "No.5"<<endl;
int i;
while(1)
{cin >> i;
if(cin.fail())
{
cin.clear();
cout << "invalid input"<<endl;
break;
}
cout << "factorial "<<i<< "! = "<<factorial(i)<<endl;
}
cout << "No.6"<< endl;
double *a;
int size = 10;
size = Fill_array(a,size);
Reverse_array(a,size);
Show_array(a,size);*/
cout << "No.10" <<endl;

return 0;
}
/*
int factorial(int a)
{
if(a==0 || a == 1)
return 1;
else
{
return a*factorial(a-1);
}
}
double harmonic_mean(double a,double b)
{
double temp;
temp = 2.0*a*b/(a+b);
return temp;
}*/
/*int Fill_array(double *a,int size)
{

for(int i = 0; i<size ; i++)
{
cin >> a[i];
if(!cin)
{
cin.clear();
cout << "input failed"<<endl;
int s = i;
return s;
}
}
return size;
}
void Show_array(double *a,int size)
{
for(int i = 0; i<size ; i++)
{
cout << a[i]<<endl;
}
}
void Reverse_array(double *a,int size)
{
double * b = new double[size];
for(int i=0; i<size ;i++)
{
b[i] = a[size - i - 1];
}
for(int i = 0; i < size ; i++)
{
a[i] = b[i];
}
delete [] b;
}*/
/*int input(double *a)
{
cout << "input score of golf match:"<< endl;
for(int i = 0; i<10 ; i++)
{
cin >> a[i];
if(!cin)
{
cin.clear();
cout << "input failed"<<endl;
int size = i;
return size;
}
}
return 10;
}*/
/*
void display(double *a,int size)
{
for(int i = 0; i<size ; i++)
{
cout << a[i]<<endl;
}
cout << "average of score:" <<ave(a,size);
}
double ave(double *a,int size)
{
double temp = 0;
for(int i = 0 ; i<size ; i++)
{
temp += a[i];
}
temp /= size;
return temp;
}

void display(box a)
{
cout << a.maker<< endl;
cout << a.height<< endl;
cout << a.width << endl;
cout << a.length<< endl;
cout << a.volume<< endl;
}
void do_the_math(box *a)
{
a->volume = a->height * a->length * a->width;
}
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: