结构的使用

来源:互联网 发布:ubuntu安装anaconda 编辑:程序博客网 时间:2024/06/14 19:58
#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;
}
}
0 0
原创粉丝点击