C语言程序的设计第四次28实验报告Word文档下载推荐.docx
《C语言程序的设计第四次28实验报告Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《C语言程序的设计第四次28实验报告Word文档下载推荐.docx(21页珍藏版)》请在冰豆网上搜索。
如果存在,原因是什么?
如果存在错误,要求在计算机上对这个例子程序进行调试修改,使之能够正确执行。
#include<
stdio.h>
voidmain(void)
{
float*p;
scanf("
%f"
p);
printf("
%f\n"
*p);
}
存在,错误为指针一开始没有初始化,而sacnf传入的是float型指针指向的地址,我们并不知道系统能给我们分配什么地址,所以说我们输入的地址很有可能使程序崩溃。
修改后代码:
intmain(void)
float*p;
floata[10];
//这里可以换成其他数字
p=&
a[0];
printf("
return0;
(1)下面的程序通过函数指针和菜单选择来调用字符串拷贝函数或字符串连接函数,请在下划线处填写合适的表达式、语句、或代码片段来完善该程序。
#include<
string.h>
char*(*p)(chara[],charb[]);
chara[80],b[80],c[160],*result=c;
intchoice,i;
do{
printf("
\t\t1copystring.\n"
);
\t\t2connectstring.\n"
\t\t3exit.\n"
\t\tinputanumber(1-3)please!
\n"
scanf("
%d"
&
choice);
}while(choice<
1||choice>
5);
switch(choice){
case1:
p=strcpy;
break;
case2:
p=strcat;
case3:
gotodown;
}
getchar();
inputthefirststringplease!
i=0;
gets(a);
inputthesecondstringplease!
gets(b);
result=p(a,b);
theresultis%s\n"
result);
down:
;
(2)请上机运行第
(1)题程序,使之能按要求输出下面结果:
((输入)表示该数据是键盘输入数据)
1copystring.
2connectstring.
3exit.
inputanumber(1-3)please!
2(输入)
themoreyoulearn,(输入)
themoreyouget.(输入)
theresultisthemoreyoulearn,themoreyouget.
char*strcpy(char*,char*);
chara[20],b[60]="
thereisaboatonthelake."
;
%s\n"
strcpy(a,b));
char*strcpy(char*s,char*t)
while(*s++=*t++)
return(s);
(1)单步执行。
进入strcpy时watch窗口中s为何值?
返回main时,watch窗口中s为何值?
进入strcpy时:
返回main时:
(2)排除错误,使程序输出结果为:
thereisaboatonthelake.
void*strcpy(char*,char*);
chara[30],b[60]="
strcpy(a,b);
a);
return0;
void*strcpy(char*s,char*t)
while(*t!
='
\0'
)
{
*s++=*t++;
*s='
//将函数类型设置为void型,然后不再返回直接打印a字符串即可
(1)一个长整型变量占4个字节,其中每个字节又分成高4位和低4位。
试从该长整型变量的高字节开始,依次取出每个字节的高4位和低4位并以数字字符的形式进行显示。
intn,i;
scanf("
%x"
n);
char*p=(char*)&
n;
inthigh_half,low_half;
for(i=3;
i>
=0;
i--){
low_half=p[i]&
0x0f;
if(low_half<
10)
low_half+='
0'
else
A'
-10;
high_half=(p[i]&
0xf0)>
>
4;
if(high_half<
high_half+='
%c%c\n"
high_half,low_half);
(2)利用大小为n的指针数组指向用gets函数输入的n行,每行不超过80个字符。
编写一个函数,它将每一行中连续的多个空格字符压缩为一个空格字符。
在调用函数中输出压缩空格后的各行,空行不予输出。
#defineN10
voidreducespace(char*p,intn);
intmain()
inti;
char*p[N],arr[N][81];
Input%d-linestrings:
N);
for(i=0;
i<
N;
i++)
*(p+i)=arr[i];
the%dline:
"
i+1);
gets(*(p+i));
\n\naftercompression:
reducespace(*(p+i),i);
putchar(10);
voidreducespace(char*p,intn)//削减空格的函数,将多行空格压缩成一个空格
intflag=1,i,j=0,sum=0;
chartemp[81];
*(p+i)!
='
if(*(p+i)!
'
&
flag)
{
temp[j++]=*(p+i);
sum++;
}
if(*(p+i)=='
flag=0;
!
temp[j++]='
flag=1;
temp[j]='
if(sum)
The%dline:
n+1,temp);
else
Ofthe%dlinethereareallspaces\n"
n+1);
(3)输入n个整数,排序后输出。
排序的原则由命令行可选参数-d决定,有参数-d时按递减顺序排序,否则按递增顺序排序。
要求将排序算法定义成函数,利用指向函数的指针使该函数实现递增或递减排序。
(main函数参数的处理见8.3节)
stdlib.h>
intmain(intargc,char*argv[])
voiddownsort(char**p,intm);
voidupsort(char**q,intk);
intn=0;
while(n<
argc)
%s"
argv[n]);
n++;
if(!
strcmp(argv[1],"
-d"
))
downsort(&
argv[2],n-2);
elseupsort(&
argv[1],n-1);
voiddownsort(char**p,intm)
inti,j;
chart;
for(i=0;
i<
m-1;
i++)
for(j=0;
j<
j++)
if(*p[j]<
*p[j+1])
t=*p[j],*p[j]=*p[j+1],*p[j+1]=t;
m;
*p[i]=*p[i]-'
%d"
*p[i]);
voidup_sort(char**q,intk)
{chart;
k-1;
if(*q[j]>
*q[j+1])
t=*q[j],*q[j]=*q[j+1],*q[j+1]=t;
k;
*q[i]=*q[i]-'
*q[i]);
注:
我的电脑总是缺少一个插件,当我把这个插件