好文档 - 专业文书写作范文服务资料分享网站

C语言程序设计第三版谭浩强课后习题答案完整版

天下 分享 时间: 加入收藏 我要投稿 点赞

char name[8]; int score[4] }stu[N];

input(struct student stu[]) {int I,j;

for(I=0;I

{printf(“input scores of student %d:\\n”,I+1); printf(“NO.:”);

scanf(“%s”,stu[i].num); printf(“name: ”);

scanf(“%s”, stu[i].name); for(j=0;j<3;j++)

{printf(“score%d:”,j++);

scanf(“%d”, &stu[i].score[j]);} }

printf(“\\n”); } }

11.5 有10个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入10个学生的数据,要求打印出3门课的总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课成绩) 解:#define N 10 struct student {char num[6] char name[8] int score[4] float avr; }stu[N]; main()

{int I,j,max,maxi,sum; float average; for(I=0;I

{printf(“\\nInput scores of student %d:\\n”,I+1); printf(“NO.:”);

scanf(“%s”,stu[i].num); printf(“name”);

scanf(“%s”,stu[i].name); for(j=0;j<3;j++)

{printf(“score %d:”,j+1);

scanf(“%d”, &stu[i].score[j]); } }

average=0; max=0; maxi=0;

for(i=0;i<3;i++) {sum=0;

for(j=0;j<3;j++)

sum+=stu[i].score[j]; stu[i].avr=sum/3.0; average+=stu[i].avr; if(sum>max) {max=sum; maxi=I; } }

average/=N;

printf(“NO. name score1 score2 score3 average\\n”); for(I=0;I

{printf(“%5ss”,stu[i].num, stu[i].name); for(j=0;j<3;j++)

printf(“?”,stu[i].score[j]); printf(“%8.2f\\n”,stu[i].avr); }

printf(“average=%6.2f\\n”,average); printf(“The highest score is:%s,score total:%d.”stu[maxi].name,max); }

11.6 编写一个函数new,对n个字符开辟连续的存储空间,此函数应返回一个指针,指向字符串开始的空间。New(n)表示分配n个字节的内存空间。 解:new函数如下: #define NULL 0

#define NEWSIZE 1000 char newbuf[NEWSIZE]; char *newp=newbuf; char *new(int n)

{if (newp+n<=newbuf+ NEWSIZE) { newp= newp+n; return(newp-n); } else

return(NULL); }

11.7写一函数free,将上题用new函数占用的空间释放。Free(p)表示将p指向的单元以后的内存段释放。 解:

#define Null o

#define NEWSIZE 1000 char newbuf[NEWSIZE]; char *newp=newbuf; free(char *p)

{if((p>=newbuf)&&(p

21

11.8已有a、b亮光链表,每个链表中的结点包括学好、成绩。要求把两个链表合并,按学号升序排列。 解:

#include #define NULL 0

#define LENsizeof(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(); sum=sum+|n;

abh=insert(ahead,bhead); print(abh); }

struct student *creat(void) {struct student *p1,*p2,*head; n=0;

p1=p2=(struct student *)malloc(LEN);

printf(“input number&scores of student:\\n”); printf(“if number Is 0,stop inputing.\\n”); scanf(“%ld,%d”,&p1->num,&p1->score); head=NULL;

while(p1->num!=0) {n=n+1;

if(n==1)head=p1; else p2->next =p1; p2=p1;

p1=(struct student *)malloc(LEN);

scanf(“%ld,,%d”,&p1->num,&p1->score); }

p2->next=NULL; return(head); }

struct student *insert(struct student *ah,struct student *bh) {struct student *pa1 , *pa2,*pb1,*pb2; pa2=pa1=ah; pb2=pb1=bh; do

{while((pb1->num>pa1->num)&&(pa1->next!=NULL)) {pa2=pa1;

pa1=pa1->next; }

if(pb->num<=pa1->num) {if(ah=pa1) ah=pb1;

else pa2->next=pb1; pb1=pb1->next; pb2->next=pa1; pa2=pb2; pb2=pb1; } }

while((pa1->next!=NULL)||(pa1==NULL&&pb1!=NULL)); if((pb1->num>pa1->num)&&(pa1->next==NULl)) ap1->next=pb1; return(ah); }

void print(struct student *head) {struct student *p;

printf(“%ld%d\\n”,p->num,p->score); p=p->next;

while(p!=NULL); }

11.9 13个人围成一圈,从第1个人开始顺序报号1、2、3。凡报到“3”者退出圈子。找出最后留在圈子中的人原来的序号。 解:

#define N 13 struct person {int number; int nextop; }link[N+1]; main()

{int I,count,h; for(I=1;I<=N;I++) {if(I==N)

link[i].nextp=1; else

link[i].nextp=I+1; link[i].number=I; }

printf(“\\n”); count=0; h=N;

printf(“sequence that person2 leave the circle:\\n”); while(count

22

{I=0;

while(I!=3)

{h=link[h].nextp; if(link[h].number) I++; }

printf(“M”,link[h].number); link[h].number=0; count++; }

printf(“\\nThe last one is”); for(I=1;ii<=N;I++) if(link[i].number) printf(“=”,lin[i].number); }

11.10有两个链表a和b,设结点中包含学号、姓名。从1链表中删去与b链表中有相同学号的那些结点。 解:

#define LA 4 #define LB 5 #define NULL 0 struct student {char nump[6]; char name[8];

struct student *next; }A[LA],b[LB]; main() {struct student a[LA]={{“101”,”Wang”},{“102”,”LI”},{“105”,”zhang”},{“106”,”Wei”}}; struct

studentb[LB]={{“103”,”Zhang”},{“104”,”Ma”},{“105”,”Chen”},{“107”,”Guo”}, {“108”,”Lui”}}; int I,j;

struct student *p, *p1,*p2,*pt,*head1,*head2; head1=a; head2=b;

printf(“list a :\\n”);

for(p1=head1,i=1;p1

p1->next=a+I;

printf(“%8s%8s\\n”,p1->num,p1->name); p1=p1->next; }

p->next=NULL; printf(“\\n list b:\\n”);

for(p2=head2,I=1;p2

{p=p2;

p2->next=b+I;

printf(“%8s%8s\\n”,p2->num,p2->name); p2=pa->next; }

p->next=NULL; printf(“\\n”); p1=head1;

while(p1!=NULL) {p2=head2;

while(p2!=NULL&&strcmp(p1->num,p2->num)!=0) p2=p2->next;

if(strcmp(p1->num,p2->num==0)) if(p1==head1)

head1=p1->next; else

p->next=p1->next; p=p1;

p1=p1->next; }

p1=hedad1;

printf{“\\n result:\\n”}; while(p1!=NULL)

{printf(“%7s %7s\\n”,p1->num,p1->name); p1=p1->next; } }

11.11建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。 解:#define NULL 0

#define LEN sizeof(struct student) struct student {char num[6]; char name[8]; char sex[2]; int age;

stuct student *next; }stu[10]; main()

{struct student *p,*pt,*head; int I,length,iage,flag=1; int find=0; while(flag==1)

{printf(“input length of list(<10):”); scanf(“%d”,&length); if(length<10) flag=0;

23

}

for(I=0;I

{p=(struct student *)malloc(LEN); if(I==0)

head=pt=p; else

pt->next=p; pt=p;

while(p!=NULL) {

printf(“%4s%8s”,p->num,p->name); printf(“%6sm”,p->sex,p->age); p=p->next; } }

11.12将一个链表按逆序排列,即将链头当链尾,链尾当链 ptintf(“NO:”);

scanf(“%s”,p->num); prntf(“name:”);

scanf(“%s”,p->name); printf(“sex:”);

scanf(“%s”,p->sex); printf(“age:”);

scanf(“%s”,p->age); }

p->next=NULL; p=head;

printf(“\\n NO. name sex age\\n”); while(p!=NULL)

{printf(“%4s%8s%6sm\\n”,p->num, p->age);

p=p->next; }

printf(“Input age:”); scanf(“%d”,&iage); pt=head; p=pt;

if(pt->age==iage) {p=pt->next; head=pt=p; find=1; } else

pt=pt->next; while(pt!=NULL) {if(pt->age==iage) {p->next=pt->next; find=1; }

else p=pt; pt=pt->next; }

if(!find)

printf(“Not found%d.”,iage); p=head;

printf(“\\n NO.name sex age\\n”);

p->name, 头。 解:

# define NULL 0 struct stu {int num;

struct stu *next; }

main() {int len=1l

struct stu *p1,*p2,*head,*new,*newhead;

p1=p2=head=(struct stu * )malloc(sizeof(strct stu)); printf(“input number(0:list end):”); scanf(“%d”,&p1->num); p->sex, while(p1->num!=o)

{p1=(struct stu*)malloc(sizeof(struct stu)); printf(“input number(n:listend):”); scanf(“%d”,&p1->num); if(p1->num==0) p2->next=null; else

{p2=>next=p1; p2=p1; len++; } }

p1=head;

pritnf(“\\n the original list:\\n”); do

{printf(“M”,p1->num); if(p1->next!=NULL) p1=p1->next; }

while(p1->next!=NULL) {p2=p1;

p1=p1->next; }

if(I==0)

newhead=new=p1; else

new=nes->next=p1;

24

p2->next=NULL; }

printf(\\n\\n The new listL\\n); p1=newhead;

for(I=0l;Inum”); p1=p1->next, }

printf(“\\n”);

第12章

12.1编写一个函数getbits,从一个16位的 单元中取出某几位(即该几位保留原值,其余位为0)。函数调用形式为:getbits(value,n1,n2)其中value为该16位单元中的数据值,n1为欲取出的起始位,n2为欲取出的结束位。 解:main()

{unsingned int a; int n1,n2;

printf(“input an octal number:”); scanf(“%o”,&a);

printf(“input n1,n2:”);

scanf(“%d,%d”,&n1,&n2);

printf(“result:%o\\n”,getbits(a,n1-1,n2)); }

getbits(unsigned value,int n1,n2) {unsigned int z; z=~0;

z=(z>>n1)&(z<<(16-n2); return(z);; }

12.2写一个函数,对一个16位的二进制数驱除它的奇数位。 解:main()

{unsigned getbits(unsigned); unsigne int a;

printf(“\\ninput an octal number:”); scanf(“%o”,&a);

printf(“result:%o\\n”,,getbits(a)); }

unsigner getbits(unsigned value) {int I,j,m,n;

unsigned int z,a q; z=0;

for(I=1;I<=15;I+=2) {q=1;

for(j=1;j<=(16-I-1)/2;j++) q=q*2;

a=value>>(16-i); a=a<<15;

a=a>>15; z=z+a*q; }

return(z); }

12.3编一程序,检查一下年一所用的计算机系统的C编译在执行有移时是按照逻辑位移的原则,还有按照算术右移的原则。如果是逻辑右移,请编一函数实现算术右移。如果是算术右移,请编写一函数实现逻辑右移。 解: main() {int a,n,m; a=~0;

if(a>>5)!=a

{printf(“\\nTurbo C,logical move!\\n”); m=0; } else

{printf(“\\nTurbo C,arithmetic move!\\n”); m=1; }

printf(“input an octal number:”); scanf(“%o”,&a);

printf(“how mang digit move thwards the right ::”); scanf(“%d”,&n); if(m==0)

printf(“Arithmetic right move,result:%o\\n”,getbits1(a,n)); else

printf(“Logical ritht move,result:%o”, getbits2(a,n)); }

getbits1(unsigned value,int n) {unsigned z; z=~0; z=z>>n; z=~z;

z=z|(value>>n); return(z); }

egtbit2(unsigned valud,int n ) {unsigned z;

z=(~(1>>n)&(value>>n)); }

12.4编一函数用来实现左右循环移位。函数名为move,调用方法为:

move(value,n) 其中value为要循环位移的数,n为位移的位数。 解: main()

25

1ogc78lgwy37lyd0yejc
领取福利

微信扫码领取福利

微信扫码分享