下载此文档

5-例程-VC2008.doc


文档分类:IT计算机 | 页数:约14页 举报非法文档有奖
1/14
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/14 下载此文档
文档列表 文档介绍
5-例程-VC2008【例5-1】访问结构变量。#include<iostream>usingnamespacestd;structWeather//说明结构类型{doubletemp;doublewind;};intmain(){Weathertoday;//=;//=;cout<<"Temp="<<<<endl;//按成员输出cout<<"Wind="<<<<endl;}【例5-2】用指针访问结构。#include<cstring>#include<iostream>usingnamespacestd;structPerson{charname[20];unsignedlongid;doublesalary;};intmain(){Personpr1;Person*pp;//定义结构指针pp=&pr1;//取结构变量地址strcpy_s(pp->name,"DavidMarat");//对结构成员赋值,等价于(*pp).namepp->id=987654321;//pp->id等价于(*pp).idpp->salary=;//pp->salary等价于(*pp).salarycout<<pp->name<<'\t'<<pp->id<<'\t'<<pp->salary<<endl;}【例5-3】结构变量赋值。#include<iostream>usingnamespacestd;structWeather{doubletemp;doublewind;}yesterday;intmain(){Weathertoday;//=;=;today=yesterday;//结构变量整体赋值1cout<<"Temp="<<<<endl;cout<<"Wind="<<<<endl;}【例5-4】对结构数组存放的数据按某一关键字排序。本例程存放一个简单的职工登记表,每个职工记录包含姓名、号码和工资。#include<iostream>usingnamespacestd;structperson//说明结构类型{charname[10];unsignedintid;doublesalary;};voidInput(person[],constint);voidSort(person[],constint);voidOutput(constperson[],constint);intmain(){personallone[100];//说明结构数组inttotal;cout<<"输入职工人数:";cin>>total;cout<<"输入职工信息:\n";Input(allone,total);cout<<"以工资做关键字排序\n";Sort(allone,total);cout<<"输出排序后信息:\n";Output(allone,total);}voidInput(personall[],constintn){inti;for(i=0;i<n;i++)//输入数据{cout<<i<<":姓名:";cin>>all[i].name;cout<<"编号:";cin>>all[i].id;cout<<"工资:";cin>>all[i].salary;2}}voidSort(personall[],constintn){inti,j;persontemp;//说明结构变量for(i=1;i<n;i++)//以成员salary作关键字排序{for(j=0;j<=n-1-i;j++)if(all[j].salary>all[j+1].salary){temp=all[j];//交换结构数组元素all[j]=all[j+1];all[j+1]=temp;}}}voidOutput(constpersonall[],constintn){for(inti=0;i<n;i++)//输出排序后数据cout<<all[i].name<<'\t'<<all[i].id<<'\t'<<all[i].salary<<endl;}【例5-5】用辅助数组对结构数组的数据做关键字排序。#include<iostream>usingnamespacestd;structperson//说明结构类型{charname[10];unsignedintid;doublesalary;};voidInput(person[],constint);voidSort(person[],person*[],constint);voi

5-例程-VC2008 来自淘豆网www.taodocs.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数14
  • 收藏数0 收藏
  • 顶次数0
  • 上传人wxc6688
  • 文件大小35 KB
  • 时间2019-11-23