结构的使用

本文通过一个C++程序示例介绍了如何使用结构体数组来存储和操作多个箱体的数据,包括制造商名称、高度、宽度、长度及体积,并演示了如何初始化这些数据以及输出它们。
#include<iostream>
using namespace std;
const int M = 5;
struct box
{
char maker[40];
float height;
float width;
float length;
double volume;
};


void print_array(box *name,int number);//显示每一个成员的值
void init_array(box *name, int number);//初始化成员的值


int main(void)
{
using namespace std;
box *p = new box[M];
cout.setf(ios_base::fixed, ios_base::floatfield);
init_array(p, M);
cout << "init succese" << endl;
print_array(p, M);


delete[] p;
cin.get();
cin.get();
return 0;
}
void print_array(box *name, int number)
{
for (int i = 0;i < number;++i)
{
cout << "This is the " << i+1 << " data" <<endl;
cout << "maker = " << name[i].maker << endl;
cout << "height = " << name[i].height << endl;
cout << "width = " << name[i].width << endl;
cout << "length = " << name[i].length << endl;
cout << "volume = " << name[i].volume << endl;
}
}
void init_array(box *name, int number)
{
for (int i = 0;i < number;++i)
{
cout << "\n\nInput " << i+1 << " data" << ",a total of  " << M << "  data" << endl;
cout << "maker:";
cin >> name[i].maker;
cout << "height:";
cin >> name[i].height;
cout << "width:";
cin >> name[i].width;
cout << "length:";
cin >> name[i].length;
name[i].volume = (name[i].height + name[i].width + name[i].length) / 3;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值