C語言课程设计报告电子版.docVIP

  • 21
  • 0
  • 约1.91万字
  • 约 36页
  • 2016-12-06 发布于重庆
  • 举报
C語言课程设计报告电子版

C语言课程设计报告 姓名:张林 班级:计算11—1班 学号:1106010124 (1、)A类基本题 1、围绕着山顶有10个圆形排列的洞,狐狸要吃兔子,兔子说:“可以,但必须先找到我,我就藏身于这十个洞中的某个洞。你从1号洞找,下次隔1个洞(即3号洞)找,第三次隔2个洞(即6号洞)找,再隔3个…以后在这个圆圈中如此类推,次数不限。”但狐狸从早到晚进进出出了1000次,仍没有找到兔子。问兔子究竟藏在哪个洞里? #includestdio.h int main() {int i,m=0,k=1; int a[10]; for(i=0;i10;i++) a[i]=i+1; for(i=0;i1000;i++) { m=m+k; k=k+1; if(m10) m=m%10; if(m==a[m-1]) a[m-1]=0; } for(i=0;i10;i++) {if(a[i]!=0) printf(兔子在第%d个洞内\n,a[i]); } return 0; } 2、编写名为strdup的函数,此函数使用动态存储分配来产生字符串的副本。 例如调用 p= strdup(str);将为和str相同的字符串分配空间,并且把字符串str的内容复制给新字符串,然后返回指向新字符串的指针,如果非配失败则返回空指针。 #includestdio.h #includemalloc.h char * strdup(char t[]) { char *p; int i; p=(char *)malloc(6*sizeof(char)); for(i=0;i10;i++) {if(t[i]==\n) break;}//开辟新空间 if(i*sizeof(char)6*sizeof(char)) return 0; else {for(i=0;i6;i++) *(p+i)=t[i]; return p;}//是其长度和原来的相同 } void jiancha(char *p) { int i; if(p==0) printf(failure\n); else { printf(copy:); for(i=0;i6;i++) printf(%c,p[i]);//输出 } 已知一个链表中存储了若干名学生的信息,每名学生的信息包括:学号、英语成绩、数学成绩、计算机成绩。现编写一个函数search(),要求根据输入的学生学号,输出他的各科成绩。 #includestdio.h struct student { int num; float English,math,Computer; struct student *next; }; int search(struct student *head,int num) { struct student *p1; if(head==NULL) { printf(\nlist null!\n); return 0; } p1=head; while(num!=p1-num p1-next!=NULL) { p1=p1-next; } if(num==p1-num) printf(\nnum:%d\nEglish:%f\nmath:%f\nComputer:%f\n,p1-num,p1-English,p1-math,p1-Computer); else printf(\n%d not been found!\n,num); return 0; } void main() {int num; struct student a,b,c,d,*head,*p; a.num=01;a.English=79;a.math=79;a.Computer=77; b.num=02;b.English=97;b.math=89;b.Computer=68; c.num=03;c.English=52;c.math=99;c.Computer=54;//输入三个学生信息 head=a; a.next=b; b.next=c; c.next=NULL; p=head;//连接 do { printf(%d %f %f %f\n,p-num,p-English,p-math,p-Computer); p=p-next; }while(p!=NULL); printf

文档评论(0)

1亿VIP精品文档

相关文档