C++题目2.docx

上传人:b****5 文档编号:11568815 上传时间:2023-03-19 格式:DOCX 页数:37 大小:23.01KB
下载 相关 举报
C++题目2.docx_第1页
第1页 / 共37页
C++题目2.docx_第2页
第2页 / 共37页
C++题目2.docx_第3页
第3页 / 共37页
C++题目2.docx_第4页
第4页 / 共37页
C++题目2.docx_第5页
第5页 / 共37页
点击查看更多>>
下载资源
资源描述

C++题目2.docx

《C++题目2.docx》由会员分享,可在线阅读,更多相关《C++题目2.docx(37页珍藏版)》请在冰豆网上搜索。

C++题目2.docx

C++题目2

供学习参考用,分开下载

cin>>str;//用字符数组输入字符串

cout<<"str="<

cout<<"strip="<

cout<<"strip=";

cin>>strip;//用字符指针变量输入字符串

cout<<"str="<

cout<<"strip="<

//利用指针变量改变其指向字符串的内容

*(strip+2)='l';

cout<<"str="<

cout<<"strip="<

//动态为字符型指针变量分配内存

strip=newchar(100);

cout<<"strip=";

cin>>strip;//用字符指针变量输入字符串

cout<<"str="<

cout<<"strip="<

}

#include

main()

{

//声明用于存放运动员号码的数组

inth[]={1001,1002,1003,1004};

//声明用于存放运动员成绩的数组

floatx[]={12.3,13.1,11.9,12.1};

//声明用于存放运动姓名的字符型指针数组

char*p[]={"Wanghua","Zhangjian","Liwei","Huaming"};

//i,j,it是用做循环控制变量和临时变量

inti,j,it;

//ft用做暂存变量

floatft;

//pt为字符型指针变量用做暂存指针变量

char*pt;

//用选择法对数组x进行排序,并相应调整数组h和p中的数据

for(i=0;i<=3;i++)

for(j=i+1;j<=3;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;

}

//以下打印排序结果

for(i=0;i<=3;i++)

cout<

}

#include

main()

{

//声明指针数组

char*colors[]={"Red","Blue","Yellow","Green"};

//指向指针的指针变量

char**pt;

//通过指向指针的变量访问其指向的内容

pt=colors;

for(inti=0;i<=3;i++){

cout<<"pt="<

cout<<"*pt="<<*pt<

cout<<"**pt="<<**pt<

pt++;

}

}

#include

main()

{

//定义结构类型

structbooks

{

chartitle[20];

charauthor[15];

intpages;

floatprice;

};

//声明结构变量

structbooksZbk={"VC++","Zhang",295,35.5};

booksWbk;

//对结构变量的输出

cout<<"Zbk:

"<

cout<

cout<

cout<

cout<

cout<<"--------------------"<

//对结构成员的运算

Zbk.pages+=10;

Zbk.price+=0.5;

cout<<"Zbk.pages="<

cout<<"Zbk.price="<

cout<<"--------------------"<

//对结构变量的输入输出

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:

"<

cout<

cout<

cout<

cout<

cout<<"--------------------"<

//结构变量之间的相互赋值

bookstemp;

temp=Wbk;

cout<<"temp:

"<

cout<

cout<

cout<

cout<

}

#include

main()

{

inti;

//定义结构类型

structstudent{

intnum;

charname[10];

floatmaths;

floatphysics;

floatchemistry;

doubletotal;

};

//声明结构数组st

studentst[3];

//从键盘上为结构数组输入值

cout<<"numnamemathsphysicschemistry"<

for(i=0;i<3;i++)

{

cout<

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["<

";

cout<

cout<

cout<

cout<

cout<

cout<

}

}

#include

main()

{

//定义结构类型

structhuman{

charname[10];

intsex;

intage;

};

//声明结构变量和结构指针变量,并初始化

structhumanx={"WangPing",1,30},*p=NULL;

//结构指针变量指向对象

p=&x;

//显示结构变量的值

cout<<"x.name="<

cout<<"x.sex="<

cout<<"x.age="<

//利用结构指针显示结构对象中的数据

cout<<"(*p).name="<<(*p).name<

cout<<"(*p).sex="<<(*p).sex<

cout<<"(*p).age="<<(*p).age<

cout<<"p->name="<name<

cout<<"p->sex="<sex<

cout<<"p->age="<age<

//通过结构指针为结构对象输入数据

cout<<"name:

";

cin>>(*p).name;

cout<<"sex:

";

cin>>(*p).sex;

cout<<"age:

";

cin>>(*p).age;

//显示结构变量的值

cout<<"x.name="<

cout<<"x.sex="<

cout<<"x.age="<

}

include

main()

{

//定义结构类型

structhuman{

charname[10];

intsex;

intage;

};

//声明结构变量和结构指针,并初始化

structhumanx={"WangPing",1,30},*p=&x;

//利用结构指针显示结构中的数据

cout<<"(*p).name="<<(*p).name<

cout<<"(*p).sex="<<(*p).sex<

cout<<"(*p).age="<<(*p).age<

cout<<"-------------------------"<

//利用new运算符为p分配内存

p=newhuman;

//从键盘上为p指向的结构对象赋值

cout<<"p->name=";

cin>>p->name;

cout<<"p->sex=";

cin>>p->sex;

cout<<"p->age=";

cin>>p->age;

cout<<"-------------------------"<

//显示p所指结构对象的值

cout<<"p->name="<name<

cout<<"p->sex="<sex<

cout<<"p->age="<age<

cout<<"-------------------------"<

//显示结构变量的值

cout<<"x.name="<

cout<<"x.sex="<

cout<<"x.age="<

//释放p指向的内存

deletep;

}

#include

main()

{

//定义结构类型

structhuman{

charname[10];

intsex;

intage;

};

//声明结构数组和结构指针变量,并初始化

humanx[]={{"WeiPing",1,30},{"LiHua",1,25},{"LiuMin",0,23}},*p=NULL;

//用下标变量的输出结构数组的元素

for(inti=0;i<3;i++)

{

cout<

cout<

cout<

}

cout<<"----------------"<

//用结构指针输出结构数组的元素

for(p=x;p<=&x[2];p++)

{

cout<name<<'\t';

cout<sex<<'\t';

cout<age<

}

}

#include

main()

{

//定义一个包含指针成员的结构类型

structtest{

char*str;

int*ip;

}x;

//使用结构变量x中的整型指针ip

x.ip=newint;//分配1个单元

*(x.ip)=100;

cout<<"x.ip:

"<

cout<<"---------------"<

deletex.ip;

x.ip=newint[5];//分配5个单元

for(inti=0;i<5;i++)

*(x.ip+i)=100+i;

cout<<"x.ip:

"<

for(i=0;i<5;i++)

cout<

deletex.ip;

cout<<"---------------"<

//使用结构变量x中的字符型指针str

x.str=newchar('A');//分配1个单元

cout<<"x.str:

"<<(*x.str)<

cout<<"---------------"<

deletex.str;

x.str=newchar[5];//分配多个单元

*x.str='G';

*(x.str+1)='o';

*(x.str+2)='o';

*(x.str+3)='d';

*(x.str+4)='\0';

cout<<"x.str:

"<

deletex.str;

cout<<"---------------"<

//在声明结构变量时初始化

testy={"VeryGood!

",NULL};

cout<<"y.str:

"<

cout<<"y.ip:

"<

}

#include

main()

{

//定义date结构

structdate

{

intyear;

intmonth;

intday;

};

//定义baby结构

structbaby{

intnum;

floatweight;

datebirthday;//date为结构类型

};

//声明baby结构变量并初始化

babyb1={10001,10,{2002,12,25}};

//下列是baby结构变量b1的引用。

cout<<"b1.num="<

cout<<"b1.weight="<

cout<<"b1.birthday.year="<

cout<<"b1.birthday.month="<

cout<<"b1.birthday.day="<

cout<<"--------------------------"<

//声明baby结构变量temp,并进行赋值运算

babytemp;

temp=b1;

cout<<"temp.num="<

cout<<"temp.weight="<

cout<<"temp.birthday.year="<

cout<<"temp.birthday.month="<

cout<<"temp.birthday.day="<

}

#include

main()

{

//定义名为list的递归结构

structlist{

charname[10];

intsex;

intage;

list*next;//成员next为指向其自身结构的指针

};

//使用递归结构变量

listL1={"WeiPing",1,35.5,NULL};

cout<<"L1:

"<

cout<<"name\t"<

cout<<"sex\t"<

cout<<"age\t"<

cout<<"next\t"<

}

#include

main()

{

inti;

//定义名为student的递归结构

structstudent{

charname[10];

intmath;

intcomputer;

floatsum;

student*next;//next成员是指向自身的结构指针

};

//用student声明3个结构指针变量

structstudent*head,*tail,*temp;

//申请第1块数据,并设置各结构指针的初值

temp=newstructstudent;//申请内存

head=temp;//头指针

tail=head;//尾指针

//循环为链表输入数据

cout<<"\tnameMathComputer"<

for(i=1;;i++){

cout<

cin>>temp->name;

if(temp->name[0]!

='*')

{

cin>>temp->math>>temp->computer;

temp->sum=temp->math+temp->computer;

temp->next=NULL;

tail=temp;//设置链表尾指针

}

else

{

//以下是输入结束处理

deletetemp;

tail->next=NULL;

break;

}

//为下一个学生申请内存

temp->next=newstructstudent;

temp=temp->next;//使处理指针temp指向新内存块

}

//将链表数据从头到尾打印出来

cout<<"--------------------"<

temp=head;

while(temp!

=NULL){

cout<name<<","<math<<",";

cout<computer<<","<sum<

temp=temp->next;

}

}

#include

main()

{

inti;

//定义名为student的递归结构

structstudent{

charname[10];

intmath;

intcomputer;

floatsum;

student*forw;//forw成员是前指针

student*next;//next成员是后指针

};

//用student声明3个结构指针变量

structstudent*head,*tail,*temp;

//申请第1块数据,并设置各结构指针的初值

temp=newstructstudent;//申请内存

head=temp;//头指针

tail=head;//尾指针

head->forw=NULL;

//循环为链表记录输入数据

cout<<"\tnameMathComputer"<

for(i=1;;i++){

cout<

cin>>temp->name;

if(temp->name[0]!

='*')

{

cin>>temp->math>>temp->computer;

temp->sum=temp->math+temp->computer;

temp->next=NULL;

tail=temp;//设置链表尾指针

}

else

{

//以下是输入结束处理

deletetemp;

tail->next=NULL;

break;

}

//为下一个学生申请内存

temp->next=newstructstudent;

temp->next->forw=temp;//设置前指针

temp=temp->next;//使处理指针temp指向新内存块

}

//将链表数据从头到尾打印出来

cout<<"head------>tail:

"<

temp=head;

while(temp!

=NULL){

cout<name<<","<math<<",";

cout<computer<<","<sum<

temp=temp->next;

}

//将链表数据从尾到头打印出来

cout<<"tail------>head:

"<

temp=tail;

while(temp!

=NULL){

cout<name<<","<math<<",";

cout<computer<<","<sum<

temp=temp->forw;

}

}

#include

main()

{

inti;

//定义联合类型

unionutag{

charc;

intk;

floatx;

};

//声明联合变量

unionutagu;

//使用联合变量中的字符型成员

u.c='*';

cout<<"u.c="<

//使用联合变量中的整型成员

u.k=1000;

cout<<"u.k="<

//使用联合变量中的浮点型成员

u.x=3.1416;

cout<<"u.x="<

//声明联合变量时初始化

utagu1={'A'};

//同时引用联合变量的各成员

cout<<"u1.c="<

cout<<"u1.k="<

cout<<"u1.x="<<

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 自然景观

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1