c++指针引用和结构体.docx

上传人:b****5 文档编号:8297267 上传时间:2023-01-30 格式:DOCX 页数:18 大小:251.75KB
下载 相关 举报
c++指针引用和结构体.docx_第1页
第1页 / 共18页
c++指针引用和结构体.docx_第2页
第2页 / 共18页
c++指针引用和结构体.docx_第3页
第3页 / 共18页
c++指针引用和结构体.docx_第4页
第4页 / 共18页
c++指针引用和结构体.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

c++指针引用和结构体.docx

《c++指针引用和结构体.docx》由会员分享,可在线阅读,更多相关《c++指针引用和结构体.docx(18页珍藏版)》请在冰豆网上搜索。

c++指针引用和结构体.docx

c++指针引用和结构体

实验5:

指针、引用和结构体

要求:

1掌握指针、指针变量、指针常量的基本概念;

2掌握指针与数组、指针与函数的关系及应用。

3初步掌握引用的概念及简单应用。

第一部分教程练习:

1.上机实验题一

观察程序运行结果,学习指针的基本运算。

⑴分析

变量和数组元素的地址都可以赋给一个指针,一个指针可以加上或减去一个整数;在两个指针指向同一数组不同元素时,可以进行减运算,结果为其间的数组元素个数。

一个完整的参考程序如下:

#include

voidmain()

{

inti,a[10];

int*p1,*p2;

p1=a;//A

p2=p1+5;

for(i=0;i<=9;i++){

a[i]=i*i;

cout<<"a["<

}

cout<<"a="<

cout<<"p1="<

cout<<"p2-p1="<

cout<<"*p2-*p1="<<*p2-*p1<

}

⑵上机要求

将程序中A行改为“p1=&a[0]”观察程序运行结果。

将程序中B行改为“cout<<"*a="<<*a<

将程序中C行改为“cout<<"a[5]-a[0]="<

(3)对以上结果写出实验分析:

将程序中A行改为“p1=&a[0]”,结果不变,因为“a”和“&a[0]”都表示数组的首地址。

将程序中B行改为“cout<<"*a="<<*a<

将程序中C行改为“cout<<"a[5]-a[0]="<

⒉上机实验题二

指出下列程序的错误。

#include

voidexchange(int,int);

voidmain()

{

cout<<"Inputtwodatasepratedbyspace:

";

inta,b;

cin>>a>>b;

cout<<"BeforeExchange:

a="<

exchange(a,b);

cout<<"AfterExchange:

a="<

}

voidexchange(intx,inty)

{

intt;

t=x;x=y;y=t;

}

⑴分析

本例的目的是在子函数中交换的两个变量的值,在主函数中使用。

但函数的数据传值调用方式是将实参的数据值传递给形参,实参和形参在内存中占用不同的地址单元,改变形参值不改变实参值。

要想通过改变形参的值而改变实参的值,则应使用指针调用或引用调用。

而且引用调用大有取代指针调用之势。

使用指针调用如下:

#include

voidexchange(int*,int*);

voidmain()

{

cout<<"Inputtwodatasepratedbyspace:

";

inta,b,*p1=&a,*p2=&b;

cin>>a>>b;

cout<<"BeforeExchange:

a="<

exchange(p1,p2);

cout<<"AfterExchange:

a="<

}

voidexchange(int*x,int*y)

{

intt;

t=*x;*x=*y;*y=t;

}

使用引用调用如下(待讲完引用再做):

#include

voidexchange(int&,int&);

voidmain()

{

cout<<"Inputtwodatasepratedbyspace:

";

inta,b;

cin>>a>>b;

cout<<"BeforeExchange:

a="<

exchange(a,b);

cout<<"AfterExchange:

a="<

}

voidexchange(int&x,int&y)

{

intt;

t=x;x=y;y=t;

}

⑵上机要求

分别输出三个程序中形参和输出变量的地址。

 

 

 

 

(3)写出实验分析及结果

第一种方法的形参,实参的地址不同,交换的只是指针的指向,故不能实现实参的交换。

第二种地址相同,交换的是实参的值。

第三种是引用调用,交换的也是实参的值。

⒊上机实验题三

自定义函数实现字符串的拷贝。

⑴分析

字符串不允许进行赋值运算,只能使用头文件string.h中定义的strcpy()库函数。

本例要求自己编制一个自定义函数实现strcpy()库函数的功能。

一个完整的参考程序如下:

#include

char*mycopy(char*,constchar*);

voidmain()

{

chars1[]="Iamastudent";

chars2[20];

mycopy(s2,s1);

cout<<"s1="<

cout<<"s2="<

}

char*mycopy(char*to,constchar*from)

{

char*temp=to;

for(;*to++=*from++;);

returntemp;

}

⑵上机要求

将自定义函数改为下列程序段:

char*mycopy(char*to,constchar*from)

{

char*temp=to;

for(inti=0;*(from+i)!

='\0';i++)

*(to+i)=*(from+i);

*(to+i)='\0';

returntemp;

}

观察现象。

 

实现了字符串的拷贝。

将自定义函数的声明改为:

“char*mycopy(constchar*,constchar*);”

自定义函数相应改为:

char*mycopy(constchar*to,constchar*from)

{

char*temp=to;

for(;*to++=*from++;);

returntemp;

}

观察现象。

出错

D:

\课程\lab1_1\lab1_1.cpp(13):

errorC2440:

'initializing':

cannotconvertfrom'constchar*'to'char*'

D:

\课程\lab1_1\lab1_1.cpp(14):

errorC2166:

l-valuespecifiesconstobject

在自定义函数中能否使用如下的语句“*from='a';”

不能。

‘a’是字符,*from是指针局部变量,不能把字符常量赋给指针,只能把地址或数组名、指针赋给指针变量。

(3)写出实验分析及结果

1、形参,实参类型应保持一致。

2、常量在自定义函数中最好不变,而是赋给一个新定义变量,再改变变量的值。

3、自定义函数结束,记得返回变量的值,而不是常量的值。

⒋上机实验题四

使用函数的指针分别调用两个函数sum1和sum2分别求不大于该数的偶数或奇数之和。

⑴分析

使用函数的指针,可以通过指针的赋值分别调用不同的函数。

#include

intsum1(int);

intsum2(int);

voidmain()

{

inta;

int(*f)(int);//A指针函数

cout<<"Inputadata:

";

cin>>a;

if(a%2==0)

{

f=sum1;

cout<<(*f)(a)<

}

else

{

f=sum2;

cout<<(*f)(a)<

}

}

intsum1(intm)

{

ints1=0;

for(inti=2;i<=m;i=i+2)

s1=s1+i;

returns1;

}

intsum2(intn)

{

ints2=0;

for(inti=1;i<=n;i=i+2)

s2=s2+i;

returns2;

}

 

⑵上机要求

将A行改为“int(*f)(int);”,观察现象。

未变化。

分别输出两个函数的函数名,观察其地址值。

 

 

(3)写出实验分析及结果

地址相差5字节。

自定义函数的内存空间大致在4~5个字节。

 

⒌上机实验题五

编写一个程序判定一个字符在一个字符串中出现的次数,如果该字符不出现则返回0。

给出源代码及运行结果。

#include

int freq(char s[],char ch) {

char *p=s;

int count=0;

while(*p!

='\0') {

if (*p==ch) 

count++;

p++;

}

return count;

}

void main() {

char s[100],ch;

cout<<”请输入字符串:

”;

cin>>s;

cout<<”请输入字符:

”;

cin>>ch;

cout<

”<

printf("%c出现次数:

%d\n",ch,freq(s,ch));

}

第二部分自测练习

1.自测练习一

使用new和delete创建动态堆栈。

⑴分析

new和delete运算符可以对数组和结构体进行动态内存分配和释放。

一个完整的应用程序如下:

#include

structStack{

intstacksize;

long*buffer;

long*sp;

}p;

voidini(intsize)

{

p.stacksize=size;

p.sp=p.buffer=newlong[size];

}

voiddel()

{

deletep.buffer;

}

voidpush(longdata)

{

if(p.sp>=p.buffer+p.stacksize)

cerr<<"stackoverflow!

\n";

else

{*p.sp++=data;

cout<

}

}

longpop()

{

if(p.sp<=p.buffer){

cerr<<"stackisempty!

\n";

return0;

}

return*--p.sp;

}

voidmain()

{

ini(5);//A

push(1234);

push(5678);

push(1357);

push(2468);

cout<

cout<

cout<

cout<

cout<

cout<

del();

}

⑵上机要求

将程序中A行分别改为“ini

(2);”和“ini(8);”观察程序运行结果。

(3)写出实验分析及结果

2.自测练习二

编写程序建立一个学生学习成绩的链表,完成链表的输出、插入、删除操作。

⑴分析

在程序中,经常用到数量不确定的相同结构类型的数据处理,用数组往往造成存储空间的浪费,链表可以通过数据的动态存储克服这一缺点。

一个完整的应用程序如下:

#include

structStudent//创建结构

{

longnumber;

floatscore;

Student*next;

};

Student*head;

Student*Create()//建立链表

{

Student*ps;

Student*pend;

ps=newStudent;

head=NULL;

pend=ps;

cout<<"Inputnumberandscore:

"<

cin>>ps->number>>ps->score;

while(ps->number!

=0){

if(head==NULL)

head=ps;

else

pend->next=ps;

pend=ps;

ps=newStudent;

cin>>ps->number>>ps->score;

pend->next=NULL;

}

deleteps;

return(head);

}

voidshowlist(Student*head)//输出链表

{

cout<<"nowtheitemsoflistare\n";

while(head){

cout<number<<","<score<

head=head->next;

}

}

voidInsert(Student*head,Student*stud)//插入链表节点

{

if(head==NULL){

head=stud;

stud->next=NULL;

return;

}

Student*pGuard=head->next;

head->next=stud;

stud->next=pGuard;

return;

}

voidDelete(longnumber)//删除链表节点

{

Student*p;

if(head->number==number){

p=head;

head=head->next;

deletep;

cout<

return;

}

for(Student*pGuard=head;pGuard->next!

=NULL;pGuard=pGuard->next){

if((pGuard->next)->number==number){

p=pGuard->next;

pGuard->next=p->next;

deletep;

cout<

return;

}

}

cout<

\n";

}

voidmain()//主函数

{

Student*ts;

head=Create();

showlist(head);

Studentps,xs;

ts=head;

cout<<"Inputinsertnumberandscore:

"<

cin>>ps.number>>ps.score;

while(ts){

if(ps.number==ts->number){

cout<<"该学号已存在,请重新输入"<

cout<<"Inputinsertnumberandscore:

"<

cin>>ps.number>>ps.score;

ts=head;

}

else

ts=ts->next;

}

Insert(head,&ps);

showlist(head);

cout<<"Inputdeletenumber:

"<

cin>>xs.number;

Delete(xs.number);

showlist(head);

}

⑵上机要求

按以下格式输入数据,观察程序运行结果。

Inputnumberandscore:

111

222

333

000

Inputinsrtnumberandscore:

444

Inputdeletenumber:

2

将主函数中的“xs.number”改写为“ps.number”观察现象,分析原因。

删除voidDelete(longnumber)函数中的“deletep;”语句,观察现象。

实验结果未发生变化。

修改程序实现对输出结果按学号从大到小的顺序排序。

(3)写出实验分析及结果。

 

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

当前位置:首页 > 工程科技 > 城乡园林规划

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

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