C语言程序设计-实验第六次上机实验报告.doc

上传人:jian****018 文档编号:8341391 上传时间:2020-03-28 格式:DOC 页数:34 大小:451KB
返回 下载 相关 举报
C语言程序设计-实验第六次上机实验报告.doc_第1页
第1页 / 共34页
C语言程序设计-实验第六次上机实验报告.doc_第2页
第2页 / 共34页
C语言程序设计-实验第六次上机实验报告.doc_第3页
第3页 / 共34页
点击查看更多>>
资源描述
7.第六次实验C语言程序设计 实验报告专业 计算机科学与技术 班级 信安1302班 日期 2014.5.24 成绩 实验组别 第 6 次实验 指针实验&结构与联合实验 指导教师 学生姓名 学号 同组人姓名 实验名称 指针实验&结构与联合实验 7.1实验目的1.熟练掌握指针的说明、赋值、使用。2.掌握用指针引用数组的元素,熟悉指向数组的指针的使用。3.熟练掌握字符数组与字符串的使用,掌握指针数组及字符指针数组的用法。4.掌握指针函数与函数指针的用法。5.掌握带有参数的main函数的用法。6.熟悉和掌握结构的说明和引用,结构的指针,结构数组,以及函数中使用结构的方法。7.掌握动态储存分配函数的用法,掌握自引用结构和单向链表的创建,遍历,结点的增删,查找等操作。8.了解字段结构和联合的用法。7.2实验内容(一)设计一个函数reverse(a, n),将一维数组a的值逆置。如,逆置前,A:20、10、90、59、60、80、70,逆置后,A:70、80、60、59、90、10、20。源程序如下:shangji6chengxu29#include void sort(int *a,int n);int main() int x100,n,i,*p=x; printf(enter n:); scanf(%d,&n); printf(enter numbers:); for(i=0;in;i+) scanf(%d,p+i); sort(x,n); for(i=0;in;i+) printf(%4d,*(p+i); return 0;void sort(int *a,int n) int i,j,t; for(i=0;in-1;i+) for(j=0;jn-1-i;j+) t=*(a+j);*(a+j)=*(a+j+1);*(a+j+1)=t; 实验步骤:1. 定义一个数组存放数据,实现数据的输入和存储。2. 定义一个子函数进行排序,用把数据两两交换来实现。调试及编译过程:测试数据:任意选取一组数据:出现的不足及修改:无 运行结果:出现预期的结果。(二)设计一个函数BubbleSort(a, n),采用冒泡排序算法,实现一维数组的整数进行排序的功能。源程序如下:shangji6chengxu30#include void BubbleSort(int *a,int n);int main() int a100,n,i; printf(enter n:); scanf(%d,&n); printf(enter numbers:); for(i=0;in;i+) scanf(%d,&ai); BubbleSort(a,n); for(i=0;in;i+) printf(%6d,ai); return 0;void BubbleSort(int *a,int n) int t,*p=a,i,j; for(i=0;in-1;i+) for(j=0;j*(p+j+1) t=*(p+j);*(p+j)=*(p+j+1);*(p+j+1)=t; 实验步骤:1. 定义数组实现数据的输入和存放。2. 定义一个子函数实现数据的冒泡法排序。调试及编译过程:测试数据:任意选取一组数据(按从小到大顺序排列)测试数据:任意选取一组数据(按任意顺序排列)出现的不足及修改:无 运行结果:出现预期的结果。 (三)设计一个一个函数find(a, n, x),实现对在一个数组的整数进行查找功能。如果给定值x在数组a中,返回其所在的位置(即下标值),否则返回-1。源程序如下:shangji6chengxu31#include int find(int *a,int n,int x);int main() int a100,n,x,i; printf(enter n:); scanf(%d,&n); printf(enter numbers:); for(i=0;in;i+) scanf(%d,(a+i); printf(enter x:); scanf(%d,&x); if(find(a,n,x)=-1) printf(not found!); else printf(the number is %d,find(a,n,x); return 0;int find(int *a,int n,int x) int i,*p=a; for(i=0;in;i+) if(*(p+i)=x) return (i); return (-1);实验步骤:1. 定义一个数组实现数据的输入和存放。2. 定义一个子函数实现查找的功能并在主函数中调用该函数。调试及编译过程测试数据:任意选取一组数据,后来输入的数能被找到测试数据:任意选取一组数据(后来输入的数不能被找到)出现的不足及修改:无 运行结果:出现预期的结果(四)输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。如果有多对数字的和等于输入的数字,输出任意一对即可。例如,输入数组1、2、4、7、11、15和数字15。由于4+11=15,因此,输出4和11。源程序如下:shangji6chengxu32#include int main() int a100,n,x,i,j; printf(enter n:); scanf(%d,&n); printf(enter numbers:); for(i=0;in;i+) scanf(%d,a+i); printf(enter x:); scanf(%d,&x); for(i=0;in;i+) for(j=0;jn;j+) if(*(a+i)+*(a+j)=x) printf(%d+%d=%d,*(a+i),*(a+j),x); return 0; return 0;实验步骤:1 定义一个数组实现数据的输入和存放。2 用两层循环逐一检验,遇到满足条件的即输出。调试及编译过程:测试数据:任意选取一组数据:出现的不足及修改:无 运行结果:出现预期的结果。(五)检验并打印魔方矩阵在下面的55阶魔方矩阵中,每一行、每一列、每一对角线上的元素之和都是相等的,试编写程序将任意55阶矩阵中的元素存储到一个二维整型数组中,然后检验其是否为魔方矩阵,并将其按如下格式显示到屏幕上。17241815235714164613202210121921311182529源程序如下:shangji6chengxu33#include int main() int a55,i,j,s=0,s1=0,s2,s3; printf(enter numbers:); for(i=0;i5;i+) for(j=0;j5;j+) scanf(%d,*(a+i)+j); for(i=0;i5;i+) for(j=0;j5;j+) printf(%4d,*(*(a+i)+j); printf(n); for(i=0;i5;i+) s+=*(*(a+i)+i); for(i=0;i5;i+) s1+=*(*(a+i)+4-i); if(s=s1) for(i=0;i5;i+) s2=0; for(j=0;j5;j+) s2+=*(*(a+i)+j); if(!(s2=s) printf(not!); return 0; for(i=0;i5;i+) s3=0; for(j=0;j5;j+) s3+=*(*(a+j)+i); if(!(s3=s) printf(not!); return 0; else printf(not!); return 0;实验步骤:1.定义数组实现数据的输入和存放。2.分别对二维矩阵的行列主次对角线进行求和运算,判断是否为魔方矩阵。调试及编译过程:测试数据:输入一组魔方矩阵的数据出现的不足及修改:无 运行结果:输出预期结果。(六)编写一个函数swap(x,y),分别采用3种不同的算法实现两个变量值的交换功能。源程序1如下:shangji6chengxu34shangji6chengxu34shangji6chengxu34#include void swap(int *x,int *y);int main()int a,b;scanf(%d%d,&a,&b);swap(&a,&b);printf(%dt%d,a,b);return 0;void swap(int *x,int *y)int t;t=*x;*x=*y;*y=t;源程序2如下:#include void swap(int *a,int *b);int main()int x,y;scanf(%d%d,&x,&y);swap(&x,&y);printf(%dt%d,x,y);return 0;void swap(int *x,int *y) *x=(*x)+(*y);*y=(*x)-(*y);*x=(*x)-(*y);源程序3如下:#include void swap(int *x,int *y);int main()int x,y;scanf(%d%d,&x,&y);swap(&x,&y);printf(%dt%d,x,y);return 0;void swap(int *x,int *y) *x=*x*y;*y=*x*y;*x=*x*y;实验步骤:1 选取三种合适的方法进行交换。2 因为要用到子函数,于是将这三种方法中的参数均改为指针操作。调试及编译过程:测试数据:任意选取一组数据测试数据:将以上数据交换输入出现的不足及修改:无 运行结果:出现预期的结果。(七)设计一个计算MAXa,b的程序,要求a和b,以命令行中参数形式给出。源程序如下:shangji6chengxu35#includeint main(int argc,char *argv)int a=atoi(argv1);int b=atoi(argv2);if(ab) printf(%d,a);else printf(%d,b);return 0;实验步骤:直接进行比较并输出较大值调试及编译过程:测试数据:任意选取一组数据测试数据:交换上面两数出现的不足及修改:刚开始没有将字符型换成整型导致错误,后来使用atoi函数解决。运行结果:改正后输出预期的结果。(八)设计一个函数create(L),其功能是建立一个“先进后出”的链表。源程序如下:shangji6chengxu36#include #include struct List int i; struct List * next;typedef struct List mylist;int main(int argc, const char * argv) struct List * h = NULL; mylist * p; h = (mylist*)malloc(sizeof(mylist); h-i = 0; h-next = NULL; int n; scanf(%d,&n); while (n) int temp; scanf(%d,&temp); p = (mylist*)malloc(sizeof(mylist); p-i = temp; p-next = h; h = p; -n; while(h&(h-i!=0) printf(%8dt,h-i); h=h-next; return 0;实验步骤:1. 定义结构型的变量,创建链表。2. 循环定义链表的起点和终点,定义结构的数据域。3. 输出。调试及编译结果:测试数据:任意选取一组数据测试数据:将上述数据反向输入出现的不足及修改:无 运行结果:输出预期的结果。(九)设计一个函数insert(L,i,e),其功能是在链表L中的第i个元素之间插入新元素e。源程序如下:shangji6chengxu37#include #include #include struct List int i; struct List * next;typedef struct List mylist;mylist* insert(mylist * L ,int i,int e) mylist * pri =L; int j; for (j = 1; j next; mylist * p = (mylist*)malloc(sizeof(mylist); p-i = e; p-next = pri-next; pri-next = p; return L;int main(int argc, const char * argv) struct List * h = NULL; int insert_number,locate; printf(enter the number you want to insert in:); scanf(%d,&insert_number); printf(enter the locate you want to insert:); scanf(%d,&locate); mylist * p; h = (mylist*)malloc(sizeof(mylist); h-i = 0; h-next = NULL; int n; printf(enter the number of list:); scanf(%d,&n); while (n) int temp; scanf(%d,&temp); p = (mylist*)malloc(sizeof(mylist); p-i = temp; p-next = h; h = p; -n; h = insert(h,locate,insert_number); mylist * temp = h; while (temp&(temp-i!=0) printf(%d ,temp-i); temp = temp-next; return 0;实验步骤:1. 根据上一个实验写出创建链表的过程。2. 定义子函数进行数据的插入,在此过程中,将要插入的数放入一个新的结构性变量中,并将此结构性变量的指针域指向原链表中的插入位置,使链表按原顺序继续下去。调试及编译过程:测试数据:任意选取一组数据测试数据:任意选取一组数据出现的不足及修改:无 运行结果:出现预期的结果。(十)设计一个函数delete(L,i),其功能是删除在链表L中的第i个元素。源程序如下:shangji6chengxu38#include #include #include struct List int i; struct List * next;typedef struct List mylist;mylist* insert(mylist * L ,int i) mylist * pri =L; int j; for (j = 1; j next; pri-next=pri-next-next; return L;int main(int argc, const char * argv) struct List * h = NULL; int locate; printf(enter the locate you want to delete:); scanf(%d,&locate); mylist * p; h = (mylist*)malloc(sizeof(mylist); h-i = 0; h-next = NULL; int n; printf(enter the number of list:); scanf(%d,&n); while (n) int temp; scanf(%d,&temp); p = (mylist*)malloc(sizeof(mylist); p-i = temp; p-next = h; h = p; -n; h = insert(h,locate); mylist * temp = h; while (temp&(temp-i!=0) printf(%d ,temp-i); temp = temp-next; return 0;实验步骤:1. 根据第一个创建链表的程序创建链表。2. 定义一个子函数完成删除第i个元素的功能。调试及编译过程:测试数据:任意选取一组数据测试数据:任意选取一组数据出现的不足及修改:无 运行结果:出现预期的结果。(十一)设计一个程序,利用链表输出约瑟夫环的结果序列。注释:约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3.n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。源程序如下:shangji6chengxu39#include #include struct list int data; struct list *next;struct list *create(int n) int i=1; struct list *head=NULL,*tail; head=(struct list *)malloc(sizeof(struct list); head-data=i; tail=head; while(n-1) tail-next=(struct list *)malloc(sizeof(struct list); tail=tail-next; tail-data=+i; n-; tail-next=head; return head;int main() int n,m,t=0,k=0; struct list *mylist=NULL,*mylist1=NULL; printf(enter the number of people:n); scanf(%d,&n); printf(enter m:n); scanf(%d,&m); mylist=create(n); mylist1=mylist; while(t!=n-1) k+; if(k=m) mylist1-next=mylist1-next-next; t+; k=0; mylist1=mylist; mylist=mylist-next; printf(the last number is %d,mylist-data); return 0;实验步骤:1. 创建链表,将链表中各部分的数据域分别设为1,2,n,其中n又键盘端输入。2. 输入人数和需要删除的数,用到上一个程序的思想,在将要删除的数前,将上一个指针直接指向下一个而不指向该数。调试及编译过程:测试数据:任意输入一组数据测试数据:任意输入一组数据出现的不足及修改: 刚开始没有注意到若直接在删除的过程中采用一个变量进行处理,则实际上去掉的是该数的下一个数,因此在修改的过程中加上了另外一个变量,使后来加上的变量指向当前变量的前一个,利用它进行删除工作,而当前变量仍起到控制循环的作用。运行结果: 修改后能输出预期的结果。(十二)设计一个程序完成文件的复制(COPY)功能。源程序如下:shangji6chengxu40#include #include int main() char c; FILE *fin,*fout; fout=fopen(out.txt,w+); fin=fopen(in.txt,r+); while(c=fgetc(fin)!=EOF) fprintf(fout,%c,c); fclose(fin); fclose(fout); return 0;实验步骤:1. 定义两个文件,一个为只读模式,一个为只写模式。2. 将只读模式的文件中的内容复制到只写模式的文件中,这一切过程都通过函数完成。调试及编译过程:测试数据:任意输入一段文字以下是in.txt中预先留有的文本内容。以下是在out.txt中的复制内容。出现的不足及修改:无 运行结果:输出预期的结果。(十三)教材66面源程序1改错源程序的错误在于没有使指针有具体的指向,使指针悬挂。修改后程序如下:shangji6jiaocai66.1#include int main() float a10,*p=a; int i; for(i=0;i10;i+) scanf(%f,p+i); printf(%f,*p); return 0;调试及编译过程:测试数据:任意选取一组数据出现的不足及修改:无 运行结果:输出预期的结果。(十四)教材68面4(1)输入一个长整型数据,依次取出每个字节的高四位和低四位并以数字字符的形式显示出来。源程序如下:shangji6jiaocai68.4(1)#include int main() int i,x; char *p,low,high; printf(enter a number:); scanf(%d,&x); p=(char *)&x; p+=3; for(i=0;i4;i+) printf(bite:n); low=(*p)&0x0f; if(low4)&0x0f; if(high10) high=high+0; else high=high-10+A; printf(%c %cn,high,low); p-; return 0;调试及编译过程:测试数据:任意输入一组数据测试数据:任意输入一组数据出现的不足及修改:无 运行结果:出现预期的结果。(十五)教材68面4(2)利用大小为n的指针数组指向用get函数输入的n行,每行不超过80个字符,将输入过程中的多个空格字符压缩成一个空格字符,空行不予输出。源程序如下:shangji6jiaocai68.4(2)#include void ys(char *a81);int main() char text1081,*pc81; int i=0; printf(please enter your texts:n); for(i=0;i10;i+) pci=gets(texti); ys(pc); for(i=0;i10;i+) if(*(pc+i) printf(%sn,*(pc+i); return 0;void ys(char *a81) int i,j,k,t,flag; char b81; for(i=0;i10;i+) flag=0;j=0;t=0; while(*(*(a+i)+j)!=0) if(*(*(a+i)+j)!= ) flag=1; *(b+t)=*(*(a+i)+j); j+; t+; else if(flag) *(b+t)=*(*(a+i)+j); flag=0; j+; t+; else if(!flag) j+; *(b+t)=0; for(k=0;k=t;k+) *(*(a+i)+k)=*(b+k); if(!*b) *(a+i)=NULL; 调试及编译过程测试数据:任意输入一段文字(其中包括一行的开始有无空格,有几个空格的情况,和空行的情况)出现的不足及修改:无 运行结果:出现预期的结果。(十六)教材68面4(3)将一个班n个学生所修m门课程的成绩输入,并计算一些数据。源程序如下:shangji6jiaocai68.4(3)#include #define S 5#define C 5void stu_ave(char nameS10,int scoreSC,int score_sS);void cou_ave(char courseC10,int scoreSC,int score_cC);void stu_low_ave(char courseC10,int scoreSC,int score_cC);void stu_fail_90(char courseC10,int scoreSC);int main() char nameS10,courseC10; int scoreSC,score_sS=0,0,0,0,0,score_cC=0,0,0,0,0; int i,j; for(i=0;iC;i+) printf(enter name of courses:); scanf(%s,*(course+i); for(i=0;iS;i+) printf(enter name of student:); while(getchar()!=n); scanf(%s,*(name+i); printf(enter the five scores of the student:); for(j=0;jC;j+) scanf(%d,*(score+i)+j); for(i=0;iS;i+) for(j=0;jC;j+) *(score_s+i)+=*(*(score+i)+j); for(j=0;jC;j+) for(i=0;iS;i+) *(score_c+i)+=*(*(score+i)+j); stu_ave(name,score,score_s); cou_ave(course,score,score_c); stu_low_ave(course,score,score_c); stu_fail_90(course,score); return 0;void stu_ave(char nameS10,int scoreSC,int score_sS) int i; for(i=0;iS;i+) printf(the average of %s is %.2lfn,*(name+i),*(score_s+i)*1.0/C); void cou_ave(char courseC10,int scoreSC,int score_cC) int i; for(i=0;iC;i+) printf(the average of %s is %.2lfn,*(course+i),*(score_c+i)*1.0/S); void stu_low_ave(char courseC10,int scoreSC,int score_cC) int i,j,num_lowS=0,0,0,0,0; float score_c_aveS; for(i=0;iC;i+) *(score_c_ave+i)=*(score_c+i)*1.0/S; for(j=0;jC;j+) for(i=0;iS;i+) if(*(*(score+i)+j)*(score_c_ave+j) (*(num_low+j)+; printf(in %s the number of students whose scores are lower than average is %dn,*(course+j),*(num_low+j); void stu_fail_90(char courseC10,int scoreSC) int i,j,stu_failC=0,0,0,0,0,stu_90C=0,0,0,0,0; for(j=0;jC;j+) for(i=0;iS;i+) if(*(*(score+i)+j)=90) (*(stu_90+j)+; printf(in %s the number of students who had failed is %dn,*(course+j),*(stu_fail+j); printf(in %s the number of students whose scores are higher than 90 is %dn8,*(course+j),*(stu_90+j); 调试及编译过程:测试数据:任意输入一组数据出现的不足及修改:无 运行结果:输出预期的结果。(十七)教材71面3(1)定义一个字段结构,同时设计八个函数,如果bit0为1,调用p_fun0,根据优先级依次调用函数。源程序如下:shangji6jiaocai71.3(1)#include struct bites unsigned short bite0:4,bite1:4,bite2:4,bite3:4,bite4:4,bite5:4,bite6:4,bite7:4;void f0(unsigned short b) printf(in f0 the function %d is called!n,b);void f1(unsigned short b) printf(in f1 the function %d is called!n,b);void f2(unsigned short b) printf(in f2 the function %d is called!n,b);void f3(unsigned short b) printf(in f3 the function %d is called!n,b);void f4(unsigned short b) printf(in f4 the function %d is called!n,b);void f5(unsigned short b) printf(in f5 the function %d is called!n,b);void f6(unsigned short b) printf(in f6 the function %d is called!n,b);void f7(unsigned short b) printf(in f7 the function %d is called!n,b);int main() struct bites mybite; void (*p_fun8)(unsigned short b)=f0,f1,f2,f3,f4,f5,f6,f7; if(mybite.bite0) p_fun0(mybite.bite0); if(mybite.bite1) p_fun1(mybite.bite1); if(mybite.bite2) p_fun2(mybite.bite2); if(mybite.bite3) p_fun3(mybite.bite3); if(mybite.bite4) p_fun4(mybite.bite4); if(mybite.bite5) p_fun5(mybite.bite5); if(mybite.bite6) p_fun6(mybite.bite6); if(mybite.bite7) p_fun7(mybite.bite7); return 0;调试及编译过程:出现的不足及修改:无 运行结果:出现预期的结果。(十八)教材72面3(2)建立学生成绩单,完成指定功能。源程序如下:shangji6jiaocai72.3(2)#include #include struct grade int number; char name20; int e_score; int m_score; int p_score; int c_score; struct grade *next;int main() struct grade *tail=NULL,*head=NULL; int n1,i; char name120; int n,e,m,p,c; printf(enter n1:); scanf(%d,&n1); tail=(struct grade *)malloc(sizeof(struct grade); printf(enter the number of the student:n); scanf(%d,&n); printf(enter the name :n); scanf(%s,name1); printf(enter the English score of the student:n); scanf(%d,&e); printf(enter the math score:n); scanf(%d,&m); printf(enter the physics score:n); scanf(%d,&p); printf(enter the c score:n); scanf(%d,&c); tail-number=n; strcpy(tail-name,name1); tail-e_score=e; tail-m_score=m; tail-p_score=p; tail-c_score=c; head=tail; while(n1-1) tail-next=(struct grade *)malloc(sizeof(struct grade); tail=tail-next; printf(enter the number of the student:n); scanf(%d,&n); printf(enter the name :n); scanf(%s,name1); printf(enter the English score of the student:n); scanf(%d,&e); printf(enter the math score:n); scanf(%d,&m); printf(enter the physics score:n); scanf(%d,&p); printf(enter the c score:n); scanf(%d,&
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 管理文书 > 工作总结


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!