下面是一个结构声明:
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
}; a. 编写一个函数,按值传递box结构,并显示每个成员的值。
b. 编写一个函数。传递box结构的地址,并将volume成员设置为其他三维长度的乘积。
c. 编写一个使用这两个函数的简单程序。
#include <iostream>
#include <Windows.h>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void show(struct box B)
{
cout << "maker = " << B.maker << endl
<< "height = " << B.height << endl
<< "width = " << B.width << endl
<< "length = " << B.length << endl
<< "volume = " << B.volume << endl;
}
void make(struct box *B)
{
B->volume = B->height*B->length*B->width;
}
int main()
{
struct box B;
cout << "请输入一个字符串,送到该结构体的maker成员里" << endl;
cin >> B.maker;
cout << "请输入3个数字,这三个数字分别为height、width和length" << endl;
cin >> B.height >> B.length >> B.width;
make(&B);
cout << "接下来会显示里面的内容" << endl;
show(B);
system("pause");
}