文档详情

C语言实验报告格式.doc

发布:2017-01-01约3.72千字共7页下载文档
文本预览下载声明
学号:22201231505236 姓名:李佳琪 班级:12物理班 实验题目 实验名称:函数 实验目的: 实验内容: 10.4有n个整数,使前面各数顺序向后移m个位置,最后m个数变成前面m个数,见图。写一函数:实现以上功能,在主函数中输入n个数和输出调整后的n个数。 #define N 10 void shift(p,x) float *p;int x; {float a[N],*q,*o;int i; o=a;q=p; for(i=0;ix;i++) *(o+i)=*(q+N-x+i); for(p=p+N-1;p=q;p--) *p=*(p-x); for(i=0;ix;i++) *(q+i)=*(o+i); return;} main() {float shuzhu[N],*u,*v; int h,i;u=v=shuzhu; scanf(%f,h); for(;uv+N;u++) scanf(%f,u); shift(v,h); for(u=v;uv+N;u++) printf(%.2f ,*u); printf(\n); } 10.5有n人围成一圈,顺序排号。从第1个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来的第几号的那位。 #define N 5 main() {int i,j,k,a[N+1],*p; for(i=0,p=a;p=a+N;i++,p++) *p=i; p=a+1;k=N; for(i=0,j=1;k!=1;j++) {if(p(a+N)) p=a+1; if(*p!=0) i++; if((i-3)==0) {*p=0;i=0;k--;} p++; } for(i=1;i=N;i++) if(a[i]!=0) printf(The last number is %d\n,a[i]);} 10.7有一字符串,包含n个字符。写一个函数,将此字符串中从第m个字符开始的全部字符复制成为另一个字符串。 解: main ( ) {int m; char * str1[20], * str2[20]; printf(“input string:”); gets(str1); printf(“Which character that begin to copy?”); scanf(“%d”,m); if (strlen(str1)m) printf(“input error!”); else {copystr(str1,str2,m); printf(“result:%s”,str2); } } copystr(char * p1,char * p2,int m) /* 字符串部分复制函数 */ {int n; n=0; while (nm-1) {n++; p1++; } while (* p1!=’\0’) {* p2= * p1; p1++; p2++; } * p2=’\0’; } 10.11在主函数中输入10个等长的字符串。用另一个函数对它们排序,然后在主函数输出这10个已排好序的字符串。 解: # include string.h main ( ) {void sort(char s[ ][ ]); int i; char str[10][6]; printf(“Input 10 strings:\n”); for (i=0;i10;i++) scanf(“%s”,str[i]); sort(str); printf(“Now,the sequence is:\n”); for (i=0;i10;i++) printf(“%s\n”,str[i]); } void sort(char s[10][6]) {int i,j; char * p,temp[10]; p=temp; for (i=0;i9;i++) for (j=0;j9-i;j++) if(strcmp(s[j],s[j+1])0) {strcpy(p,s[j]); strcpy(s[j],s[+j+1]); strcpy(s[j+1],p); } } 10.15有一个班4个学生,5门课。(1)求第一门课的平均分;(2)找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩及平均成绩;(3)
显示全部
相似文档