ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:18.54KB ,
资源ID:24191655      下载积分:10 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/24191655.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(《C语言与程序设计》实验报告28.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

《C语言与程序设计》实验报告28.docx

1、C语言与程序设计实验报告28C语言程序设计 实验报告专业 计算机科学与技术学院 班级 日期 2013/12/12 成绩 实验组别 第6次实验 指导教师 学生姓名 学号 同组人姓名 实验名称 指针实验 1、实验目的(1)熟练掌握指针的说明、赋值、使用。(2)掌握用指针引用数组的元素,熟悉指向数组的指针的使用。(3)熟练掌握字符数组与字符串的使用,掌握指针数组及字符指针数组的用法。(4)掌握指针函数与函数指针的用法。(5)掌握带有参数的main函数的用法。2、实验内容及要求1.源程序改错下面的源程序中是否存在错误?如果存在,原因是什么?如果存在错误,要求在计算机上对这个源程序进行调试修改,使之能够

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

3、connect string.n); printf(tt3 exit.n); printf(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; _ printf(input the second string please!

4、n); i=0; _ result=_(a,b); printf(the result is %sn, result); down: ; return 0;(2)请上机运行第(1)题程序,使之能按下面要求输出结果(注:(输入)表示该行数据是键盘输入数据):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,(输

5、入)the result is the more you learn,the more you get.3.跟踪调试源程序请按下面的要求对所给源程序进行操作,并回答问题和排除错误。(1)单步执行源程序。进入strcpy时,watches窗口中s为何值?返回main时,watches窗口中s为何值?(2)排除源程序中的错误,使程序输出结果为:there is a boat on the lake.源程序:#include char * strcpy(char *, char *);int main(void) char a20, b60=there is a boat on the lake.;

6、 printf(%sn, strcpy(a,b); return 0;char * strcpy(char *s, char *t) while (*s+ = *t+) ; return (s);4.程序设计编写并上机调试运行能实现以下功能的程序的函数:(1)已知一长整型变量占4个字节,其中每个字节又分成高4位和低4位。试编写一个程序,从该长整型变量的高字节开始,依次取出每个字节的高4位和低4位并以数字字符的形式进行显示。(2)利用大小为n的指针数组指向用gets函数输入的n行,每行不超过80个字符。试编写一个函数,它将每一行中连续的多个空格字符压缩为一个空格字符。在调用函数中输出压缩空格后的

7、各行,空行不予输出。(3)编写一个程序,输入n和整数,排序后输出。排序的原则由命令行可选参数-d决定,并且有参数-d时按递减顺序排序,否则按递增顺序排序。要求将排序算法定义成函数,利用指向函数的指针使该函数实现递增或递减排序。(main函数参数的处理见下面的第(三)大点)(4)设某个班有N个学生,每个学生修了M课程(用#define定义N,M)。输入M门课程的名称,然后依次输入N个学生中每个学生所修M门课程的成绩,并且都存放到相应的数组中。试编写下列函数:计算每个学生各门课程平均成绩。计算全班每门课程的平均成绩。分别统计低于全班各门课程平均成绩的人数。分别统计全班各门课程不及格的人数和90分以

8、上(含90分)的人数。在调用函数中输出上面各函数的计算结果。(要求都用指针操作,不得使用下标操作。)5.选做题编写并上机调试运行能执行以下功能的程序:(1)设有N位整数和M位小数(N=20,M=10)的两个数据a,b。编程计算a+b并输出结果。如:12345678912345678912.1234567891+98765432109876543210.0123456789(2)编写一个使用复杂声明“char * ( * p2)(const char *, const char *);”的程序。提示:p中元素可为strcmp、strstr等函数名。3、实验步骤及结果1.没有对指针p进行指向就对其

9、赋值,可能导致的结果是,改写内存中不该改写的内容,从而运行出错。应该首先定义一个float变量,先使指针指向这个变量,再对其进行赋值。最终源程序:#include int main(void) float f,*p=&f; scanf(%f, p); printf(%fn, *p); return 0;2.(1)该程序需要定义一个函数指针,用于指向strcpy和strcat,并在后面输入a,b两个字符串。最终的源代码:#include #include int main(void) char *(*p)(char *, const char *); char a80, b80, c160, *

10、result=c; int choice, i; do printf(tt1 copy string.n); printf(tt2 connect string.n); printf(tt3 exit.n); printf(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

11、 string please! n); i=0; while (ai+=getchar()!=n) ; ai-1=0; printf(input the second string please! n); i=0; while (bi+=getchar()!=n) ; bi-1=0; result=p(a,b); printf(the result is %sn, result); down: ; return 0;(2)运行结果: 1 copy string. 2 connect string. 3 exit. input a number(1-3)please!2input the fir

12、st 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.(1)在进入strcpy时,watches窗口中s的值是一些乱码。在返回main时,watches窗口中s的值是空字符串。(2)通过调试发现,a的字符数组长度不够,于是扩充为60,以避免没有足够的空间保存字符串。并且,不能直接输出strcpy(a,b)的结果,而且最终会改变a的值,于是,在printf中输出a即可。最终的源代码

13、:#include char * strcpy(char *, char *);int main(void) char a60, b60=there is a boat on the lake.; strcpy(a,b); printf(%sn, a); return 0;char * strcpy(char *s, char *t) while (*s+ = *t+) ; return (s);4.(1)#include int main(void) long a=4294967295; char *p=&a; int i=0; while (i4)&0x0f, (*p)&0x0f); i+

14、; p+; return 0;(2)#include #define MAXN 100void readlines_and_print(char (*)81, int);int main(void) char aMAXN81,(*p)81=a; int n; printf(Input n:); scanf(%d, &n); getchar(); readlines_and_print(p,n); return 0;void readlines_and_print(char (*p)81, int n) int i=0,j=0,k,flag=0,nonspace=0; while (in) ge

15、ts(*(p+i),i+; i=0; while (in) k=0; nonspace=0; while (k81) 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 (j81) if (*(*(p+i)+j) = 0) break; if (*(*(p+i)+j) = | (*(*(p+i)+j) = t) if (!flag) putchar( ); flag+; else fl

16、ag+; else flag=0; putchar(*(*(p+i)+j); j+; putchar(n); i+; (3)#include #include #define MAXN 100void deal(int *, int);int flag=1;int main(int argc, char *argv) int i,n,aMAXN; void (*p)(int *, int); printf(Input n:); scanf(%d, &n); for (i=0; i1) if (!strcmp(argv1,-d) flag=0; p=deal; p(a,n); return 0;

17、void deal(int *a, int n) int i,j,m,mn,t; for (i=0; in-1; i+) m=ai,mn=i; for (j=i+1; jn; j+) if (flag) if (maj) m=aj,mn=j; t=ai,ai=amn,amn=t; for (i=0; in; i+) printf(%dt, ai);(4)#include #define N 2#define M 2void individual_average(float (*)M);void entire_average(float (*)M);void below_average_num(

18、float (*)M);void failing_outstanding_num(float (*)M);void initial(float (*)M);int main(void) void (*deal)(float (*)M); int n; float aNM,(*p)M=a; initial(p); printf(Please choose the function:n); printf(t1.Calculate and print each students average scores of each course.n); printf(t2.Calculate and pri

19、nt entire average scores of each course of the class.n); printf(t3.Calculate and print the number of students below each courses average score.n); printf(t4.Calculate and print the number of students below 60 and above 90.n); printf(t5.Print the table.n); printf(t6.Exit.n); iNput: scanf(%d, &n); swi

20、tch (n) case 1: deal=individual_average; break; case 2: deal=entire_average; break; case 3: deal=below_average_num; break; case 4: deal=failing_outstanding_num; break; case 5: return 0; break; default: printf(Input illegal!n); return -1; deal(p); goto iNput; return 0;void initial(float (*p)M) int i,

21、j; printf(Input the scores individualy (Use TAB between scores):n); printf(tCourset); for (i=0; iM; i+) printf(%dt, i+1); putchar(n); for (i=0; iN; i+) printf(Student %dt,i+1); for (j=0; jM; j+) scanf(%f, *(p+i)+j); void individual_average(float (*a)M) float averageN,*p=average; int i,j; for (i=0; i

22、N; i+) *(p+i)=0.0; for (j=0; jM; j+) *(p+i)+=*(*(a+i)+j); *(p+i)/=M; for (i=0; iN; i+) printf(Average score of student %d is %f.n, i+1, *(p+i);void entire_average(float (*a)M) float averageM,*p=average; int i,j; for (i=0; iM; i+) *(p+i)=0.0; for (j=0; jN; j+) *(p+i)+=*(*(a+j)+i); *(p+i)/=N; printf(A

23、verage score of course %d is %f.n, i+1, *(p+i); void below_average_num(float (*a)M) float averageM,*p=average; int i,j,belowM,*b=below; for (i=0; iM; i+) *(p+i)=0.0; for (j=0; jN; j+) *(p+i)+=*(*(a+j)+i); *(p+i)/=N; for (i=0; iM; i+) *(b+i)=0; for (j=0; jN; j+) if (*(*(a+j)+i)*(p+i) (*(b+i)+; printf

24、(The number of students below the average score of course %d is %d.n, i+1, *(b+i); void failing_outstanding_num(float (*a)M) int failingM,outstandingM,*f=failing,*o=outstanding,i,j; for (i=0; iM; i+) *(f+i)=*(o+i)=0; for (j=0; jN; j+) if (*(*(a+j)+i) = 90) (*(o+i)+; printf(The numbers of students failing and outstanding of course %d are %d and %d.n, i+1, *(f+i), *(o+i);

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

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