C程序设计(谭浩强)课后习题答案选讲.ppt_第1页
C程序设计(谭浩强)课后习题答案选讲.ppt_第2页
C程序设计(谭浩强)课后习题答案选讲.ppt_第3页
C程序设计(谭浩强)课后习题答案选讲.ppt_第4页
C程序设计(谭浩强)课后习题答案选讲.ppt_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Unit 11,C Language Program,11.2,写一个函数days,实现上面的计算。由主函数将年、月、日传递给days 函数,计算后将日数传回主函数输出。,解法一: struct y_m_d /定义结构体 int year: int month; int day; date; intdays(struct y_m_d date1) /*形参date1为结构体struct y_m_d类型*/ int sum; switch(data.month) case 1:sum=date1.day; break; case 2:sum=date1.day+31; break; case 3:sum=date1.day+59; break; case 4:sum=date1.day+90; break; case 5:sum=date1.day+120; break; case 6:sum=date1.day+151; break; case 7:sum=date1.day+181; break;,case 8:sum=date1.day+212; break; case 9:sum=date1.day+243; break case 10:sum=date1.day+273; break case 11:sum=date1.day+304; break case 12:sum=date1.day+334; break If(date1.year%4=0 注意:在main函数中的printf函数用days(date)调用days函数,其返回值就是天数。,解法二: struct y_m_d /定义结构体 int year: int month; int day; date; void main() int days(int,int,int);/对days函数的声明 int i,day_sum; printf(“Input year,month,day:”); scanf(“%d,%d,%d”,for(i=1;i=3 ) day_sum+=1; return day_sum; 11.5 ,有10个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入10个学生的数据,要求输出3门课的总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课成绩、平均分) 解:N-S图如下图 变量说明: max:当前最好成绩;maxi:当前最好成绩对应的下标序号;sum:第i个学生的总成绩,#define N 10 struct student char num6 char name8 int score4 float avr; stuN; main() int i,j,max,maxi,sum; float average; /*输入*/ for(i=0;iN;i+) printf(“nInput scores of student %d:n”,i+1); printf(“NO.:”); scanf(“%s”,stui.num); printf(“name”); scanf(“%s”,);,for(j=0;j3;j+) printf(“score %d:”,j+1); scanf(“%d”, ,if(summax) max=sum; maxi=i; average/=N; /*打印*/ printf(“NO. name score1 score2 score3 averagen”); for(i=0;iN;i+) printf(“%5s%10s”,stui.num, ); for(j=0;j3;j+) printf(“%9d”,stui.scorej); printf(“%8.2fn”,stui.avr); printf(“average=%6.2fn”,average); printf(“The highest score is:%s,score total:%d.”,max); ,11.6,编写一个函数new,对n个字符开辟连续的存储空间,此函数应返回一个指针,指向字符串开始的空间。New(n)表示分配n个字节的内存空间,解: new函数是分配n个连续字符的存储区,为此,应先开辟一个足够大的连续存储区。设置字符数组newbuf1000,new函数将在此上进行操作。 newp是指向可用存储区的起始地址的指针。每当请求new开出n个字符的存储区时,要先检查一下newp为newp+n,指向下一次可用空间的开始地址;若存储不够分配,则返回NULL.,new函数如下: #define NULL 0 #define NEWSIZE 1000 char newbufNEWSIZE; char *newp=newbuf; char *new(int n) /*定义new函数*/ if (newp+n=newbuf+ NEWSIZE) newp= newp+n; return(newp-n); else return (NULL); ,11.7,写一函数free,将上题用new函数占用的空间释放。Free(p)表示将p(地址)指向的单元以后的内存段释放。,解:free的作用是将newp得值改为p的值 #define Null 0 #define NEWSIZE 1000 char newbufNEWSIZE; char *newp=newbuf; free(char *p) if( (p=newbuf) ,11.8,已有a、b两个链表,每个链表中的结点包括学好、成绩。要求把两个链表合并,按学号升序排列。,解: #include #define NULL 0 #define LEN sizeof(struct student) strut student long num; int scor; struct student *next ; struct student listA,listB; int n,sum=0;,main() struct student *creat(void); /*函数声明*/ struct student *insert(struct student *,struct student *); /*函数声明*/ void print(struct student *); /*函数声明*/ stuct student *ahead , *bhead,*abh; printf(“ninput list a:n”); ahead=creat(); /*调用creat函数,输入链表a*/ printf(“ninput list b:n”); bhead=creat(); /*调用creat函数,输入链表b*/ sum=sum+n; abh=insert(ahead,bhead); /*调用insert函数,将两个链表合并*/ print(abh); /*输出合并后的链表*/ struct student *creat (void) /*建立链表的函数*/ struct student *p1,*p2,*head; n=0; p1=p2=(struct student *)malloc(LEN); printf(“input number ,head=NULL; while(p1-num!=0) n=n+1; if (n=1) head=p1; else p2-next =p1; p2=p1; p1=(struct student *)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论