结构类型的基本程序
来源:互联网 发布:灰色的天空网络歌曲 编辑:程序博客网 时间:2024/06/14 07:50
1、
#include<iostream.h>main(){ //定义结构类型 struct books { char title[20]; char author[15]; int pages; float price; } ; //声明结构变量 struct books Zbk={"VC++ ","Zhang",295,35.5}; books Wbk; //对结构变量的输出 cout<<"Zbk:"<<endl; cout<<Zbk.title <<endl; cout<<Zbk.author<<endl; cout<<Zbk.pages<<endl; cout<<Zbk.price<<endl; cout<<"--------------------"<<endl; //对结构成员的运算 Zbk.pages+=10; Zbk.price+=0.5; cout<<"Zbk.pages="<<Zbk.pages<<endl; cout<<"Zbk.price="<<Zbk.price<<endl; cout<<"--------------------"<<endl; //对结构变量的输入输出 cout<<"Wbk.title ="; cin>>Wbk.title; cout<<"Wbk.author="; cin>>Wbk.author; cout<<"Wbk.pages="; cin>>Wbk.pages; cout<<"Wbk.price="; cin>>Wbk.price; cout<<"Wbk:"<<endl; cout<<Wbk.title <<endl; cout<<Wbk.author<<endl; cout<<Wbk.pages<<endl; cout<<Wbk.price<<endl; cout<<"--------------------"<<endl; //结构变量之间的相互赋值 books temp; temp=Wbk; cout<<"temp:"<<endl; cout<<temp.title<<endl; cout<<temp.author<<endl; cout<<temp.pages<<endl; cout<<temp.price<<endl;}
2、
#include<iostream.h>main(){ int i; //定义结构类型 struct student { int num; char name[10]; float maths; float physics; float chemistry; double total; }; //声明结构数组st student st[3]; //从键盘上为结构数组输入值 cout<<" num name maths physics chemistry "<<endl; for (i=0;i<3;i++) { cout<<i+1<<" "; cin>>st[i].num; cin>>st[i].name; cin>>st[i].maths; cin>>st[i].physics; cin>>st[i].chemistry; } //计算每个学生的总成绩 for (i=0;i<3;i++) st[i].total=st[i].maths+st[i].physics+st[i].chemistry; //输出结构数组各元素的值 for (i=0;i<3;i++) { cout<<"st["<<i<<"]: "; cout<<st[i].num<<'\t'; cout<<st[i].name<<'\t'; cout<<st[i].maths<<'\t'; cout<<st[i].physics<<'\t'; cout<<st[i].chemistry<<'\t'; cout<<st[i].total<<endl; }}
3、
include<iostream.h>main(){ //定义结构类型 struct human { char name[10]; int sex; int age; }; //声明结构变量和结构指针变量,并初始化 struct human x={"WangPing",1,30},*p=NULL; //结构指针变量指向对象 p=&x; //显示结构变量的值 cout<<"x.name="<<x.name<<endl; cout<<"x.sex="<<x.sex<<endl; cout<<"x.age="<<x.age<<endl; //利用结构指针显示结构对象中的数据 cout<<"(*p).name="<<(*p).name<<endl; cout<<"(*p).sex="<<(*p).sex<<endl; cout<<"(*p).age="<<(*p).age<<endl; cout<<"p->name="<<p->name<<endl; cout<<"p->sex="<<p->sex<<endl; cout<<"p->age="<<p->age<<endl; //通过结构指针为结构对象输入数据 cout<<"name:"; cin>>(*p).name; cout<<"sex:"; cin>>(*p).sex; cout<<"age:"; cin>>(*p).age; //显示结构变量的值 cout<<"x.name="<<x.name<<endl; cout<<"x.sex="<<x.sex<<endl; cout<<"x.age="<<x.age<<endl;}
4、
#include<iostream.h>main(){ //定义结构类型 struct human { char name[10]; int sex; int age; }; //声明结构变量和结构指针,并初始化 struct human x={"WangPing",1,30},*p=&x; //利用结构指针显示结构中的数据 cout<<"(*p).name="<<(*p).name<<endl; cout<<"(*p).sex="<<(*p).sex<<endl; cout<<"(*p).age="<<(*p).age<<endl; cout<<"-------------------------"<<endl; //利用new运算符为p分配内存 p=new human; //从键盘上为p指向的结构对象赋值 cout<<"p->name="; cin>>p->name; cout<<"p->sex="; cin>>p->sex; cout<<"p->age="; cin>>p->age; cout<<"-------------------------"<<endl; //显示p所指结构对象的值 cout<<"p->name="<<p->name<<endl; cout<<"p->sex="<<p->sex<<endl; cout<<"p->age="<<p->age<<endl; cout<<"-------------------------"<<endl; //显示结构变量的值 cout<<"x.name="<<x.name<<endl; cout<<"x.sex="<<x.sex<<endl; cout<<"x.age="<<x.age<<endl; //释放p指向的内存 delete p; }
5、
#include<iostream.h>main(){ //定义结构类型 struct human { char name[10]; int sex; int age; }; //声明结构数组和结构指针变量,并初始化 human x[]={{"WeiPing",1,30},{"LiHua",1,25},{"LiuMin",0,23}},*p=NULL; //用下标变量的输出结构数组的元素 for (int i=0;i<3;i++) { cout<<x[i].name<<'\t'; cout<<x[i].sex<<'\t'; cout<<x[i].age<<endl; } cout<<"----------------"<<endl; //用结构指针输出结构数组的元素 for (p=x;p<=&x[2];p++) { cout<<p->name<<'\t'; cout<<p->sex<<'\t'; cout<<p->age<<endl; }}
6、
#include<iostream.h>main(){ //定义date结构 struct date { int year; int month; int day; }; //定义baby结构 struct baby { int num; float weight; date birthday; // date为结构类型 }; //声明baby结构变量并初始化 baby b1={10001,10,{2002,12,25}}; //下列是baby结构变量b1的引用。 cout<<"b1.num="<<b1.num<<endl; cout<<"b1.weight="<<b1.weight<<endl; cout<<"b1.birthday.year="<<b1.birthday.year<<endl; cout<<"b1.birthday.month="<<b1.birthday.month<<endl; cout<<"b1.birthday.day="<<b1.birthday.day<<endl; cout<<"--------------------------"<<endl; //声明baby结构变量temp,并进行赋值运算 baby temp; temp=b1; cout<<"temp.num="<<temp.num<<endl; cout<<"temp.weight="<<temp.weight<<endl; cout<<"temp.birthday.year="<<temp.birthday.year<<endl; cout<<"temp.birthday.month="<<temp.birthday.month<<endl; cout<<"temp.birthday.day="<<temp.birthday.day<<endl;}
阅读全文
0 0
- 结构类型的基本程序
- 程序的基本结构
- 程序的基本结构
- 程序的基本结构
- 组织结构的基本类型
- COBOL程序的基本结构
- Windows程序的基本结构
- jsp程序的基本结构
- WML程序的基本结构
- VHDL程序的基本结构
- wml程序的基本结构
- JAVA程序的基本结构
- Windows程序的基本结构
- C#程序的基本结构
- C程序的基本结构
- UI_程序的基本结构
- C程序的基本结构
- Flask:程序的基本结构
- 再玩下linux中的的epoll函数------可以用任何描述符号fd而不限于网络socket
- leetcode 654. Maximum Binary Tree
- SSH框架整合的一个小案例
- Maven使用
- FCC Inventory Update
- 结构类型的基本程序
- Alphabet
- js几种跨域请求最实用的方法
- JS如何捆绑TypeScript声明文件
- 《angular2入门系列实践》——表单验证详解
- 友盟统计
- 枚举的一个简单程序
- 项目——通过自动回复机器人学Mybatis(六)
- 【NOIP模拟】赤壁情(DP)