您的位置:首页 > 其它

结构的使用

2015-12-09 18:03 369 查看
#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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: