第七章 函数——C++的编程模板
7.12 编程练习
#include <iostream>
using namespace std;
inline double TAve(double x,double y)
{
double t_ave = 0;
t_ave = 2.0 * x * y /(x + y);
return t_ave;
}
int main()
{
double x;
double y;
while(cin>>x>>y && x !=0 && y != 0)
{
cout<<TAve(x,y)<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
int storeScores(int* a)
{
int i = 0;
while(cin>>a[i])
{
i++;
}
return i;
}
void printScores(int* a,int n)
{
for(int i = 0;i < n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
double aveScores(int* a,int n)
{
double temp = 0;
for(int i = 0;i < n;i++)
{
temp += a[i];
}
temp /= n;
return temp;
}
int main()
{
int a[10];
int n = storeScores(a);
printScores(a,n);
cout<<aveScores(a,n)<<endl;
return 0;
}
答: a.
#include <iostream>
#include <cstring>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void printbox(char* maker,float height,float width,float length,float volume)
{
box box2;
strcpy(box2.maker,maker);
cout<<box2.maker<<endl;
cout<<height<<endl;
cout<<width<<endl;
cout<<length<<endl;
cout<<volume<<endl;
}
int main()
{
box box1 = {"maker1",1.1,1.2,1.3,5};
printbox(box1.maker,box1.height,box1.width,box1.length,box1.volume);
return 0;
}
b.
#include <iostream>
#include <cstring>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void printbox(box* box2)
{
cout<<box2->maker<<endl;
cout<<box2->height<<endl;
cout<<box2->width<<endl;
cout<<box2->length<<endl;
cout<<box2->volume<<endl;
}
int main()
{
box box1 = {"maker1",1.1,1.2,1.3,5};
printbox(&box1); //传递的类型一定是一个地址
return 0;
}
c.
#include <iostream>
#include <cstring>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void printbox1(box* box2)
{
cout<<box2->maker<<endl;
cout<<box2->height<<endl;
cout<<box2->width<<endl;
cout<<box2->length<<endl;
cout<<box2->volume<<endl;
}
void printbox2(char* maker,float height,float width,float length,float volume)
{
box box2;
strcpy(box2.maker,maker);
box2.height = height;
box2.width = width;
box2.length = length;
box2.volume = volume;
cout<<box2.maker<<endl;
cout<<box2.height<<endl;
cout<<box2.width<<endl;
cout<<box2.length<<endl;
cout<<box2.volume<<endl;
}
int main()
{
box box1 = {"maker1",1.1,1.2,1.3,5};
printbox1(&box1);
printbox2(box1.maker,box1.height,box1.width,box1.length,box1.volume);
return 0;
}
这里涉及到排列组合的知识,简单的回顾一下:
排列: 就是按照顺序排列出出多少种组合。
组合:就是无序的选出一些数看有多少种结果。
另外需要知道的几个等式:
#include <iostream>
using namespace std;
int main()
{
int n1 = 47,m1 = 5;
int n2 = 27,m2 = 1;
long double per = 1;
for(int i = n1;i >= n1 - m1 + 1;i--)
{
per *= i;
cout<<per<<endl;
}
for(int i = n2;i >= n2 - m2 + 1;i--)
{
per *= i;
}
per = 1/per;
cout<<per<<endl;
return 0;
}
- 非递归方式:
#include <iostream>
using namespace std;
int main()
{
int jie = 1;
int n;
cin>>n;
if(n == 0)
cout<<1;
if(n > 0)
{
for(int i = n;i >= 1;i--)
{
jie *= i;
}
}
cout<<jie;
return 0;
}
- 递归方式:
#include <iostream>
using namespace std;
int Jiecheng(int n)
{
if(n == 0)
return 1;
else if(n > 0)
return n * Jiecheng(n-1);
}
int main()
{
int jie = 1;
int n;
cin>>n;
cout<<Jiecheng(n);
return 0;
}
#include <iostream>
using namespace std;
int Fill_array(double* arr,int n)
{
cout<<"请输入double型值:"<<endl;
int i;
for(i = 0;cin>>arr[i] && i < n;i++)
{
;
}
return i;
}
void Show_array(double* arr,int n)
{
int temp;
for(int i = n - 1,j = 0;i >= n/2;i--,j++)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
for(int i = 0;i < n;i++)
{
cout<<arr[i]<<"\t";
}
}
int main()
{
int n;
cin>>n;
double arr[n];
int i = Fill_array(arr,n);
Show_array(arr,i);
return 0;
}