第八天第一个问题

发布时间 2023-04-20 19:23:28作者: 序章0

问题描述:

1.编写一个程序,按值传递box结构,并显示成员的值;

2.编写一个程序,传递box结构的地址,并将volume成员设置为其他三维长度的乘积

3.编写一个使用这两个函数的简单程序

解决思路:

1.建立两个函数,一个函数用于显示成员的值,使用值传递,另一个用于修改成员的值,使用地址传递

2.在主函数中调用两个函数即可

代码:

#include <iostream>
using namespace std;
struct box{

char maker[40];
float height;
float width;
float length;
float volume;
};
void a(box t)
{
cout << t.maker << " " << t.height << " " << t.width << " " << t.length << " " << t.volume;
}
void b(box*j)
{
j->volume = j->height*j->length*j->width;
}
int main()
{
box t = { "chen", 1, 1, 1 };
box n = t;
b(&n);
a(t);
cout << endl;
a(n);
return 0;
}

注:图方便,代码中的初始值就固定了,也可以询问用户,将用户输入的值存储进结构体当做初始值。