《C语言与程序设计》实验报告28.docx
《《C语言与程序设计》实验报告28.docx》由会员分享,可在线阅读,更多相关《《C语言与程序设计》实验报告28.docx(16页珍藏版)》请在冰豆网上搜索。
《C语言与程序设计》实验报告28
C语言程序设计实验报告
专业计算机科学与技术学院班级日期2013/12/12成绩
实验组别第6次实验指导教师
学生姓名学号同组人姓名
实验名称指针实验
1、实验目的
(1)熟练掌握指针的说明、赋值、使用。
(2)掌握用指针引用数组的元素,熟悉指向数组的指针的使用。
(3)熟练掌握字符数组与字符串的使用,掌握指针数组及字符指针数组的用法。
(4)掌握指针函数与函数指针的用法。
(5)掌握带有参数的main函数的用法。
2、实验内容及要求
1.源程序改错
下面的源程序中是否存在错误?
如果存在,原因是什么?
如果存在错误,要求在计算机上对这个源程序进行调试修改,使之能够正确执行。
源程序:
#include
intmain(void){
float*p;
scanf("%f",p);
printf("%f\n",*p);
return0;
}
2.源程序完善、修改、替换
(1)下面源程序的功能是,通过函数指针和菜单选择来调用字符串拷贝函数或字符串连接函数。
请在程序中的下划线处填写合适的表达式、语句或代码片段来完善该程序。
源程序:
#include
#include
intmain(void){
_________
chara[80],b[80],c[160],*result=c;
intchoice,i;
do{
printf("\t\t1copystring.\n");
printf("\t\t2connectstring.\n");
printf("\t\t3exit.\n");
printf("\t\tinputanumber(1-3)please!
\n");
scanf("%d",&choice);
}while(choice<1||choice>5);
switch(choice){
case1:
p=strcpy;
break;
case2:
p=strcat;
break;
case3:
gotodown;
}
getchar();
printf("inputthefirststringplease!
\n");
i=0;
_________________
printf("inputthesecondstringplease!
\n");
i=0;
_________________
result=_____(a,b);
printf("theresultis%s\n",result);
down:
;
return0;
}
(2)请上机运行第
(1)题程序,使之能按下面要求输出结果(注:
(输入)表示该行数据是键盘输入数据):
1copystring.
2connectstring.
3exit.
inputanumber(1-3)please!
2(输入)
inputthefirststringplease!
themoreyoulearn,(输入)
inputthesecondstringplease!
themoreyouget,(输入)
theresultisthemoreyoulearn,themoreyouget.
3.跟踪调试源程序
请按下面的要求对所给源程序进行操作,并回答问题和排除错误。
(1)单步执行源程序。
进入strcpy时,watches窗口中s为何值?
返回main时,watches窗口中s为何值?
(2)排除源程序中的错误,使程序输出结果为:
thereisaboatonthelake.
源程序:
#include
char*strcpy(char*,char*);
intmain(void){
chara[20],b[60]="thereisaboatonthelake.";
printf("%s\n",strcpy(a,b));
return0;
}
char*strcpy(char*s,char*t){
while(*s++=*t++)
;
return(s);
}
4.程序设计
编写并上机调试运行能实现以下功能的程序的函数:
(1)已知一长整型变量占4个字节,其中每个字节又分成高4位和低4位。
试编写一个程序,从该长整型变量的高字节开始,依次取出每个字节的高4位和低4位并以数字字符的形式进行显示。
(2)利用大小为n的指针数组指向用gets函数输入的n行,每行不超过80个字符。
试编写一个函数,它将每一行中连续的多个空格字符压缩为一个空格字符。
在调用函数中输出压缩空格后的各行,空行不予输出。
(3)编写一个程序,输入n和整数,排序后输出。
排序的原则由命令行可选参数-d决定,并且有参数-d时按递减顺序排序,否则按递增顺序排序。
要求将排序算法定义成函数,利用指向函数的指针使该函数实现递增或递减排序。
(main函数参数的处理见下面的第(三)大点)
(4)设某个班有N个学生,每个学生修了M课程(用#define定义N,M)。
输入M门课程的名称,然后依次输入N个学生中每个学生所修M门课程的成绩,并且都存放到相应的数组中。
试编写下列函数:
①计算每个学生各门课程平均成绩。
②计算全班每门课程的平均成绩。
③分别统计低于全班各门课程平均成绩的人数。
④分别统计全班各门课程不及格的人数和90分以上(含90分)的人数。
在调用函数中输出上面各函数的计算结果。
(要求都用指针操作,不得使用下标操作。
)
5.选做题
编写并上机调试运行能执行以下功能的程序:
(1)设有N位整数和M位小数(N=20,M=10)的两个数据a,b。
编程计算a+b并输出结果。
如:
12345678912345678912.1234567891+98765432109876543210.0123456789
(2)编写一个使用复杂声明“char*(*p[2])(constchar*,constchar*);”的程序。
提示:
p中元素可为strcmp、strstr等函数名。
3、实验步骤及结果
1.没有对指针p进行指向就对其赋值,可能导致的结果是,改写内存中不该改写的内容,从而运行出错。
应该首先定义一个float变量,先使指针指向这个变量,再对其进行赋值。
最终源程序:
#include
intmain(void){
floatf,*p=&f;
scanf("%f",p);
printf("%f\n",*p);
return0;
}
2.
(1)该程序需要定义一个函数指针,用于指向strcpy和strcat,并在后面输入a,b两个字符串。
最终的源代码:
#include
#include
intmain(void){
char*(*p)(char*,constchar*);
chara[80],b[80],c[160],*result=c;
intchoice,i;
do{
printf("\t\t1copystring.\n");
printf("\t\t2connectstring.\n");
printf("\t\t3exit.\n");
printf("\t\tinputanumber(1-3)please!
\n");
scanf("%d",&choice);
}while(choice<1||choice>5);
switch(choice){
case1:
p=strcpy;
break;
case2:
p=strcat;
break;
case3:
gotodown;
}
getchar();
printf("inputthefirststringplease!
\n");
i=0;
while((a[i++]=getchar())!
='\n');
a[i-1]='\0';
printf("inputthesecondstringplease!
\n");
i=0;
while((b[i++]=getchar())!
='\n');
b[i-1]='\0';
result=p(a,b);
printf("theresultis%s\n",result);
down:
;
return0;
}
(2)运行结果:
1copystring.
2connectstring.
3exit.
inputanumber(1-3)please!
2
inputthefirststringplease!
themoreyoulearn,
inputthesecondstringplease!
themoreyouget.
theresultisthemoreyoulearn,themoreyouget.
3.
(1)在进入strcpy时,watches窗口中s的值是一些乱码。
在返回main时,watches窗口中s的值是空字符串。
(2)通过调试发现,a的字符数组长度不够,于是扩充为60,以避免没有足够的空间保存字符串。
并且,不能直接输出strcpy(a,b)的结果,而且最终会改变a的值,于是,在printf中输出a即可。
最终的源代码:
#include
char*strcpy(char*,char*);
intmain(void){
chara[60],b[60]="thereisaboatonthelake.";
strcpy(a,b);
printf("%s\n",a);
return0;
}
char*strcpy(char*s,char*t){
while(*s++=*t++)
;
return(s);
}
4.
(1)
#include
intmain(void){
longa=4294967295;
char*p=&a;
inti=0;
while(i<4){
printf("%d\t%d\t",(*p>>4)&0x0f,(*p)&0x0f);
i++;
p++;
}
return0;
}
(2)
#include
#defineMAXN100
voidreadlines_and_print(char(*)[81],int);
intmain(void){
chara[MAXN][81],(*p)[81]=a;
intn;
printf("Inputn:
");
scanf("%d",&n);
getchar();
readlines_and_print(p,n);
return0;
}
voidreadlines_and_print(char(*p)[81],intn){
inti=0,j=0,k,flag=0,nonspace=0;
while(ii=0;
while(ik=0;
nonspace=0;
while(k<81){
if((*(*(p+i)+k))=='\0')break;
if(!
((*(*(p+i)+k))==''||(*(*(p+i)+k))=='\t'))nonspace=1;
k++;
}
if(!
nonspace){
i++;
continue;
}
j=0;
flag=0;
while(j<81){
if((*(*(p+i)+j))=='\0')break;
if((*(*(p+i)+j))==''||(*(*(p+i)+j))=='\t')
if(!
flag){
putchar('');
flag++;
}elseflag++;
else{
flag=0;
putchar(*(*(p+i)+j));
}
j++;
}
putchar('\n');
i++;
}
}
(3)
#include
#include
#defineMAXN100
voiddeal(int*,int);
intflag=1;
intmain(intargc,char**argv){
inti,n,a[MAXN];
void(*p)(int*,int);
printf("Inputn:
");
scanf("%d",&n);
for(i=0;iif(argc>1)
if(!
strcmp(argv[1],"-d"))
flag=0;
p=deal;
p(a,n);
return0;
}
voiddeal(int*a,intn){
inti,j,m,mn,t;
for(i=0;im=a[i],mn=i;
for(j=i+1;jif(flag){
if(m}
if(!
flag){
if(m>a[j])m=a[j],mn=j;
}
}
t=a[i],a[i]=a[mn],a[mn]=t;
}
for(i=0;iprintf("%d\t",a[i]);
}
(4)
#include
#defineN2
#defineM2
voidindividual_average(float(*)[M]);
voidentire_average(float(*)[M]);
voidbelow_average_num(float(*)[M]);
voidfailing_outstanding_num(float(*)[M]);
voidinitial(float(*)[M]);
intmain(void){
void(*deal)(float(*)[M]);
intn;
floata[N][M],(*p)[M]=a;
initial(p);
printf("Pleasechoosethefunction:
\n");
printf("\t1.Calculateandprinteachstudent'saveragescoresofeachcourse.\n");
printf("\t2.Calculateandprintentireaveragescoresofeachcourseoftheclass.\n");
printf("\t3.Calculateandprintthenumberofstudentsbeloweachcourse'saveragescore.\n");
printf("\t4.Calculateandprintthenumberofstudentsbelow60andabove90.\n");
printf("\t5.Printthetable.\n");
printf("\t6.Exit.\n");
iNput:
scanf("%d",&n);
switch(n){
case1:
deal=individual_average;break;
case2:
deal=entire_average;break;
case3:
deal=below_average_num;break;
case4:
deal=failing_outstanding_num;break;
case5:
return0;break;
default:
printf("Inputillegal!
\n");return-1;
}
deal(p);
gotoiNput;
return0;
}
voidinitial(float(*p)[M]){
inti,j;
printf("Inputthescoresindividualy(UseTABbetweenscores):
\n");
printf("\tCourse\t");
for(i=0;iputchar('\n');
for(i=0;iprintf("Student%d\t",i+1);
for(j=0;jscanf("%f",*(p+i)+j);
}
}
}
voidindividual_average(float(*a)[M]){
floataverage[N],*p=average;
inti,j;
for(i=0;i*(p+i)=0.0;
for(j=0;j*(p+i)+=*(*(a+i)+j);
*(p+i)/=M;
}
for(i=0;iprintf("Averagescoreofstudent%dis%f.\n",i+1,*(p+i));
}
voidentire_average(float(*a)[M]){
floataverage[M],*p=average;
inti,j;
for(i=0;i*(p+i)=0.0;
for(j=0;j*(p+i)+=*(*(a+j)+i);
*(p+i)/=N;
printf("Averagescoreofcourse%dis%f.\n",i+1,*(p+i));
}
}
voidbelow_average_num(float(*a)[M]){
floataverage[M],*p=average;
inti,j,below[M],*b=below;
for(i=0;i*(p+i)=0.0;
for(j=0;j*(p+i)+=*(*(a+j)+i);
*(p+i)/=N;
}
for(i=0;i*(b+i)=0;
for(j=0;jif(*(*(a+j)+i)<*(p+i))(*(b+i))++;
printf("Thenumberofstudentsbelowtheaveragescoreofcourse%dis%d.\n",i+1,*(b+i));
}
}
voidfailing_outstanding_num(float(*a)[M]){
intfailing[M],outstanding[M],*f=failing,*o=outstanding,i,j;
for(i=0;i*(f+i)=*(o+i)=0;
for(j=0;jif(*(*(a+j)+i)<60)(*(f+i))++;
if(*(*(a+j)+i)>=90)(*(o+i))++;
}
printf("Thenumbersofstudentsfailingandoutstandingofcourse%dare%dand%d.\n",i+1,*(f+i),*(o+i));
}
}