C语言程序设计第四次(2.8)实验报告由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“c语言程序设计第八章”。
C语言程序设计
实验报告
专业
班级
日期
11月26日
成绩
实验组别
第 3(2.7)次实验
指导教师
李开
学生姓名
学号
同组人姓名
实验名称 实验8 指针实验
一、实验目的(1)熟练掌握指针的说明、赋值、使用。
(2)掌握用指针引用数组的元素,熟悉指向数组的指针的使用。
(3)熟练掌握字符数组与字符串的使用,掌握指针数组及字符指针数组的用法。(4)掌握指针函数与函数指针的用法。(5)掌握带有参数的main函数的用法。
二、实验任务
8.2 实验内容及要求 1.源程序改错
2.源程序完善、修改、替换 3.跟踪调试 4.程序设计 5.选做题
8.3 指定main函数的参数
三、实验步骤及结果
(要求给出源程序和程序运行结果。另外,根据实验内容,记录编辑、编译、链接、调试程序的操作过程和实验现象)8.2 实验内容及要求 1.源程序改错
下面程序是否存在错误?如果存在,原因是什么?如果存在错误,要求在计算机上对这个例子程序进行调试修改,使之能够正确执行。#include void main(void){ float *p;scanf(“%f”,p);printf(“%fn”,*p);}
存在,错误为指针一开始没有初始化,而sacnf传入的是float型指针指向的地址,我们并不知道系统能给我们分配什么地址,所以说我们输入的地址很有可能使程序崩溃。修改后代码:
#include int main(void){ float *p;float a[10];//这里可以换成其他数字 p=&a[0];scanf(“%f”,p);printf(“%fn”,*p);return 0;}
2.源程序完善、修改、替换
(1)下面的程序通过函数指针和菜单选择来调用字符串拷贝函数或字符串连接函数,请在下划线处填写合适的表达式、语句、或代码片段来完善该程序。#include #include void main(void){ char*(*p)(char a[],char b[]);char a[80],b[80],c[160],*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 string please!n”);i=0;gets(a);printf(“input the second string please!n”);i=0;gets(b);
result= p(a,b);printf(“the result is %sn”,result);down:;}(2)请上机运行第(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.跟踪调试
#include char *strcpy(char *,char *);void main(void){ char a[20],b[60]=“there is a boat on the lake.”;printf(“%sn”,strcpy(a,b));} char *strcpy(char *s,char *t){ while(*s++=*t++);return(s);}(1)单步执行。进入strcpy时watch窗口中s为何值?返回main时, watch窗口中s为何值?
进入strcpy时:
返回main时:
(2)排除错误,使程序输出结果为: there is a boat on the lake.#include void *strcpy(char *,char *);int main(void){ char a[30],b[60]=“there is a boat on the lake.”;strcpy(a,b);printf(“%sn”,a);return 0;} void *strcpy(char *s,char *t){ while(*t!=' '){ *s++=*t++;} *s=' ';//将函数类型设置为void型,然后不再返回直接打印a字符串即可 }
4.程序设计
(1)一个长整型变量占4个字节,其中每个字节又分成高4位和低4位。试从该长整型变量的高字节开始,依次取出每个字节的高4位和低4位并以数字字符的形式进行显示。#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 = p[i] & 0x0f;if(low_half
return 0;}
(2)利用大小为n的指针数组指向用gets函数输入的n行,每行不超过80个字符。编写一个函数,它将每一行中连续的多个空格字符压缩为一个空格字符。在调用函数中输出压缩空格后的各行,空行不予输出。
#include #define N 10 void reducespace(char *p,int n);int main(){ int i;char *p[N],arr[N][81];printf(“Input %d-line strings:n”,N);for(i = 0;i
*(p + i)= arr[i];
printf(“the %d line:”, i + 1);
gets(*(p + i));} printf(“nnafter compreion:n”);for(i = 0;i
reducespace(*(p + i),i);} putchar(10);return 0;} void reducespace(char *p,int n)//削减空格的函数,将多行空格压缩成一个空格 { int flag = 1, i, j = 0, sum = 0;char temp[81];for(i = 0;*(p + i)!= ' ';i++){
if(*(p + i)!= ' '&&flag)
{
temp[j++] = *(p + i);
sum++;
}
if(*(p + i)== ' '&&flag)
flag = 0;
if(*(p + i)!= ' '&&!flag)
{
temp[j++] = ' ';
temp[j++] = *(p + i);
flag = 1;
sum++;
}
temp[j] = ' ';} if(sum)
printf(“The %d line:%sn”, n+1,temp);else
printf(“Of the %d line there are all spacesn”, n + 1);}
(3)输入n个整数,排序后输出。排序的原则由命令行可选参数-d决定,有参数-d时按递减顺序排序,否则按递增顺序排序。要求将排序算法定义成函数,利用指向函数的指针使该函数实现递增或递减排序。(main函数参数的处理见8.3节)
#include #include #include int main(int argc,char *argv[]){ void downsort(char* *p,int m);void upsort(char * *q,int k);int n=0;while(n*q[j+1])t=*q[j],*q[j]=*q[j+1],*q[j+1]=t;for(i=0;i
(4)设某个班有N个学生,每个学生修了M门课程(用#define定义N、M)。输入M门课程的名称,然后依次输入N个学生中每个学生所修的M门课程的成绩并且都存放到相应的数组中。编写下列函数:
a.计算每个学生各门课程平均成绩; b.计算全班每门课程的平均成绩;
c.分别统计低于全班各门课程平均成绩的人数;
d.分别统计全班各门课程不及格的人数和90分以上(含90分)的人数。
在调用函数中输出上面各函数的计算结果。(要求都用指针操作,不得使用下标操作。)#include #define M 2//course #define N 2//students int main(void){
char courses[M][30],students[N][20];
float tables[N][M];
int c,s;
char *co=&courses[0][0];//courses[2]
co+2*30
char *st=&students[0][0];
float *gr=&tables[0][0];
for(c=0;c
{
printf(“Please Input The %d Coursen”,c+1);
scanf(“%s”,courses[c]);
}//course names
for(s=0;s
{
printf(“Please Input The %d Studentn”,s+1);
scanf(“%s”,students[s]);
}//students names
for(s=0;s
{
printf(“For students : %sn”,students[s]);
printf(“Input his/her grades:n”);
for(c=0;c
{
printf(“Please Input The %s n”,courses[c]);
scanf(“%f”,&tables[s][c]);
}
}//tables
float sum=0;
for(s=0;s
{
sum=0;
printf(“the average grades of %s:n”,st+s*20);
for(c=0;c
{
sum+=*(gr+s*M+c);
}
printf(“%fn”,sum/M);
}//(1)
float sum_=0;
for(c=0;c
{
sum_=0;
printf(“the average grades of %s:n”,co+c*30);
for(s=0;s
{
sum_+=*(gr+s*M+c);
}
printf(“%fn”,sum_/N);
}//(2)
for(c=0;c
{
sum_=0;
printf(“the number whose grades under average of %sn”,co+c*30);
int cou=0;
for(s=0;s
{
sum_+=*(gr+s*M+c);
}
for(s=0;s
{
if(*(gr+s*M+c)
{cou++;}
}
printf(“%dn”,cou);
cou=0;
}//(3)
for(c=0;c
{
printf(“the number whose grades under 60 of %sn”,co+c*30);
int cou=0;
for(s=0;s
{
if(*(gr+s*M+c)
cou++;
}
printf(“%dn”,cou);
cou=0;
}
for(c=0;c
printf(“the number whose grades higher than 90 of %sn”,co+c*30);
int cou=0;
for(s=0;s
{
if(*(gr+s*M+c)>=90)
cou++;
}
printf(“%dn”,cou);
cou=0;
}
return 0;}
5.选做题
(1)设有N位整数和M位小数(N=20,M=10)的数据a,b。编程计算a+b并输出结果。如:***78912.1234567891 + ***43210.0123456789 #include “stdio.h” #include “ctype.h” #define N 100 /* N表示参与运算数据最长的长度 */ void add_decimals(char *a,char *b,int *c,int n,char *d);void add_inte(char *a,char *b,int c,int n,char *d);int main(void){ char a1[N],b1[N],c1[N+1],a2[N],b2[N],c2[N+1];char c;int q,w,e,r;//to count q=0;while((c=getchar())!='.'){ if(isdigit(c))*(a1+q)=c;q++;} *(a1+q)=' ';w=0;while((c=getchar())!='n'){ if(isdigit(c))*(b1+w)=c;w++;} *(b1+w)=' ';e=0;char d;while((d=getchar())!='.'){ if(isdigit(d))*(a2+e)=d;e++;} *(a2+e)=' ';r=0;while((d=getchar())!='n'){ if(isdigit(d))*(b2+r)=d;r++;} *(b2+r)=' ';//input
int max,max_;if(w>r){ max=w;int t;for(t=r;te){ max_=q;int y;for(y=0;y
} else { max_=e;int y;for(y=0;y
return 0;} void add_decimals(char *a,char *b,int *c,int n,char *d){ int o,jin=0;for(o=n-1;o>-1;o--){ if(*(a+o)+*(b+o)-2*'0'+jin>=10){*(d+o)=*(a+o)+*(b+o)+jin-1*'0'-10;jin=1;} else { *(d+o)=*(a+o)+*(b+o)-'0';jin=0;}
} *(d+n)=' ';*c=jin;} void add_inte(char *a,char *b,int c,int n,char *d){ int o;for(o=n-1;o>0;o--){ if(*(a+o)+*(b+o)-2*'0'+c>=10){*(d+o+1)=*(a+o)+*(b+o)-1*'0'-10;c=1;} else { *(d+o+1)=*(a+o)+*(b+o)-1*'0';c=0;}
} if(*a+*b+c-2*'0'>=10){ *(d+1)=*a+*b+c-'0'-10;*d='1';} else {*(d+1)=*a+*b+c-'0';*d=' ';} }
(2)编写使用复杂声明char *(*p[2])(const char *,const char *);的程序。提示:p中元素可为strcmp、strstr等函数名。8.3 指定main函数的参数
选择“project/ set programs' arguments…”菜单命令,即可打开图2.12所示的对话框,在“Program arguments”文本框中输入main函数的参数。注意只输入命令行中文件名后的参
数,文件名不输人。
图2.12 输入main
函数的参数
四、实验体会
好好写代码,不该跟老师吵,星期三的晚上应该按课程来讲只是写代码,不能干其他事情,对此我深刻检讨。