C++题目2文档格式.docx
- 文档编号:7960127
- 上传时间:2023-05-09
- 格式:DOCX
- 页数:37
- 大小:23.01KB
C++题目2文档格式.docx
《C++题目2文档格式.docx》由会员分享,可在线阅读,更多相关《C++题目2文档格式.docx(37页珍藏版)》请在冰点文库上搜索。
floatft;
//pt为字符型指针变量用做暂存指针变量
char*pt;
//用选择法对数组x进行排序,并相应调整数组h和p中的数据
for(i=0;
i<
=3;
i++)
for(j=i+1;
j<
j++)
if(x[i]>
=x[j]){
ft=x[i],x[i]=x[j],x[j]=ft;
it=h[i],h[i]=h[j],h[j]=it;
pt=p[i],p[i]=p[j],p[j]=pt;
}
//以下打印排序结果
i++)
h[i]<
"
p[i]<
x[i]<
//声明指针数组
char*colors[]={"
Red"
Blue"
Yellow"
Green"
//指向指针的指针变量
char**pt;
//通过指向指针的变量访问其指向的内容
pt=colors;
for(inti=0;
i++){
pt="
pt<
*pt="
*pt<
**pt="
**pt<
pt++;
//定义结构类型
structbooks
{
chartitle[20];
charauthor[15];
intpages;
floatprice;
};
//声明结构变量
structbooksZbk={"
VC++"
Zhang"
295,35.5};
booksWbk;
//对结构变量的输出
Zbk:
Zbk.title<
Zbk.author<
Zbk.pages<
Zbk.price<
--------------------"
//对结构成员的运算
Zbk.pages+=10;
Zbk.price+=0.5;
Zbk.pages="
Zbk.price="
//对结构变量的输入输出
Wbk.title="
Wbk.title;
Wbk.author="
Wbk.author;
Wbk.pages="
Wbk.pages;
Wbk.price="
Wbk.price;
Wbk:
Wbk.title<
Wbk.author<
Wbk.pages<
Wbk.price<
//结构变量之间的相互赋值
bookstemp;
temp=Wbk;
temp:
temp.title<
temp.author<
temp.pages<
temp.price<
inti;
//定义结构类型
structstudent{
intnum;
charname[10];
floatmaths;
floatphysics;
floatchemistry;
doubletotal;
};
//声明结构数组st
studentst[3];
//从键盘上为结构数组输入值
numnamemathsphysicschemistry"
3;
i+1<
"
st[i].num;
st[i].name;
st[i].maths;
st[i].physics;
st[i].chemistry;
//计算每个学生的总成绩
st[i].total=st[i].maths+st[i].physics+st[i].chemistry;
//输出结构数组各元素的值
st["
]:
st[i].num<
'
\t'
st[i].name<
st[i].maths<
st[i].physics<
st[i].chemistry<
st[i].total<
structhuman{
intsex;
intage;
//声明结构变量和结构指针变量,并初始化
structhumanx={"
WangPing"
1,30},*p=NULL;
//结构指针变量指向对象
p=&
x;
//显示结构变量的值
x.name="
x.name<
x.sex="
x.sex<
x.age="
x.age<
//利用结构指针显示结构对象中的数据
(*p).name="
(*p).name<
(*p).sex="
(*p).sex<
(*p).age="
(*p).age<
p->
name="
name<
sex="
sex<
age="
age<
//通过结构指针为结构对象输入数据
name:
(*p).name;
sex:
(*p).sex;
age:
(*p).age;
include<
//声明结构变量和结构指针,并初始化
1,30},*p=&
//利用结构指针显示结构中的数据
-------------------------"
//利用new运算符为p分配内存
p=newhuman;
//从键盘上为p指向的结构对象赋值
name;
sex;
age;
//显示p所指结构对象的值
//释放p指向的内存
deletep;
//声明结构数组和结构指针变量,并初始化
humanx[]={{"
WeiPing"
1,30},{"
LiHua"
1,25},{"
LiuMin"
0,23}},*p=NULL;
//用下标变量的输出结构数组的元素
x[i].name<
x[i].sex<
x[i].age<
----------------"
//用结构指针输出结构数组的元素
for(p=x;
p<
=&
x[2];
p++)
//定义一个包含指针成员的结构类型
structtest{
char*str;
int*ip;
}x;
//使用结构变量x中的整型指针ip
x.ip=newint;
//分配1个单元
*(x.ip)=100;
x.ip:
x.ip<
*(x.ip)<
---------------"
deletex.ip;
x.ip=newint[5];
//分配5个单元
for(inti=0;
5;
*(x.ip+i)=100+i;
for(i=0;
x.ip+i<
(*(x.ip+i))<
//使用结构变量x中的字符型指针str
x.str=newchar('
A'
);
x.str:
(*x.str)<
deletex.str;
x.str=newchar[5];
//分配多个单元
*x.str='
G'
*(x.str+1)='
o'
*(x.str+2)='
*(x.str+3)='
d'
*(x.str+4)='
\0'
x.str<
//在声明结构变量时初始化
testy={"
VeryGood!
NULL};
y.str:
y.str<
y.ip:
y.ip<
//定义date结构
structdate
intyear;
intmonth;
intday;
//定义baby结构
structbaby{
floatweight;
datebirthday;
//date为结构类型
//声明baby结构变量并初始化
babyb1={10001,10,{2002,12,25}};
//下列是baby结构变量b1的引用。
b1.num="
b1.num<
b1.weight="
b1.weight<
b1.birthday.year="
b1.birthday.year<
b1.birthday.month="
b1.birthday.month<
b1.birthday.day="
b1.birthday.day<
--------------------------"
//声明baby结构变量temp,并进行赋值运算
babytemp;
temp=b1;
temp.num="
temp.num<
temp.weight="
temp.weight<
temp.birthday.year="
temp.birthday.year<
temp.birthday.month="
temp.birthday.month<
temp.birthday.day="
temp.birthday.day<
//定义名为list的递归结构
structlist{
list*next;
//成员next为指向其自身结构的指针
//使用递归结构变量
listL1={"
1,35.5,NULL};
L1:
name\t"
L1.name<
sex\t"
L1.sex<
age\t"
L1.age<
next\t"
L1.next<
//定义名为student的递归结构
intmath;
intcomputer;
floatsum;
student*next;
//next成员是指向自身的结构指针
//用student声明3个结构指针变量
structstudent*head,*tail,*temp;
//申请第1块数据,并设置各结构指针的初值
temp=newstructstudent;
//申请内存
head=temp;
//头指针
tail=head;
//尾指针
//循环为链表输入数据
\tnameMathComputer"
for(i=1;
\t"
temp->
if(temp->
name[0]!
='
*'
)
math>
computer;
temp->
sum=temp->
math+temp->
next=NULL;
tail=temp;
//设置链表尾指针
else
//以下是输入结束处理
deletetemp;
tail->
break;
//为下一个学生申请内存
next=newstructstudent;
temp=temp->
next;
//使处理指针temp指向新内存块
//将链表数据从头到尾打印出来
temp=head;
while(temp!
=NULL){
math<
computer<
sum<
student*forw;
//forw成员是前指针
//next成员是后指针
head->
forw=NULL;
//循环为链表记录输入数据
next->
forw=temp;
//设置前指针
//使处理指针temp指向新内存块
//将链表数据从头到尾打印出来
head------>
tail:
//将链表数据从尾到头打印出来
tail------>
head:
temp=tail;
forw;
//定义联合类型
unionutag{
charc;
intk;
floatx;
//声明联合变量
unionutagu;
//使用联合变量中的字符型成员
u.c='
u.c="
u.c<
//使用联合变量中的整型成员
u.k=1000;
u.k="
u.k<
//使用联合变量中的浮点型成员
u.x=3.1416;
u.x="
u.x<
//声明联合变量时初始化
utagu1={'
//同时引用联合变量的各成员
u1.c="
u1.c<
u1.k="
u1.k<
u1.x="
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 题目