C语言程序设计第四次实验报告

上传人:m**** 文档编号:503187119 上传时间:2022-08-02 格式:DOCX 页数:14 大小:41.54KB
返回 下载 相关 举报
C语言程序设计第四次实验报告_第1页
第1页 / 共14页
C语言程序设计第四次实验报告_第2页
第2页 / 共14页
C语言程序设计第四次实验报告_第3页
第3页 / 共14页
C语言程序设计第四次实验报告_第4页
第4页 / 共14页
C语言程序设计第四次实验报告_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《C语言程序设计第四次实验报告》由会员分享,可在线阅读,更多相关《C语言程序设计第四次实验报告(14页珍藏版)》请在金锄头文库上搜索。

1、C语言程序设计第四次实验报告 C语言程序设计 实验报告 专业 班级 日期 x月x日 成绩 实验组别 第 3(2.7) 次实验 指导教师 李开 学生姓名 学号 同组人姓名 实验名称 实验8 指针实验 一、实验目的 熟练掌握指针的说明、赋值、使用。 掌握用指针引用数组的元素,熟悉指向数组的指针的使用。 熟练掌握字符数组与字符串的使用,掌握指针数组及字符指针数组的用法。 掌握指针函数与函数指针的用法。 掌握带有参数的main函数的用法。 二、实验任务 8.2 实验内容及要求 1源程序改错 2源程序完善、修改、替换 3跟踪调试 4程序设计 5选做题 8.3 指定main函数的参数 三、实验步骤及结果

2、8.2 实验内容及要求 1源程序改错 下面程序是否存在错误?如果存在,原因是什么?如果存在错误,要求在计算机上对这个例子程序进行调试修改,使之能够正确执行。 #include void main(void) float *p; scanf(%f,p); printf(%fn,*p); 存在,错误为指针一开始没有初始化,而sacnf传入的是float型指针指向的地址,我们并不知道系统能给我们分配什么地址,所以说我们输入的地址很有可能使程序崩溃。 修改后代码: #include int main(void) float *p; float a10;/这里可以换成其他数字 p=&a0; scanf(

3、%f,p); printf(%fn,*p); return 0; 2源程序完善、修改、替换 下面的程序通过函数指针和菜单选择来调用字符串拷贝函数或字符串连接函数,请在下划线处填写合适的表达式、语句、或代码片段来完善该程序。 #include #include void main(void) char*(*p)(char a,char b); char a80,b80,c160,*result=c; int choice,i; do printf(tt1 copy string.n); printf(tt2 connect string.n); printf(tt3 exit.n); print

4、f(ttinput a number (1-3) please!n); scanf(%d,&choice); while(choice5); switch(choice) case 1: p=strcpy; break; case 2: p=strcat; break; case 3: goto down; getchar; printf(input the first string please!n); i=0; gets(a); printf(input the second string please!n); i=0; gets(b); result= p(a,b); printf(th

5、e result is %sn,result); down: ; 请上机运行第题程序,使之能按要求输出下面结果:表示该数据是键盘输入数据) 1 copy string. 2 connect string. 3 exit. input a number (1-3) please! 2 input the first string please! the more you learn, input the second string please! the more you get. the result is the more you learn,the more you get. 3跟踪调试

6、#include char *strcpy(char *,char *); void main(void) char a20,b60=there is a boat on the lake.; printf(%sn,strcpy(a,b); char *strcpy(char *s,char *t) while(*s+=*t+) ; return (s); 单步执行。进入strcpy时watch窗口中s为何值?返回main时, watch窗口中s为何值? 进入strcpy时: 返回main时: 排除错误,使程序输出结果为: there is a boat on the lake. #inclu

7、de void *strcpy(char *,char *); int main(void) char a30,b60=there is a boat on the lake.; strcpy(a,b); printf(%sn,a); return 0; void *strcpy(char *s,char *t) while(*t!=0) *s+=*t+; *s=0;/将函数类型设置为void型,然后不再返回直接打印a字符串即可 4程序设计 一个长整型变量占4个字节,其中每个字节又分成高4位和低4位。试从该长整型变量的高字节开始,依次取出每个字节的高4位和低4位并以数字字符的形式进行显示。 #

8、include int main(void) int n,i; scanf(%x,&n); char *p=(char *)&n; int high_half,low_half; for(i=3; i=0; i-) low_half = pi & 0x0f; if (low_half 4; if (high_half 10) high_half += 0; else high_half += A - 10; printf(%c %cn,high_half,low_half); return 0; 利用大小为n的指针数组指向用gets函数输入的n行,每行不超过80个字符。编写一个函数,它将每一行

9、中连续的多个空格字符压缩为一个空格字符。在调用函数中输出压缩空格后的各行,空行不予输出。 #include #define N 10 void reducespace(char *p,int n); int main int i; char *pN,arrN81; printf(Input %d-line strings:n,N); for (i = 0; i N; i+) *(p + i) = arri; printf(the %d line:, i + 1); gets(*(p + i); printf(nnafter compression:n); for (i = 0; i N; i+

10、) reducespace(*(p + i),i); putchar(10); return 0; void reducespace(char *p,int n)/削减空格的函数,将多行空格压缩成一个空格 int flag = 1, i, j = 0, sum = 0; char temp81; for (i = 0; *(p + i) != 0; i+) if (*(p + i) != &flag) tempj+ = *(p + i); sum+; if (*(p + i) = &flag) flag = 0; if (*(p + i) != &!flag) tempj+ = ; tempj

11、+ = *(p + i); flag = 1; sum+; tempj = 0; if (sum) printf(The %d line:%sn, n+1,temp); else printf(Of the %d line there are all spacesn, n + 1); 输入n个整数,排序后输出。排序的原则由命令行可选参数-d决定,有参数-d时按递减顺序排序,否则按递增顺序排序。要求将排序算法定义成函数,利用指向函数的指针使该函数实现递增或递减排序。 #include #include #include int main(int argc,char *argv) void dow

12、nsort(char* *p,int m); void upsort(char * *q,int k); int n=0; while(nargc) printf( %s,argvn); n+; printf(n); if(!strcmp(argv1,-d) downsort(&argv2,n-2); else upsort(&argv1,n-1); return 0; void downsort(char * *p,int m) int i,j; char t; for(i=0;im-1;i+) for(j=0;jm-1;j+) if(*pj*pj+1) t=*pj,*pj=*pj+1,*pj+

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号