网站LOGO
二级教程C语言课后参考答案之二
第9章

【9-1】D【9-2】A【9-3】A【9-4】A【9-5】C【9-6】A【9-7】B【9-8】D

【9-9】C【9-10】C【9-11】C【9-12】D【9-13】D【9-14】A,C(?)

【9-15】A【9-16】A【9-17】C【9-18】C【9-19】9,0【9-20】6【9-21】12

【9-22】3【9-23】2721【9-24】-850,2,0【9-25】k=p,(k) 

【9-26】c=getchar(),c-65

【9-27】

#include <ctype.h>

main()

{ char *s,a[100];

  int i,k=0,num[10]={0};

  s=a;

  printf("Input an number string: ");

  scanf("%s",s);

  while(*s!='\0')

  { if (isdigit(*s)&&(*s>'0'))

    num[*s-49]++;

    if (*s==48) num[9]++;

    s++;

  }

  for (i=0;i<9;i++)

   printf("%2d-->%3d\n",i+1,num[i]);

  printf(" 0-->%3d\n",num[9]);

  printf("***** TOTAL *****\n");

  for (i=0;i<=9;i++) k+=num[i];

  printf("      %d      ",k);

 getch();

}

【9-28】

move(int a[10],int n)

{int i;

 for (i=n;i<10;i++)

   a[i-1]=a[i];

a[9]=0;

}

main()

{int x[10],i,n;

 printf("input 10 number:");

 for(i=0;i<10;i++)

  scanf("%d",&x[i]);

 printf("\ninput the N:");

 scanf("%d",&n);

 move(x,n);

 printf("\nAfter move tne member list is :\n");

 for (i=0;i<10;i++)

 printf("%d ",x[i]);

 getch();

}

【9-29】

main()

{int a[100]={0},b[100]={0},i,j;

 printf("\nInput the number list (end with 32767) :\n ");

 for(i=0;i<100;i++)

  {scanf("%d",&a[i]);

   if ( a[i]==32767) break;

   }

 i--;

 odd(a,b,i);

 printf("\narray B :\n");

 for (i=0;i<100;i++)

   if (b[i]!=0) printf("%d ",b[i]);

   else break;

 getch();

}

odd(int a[100],int b[100],int n)

{ int i,j=0;

  for(i=0;i<=n;i++)

  if(a[i]%2!=0) b[j++]=a[i];

}

【9-30】

#include <string.h>

sort(char s[],int n)

{int i,j,p,t;

 for(j=0;j<(n-1);j++)

  {p=j;

   for(i=j+1;i<n;i++)

    if(s[p]<s[i]) p=i;

    if(p!=j) { t=s[j]; s[j]=s[p]; s[p]=t; }

  }

}

main()

{char *s;

 int n;

 printf("\nInput s:");

 scanf("%s",s);

 n=strlen(s);

 sort(s,n);

 printf("\nthe sorted string is %s \n",s);

 getch();

}

【9-31】

main()

{int a[100],n,*p,i=1;

 a[0]=-32768; p=a+1;

 printf("\nInput an number list (end with 32767) :\n");

 do

   {scanf("%d",&a[i]);

    if (a[i]>=a[i-1]) i++;

   }

 while(a[i-1]!=32767);

 printf("\nInput inserted number: ");

 scanf("%d",&n);

 p=a;

 insert(p,n);

 p=a+1;

 printf("\nOutput array a:\n");

 do

   if (*p!=32767) printf("%d  ",*p++);

 while (*p!=32767);

 getch();

}

 

insert( int *q, int n)

{ int *k;

  k=q;

  while (*q!=32767) q++;

  *(q+1)=*q;

  while(q>k)

  { if (n>*(q-1)) {  *q=n; break; }

    else {q--; *(q+1)=*q; }

  }

}

 

【9-32】

main()

{int n,a[16]={0},*p;

 printf("\nInput an number:");

 scanf("%d",&n);

 p=a;

 change(a,n,p);

 printf("n=%d\n",n);

 while(p<=a+15)

  printf("%d",*p++);

}

 

change(x,n,p)

int x[16],n,*p;

{

 p=x+15; *p=0;

 if(n==0) return(0);

 while(n!=0)

   {*p=n%2;

    p--;

    n/=2;

   }

 }

【9-33】

#include <stdio.h>

main()

{int a[15],*p,i;

 p=a;

 frandm(a);

 printf("\nThe array a is: ");

 for(i=0;i<15;i++)

   printf("%d ",*p++);

}

 

frandm( a[]);

{int k=0,i,x,*q;

 for (i=0;i<15;i++) a[i]=20;

 while(k<15)

 {x=rand()%20;

  for (i=0;i<15;i++)

    if (a[i]==x ) continue;

    else a[k++]=x;

 }

}

【9-34】

#define N 20

main()

{int a[N][N],x[N]={0},y[N]={0},i,j,m,sum=0;

 printf("\n Input N (<20) :");

 scanf("%d",&m);

 printf("\n Input array a[%d][%d]:\n",m,m);

 for(i=0;i<m;i++)

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

   { scanf("%d",&a[i][j]);

     x[i]+=a[i][j]; y[j]+=a[i][j];

     if (i==j ) sum+=a[i][i];

   }

 printf("\n After compute : \n");

 for (i=0;i<m;i++)

  { for (j=0;j<m;j++)

     printf("%5d",a[i][j]);

     printf("%5d\n",x[i]);

  }

 for (i=0;i<m;i++)

  printf("%5d",y[i]);

printf("\n\nSum=%d\n",sum);

getch();

}

【9-35】

#define N 20

main()

{int a[N][N],b[N][N],c[N][N],m,n,i,j;

 printf("\n Input m,n (<20) :");

 scanf("%d%d",&m,&n);

 printf("\n Input array A[%d][%d]:\n",m,n);

 for(i=0;i<m;i++)

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

   { scanf("%d",&a[i][j]);

   }

 printf("\n Input array B[%d][%d]:\n",m,n);

 for(i=0;i<m;i++)

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

   { scanf("%d",&b[i][j]); c[i][j]=a[i][j]+b[i][j];

   }

 

 printf("\n After compute array C: \n");

 for (i=0;i<m;i++)

  { for (j=0;j<n;j++)

     printf("%5d",c[i][j]);

     printf("\n");

  }

getch();

}

【9-36】

main()

{int i,j,k;

 printf("\n              ** A MULTIPLICATION  TABLE **\n");

 printf("     ");

 for(i=1;i<10;i++)  printf("(%3d)",i);

 printf("\n        --------------------------------------------\n");

 for(i=1;i<10;i++)

  { for(j=0;j<10;j++)

     if(j==0) printf("(  %d)",i);

     else printf("%5d",i*j);

    printf("\n");

  }

 printf("\n        --------------------------------------------\n");

 getch();

}

【9-37】

#include "stdio.h"

#include "stdlib.h"

main()

{static  int m[5][5]={0},i,j;

 int  k=0;

  printf("\nBefore :\n");

  for (i=0;i<5;i++)

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

     { m[i][j]=rand()%100;

       printf("%4d",m[i][j]);

       if ((++k)%5==0)  printf("\n"); }

  printf("\nAfter:\n");

  for (i=0;i<5;i++)

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

     { k=m[i][j]; m[i][j]=m[j][i];  m[j][i]=k; }

   k=0;

   for (i=0;i<5;i++)

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

     { printf("%4d",m[i][j]);

       if ((++k)%5==0)  printf("\n"); }

  printf("\n   Program end !    \n");

 

}

【9-38】

#include "stdio.h"

main()

{ int m[5][5]={0},i,j;

 int  k=0;

  printf("\nBefore :\n");

  for (i=0;i<5;i++)

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

     { m[i][j]=rand()%100;

       printf("%4d",m[i][j]);

       if ((++k)%5==0)  printf("\n"); }

  printf("\nAfter:\n");

  for (i=0;i<5;i++)

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

     { k=m[i][j]; m[i][j]=m[j][i];  m[j][i]=k; }

       printf("%4d",m[i][j]);

       if ((++k)%5==0)  printf("\n"); }

   k=0;

   for (i=0;i<5;i++)

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

     { printf("%4d",m[i][j]);

       if ((++k)%5==0)  printf("\n"); }

  printf("\n   Program end !    \n");

  getch();

}

 

第十章

【10-1】B【10-2】B【10-3】C【10-4】B【10-5】A【10-6】A【10-7】C

【10-8】A【10-9】C【10-10】? 【10-11】GFEDCB【10-12】XYZA

【10-13】SO【10-14】qwertyabcd【10-15】Itiss

【10-16】strlen(str),j--【10-17】7【10-18】gotogood

*【10-10】

#include <string.h>

main()

{ char str1[]="string",str2[8]

,*str3,*str4="string";

  strcpy(str1,"HELLO1");

  strcpy(str2,"HELLO2");

  strcpy(str3,"HELLO3");

  strcpy(str4,"HELLO4");

  printf("%s\n%s\n%s\n%s\n",str1,str2,str3,str4);

  getch();

}

*【10-13】

main()

{char *p[]={"BOOL","OPK","H","SP"};

 int i;

 for(i=3;i>=0;i--,i--) printf("%c",*p[i]);

 printf("\n");

 getch();

}

【10-19】

#include <stdio.h>

#include <string.h>

mygets(s)

char *s;

{ char ch;

  ch=getchar();

  while (ch!=10)

    {*s=ch; s++;ch=getchar();}

  *s='\0';

}

myputs(s)

char *s;

{ int i,n;

  n=strlen(s);

  for(i=0;i<n;i++)

   putchar(*s++);

}

 

main()

{char *str,s[80];

 str=s;

 printf("Input a string:");

 mygets(str);

 printf("\nOutput the string:\n");

 myputs(str);

 printf("\n");

 getch();

}

【10-20】

#include <string.h>

main()

{ char *str ;

  str=(char*)malloc(1);

  printf("Input a string:\n" );

  gets(str);

  if (fun(str)) printf("\nThis is back-round-text\n");

  else   printf("\nThis is not back-round-text\n");

  getch();

}

fun(char *s)

{int n,i,j, flag=1;

 n=strlen(s);

 for (i=0,j=n-1;i<j;i++,j--)

  if(*(s+i)!=*(s+j)) flag=0;

 return(flag);

 }

【10-21】

#include <string.h>

#include <stdio.h>

char deltet(s,n)

char *s; int n;

{ char ch;

  if (n>strlen(s)) {printf("\nCANNOT DELETE IT !!! %c",007);

       return('\0'); }

 else

    { ch=*(s+n);

      do

       { *(s+n)=*(s+n+1); n++; }

      while(*(s+n-1)!='\0');

      return(ch);

     }

 }

main()

{char str[80],*p,ch;

 int n;

 printf("\nInput a string:\n");

 p=str+1;

 gets(p);

 printf("\nDelete the n'th character: ");

 scanf("%d",&n);

 ch=deltet(str,n);

 printf("\nAfter delete the string :(ch=%c)\n",ch);

 for(n=1;n<strlen(str);n++)

 putchar(*p++);

 getch();

}

 

第十一章

【11-1】D【11-2】B【11-3】D【11-4】C【11-5】IJKLEFGHABCD【11-6】7

【11-7】8【11-8】*(s+j),i+1,i【11-9】17

【11-10】(*fun)(),(*fun)(a+i*h),mypoly

【11-11】

#include <string.h>

main(int argc, char *argv[])

{char *str, *p;

 int i,k;

 str=(char*)malloc(50);

 printf("\nInput a text line ( > 10 letters):\n");

 scanf("%s",str);

 p=argv[1];

 k=strlen(str);

 if(argc==2)

  {if (*p=='+')

       for(i=0;i<(*(p+1)-'0');i++) printf("%c",*(str+i));

   else if (*p=='-')

       for(i=k-*(p+1)+'0';i<=k;i++)

         printf("%c",*(str+i));      }

   else for(i=k-10;i<k;i++) printf("%c",*(str+i));

 getch();

}

【11-12】

int i;

ten_to_two(int a[ ],int n)

{ if (n==0)   a[i]=0;

  else if (n==1) a[i]=1;

  else { a[i++]=n%2;

     n=n/2;

     ten_to_two(a,n); }

 

}

main()

{int a[16]={0};

 int n,k;

 i=0 ;

 error:printf("\nInput an number: ");

 scanf("%d",&n);

 if (n<0) { printf("Input error ! %c" ,7); goto error; }

 ten_to_two(a,n);

 if(i!=0) a[i]=1;

 printf("\nThe converted number is :\n");

 for (k=i;k>=0;k--)

   printf("%d",a[k]);

 getch();

}

【11-13】

sumf(int m)

{ if (m==1) return(1);

  else   return(m+sumf(m-1));

}

 

main()

{ int n;

  printf("Input n: ");

  scanf("%d",&n);

  if (n>0) printf("%d",sumf(n));

  else  printf("Input error ! %c",7);

  getch();

}

【11-14】

long fib(int n)

{if (n==0||n==1) return(1);

 else return(fib(n-2)+fib(n-1));

}

main()

{ int n;

  printf("\nInput n(<25):");

  scanf("%d",&n);

  if (n>=0)

   printf("%ld",fib(n));

  else printf("\nInput error ! %c",7);

  getch();

}

 

第十二章

【12-1】B【12-2】B【12-3】A【12-4】C【12-5】D【12-6】B【12-7】A

【12-8】A【12-9】2,5,1,2,3,-2【12-10】2468

第十三章

【13-1】B【13-2】C【13-3】B【13-4】C【13-5】D【13-6】A【13-7】D

【13-8】ar=9 ar=9 ar=11【13-9】int*,*s,*b

【13-10】

#include <ctype.h>

#define MYALPHA(C) (isalpha(C))

main()

{ char ch;

  printf("Input a character: ");

  scanf("%c",&ch);

  if (MYALPHA(ch)) printf("\nThis is a letter !");

  else printf("\nThis is not a letter  ! ");

  getch();

}

【13-11】

#define SWAP(t,x,y) {(t)=(x);(x)=(y);(y)=(t);}

main()

{int x,y,t;

 printf("\nInput x & y :");

 scanf("%d%d",&x,&y);

 SWAP(t,x,y)

 printf("\nx=%d   y=%d",x,y);

 getch();

}

【13-12】

main()

{int w,*px,*py,*pz;

 px=(int *)malloc(sizeof(int));

 py=(int *)malloc(sizeof(int));

 pz=(int *)malloc(sizeof(int));

 printf("\nInput three number :");

 scanf("%d%d%d",px,py,pz);

 if(*px>*py) {w=*px;*px=*py;*py=w;}

 if(*px>*pz) {w=*px;*px=*pz;*pz=w;}

 if(*py>*pz) {w=*py;*py=*pz;*pz=w;}

 printf("\nAfter exchange : ");

 printf("%d    %d     %d",*px,*py,*pz);

 getch();

}

 

第十四章

【14-1】D【14-2】D【14-3】D【14-4】A【14-5】C【14-6】C【14-7】C

【14-8】B 【14-9】struct link *next; 【14-10】p->next,p->data<m

【14-11】(struct list *),struct list, (struct list *),struct list,return(h)

*【14-6】struct st

{int x;

 int *y;

 } *p;

 int dt[4]={10,20,30,40};

 struct st aa[4]={50,&dt[0],60,&dt[0],60,&dt[0],60,&dt[0]};

 main()

 {p=aa;

  printf("   %d\n",++p->x);

  printf("   %d\n",(++p)->x);

  printf("   %d\n",++(*p->y));

  }

【14-12】

struct stud

{char num[5],name[10];

 int s[4];

 float ave;

};

typedef struct stud STU;

 

STU readrec(a)

STU a[31];

{ int i;

  for(i=0;i<4;i++) a[0].s[i]=0;

  printf("\n   Input 30  student's data:\n ");

  printf("--- num  name  s1  s2  s3  s4 ---\n");

  for (i=1;i<5  ;i++)

    {printf("%d: ",i);

     scanf("%s%s%d%d%d%d",a[i].num,a[i].name,&a[i].s[0],&a[i].s[1],

        &a[i].s[2],&a[i].s[3]);

        a[i].ave=(a[i].s[0]+a[i].s[1]+a[i].s[2]+a[i].s[3])/4.0;

     a[0].s[0]+=a[i].s[0];a[0].s[1]+=a[i].s[1];

     a[0].s[2]+=a[i].s[2];a[0].s[3]+=a[i].s[3];

     printf("\n");

     }

a[0].s[0]/=4;a[0].s[1]/=4;a[0].s[2]/=4;a[0].s[3]/=4;

}

 

STU writerec(a)

STU a[31];

{int i;

 printf("\n      Output 30 student's data:  \n");

 printf("----- num    name  s1   s2   s3   s4  ave -----\n");

 for(i=1;i<5 ;i++)

   {printf("\n%5s  %10s %4d  %4d  %4d  %4d  %6.2f",a[i].num,a[i].name,

        a[i].s[0],a[i].s[1], a[i].s[2],a[i].s[3],a[i].ave);

   }

 printf("\n\n AVE:  %d    %d    %d    %d ",

    a[0].s[0],a[0].s[1],a[0].s[2],a[0].s[3]);

}

 

main()

{STU x[31];

 readrec(x);

 writerec(x);

 getch();

}

【14-13】

struct node

{ int data;

  struct node *next;

};

typedef struct node NODE;

 

int max(NODE *h)

{NODE *p;

 int m;

 p=h;

 m=p->data;

 while(p->next!='\0')

 {if (p->data>m) m=p->data;

  p=p->next;

 }

 printf("\n****  m=%d",m);

 return(m);

}

 

NODE *maxaddress( NODE *h)

{NODE *p,*q;

 int m;

 p=h;

 m=p->data;

 while(p->next!='\0')

 {if (p->data>m) {m=p->data;q=p;}

  p=p->next;

 }

 printf("\n****  madd=%ld",q);

 return(q);

}

 

main()

{NODE *h,*s,*q;

 int maxnumber;

 printf("\nInput some numbers (END WITH 0): \n");

 h=(NODE*)malloc(sizeof(NODE));

 q=h;

 scanf("%d",&maxnumber);

 while(maxnumber!=0)

   { s=(NODE*)malloc(sizeof(NODE));

     s->data=maxnumber;

     q->next=s;

     q=s;

     scanf("%d",&maxnumber);

    }

  q->next='\0';

  printf("\nMaxnumber is %d",max(h));

  printf("\nMaxnumber Address is %ld",maxaddress(h));

  getch();

}

 

第十五章

【15-1】D【15-2】B【15-3】B【15-4】A【15-5】11110000【15-6】A&0

【15-7】0|0XFF【15-8】x|0XFF00【15-9】a=012500>>2【15-10】ch|32(ch|0x20)

 

第十六章

【16-1】B【16-2】C【16-3】3,!feof(f1),f2,fclose(f1),fclose(f2)

【16-4】fopen(fname,”w”),ch【16-5】”r”,(!feof(fp)),fseek(fp,1,1)

【16-6】AAAABBBBCCCC

【16-7】

#include "stdio.h"

main()

{ char *s,a[200];

  int i;

  FILE *fp;

  if((fp=fopen("file_a","w+"))==NULL)

  { printf("\nCannot open file !!! %c%c",7,7);

    exit(0);

  }

  printf("\nInput 10 string :");

  for(i=1;i<11;i++)

  {printf("\n%d: ",i);

   scanf("%s",s);

   fputs(s,fp);

  }

  i=0; rewind(fp);

  while(!feof(fp))

    fscanf(fp,"%c",&a[i++]);

  a[i]='\0';

  i=0;

  printf("\nThe array a is :\n");

  while(a[i]!='\0')

    printf("%c",a[i++]);

  fclose(fp);

  getch();

}

【16-8】

#include "stdio.h"

main()

{ float x;

  int i;

  FILE *fp;

  if((fp=fopen("file_b","wb+"))==NULL)

  { printf("\nCannot open file !!! %c%c",7,7);

    exit(0);

  }

  rewind(fp);

  printf("\nInput 10 number:\n");

  for(i=1;i<11;i++)

  {printf("%d: ",i);

   scanf("%f",&x);

   fwrite(&x,4,1,fp);                    /* cannot use fprintf */

  }

  rewind(fp);

  while(!feof(fp)) {fread(&x,4,1,fp);printf("\n*%f",x);}

                   /* cannot use fscanf */

  printf("\n----------------------------------");

  printf("\nthe 4th number chang to : ");

  scanf("%f",&x);

  fseek(fp,3L*sizeof(float),0);

  fwrite(&x,4,1,fp);

   rewind(fp);

  while(!feof(fp)) {fread(&x,4,1,fp);printf("\n*%f",x);}

  fclose(fp);

  getch();

}

  相关文章
 
热点文章
 
热点下载
Copyright © 2007 KaoEase.Com, All Rights Reserved