}
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)写出实验分析及结果。