在程序中使用结构
#include<iostream>
using namespace std;
struct inflatable
{
char name[20];
float volume;
double price;
};
int main()
{
inflatable guest=
{
"Glorious Cloria",
1.88,
29.99
};
inflatable pal=
{
"Audacious Arthur",
1.88,
32.99
};
cout<<"Expand your guest list with "<<guest.name;
cout<<" and "<<pal.name<<"!\n";
cout<<"You can have both for $";
cout<<guest.price+pal.price<<"!\n";
system("pause");
return 0;
}
结构赋值
#include<iostream>
using namespace std;
struct inflatable
{
char name[20];
float volume;
double price;
};
int main()
{
inflatable bouquet=
{
"sunflowers",
0.20,
12.49
};
inflatable choice;
cout<<"bouquet:"<<bouquet.name<<" for $";
cout<<bouquet.price<<endl;
choice=bouquet;
cout<<"choice:"<<choice.name<<" for $";
cout<<choice.price<<endl;
system("pause");
return 0;
}
使用结构数组
#include<iostream>
using namespace std;
struct inflatable
{
char name[20];
float volume;
double price;
};
int main()
{
inflatable guests[2]=
{
{"Bambi",0.5,21.99},
{"Godzilla",2000,565.99}
};
cout<<"The guest "<<guests[0].name<<" and "<<guests[1].name
<<"\nhave a combined volume of "
<<guests[0].volume+guests[1].volume<<" cubic feet.\n";
system("pause");
return 0;
}
posted on
浙公网安备 33010602011771号