乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C語言程序設(shè)計(jì)第三版譚浩強(qiáng)課后習(xí)題答案完整版

       ◇.曉圣ヤér 2010-11-01

       1. 5請參照本章例題,編寫一個(gè)C程序,輸出以下信息: 
      ************************** 
               Very    Good! 
      ************************** 
      解: 
        mian() 
      {printf(“**************************”); 
      printf(“\n”); 
      printf(“Very    Good!\n”); 
      printf(“\n”); 
      printf(“**************************”); 

      1.6 編寫一個(gè)程序,輸入a、b、c三個(gè)值,輸出其中最大值。 
      解: 
        mian() 
        {int a,b,c,max; 
         printf(“請輸入三個(gè)數(shù)a,b,c:\n”); 
         scanf(“%d,%d,%d”,&a,&b,&c); 
         max=a; 
         if(max<b) 
      max=b; 
      if(max<c) 
      max=c; 
         printf(“最大數(shù)為:“%d”,max); 

      第三章 
      3.6寫出以下程序運(yùn)行的結(jié)果。 
         main() 
         {char c1=’a’,c2=’b’,c3=’c’,c4=’\101’,c5=’\116’; 
      printf(“a%cb%c\tc%c\tabc\n”,c1,c2,c3); 
      printf(“\t\b%c %c”,c4,c5); 
      解: 
         aaㄩbbㄩㄩㄩccㄩㄩㄩㄩㄩㄩabc 
                    AㄩN 
      3.7 要將"China"譯成密碼,譯碼規(guī)律是:用原來字母后面的第4個(gè)字母代替原來的字母.例如,字母"A"后面第4個(gè)字母是"E"."E"代替"A"。因此,"China"應(yīng)譯為"Glmre"。請編一程序,用賦初值的方法使cl、c2、c3、c4、c5五個(gè)變量的值分別為,’C’、’h’、’i’、’n’、’a’,經(jīng)過運(yùn)算,使c1、c2、c3、c4、c5分別變?yōu)?#8217;G’、’l’、’m’、’r’、’e’,并輸出。 
      解: 
      #include <stdio.h> 
      main()  
      { char c1=’C’,c2=’h’,c3=’i’,c4=’n’,c5=’a’; 
        c1+=4; 
        c2+=4; 
        c3+=4; 
        c4+=4; 
        c5+=4; 
        printf("密碼是%c%c%c%c%c\n",c1,c2,c3,c4,c5); 
      }  
      運(yùn)行結(jié)果:  
      密碼是Glmre 
      3.9求下面算術(shù)表達(dá)式的值。 
      (1)x+a%3*(int)(x+y)%2/4 
           設(shè)x=2.5,a=7,y=4.7 
      (2)(float)(a+b)/2+(int)x%(int)y 
           設(shè)a=2,b=3,x=3.5,y=2.5 
      (1)2.5 
      (2)3.5 
      3.10寫出程序運(yùn)行的結(jié)果。 
      main() 
      {int i,j,m,n; 
       i=8; 
       j=10; 
       m=++i; 
       n=j++; 
       printf(“%d,%d,%d,%d”,i,j,m,n); 
      解: 
         9,11,9,10 
      3.12 寫出下面表達(dá)式運(yùn)算后a的值,設(shè)原來a=12。設(shè)a和n都已定義為整型變量。 
      (1)a+=a   (2) a-=2  (3) a*=2+3  (4)a/=a+a  
      (5) a%=(n%=2),n的值等于5 
      (6)a+=a-=a*=a 
      解: 
        (1) 24    (2) 10    (3) 60   (4) 0    (5) 0   (6) 0 
      第四章 
      4.4若a=3,b=4,c=5,x=1.2,y=2.4,z=-3.6,u=51274,n=128765,c1=’a’,c2=’b’。想得到以下輸出格式和結(jié)果,請寫出程序(包括定義變量類型和設(shè)計(jì)輸出)。 
      a=_3_ _b=_4_ _c=_5 
      x=1.200000,y=2.400000,z=-3.600000 
      x+y=_3.600_ _y+z=-1.20_ _z+x=-2.40 
      c1=ˊaˊ_or_97(ASCII) 
      c2=ˊbˊ_or_98(ASCII) 
      main() 
      {int a=3,b=4,c=5; 
      long int u=51274,n=128765; 
      float x=1.2,y=2.4,z=3.6; 
      char c1=’a’,c2=’b’; 
      printf("a=%2d b=%2d c=%2d\n",a,b,c); 
      printf("x=%f,y=%f,z=%f\n",x,y,z); 
      printf("x+y=%5.2f y+z=%5.2f z+x=%5.2f\n",x+y,y+z,z+x); 
      printf("u=%6ld n=%9ld\n",u,n); 
      printf("%s %s %d%s\n","c1=’a’","or",c1,"(ASCII)"); 
      printf("%s %s %d%s\n","c2=’a’","or",c2,"(ASCII)"); 
      4.7用scanf下面的函數(shù)輸入數(shù)據(jù),使a=3,b=7,x=8.5,y=71.82,c1=ˊAˊ,c2=ˊaˊ,問在鍵盤上如何輸入? 
      main() 

      int a,b;float x,y;char c1c2; 
      scanf("a=%d_b=%d",&a,&b); 
      scanf("_x=%f_y=%e",&x,&y); 
      scanf("_c1=%c_c2=%c",&c1,&c2); 

      a=3_b=7 
      _x=8.5_y=71.82 
      _c1=A_c2=a  
      4.8設(shè)圓半徑r=1.5,圓柱高h(yuǎn)=3,求圓周長、圓面積、圓球表面積、圓球體積、圓柱體積。用scanf輸入數(shù)據(jù),輸出計(jì)算結(jié)果,輸出時(shí)要求文字說明,取小數(shù)點(diǎn)后兩位數(shù)字。請編程序。 
      main() 
      {float r,h,C1,Sa,Sb,Va,Vb; 
      scanf("%f,%f",&r,&h); 
      C1=2*3.14*r; 
      Sa=3.14*r*r; 
      Sb=4*Sa; 
      Va=4*3.14*r*r*r/3; 
      Vb=Sa*h; 
      printf("C1=%.2f\n",C1); 
      printf("Sa=%.2f\nSb=%.2f\nVa=%.2f\nVb=%.2f\n",Sa,Sb,Va,Vb); 

      4.9輸入一個(gè)華氏溫度,要求輸出攝氏溫度。公式為 
      c=5(F-32)/9 
      輸出要求有文字說明,取位2小數(shù)。 
      main() 
      {float F,c; 
      scanf("%f",&F); 
      c=5*(F-32)/9; 
      printf("c=%.2f",c); 
      4.10編程序,用getchar函數(shù)讀入兩個(gè)字符給c1、c2,然后分別用函數(shù)和函數(shù)輸出這兩個(gè)字符。并思考以下問題:(1)變量c1、c2應(yīng)定義為字符型或整形?抑二者皆可?(2)要求輸出c1和c2值的ASCII碼,應(yīng)如何處理?用putchar函數(shù)還是printf函數(shù)?(3)整形變量與字符變量是否在任何情況下都可以互相代替?如: 
      char c1,c2; 
      與 
      int c1,c2; 
      是否無條件的等價(jià)? 
      #include"stdio.h" 
      main() 
      {char c1,c2; 
      c1=getchar();c2=getchar(); 
      putchar(c1);putchar(’\n’);putchar(c2);putchar(’\n’); 
      #include"stdio.h" 
      main() 
      {char c1,c2; 
      c1=getchar();c2=getchar(); 
      printf("c1=%d c2=%d\n",c1,c2); 
      printf("c1=%c c2=%c\n",c1,c2); 
      第五章 
      5.1  什么是算術(shù)運(yùn)算?什么是關(guān)系運(yùn)算?什么是邏輯運(yùn)算? 
      解:略。 
        
      5.2  C語言中如何表示“真”和“假”?系統(tǒng)如何判斷一個(gè)量的“真”和“假”? 
      解:設(shè)有一個(gè)邏輯表達(dá)式,若其結(jié)果為“真”,則以1表示;若其結(jié)果為“假”,則以0表示。但是判斷一個(gè)邏輯量的值時(shí),以0代表“真”,以非0代表“假”。例如3&&5的值為“真”,系統(tǒng)給出3&&5的值為1。 
        
      5.3  寫出下面各邏輯表達(dá)式的值。設(shè)a=3,b=4,c=5。 
      (1)  a+b>c&&b==c 
      (2)  a||b+c&&b-c 
      (3)  !(a>b)&&!c||1 
      (4)  !(x=a)&&(y=b)&&0 
      (5)  !(a+b)+c-1&&b+c/2 
        
      解: 
      (1)  0 
      (2)  1 
      (3)  1 
      (4)  0 
      (5)    1  
      5.4  有3個(gè)整數(shù)a、b、c,由鍵盤輸入,輸出其中最大的數(shù)。 
        
      解: 
      方法一 
      #include <stdio.h> 
      main() 
      { int a,b,c; 
      printf("請輸入3個(gè)整數(shù):"); 
      scanf("%d,%d,%d",&a,&b,&c); 
      if(a<b) 
      if(b<c)  printf("max=%d\n",c); 
      else  printf("max=%d\n",b); 
      else  if(a<c)  printf("max=%d\n",c); 
      else  printf("max=%d\n",a); 

        
        
      方法二:使用條件表達(dá)式,可以使程序更加簡明、清晰。 
      程序如下:  
      #include <stdio.h> 
      main() 
      { int a,b,c,temp,max; 
      printf("請輸入3個(gè)整數(shù):"); 
      scanf("%d,%d,%d",&a,&b,&c); 
      temp=(a>b)?a:b;          /* 將a和b中的大者存人temp中 */ 
      max=(temp>c)?temp:c;     /* 將a和b中的大者與c比較,取最大者*/ 
      printf("3個(gè)整數(shù)的最大數(shù)是%d\n”,max); 
        
      5.5  有一函數(shù): 
       
       
         
      寫一程序,輸入x值,輸出y值。 
      解:  
      #include <stdio.h> 
      main() 
      {int x,y; 
      printf("輸入x:"); 
      scanf("%d",&x); 
      if(x<1)                         /* x<1 */ 
      { y=x; 
      printf("x=%3d,  y=x=%d\n",x,y); 

      else  if (x<10)                 /* 1≤x-10 */ 
      { y=2*x-1; 
      printf("x=%3d,  y=2*x-1=%d\n",x,y); 

      else                     /* x≥10 */ 
      { y=3*x-11; 
      printf("x=%3d,  y=3*x-11=%d\n",x,y); 


        
      5.6 給一個(gè)百分制成績,要求輸出等級’A’、’B’、’C’、’D’、’E’。90分以上為’A’,80~90分為’B’,70~79分為’C’,60分以下為’D’。 
      解: 
      程序如下: 
      #include <stdio.h> 
      main() 
      { float score; 
      char grade; 
      printf("請輸入學(xué)生成績:"); 
      scanf("%f",&score); 
      while(score>100||(score<0) 
      { printf("\n輸入有誤,請重新輸入:"); 
      scanf("%f",&score); 

      switch((int)(score/10)) 
      { case 10: 
      case 9: grade=’A’;break; 
      case 8: grade=’B’;break; 
      case 7: grade=’C’;break; 
      case 6: grade=’D’;break; 
      case 5: 
      case 4: 
      case 3: 
      case 2: 
      case 1: 
      case 0: grade=’E’; 

      printf("成績是%5.1f,相應(yīng)的等級是%c。\n",score,grade); 
      說明:對輸入的數(shù)據(jù)進(jìn)行檢查,如小于0或大于100,要求重新輸入。(int)(score/10)的作用是將 (score/10) 的值進(jìn)行強(qiáng)制類型轉(zhuǎn)換,得到一個(gè)整型值。 
      5.7 給定一個(gè)不多于5位的正整數(shù),要求:① 求它是幾位數(shù);② 分別打印出每一位數(shù)字;③ 按逆序打印出各位數(shù)字。例如原數(shù)為321,應(yīng)輸出123。 
      解:  
      #include <stdio.h> 
      main() 
      { long int num; 
        int indiv,ten,hundred,thousand,ten_thousand,place; 
                       /*分別代表個(gè)位、十位、百位、千位、萬位和位數(shù)*/ 
        printf("請輸入一個(gè)整數(shù)(0~99999):"); 
        scanf("%ld",&num); 
        if (num>9999)  place=5; 
        else  if(num>999)  place=4; 
        else  if(num>99)  place=3; 
        else  if(num>9)  place=2; 
        else  place=1; 
        printf("place =%d\n", place); 
        ten_thousand=num/10000; 
        thousand=num/1000%10; 
        hundred=num/100%10; 
        ten=num%100/10; 
        indiv=num%10; 
        switch(place) 
        { case 5: printf("%d,%d,%d,%d,%d",ten_thousand,thousand,hundred,ten,indiv); 
                printf("\n反序數(shù)字為;"); 
                printf("%d%d%d%d%d\n",indiv,ten,hundred,thousand,ten_thousand); 
                break; 
          case 4: printf("%d,%d,%d,%d",thousand,hundred,ten,indiv); 
                printf("\n反序數(shù)字為:"); 
                printf("%d%d%d%d\n",indiv,ten,hundred,thousand); 
                break; 
          case 3: printf("%d,%d,%d",hundred,ten,indiv); 
                printf("\n反序數(shù)字為:"); 
                printf("%d%d%d\n",indiv,ten,hundred); 
                break; 
          case 2: printf("%d,%d",ten,indiv); 
                printf("\n反序數(shù)字為:"); 
                printf("%d%d\n",indiv,ten); 
                break; 
          case 1: printf("%d",indiv); 
                printf("\n反序數(shù)字為:"); 
                printf("%d\n",indiv); 
                break; 
        } 

         
      5.8 企業(yè)發(fā)放的獎(jiǎng)金根據(jù)利潤提成。利潤I低于或等于10萬元時(shí),獎(jiǎng)金可提成10% ;利潤高于10萬元,低于20萬元(100000<I≤200000)時(shí),其中10萬元按10%提成,高于10萬元的部分,可提成7.5% ;200000<I≤400000時(shí),其中20萬元仍按上述辦法提成(下同),高于20萬元的部分按5%提成;400000<I≤600000時(shí),高于40萬元的部分按3%提成;600000〈I≤1000000時(shí),高于60萬的部分按1.5%提成;I>1000000時(shí),超過100萬元的部分按1%提成。從鍵盤輸入當(dāng)月利潤I,求應(yīng)發(fā)放獎(jiǎng)金總數(shù)。要求:(1)用if語句編程序;(2)用switch語句編程序。 
      解:計(jì)算利潤時(shí),要特別注意不同利潤的不同提成比例。例如,利潤為15萬元,其中有10萬元按10%的比例提成,另外5萬元?jiǎng)t按7.5%提成。 
      (1) 用if語句編程序。 
        
      #include <stdio.h> 
      main() 
      { long i; 
      float bonus,bon1,bon2,bon4,bon6,bon10; 
      bon1=100000*0.1;                /*利潤為10萬元時(shí)的獎(jiǎng)金*/ 
      bon2=bon1+100000*0.075;         /*利潤為20萬元時(shí)的獎(jiǎng)金*/ 
      bon4=bon2+200000*0.05;          /*利潤為40萬元時(shí)的獎(jiǎng)金*/ 
      bon6=bon4+200000*0.03;          /*利潤為60萬元時(shí)的獎(jiǎng)金*/ 
      bon10=bon6+400000*0.015;        /*利潤為100萬元時(shí)的獎(jiǎng)金*/ 
      printf("請輸入利潤i:"); 
      scanf("%ld",&i); 
      if(i<=100000) 
      bonus=i*0.1;                   /*利潤在10萬元以內(nèi)按0.1提成獎(jiǎng)金*/ 
      else  if(i<=200000) 
      bonus=bon1+(i-100000)*0.075;    /*利潤在10萬至20萬元時(shí)的獎(jiǎng)金*/ 
      else  if(i<=400000) 
      bonus=bon2+(i-200000)*0.05;     /*利潤在20萬至40萬元時(shí)的獎(jiǎng)金*/ 
      else  if(i<=600000) 
      bonus=bon4+(i-400000)*0.03;     /*利潤在40萬至60萬元時(shí)的獎(jiǎng)金*/ 
      else  if(i<=1000000) 
      bonus=bon6+(i-600000)*0.015;    /*利潤在60萬至100萬元時(shí)的獎(jiǎng)金*/ 
      else 
      bonus=bon10+(i-1000000)*0.01;   /*利潤在100萬元以上時(shí)的獎(jiǎng)金*/ 
      printf(”獎(jiǎng)金是%10.2f\n",bonus); 

        
      此題的關(guān)鍵在于正確寫出每一區(qū)間的獎(jiǎng)金計(jì)算公式。例如利潤在10萬元至20萬時(shí),獎(jiǎng)金應(yīng)由兩部分組成:①利潤為10萬元時(shí)應(yīng)得的獎(jiǎng)金。即100000ⅹ0.1;②10萬元以上部分應(yīng)得的獎(jiǎng)金。即(num-100000)ⅹ0.075。同理,20萬~40萬這個(gè)區(qū)間的獎(jiǎng)金也應(yīng)由兩部分組成:①利潤為20萬元時(shí)應(yīng)得的獎(jiǎng)金,即100000ⅹ0.1ⅹ10萬ⅹ0.075;②20萬元以上部分應(yīng)得的獎(jiǎng)金,即(num-200000)ⅹ0.05。程序中先把10萬、20萬、40萬、60萬、100萬各關(guān)鍵點(diǎn)的獎(jiǎng)金計(jì)算出來,即bon1、bon2、bon4、bon6、hon10;然后再加上各區(qū)間附加部分的獎(jiǎng)金。 
        
      (2) 用switch語句編程序。 
      輸入利潤i,確定相應(yīng)的提成等級branch 
      根據(jù)branch確定獎(jiǎng)金值 
          0    獎(jiǎng)金=i*0.1 
          1    獎(jiǎng)金=bon1+(i-105)*0.075 
          2    獎(jiǎng)金=bon2+(i-2*105)*0.05 
          3     
          4    獎(jiǎng)金=bon4+(i-4*105)*0.03 
          5     
          6    獎(jiǎng)金=bon6+(i-6*105)*0.015 
                              7     
                              8     
                              9     
                             10    獎(jiǎng)金=bon10+(i-106)*0.01 
      輸出獎(jiǎng)金 
      #include <stdio.h> 
      main() 
      { long i; 
      float bonus, bon1, bon2, bon4, bon6, bon10; 
      int c; 
      bon1=100000*0.1; 
      bon2=bon1+100000*0.075; 
      bon4=bon2+200000*0.05; 
      bon6=bon4+200000*0.03; 
      bon10=bon6+400000*0.015; 
      printf("請輸入利潤i:"); 
      scanf("%ld",&i); 
      c=i/100000; 
      if(c>10)  c=10; 
      switch(c) 
      { case 0: bonus=1*0.1;break; 
      case 1: bonus=bon1+(i-100000)*0.075;break; 
      case 2 : 
      case 3: bonus=bon2+(i-200000)*0.05; break; 
      case 4: 
      case 5: bonus=bon4+(i-400000)*0.03;break; 
      case 6: 
      case 7: 
      case 8: 
      case 9: bonus=bon6+(i-600000)*0.015;break; 
      case 10: bonus=bon10+(i-1000000)*0.01; 

      printf("獎(jiǎng)金是%10.2f",bonus); 
      5.9  輸入4個(gè)整數(shù),要求按由大到小的順序輸出。 
      解:此題采用依次比較的方法排出其大小順序。在學(xué)習(xí)了循環(huán)和數(shù)組以后,可以有更多的排序方法。 
      #include <stdio.h> 
      main() 
      { int t,a,b,c,d; 
        printf("請輸入4個(gè)整數(shù):"); 
        scanf("%d,%d,%d,%d",&a,&b,&c,&d); 
      printf("\n a=%d,b=%d,c=%d,d=%d\n",a,b,c,d); 
      if(a>b)  {t=a; a=b; b=t;} 
      if(a>c)  {t=a; a=c; c=t;} 
      if(a>d)  {t=a; a=d; d=t;} 
      if(b>c)  {t=a; b=c; c=t;} 
      if(b>d)  {t=b; b=d; d=t;} 
      if(c>d)  {t=c; c=d; d=t;} 
      printf("排序結(jié)果如下:\n"); 
      printf("%d, %d, %d, %d\n",a,b,c,d); 

        
      5.10  有4個(gè)圓塔,圓心分別為(2,2)、(-2,2)、(2,-2)、(-2,-2),圓半徑為1。這4個(gè)塔的高度分別為10m。塔以外無建筑物。今輸入任一點(diǎn)的坐標(biāo),求該點(diǎn)的建筑高度(塔外的高度為零)。 
         
       
       
       
       
       
       

      程序如下: 
      #include <stdio.h> 
      main() 
      { int h=10; 
      float x1=2,y1=2,x2=-2,y2=2,x3=-2,y3=-2,x4=-2,y4=-2,x,y,d1,d2,d3,d4; 
      printf("請輸入一個(gè)點(diǎn)(x,y):"); 
      scanf("%f,%f",&x,&y); 
      d1=(x-x1)*(x-x1)+(y-y1)*(y-y1);       /*求該點(diǎn)到各中心點(diǎn)的距離*/ 
      d2=(x-x2)*(x-x2)+(y+y2)*(y+y2); 
      d3=(x+x3)*(x+x3)+(y-y3)*(y-y3); 
      d4=(x+x4)*(x-x4)*(y+y4)*(y+y4); 
      if(d1>1&&d2>1&&d3>1&&d4>1)  h=0;      /*判斷該點(diǎn)是否在塔外*/ 
      printf("該點(diǎn)高度為%d\n",h); 

        
      第六章 
      第六章 循環(huán)控制 
        
      6.1輸入兩個(gè)正整數(shù)m和n,求其最大公約數(shù)和最小公倍數(shù)。 
      main() 
      {long m,n,i=1,j,s; 
      scanf("%ld,%ld",&m,&n); 
      for(;i<=m&&i<=n;i++) 
      {if(m%i==0&&n%i==0) s=i;} 
      if(m>=n) j=m; 
      else j=n; 
      for(;!(j%m==0&&j%n==0);j++); 
      printf("s=%ld,j=%ld\n",s,j); 
      6.2輸入一行字符,分別統(tǒng)計(jì)出其中英文字母、空格、數(shù)字和其他字符的個(gè)數(shù)。 
      #include"stdio.h" 
      main() 
      {char c;int i=0,j=0,k=0,l=0; 
      while((c=getchar())!=’\n’) 
      {if(c>=65&&c<=90||c>=97&&c<=122) i++; 
      else if(c>=48&&c<=57) j++; 
      else if(c==32) k++; 
      else l++;} 
      printf("i=%d,j=%d,k=%d,l=%d\n",i,j,k,l); 
      6.3求Sn=a+aa+aaa+…+aa…aaa(有n個(gè)a)之值,其中a是一個(gè)數(shù)字。例如:2+22+222+2222+22222(n=5),n由鍵盤輸入。 
      #include"math.h" 
      main() 
      {int n,sum=0,i=1,s=2; 
      scanf("%d",&n); 
      while(i<=n) 
      {sum=sum+s;s=s+2*pow(10,i); 
      i++;} 
      printf("sum=%d\n",sum); 
      6.4 求 ,(即求1!+2!+3!+4!+5!+…+20!) 
      main() 
      {int n,i=1;long sum=0,s=1; 
      scanf("%d",&n); 
      while(i<=n) {s=s*i;sum=sum+s;i++;} 
      printf("sum=%ld\n",sum); 
      6.5 求  
      main() 
      {double i=1,j=1,k=1,s1=0,s2=0,s3=0,sum; 
      for(;i<=100;i++) s1=s1+i; 
      for(;j<=50;j++) s2=s2+j*j; 
      for(;k<=10;k++) s3=s3+1/k; 
      sum=s1+s2+s3; 
      printf("sum=%f\n",sum); 
      6.6打印出所有"水仙花數(shù)",所謂"水仙花數(shù)"是指一個(gè)三位數(shù),其各位數(shù)字立方和等于該本身。例如:153是一個(gè)水仙花數(shù),因?yàn)?53=1^3+5^3+3^3。 
      #include"math.h" 
      main() 
      {int x=100,a,b,c; 
      while(x>=100&&x<1000) {a=0.01*x;b=10*(0.01*x-a);c=x-100*a-10*b; 
      if(x==(pow(a,3)+pow(b,3)+pow(c,3))) printf("%5d",x);x++;} 
      6.7一個(gè)數(shù)如果恰好等于它的因子之和,這個(gè)數(shù)就稱為"完數(shù)"。例如,6的因子為1、2、3,而6=1+2+3,因此6是"完數(shù)"。編程序找出1000之內(nèi)的所有完數(shù),并按下面格式輸出其因子: 
      6 its factors are 1、2、3 
      main() 
      {int m,i,j,s; 
      for(m=6;m<10000;m++) 
      {s=1; 
      for(i=2;i<m;i++) 
      if(m%i==0) s=s+i; 
      if(m-s==0) 
      {printf("%5d its fastors are 1 ",m);for(j=2;j<m;j++) if(m%j==0) 
      printf("%d ",j);printf("\n");} 


      或 
      main() 
      {int m,i,j,s; 
      for(m=6;m<1000;m++) 
      {s=m-1; 
      for(i=2;i<m;i++) 
      if(m%i==0) s=s-i; 
      if(s==0) 
      {printf("%5d its fastors are 1 ",m);for(j=2;j<m;j++) if(m%j==0) 
      printf("%d ",j);printf("\n");} 

      6.8有一分?jǐn)?shù)序列: 
          
      求出這個(gè)數(shù)列的前20項(xiàng)之和。 
      main() 
      {int i=1,n;double t,x=1,y=2,s,sum=0; 
      scanf("%ld",&n); 
      while(i<=n) {s=y/x;sum=sum+s;t=y;y=y+x;x=t;i++;} 
      printf("%f\n",sum); 
      6.9一球從100米高度自由下落,每次落地后返回原高度的一半,再落下。求它在第10次落地時(shí)共經(jīng)過多少米?第10次反彈多高?  
      main() 
      {int i,n;double h=100,s=100; 
      scanf("%d",&n); 
      for(i=1;i<=n;i++) 
      {h*=0.5;if(i==1) continue;s=2*h+s;} 
      printf("h=%f,s=%f\n",h,s); 
      6.10猴子吃桃問題。猴子第一天摘下若干個(gè)桃子,當(dāng)即吃了一半,還不過癮,又多吃了一個(gè)。第二天早上又將剩下的桃子吃掉一半,又多吃一個(gè)。以后每天早上都吃了前一天剩下的一半零一個(gè)。到第10天早上想再吃時(shí),見只剩下一個(gè)桃子了。求第一天共摘多少桃子。 
      main() 
      {int i=1,sum=0; 
      for(;i<=10;sum=2*sum+1,i++); 
      printf("sum=%d\n",sum); 
      6.11用迭代法求 。求平方根的迭代公式為: 
        
      要求前后兩次求出的得差的絕對值少于0.00001。 
      #include"math.h" 
      main() 
      {float x0,x1,a; 
      scanf("%f",&a); 
      x1=a/2; 
      do 
      {x0=x1;x1=(x0+a/x0)/2;} 
      while(fabs(x0-x1)>=0.00001); 
      printf("%.3f\n",x1); 
      6.12 用牛頓迭代法求方程在1.5附近的根。 
        
      main() 
      {double x,y;x=1.5; 
      do{y=2*x*x*x-4*x*x+3*x-6; 
      x=x-y/(6*x*x-8*x+3);} 
      while(y!=0); 
      printf("x=%.3f\n",x); 
      6.13用二分法求方程在(-10,10)之間的根 
        
      main() 
      {double x1,x2,y1,y2;x1=-10;x2=10; 
      do{y1=2*x1*x1*x1-4*x1*x1+3*x1-6; 
      x1=x1-y1/(6*x1*x1-8*x1+3);} 
      while(y1!=0); 
      do 
      {y2=2*x2*x2*x2-4*x2*x2+3*x2-6; 
      x2=x2-y2/(6*x2*x2-8*x2+3);} 
      while(y2!=0); 
      printf("x1=%.3f,x2=%.3f\n",x1,x2); 
      6.14打印以下圖案 
       
       

      *   *   * 
      *   *   *   *   * 
      *   *   *   *   *   *   * 
      *   *   *   *   * 
      *   *   * 

      #include"math.h" 
      main() 
      {int i,j,k; 
       for(i=0;i<=3;i++) 
        {for(j=0;j<=2-i;j++) 
          printf(" "); 
          for(k=0;k<=2*i;k++) 
             printf("*"); 
          printf("\n"); 
         } 
       for(i=0;i<=2;i++) 
        {for(j=0;j<=i;j++) 
           printf(" "); 
         for(k=0;k<=4-2*i;k++) 
           printf("*"); 
         printf("\n"); 
         }  

      第七章 
      第七章 數(shù)組 
        
      7.1 用篩法求之內(nèi)的素?cái)?shù)。 
      main() 
      { int i,j,a[100]; 
      for(i=2;i<100;i++) 
      { a[i]=i; 
      for(j=2;j<=i;j++) 
      {if(j<i) 
       if(a[i]%j==0) 
       break; 
      if(a[i]-j==0) 
       printf("%5d",a[i]); 


      printf("\n"); 

      或 
      #include"math.h" 
      main() 
      {static int i,j,k,a[98]; 
      for(i=2;i<100;i++) 
      {a[i]=i;k=sqrt(i); 
      for(j=2;j<=a[i];j++) 
      if(j<k) if(a[i]%j==0)  
      break; 
      if(j>=k+1)  
      printf("%5d",a[i]); 

      printf("\n"); 

      7.2用選擇法對10個(gè)整數(shù)從小到大排序。 
      main() 
      { int i,j,a[10],t; 
      for(i=0;i<10;i++) 
      scanf("%d",&a[i]); 
      for(j=1;j<10;j++) 
      for(i=0;i<=9-j;i++) 
      if(a[i]>a[i+1])  
      {t=a[i+1];a[i+1]=a[i];a[i]=t;} 
      for(i=0;i<10;i++) 
      printf("%5d",a[i]); 

      或 
      main() 
      {static int a[10],i,j,k,t; 
      for(i=1;i<11;i++) 
      scanf("%d",&a[i]); 
      for(j=1;j<10;j++) 
      for(i=1;i<=10-j;j++) 
      if (a[i]>a[i+1])  
      {t=a[i+1];a[i+1]=a[i];a[i]=t;} 
      for(i=1;i<11;i++) 
      printf("%d",a[i]); 
      printf("\n"); 
      7.3求一個(gè)3×3矩陣對角線元素之和。 
      main() 
      {int i=0,j=0,a[3][3],s1,s2; 
      for(i=0;i<3;i++) 
      for(j=0;j<3;j++) 
      scanf("%d",&a[i][j]); 
      s1=a[0][0]+a[1][1]+a[2][2]; 
      s2=a[0][2]+a[1][1]+a[2][0]; 
      printf("s1=%d,s2=%d\n",s1,s2); 

      或 
      main() 

      static int i,j,s1,s2,a[3][3]; 
      for(i=1;i<=3;i++) 
      for(j=1;j<=3;j++) 
      scanf("%d",&a[i][j]); 
      s1=a[1][1]+a[2][2]+a[3][3]; 
      s2=a[1][3]+a[2][2]+a[3][1]; 
      printf("%d,%d\n",s1,s2); 
      7.4已有一個(gè)已排好的數(shù)組今輸入一個(gè)數(shù)要求按原來排序的規(guī)律將它插入數(shù)組中。 
      main() 
      { static int a[10]={1,7,8,17,23,24,59,62,101};int i,j,t; 
      scanf("%d",&a[9]); 
      for(i=9;i>0;i--) 
      if(a[i]<a[i-1]) 
      {t=a[i-1];a[i-1]=a[i];a[i]=t;} 
      for(i=0;i<10;i++) 
      printf("%5d",a[i]);printf("\n"); 

      或 
      main() 

      static int a[5]={1,4,5,6,7}; 
      int i,t,b; 
      scanf("%d",&b); 
      for(i=0;i<5;i++) 
      {if(b<=a[i]) 
      {t=a[i];a[i]=b;b=t;} 
      printf("%d ",a[i]);} 
      printf("%d",b); 
      7.5將一個(gè)數(shù)組的值按逆序重新存放,例如,原來順序?yàn)椋?,6,5,4,1。要求改為:1,4,5,6,8。 
      main() 
      { int i,b[10]; 
      for(i=0;i<10;i++) 
      scanf("%d",&b[i]); 
      for(i=9;i>-1;i--) 
      printf("%5d",b[i]); 
      printf("\n");} 
      7.6打印出以下楊輝三角形(要求打印出10行)。 

      1  1 
      1  2  1 
      1  3  3  1 
      1  4  6  4  1 
      1  5  10 10  5  1 
      ∶  
      ∶ 
      main() 
      { static int m,n,k,b[15][15]; 
      b[0][1]=1; 
      for(m=1;m<15;m++) 
      {for(n=1;n<=m;n++) 
      { b[m][n]=b[m-1][n-1]+b[m-1][n]; 
      printf("%-5d",b[m][n]);}printf("\n"); 



      或 
      main() 
      { int i,j,n,k,a[10][10]; 
      static a[][1]={{1},{1},{1},{1},{1},{1},{1},{1},{1},{1}}; 
      a[1][1]=1; 
      for(k=2,k<11;k++) 
      for(i=2;i<=k;i++) 
      for(j=2;j<=i;j++) 
      a[i][j]=a[i-1][j-1]+a[i-1][j]; 
      for(k=1;k<11;k++) 
      for(i=1;i<=k;i++) 
      for(j=1;j<=i;j++) 
      printf("%d",a[i][j]); 

      7.7  打印“魔方陣”,所謂魔方陣是指這樣的方陣,它的每一行、每一列和對角線之和均相等。例如,三階魔方陣為 
         8   1   6 
         3   5   7 
         4   9   2 
      要求打印出由1~n2的自然數(shù)構(gòu)成的魔方陣。 
      解:  
      #include <stdio.h> 
      main() 
      { int a[16][16],i,i,k,p,m,n; 
        p=1; 
        while(p==1)                             /*要求階數(shù)為1~15的商數(shù)*/ 
        { printf("Enter n(n=1~15):"); 
            scanf("%d",&n); 
          if((n!=0)&&(n<=15)&&(n%2!=0))  p=0; 
        } 
        for(i=1;i<=n;i++)                      /*初始化*/ 
          for(j=1;j<=n;j++)  a[i][j]=0; 
        j=n/2+1;                               /*建立魔方陣*/ 
        a[1][j]=1; 
        for(k=2;k<=n*n;k++) 
        { i=i-1; 
          j=j+1; 
          if((i<1)&&(j>n)) 
          { i=i+2; 
            j=j-1; 
          } 
          else 
          { if(i<1)  i=n; 
            if(j>n)  j=1; 
          } 
          if(a[i][j]==0)  a[i][j]=k; 
          else 
          { i=i+2; 
            j=j-1; 
            a[i][j]=k; 
          } 
        } 
        for(i=1;i<=n;i++)                      /*輸出魔方陣*/ 
        { for(j=1;j<=n;j++) 
            printf("%4d",a[i][j]); 
          printf("\n"); 
        } 
      7.8找出一個(gè)二位數(shù)組中的鞍點(diǎn),即該位置上的元素在該行上最大,在該列上最小,也可能沒有鞍點(diǎn)。 
      main() 
      {int a[5][5],b[5],c[5],d[5][5],k=0,l=0;int i,j; 
      for(i=0;i<5;i++) 
      for(j=0;j<5;j++) 
      scanf("%d",&d[i][j]); 
      for(i=0;i<5;i++) 
      for(j=0;j<5;j++,a[i][j]=d[i][j]); 
      for(i=0,k=0;i<5;i++,k++) 
      for(j=0;j<4;j++) 
      {if(a[i][j]>=a[i][j+1]) 
       b[k]=a[i][j+1]=a[i][j]; 
      else  
      b[k]=a[i][j+1]; 

      for(j=0,l=0;j<5;j++,l++) 
      for(i=0;i<4;i++) 
      {if(a[i][j]<=a[i+1][j]) 
      c[l]=a[i+1][j]=a[i][j]; 
      else  
      c[l]=a[i+1][j]; 

      for(i=0,k=0;i<5;i++,k++) 
      for(j=0,l=0;j<5;j++,l++) 
      if(d[i][j]-b[k]==0) 
      {if(d[i][j]-c[l]==0) 
       printf("d[%d][%d]=%d\n",i,j,d[i][j]); 
      else 
      printf("d[%d][%d]=%d isnot andi\n",i,j,d[i][j]); 

      7.9有個(gè)15數(shù)按由小到大順序存放在一個(gè)數(shù)組中,輸入一個(gè)數(shù),要求用折半查找法找出該數(shù)組中第幾個(gè)元素的值。如果該數(shù)不在數(shù)組中,則打印出"無此數(shù)" 
      #include"math.h" 
      main() 
      {static int i,j,m,a[15]={1,4,9,13,21,34,55,89,144,233,377,570,671,703,812}; 
      scanf("%d",&m); 
      for(j=0;j<15;j++) 
      printf("%4d",a[j]); 
      printf("\n"); 
      i=7; 
      while(fabs(i-7)<8) 
      {if(m<a[7]) 
      {if(a[i]-m==0)  
      {printf("it is at (%d)\n",i+1);break;}i--;} 
      else if(m>a[7]) 
      {if(a[i]-m==0)  
      {printf("it is at (%d)\n",i+1);break;}i++;} 
      else 
      printf("8\n"); 

      if(fabs(i-7)-8==0) 
       printf("There is not\n"); 
      7.10有一篇文章,共有3行文字,每行有個(gè)80字符。要求分別統(tǒng)計(jì)出其中英文大寫字母、小寫字母、空格以及其它字符的個(gè)數(shù)。 
      main() 
      {int i,j=0,k=0,l=0,m=0,n=0;char str0[301],str1[100],str2[100],str3[100]; 
      gets(str1);gets(str2);gets(str3); 
      strcat(str0,str1);strcat(str0,str2);strcat(str0,str3); 
      for(i=0;str0[i]!=’\0’;i++) 
      {if(str0[i]>=65&&str0[i]<=90) j++; 
      else if(str0[i]>=97&&str0[i]<=122) k++; 
      else if(str0[i]>=48&&str0[i]<=57) l++; 
      else if(str0[i]==32) m++; 
      else n++;} 
      printf("Daxie Xiaoxie Shuzi Kongge Qita\n"); 
      printf("%5d %7d %5d %6d %4d\n",j,k,l,m,n); 
      7.11打印以下圖案 
      *?。。。。?nbsp;
      *?。。。。?nbsp;
      * *?。。。?nbsp;
      *   *?。。。?nbsp;
      *?。。。。?nbsp;
      main() 
      {int i,j,k;char a[5][5]; 
      for(i=0;i<5;i++) 
      {for(j=0;j<5;j++) 
      {a[i][j]=’*’;printf("%c",a[i][j]);} 
      printf("\n"); 
      for(k=1;k<=i+1;k++) 
       printf("\40");} 
      printf("\n"); 
      7.12有一行電文譯文下面規(guī)律譯成密碼: 
      A->Z a->z 
      B->Y b->y 
      C->X c->x 
      … 
      即第一個(gè)字母變成第26個(gè)字母,第i個(gè)字母變成第(26-i+1)個(gè)字母。非字母字符不變,要求編程序?qū)⒚艽a回原文,并打印出密碼和原文。 
      main() 
      { int i;char str1[100],str2[100]; 
      gets(str1); 
      for(i=0;str1[i]!=’\0’;i++) 
      if(str1[i]>=65&&str1[i]<=90)  
      str2[i]=155-str1[i]; 
      else if(str1[i]>=97&&str1[i]<=122) 
       str2[i]=219-str1[i]; 
      else  
      str2[i]=str1[i]; 
      printf("%s\n%s\n",str1,str2); 
      7.13編一程序,將兩個(gè)字符串連接起來,不要strcat函數(shù)。 
      main() 
      { int i,j;char str1[100],str2[100],str3[201]; 
      gets(str1); 
      gets(str2); 
      for(i=0;str1[i]!=’\0’;i++) 
      str3[i]=str1[i]; 
      for(j=0;str2[j]!=’\0’;j++) 
       str3[j+i]=str2[j]; 
      printf("%s\n%s\n%s\n",str1,str2,str3); 
      7.14編一個(gè)程序,將兩個(gè)字符串S1和S2比較,如果S1>S2,輸出一個(gè)正數(shù);S1=S2,輸出0;S1<S2,輸出一個(gè)負(fù)數(shù)。不要用strcpy函數(shù)。兩個(gè)字符串用gets函數(shù)讀入。輸出的正數(shù)或負(fù)數(shù)的絕對值應(yīng)是相比較的兩個(gè)字符串相對應(yīng)字符的ASCII碼的差值。例如,’A’與’C’相比,由于’A’<’C’,應(yīng)輸出負(fù)數(shù),由于’A’與’C’的碼差值為2,因此應(yīng)輸出"-2"。同理:"And"和"Aid"比較,根據(jù)第2個(gè)字符比較結(jié)果,’n’比’i’大5,因此應(yīng)輸出"5"。 
      #include <stdio.h> 
      #include <string.h> 
      main() 
      { int i,resu; 
        char s1[100],s2[100]; 
        printf("\n input string1:"); 
        gets(s1); 
        printf("\n Input string2:"); 
        gets(s2); 
        i=0; 
        while(s1[i]==s2[i]&&s1[i]!=’\0’)  i++; 
        if(s1[i]==’\0’&&s2[i]==’0’)  resu=0; 
        else  resu=s1[i]-s2[i]; 
        printf("\n result:%d\n",resu); 
      7.15 編寫一個(gè)程序,將字符數(shù)組s2中的全部字符拷貝到字符數(shù)組s1中,不用strcpy函數(shù)??截悤r(shí),’\0’也要拷貝過去,’\0’后面的字符不拷貝。 
        
      解:  
      #include "stdio.h" 
      main() 
      { char s1[80],s2[80]; 
        int i; 
        printf("Input s2:"); 
        scanf("%s",s2); 
        for(i=0;i<strlen(s2);i++) 
          s1[i]=s2[i]; 
        printf("s1:%s\n",s1); 
      第八章 
      第八章 函數(shù) 
        
      1.1寫兩個(gè)函數(shù),分別求兩個(gè)整數(shù)的最大公約數(shù)和最小公倍數(shù),用主函數(shù)調(diào)用這兩個(gè)函數(shù),并輸出結(jié)果兩個(gè)整數(shù)由鍵盤輸入。 
      maxyueshu(m,n) 
      int m,n; 
      {  int i=1,t; 
      for(;i<=m&&i<=n;i++) 
      {if(m%i==0&&n%i==0) 
       t=i; 

      return(t); 

      minbeishu(m,n) 
      int m,n; 
      {int j; 
      if(m>=n) j=m; 
      else j=n; 
      for(;!(j%m==0&&j%n==0);j++); 
      return j; 

      main() 
      {int a,b,max,min; 
      printf("enter two number is: "); 
      scanf("%d,%d",&a,&b); 
      max=maxyueshu(a,b); 
      min=minbeishu(a,b); 
      printf("max=%d,min=%d\n",max,min); 
      8.2求方程 的根,用三個(gè)函數(shù)分別求當(dāng)b2-4ac大于0、等于0、和小于0時(shí)的根,并輸出結(jié)果。從主函數(shù)輸入a、b、c的值。 
      #include"math.h" 
      float yishigen(m,n,k) 
      float m,n,k; 
      {float x1,x2; 
      x1=(-n+sqrt(k))/(2*m); 
      x2=(-n-sqrt(k))/(2*m); 
      printf("two shigen is x1=%.3f and x2=%.3f\n",x1,x2); 

      float denggen(m,n) 
      float m,n; 
      {float x; 
      x=-n/(2*m); 
      printf("denggen is x=%.3f\n",x); 

      float xugen(m,n,k) 
      float m,n,k; 
      {float x,y; 
      x=-n/(2*m); 
      y=sqrt(-k)/(2*m); 
      printf("two xugen is x1=%.3f+%.3fi and x2=%.3f-%.3fi\n",x,y,x,y); 

      main() 
      {float a,b,c,q; 
      printf("input a b c is "); 
      scanf("%f,%f,%f",&a,&b,&c); 
      printf("\n"); 
      q=b*b-4*a*c; 
      if(q>0) yishigen(a,b,q); 
      else if(q==0) denggen(a,b); 
      else xugen(a,b,q); 
      8.2寫一個(gè)判斷素?cái)?shù)的函數(shù),在主函數(shù)輸入一個(gè)整數(shù),輸出是否是素?cái)?shù)的消息。 
      psushu(m) 
      int m; 
      {int i=2,t; 
      for(;i<=m;i++) 
      if(m%i==0&&i<m) break; 
      if(m-i==0) t=1; 
      else t=0; 
      return m; 

      main() 
      {int a,s; 
      printf("enter sushu is \n"); 
      scanf("%d",&a); 
      s=psushu(a); 
      if(s==1) printf("a is sushu\n"); 
      else printf("s is not sushu\n"); 
      8.4寫一個(gè)函數(shù),使給定的一個(gè)二維數(shù)組(3×3)轉(zhuǎn)置,即行列互換。 
      int zhuangzhi(b) 
      int b[3][3]; 
      {int i,j,t; 
      for(i=0;i<3;i++) 
      for(j=0;j>=i&&j<3-i;j++) 
      {t=b[i][j];b[i][j]=b[j][i];b[j][i]=t;} 

      main() 
      {int a[3][3];int i,j; 
      for(i=0;i<3;i++) 
      for(j=0;j<3;j++) 
      scanf("%d",&a[i][j]); 
      for(i=0;i<3;i++) 
      {for(j=0;j<3;j++) 
      printf(" %d",a[i][j]); 
      printf("\n");} 
      zhuangzhi(a); 
      for(i=0;i<3;i++) 
      {for(j=0;j<3;j++) 
      printf(" %d",a[i][j]); 
      printf("\n");} 
      8.5寫一函數(shù),使輸入的一個(gè)字符串按反序存放,在主函數(shù)中輸入輸出字符串。 
      main() 
      {char str0[100]; 
      gets(str0); 
      fanxu(str0); 
      puts(str0); 

      fanxu(str1) 
      char str1[100]; 
      {int i,t,j; 
      char str2[100];strcpy(str2,str1); 
      t=strlen(str1); 
      for(i=0,j=t-1;j>-1;i++,j--) 
      str1[i]=str2[j]; 
      8.6寫一函數(shù),將兩個(gè)字符串連接。 
      lianjie(a,b) 
      char a[100],b[100]; 
      {strcat(a,b); 

      main() 
      {char str1[100],str2[100]; 
      gets(str1);gets(str2); 
      lianjie(str1,str2); 
      puts(str1); 
      8.7寫一函數(shù),將兩個(gè)字符串中的元音字母復(fù)制到另一個(gè)字符串,然后輸出。 
      fuzhi(a,b) 
      char a[100],b[100]; 
      {int i,j=0; 
      for(i=0;a[i]!=’\0’;i++) 
      if(a[i]==97||a[i]==101||a[i]==105||a[i]==111||a[i]==117||a[i]==65|| 
      a[i]==69||a[i]==73||a[i]==85) {b[j]=a[i];j++;} 

      main() 
      {char str1[100],str2[100]; 
      gets(str1); 
      fuzhi(str1,str2); 
      puts(str2); 
      8.8寫一函數(shù),輸入一個(gè)四位數(shù)字,要求輸出這四個(gè)數(shù)字字符,但每兩個(gè)數(shù)字間空格。如輸入1990,應(yīng)輸出"1_9_9_0"。 
      char f(b) 
      char b[4]; 
      {int i=0; 
      for(;i<4;i++) 
      {printf(" "); 
      printf("%c",b[i]);} 
      printf("\n"); 

      main() 
      {int a,u,v,w,t;char c[4]; 
      scanf("%4d",&a); 
      u=a*0.001;v=0.01*(a-1000*u);w=(a-1000*u-100*v)*0.1;t=a-1000*u-100*v-10*w; 
      c[0]=u+48; 
      c[1]=v+48; 
      c[2]=w+48; 
      c[3]=t+48; 
      f(c); 
      8.9編寫一函數(shù),由實(shí)參傳來一個(gè)字符串,統(tǒng)計(jì)此字符串中字母、數(shù)字、空格和其它字符的個(gè)數(shù),在主函數(shù)中輸入字符串以及輸出上述結(jié)果。 
      char tongji(str0,b) 
      char str0[100]; 
      int b[4]; 
      {int i; 
      for(i=0;str0[i]!=’\0’;i++) 
      {if(str0[i]>=65&&str0[i]<=90||str0[i]>=97&&str0[i]<=122) b[0]++; 
      else if(str0[i]>=48&&str0[i]<=57) b[1]++; 
      else if(str0[i]==32) b[2]++; 
      else b[3]++;} 

      main() 
      {char str1[100];static int i,a[4]; 
      gets(str1); 
      tongji(str1,a); 
      printf("zimu Shuzi Kongge Qita\n"); 
      for(i=0;i<4;i++) 
      printf("%-8d ",a[i]);printf("\n"); 
      8.10寫一函數(shù),輸入一行字符,將此字符串中最長的單詞輸出。 
      cechang(str1,word0) 
      char str1[100],word0[15]; 
      {int i=0,j=0,t=0; 
      static char word1[15]; 
      for(;str1[i]!=’\0’;i++) 
      {if(!(str1[i]>=97&&str1[i]<=122||str1[i]>=65&&str1[i]<=90)) 
      {t=j;j=0;continue;} 
      word1[j]=str1[i];j++; 
      if(j>=t) strcpy(word0,word1);} 

      main() 
      {char str0[100],longword[15]; 
      gets(str0); 
      cechang(str0,longword); 
      puts(longword); 
      8.11寫一函數(shù)用起泡法對輸入的個(gè)字符按由小到大的順序排列。 
      int paixu(x) 
      int x[]; 
      {int i,j,t; 
      for(j=1;j<10;j++) 
      for(i=0;i<=9-j;i++) 
      if(x[i]>x[i+1]) {t=x[i+1];x[i+1]=x[i];x[i]=t;} 

      main() 
      {int y[10];int i; 
      for(i=0;i<10;i++) 
      scanf("%d",&y[i]); 
      paixu(y); 
      for(i=0;i<10;i++) 
      printf("%5d",y[i]); 
      printf("\n"); 
      8.12用牛頓迭代法求根。方程為: ,系數(shù)a,b,c,d由主函數(shù)輸入。求X在1附近的一個(gè)實(shí)根。求出后由主函數(shù)輸出。 
      double qigen(s,t,u,v) 
      int s,t,u,v; 
      {double x,y;x=1; 
      do{y=s*x*x*x+t*x*x+u*x+v; 
      x=x-y/(3*s*x*x+2*t*x+u);} 
      while(y!=0); 
      return x; 

      main() 
      {int a,b,c,d;double x; 
      scanf("%d,%d,%d,%d",&a,&b,&c,&d); 
      x=qigen(a,b,c,d); 
      printf("x=%.3f\n",x); 
      8.13用遞歸方法求n階勒讓德多項(xiàng)式的值遞歸公式為 
       
       
      float p(x0,n) 
      int n;float x0; 
      {float y; 
      if(n==0||n==1) if(n==1) y=x0;else y=1; 
      else y=((2*n-1)*x0*p(x0,n-1)-(n-1)*p(x0,n-2))/n; 
      return(y); 

      main() 
      {float x,y0;int a,i; 
      scanf("%f,%d",&x,&a); 
      y0=p(x,a); 
      printf("y0=%.3f\n",y0); 
      8.14輸入10個(gè)學(xué)生5門課的成績,分別用函數(shù)求:①每個(gè)學(xué)生平均分;②每門課的平均分;③找出最高分所對應(yīng)的學(xué)生和課程;④求平均分方差:δ=[SXi^2]/n-(SXi/n)^2,為一學(xué)生的平均分 
      float x1[10],x2[5]; 
      float pp(),cc(),find(),xx(); 
      main() 
      {char name[10][20],class[5][20];float score[10][5],o,k=0,max[5];int a[5],i,j; 
      for(i=0;i<10;i++) 
      gets(name[i]); 
      for(j=0;j<5;j++) gets(class[j]); 
      for(i=0;i<10;i++) 
      for(j=0;j<5;j++) 
      scanf("%f",&score[i][j]); 
      pp(score); 
      cc(score); 
      find(score,max,a); 
      o=xx(k); 
      for(i=0;i<10;i++) 
      {puts(name[i]); 
      printf("%.3f\n",x1[i]);} 
      for(j=0;j<5;j++) 
      {puts(class[j]);printf("%.3f\n",x2[j]);} 
      for(j=0;j<5;j++) {printf("%.3f \n",max[j]); 
      puts(name[a[j]]); 
      puts(class[j]);} 
      printf("o=%.3f\n",o); 

      float pp(f) 
      float f[10][5]; 
      {float sum=0;int i,j; 
      for(i=0,sum=0;i<10;i++) 
      {for(j=0;j<5;j++) 
      sum=sum+f[i][j]; 
      x1[i]=sum/5;} 

      float cc(y) 
      float y[10][5]; 
      {float sum=0;int i,j; 
      for(j=0;j<5;j++) 
      {for(i=0;i<10;i++) 
      sum=sum+y[i][j]; 
      x1[j]=sum/10;} 

      float find(z,s,t) 
      float z[10][5],s[5];int t[5]; 
      {int i,j; 
      for(j=0,s[j]=z[0][j];j<5;j++) 
      for(i=0;i<10;i++) 
      if(s[j]<z[i][j]) {s[j]=z[i][j];t[j]=i;} 

      float xx(q) 
      float q; 
      {float f=0,e=0;int i; 
      for(i=0;i<10;i++) 
      {e=x1[i]*x1[i]+e; 
      f=f+x1[i];} 
      q=e/10-(f/10)*(f/10); 
      return(q); 

      8.15寫幾個(gè)函數(shù):①輸個(gè)職工的姓名和職工號;②按職工號由小到大順序排序,姓名順序也隨之調(diào)整;③要求輸入一個(gè)職工號,用折半法找出該職工的姓名,從主函數(shù)輸入要查找的職工號,輸出該職工姓名。  
      #define N 10 
      find(a,b) 
      int a[],b[]; 
      {int i,j,s,t,c[N][2]; 
      for(i=0;i<N;i++) 
      {c[i][1]=a[i];c[i][1]=i;} 
      for(i=0;i<N;i++) 
      for(j=0;j<N-i-1;j++) 
      if(c[i][0]>c[i+1][0]) 
      {t=c[i][0];c[i][0]=c[i+1][0];c[i+1][0]=t; 
      s=c[i][1];c[i][1]=c[i+1][1];c[i+1][1]=s;} 
      for(i=0;i<N;i++) 
      b[i]=c[i][1]; 
      return; 

      lookfor(h,k) 
      int h[],k; 
      {int i,j; 
      for(i=0;i<N;i++) 
      if(h[i]-k==0) j=i; 
      return j; 

      main() 
      {int number[N],x[N],i,j,u,p;char name[N][20]; 
      for(i=0;i<N;i++) 
      {gets(name[i]); 
      scanf("%d",&number[i]);} 
      scanf("%d",&p); 
      find(number,x); 
      u=lookfor(number,p); 
      for(i=0;i<N;i++) 
      {printf("%d",number[i]); 
      puts(name[x[i]]);} 
      puts(name[x[u]]); 
      8.16寫一函數(shù),輸入一個(gè)十六進(jìn)制數(shù),輸出相應(yīng)的十進(jìn)制數(shù)。 
      #include"math.h" 
      int x; 
      ff(shu) 
      char shu[]; 
      {int i=strlen(shu)-1,sum=0; 
      for(;i>-1;i++) 
      {if {if(48<=shu[i]<=57) sum=sum+(shu[i]-48)*pow(16,(i-1)); 
      else if(65<=shu[i]<=90) sum=sum+(shu[i]-55)*pow(16,(i-1)); 
      else if(97<=shu[i]<=102) sum=sum+(shu[i]-87)*pow(16,(i-1)); 
      x=1;} 
      else x=0; 
      return x; 

      main() 
      {char shufu[100];int s; 
      gets(shufu);s=ff(shufu); 
      if(x) printf("s(D)=%d\n",s); 
      else printf("The number is not ox\n"); 
      8.17用遞歸法將一個(gè)整數(shù)n轉(zhuǎn)換成字符串。例如,輸入486,應(yīng)輸出字符串"486"。n的位數(shù)不確定,可以是任意位數(shù)的整數(shù)。 
      #include"math.h" 
      int x[10]; 
      pf(m,n) 
      unsigned long m;int n; 
      {int y; 
      if(n==0) {y=(int)(m%10);x[0]=y;} 
      else {y=(unsigned long)((m-pf(m,n-1))/pow(10,n))%10;x[n]=y;} 
      return(y); 

      main() 
      {unsigned long a,b;int i,j,k;char c[11]; 
      scanf("%ld",&a); 
      for(j=0,b=a;b>0.1;j++,b/=10); 
      pf(a,j-1); 
      for(i=0,k=j-1;i<j;i++,k--) 
      c[i]=x[k]+48;c[10]=’\0’; 
      puts(c);} 
      或 
      #include"math.h" 
      char x[11]; 
      pf(m,o) 
      unsigned long m;int o; 
      {int j,i; 
      for(i=o-1,j=0;i>-1;i--,j++) 
      x[i]=(int)((unsigned long)(m/pow(10,j))%10)+48; 
      return; 

      main() 
      {unsigned long a,b;int j,i; 
      scanf("%ld",&a); 
      for(j=0,b=a;b>0.1;j++,b/=10); 
      pf(a,j); 
      puts(x);printf("%d\n",j);} 
      或 
      #include"math.h" 
      int x[10];unsigned long m; 
      pf(n) 
      int n; 
      {int y; 
      if(n==0) {y=m%10;x[0]=y;} 
      else {y=(unsigned long)((m-pf(n-1))/pow(10,n))%10;x[n]=y;} 
      return(y); 

      main() 
      {unsigned long a;int i,j,k;char c[11]; 
      scanf("%ld",&m); 
      for(j=0,a=m;a>0.1;j++,a/=10); 
      pf(j-1); 
      for(i=0,k=j-1;i<j;i++,k--) 
      c[i]=x[k]+48;c[10]=’\0’; 
      puts(c);} 
      8.18給出年、月、日,計(jì)算該日是該年的第幾天。 
      int find(x,y,z) 
      int x,y,z; 
      { int i,t,s,days=0; 
      if(x%4==0) t=1; 
      else t=0; 
      for(i=1;i<y;i++) 
      {if(i==2) s=2-t; 
      else s=0; 
      days=days+30+i%2-s; 

      days=days+z; 
      return(days); 

      main() 
      {int year,month,date,day; 
      scanf("%d %d %d",&year,&month,&date); 
      day=find(year,month,date); 
      printf("THE DATE IS THE %dth DAYS\n",day); 

        
      第九章 
      第九章 編譯預(yù)處理 
        
      9.1定義一個(gè)帶參的宏,使兩個(gè)參數(shù)的值互換,并寫出程序,輸入兩個(gè)數(shù)作為使用宏時(shí)的實(shí)參。輸出已交換后的兩個(gè)值。 
      #define CHANGE(a,b,t) t=a;a=b;b=a 
      main() 
      {int c,d,s; 
      scanf("%d,%d",&c,&d); 
      CHANGE(c,d,s); 
      printf("c=%d,d=%d\n",c,d); 
      9.2輸入兩個(gè)整數(shù),求他們相除的余數(shù)。用帶參的宏來實(shí)現(xiàn),編程序。 
      #define Q(a,b) a%b 
      main() 
      {int c,d,t; 
      scanf("%d %d",&c,&d); 
      t=Q(c,d); 
      printf("t=%d\n",t); 
      9.3三角形面積為: 
        
      其中S=(a+b+c)/2,a、b、c為三角形的三邊。定義兩個(gè)帶參的宏S,一個(gè)用來求area,另一個(gè)宏用來求。寫程序,在程序中用帶實(shí)參的宏名來求面積area。 
      #include"math.h" 
      #define SSS(m,n,k) (m+n+k)/2 
      #define AQRT(m,n,k) sqrt(SSS(m,n,k)*(SSS(m,n,k)-m)*(SSS(m,n,k)-n)*(SSS(m,n,k)-k)) 
      main() 
      {float a,b,c,s,area; 
      scanf("%f %f %f",&a,&b,&c); 
      s=SSS(a,b,c); 
      area=AQRT(a,b,c); 
      printf("s=%.3f area=%.3f\n",s,area); 

      或 
      #include"math.h" 
      #define SSS(m,n,k) (m+n+k)*0.5 
      #define AQRT(m,n,k) sqrt(((m+n+k)/2)*((m+n+k)/2-m)*((m+n+k)/2-n)*((m+n+k)/2-k)) 
      main() 
      {float a,b,c,t,area; 
      scanf("%f %f %f",&a,&b,&c); 
      t=SSS(a,b,c); 
      area=AQRT(a,b,c); 
      printf("t=%.3f;area=%.3f\n",t,area); 

      或 
      #include"math.h" 
      #define SSS(m,n,k) ((m+n+k)/2) 
      #define AQRT(m,n,k,m,n,k,m,n,k,m,n,k,m,n,k,m,n,k,m,n,k,m,n,k,m,n,k) sqrt(SSS*(SSS-m)*(SSS-n)*(SSS-k)) 
      main() 
      {float a,b,c,t,area; 
      scanf("%f %f %f",&a,&b,&c); 
      t=SSS(a,b,c); 
      area=AQRT(a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,a,b,c); 
      printf("t=%.3f;area=%.3f\n",t,area); 
      9.4給年份year,定義一個(gè)宏,以判別該年份是否閏年。提示:宏名可以定義為LEAP_YEAR,形參為y,既定義宏的形式為 
      #define LEAP_YEAR(y) (讀者設(shè)計(jì)的字符串) 
      在程序中用以下語句輸出結(jié)果: 
      if (LEAP_YEAR(year)) printf("%d is a leap year",year); 
      else printf ("%d is not a leap year",year); 
      #define LEAPYEAR(y) y%4 
      main() 
      {int y; 
      scanf("%d",&y); 
      if(LEAPYEAR(y)) printf("%d is a not leap year\n",y); 
      else printf("%d is a lear year\n",y); 

      9.6請?jiān)O(shè)計(jì)輸出實(shí)數(shù)的格式,包括:⑴一行輸出一個(gè)實(shí)數(shù);⑵一行內(nèi)輸出兩個(gè)實(shí)數(shù);⑶一行內(nèi)輸出三個(gè)實(shí)數(shù)。實(shí)數(shù)用"6.2f"格式輸出。 
      #define PR1(x) printf("%6.2f\n",x) 
      #define PR2(x) printf("%6.2f\t%6.2f\n",x,x) 
      #define PR3(x) printf("%6.2f\t%6.2f\t%6.2f\n",x,x,x) 
      main() 
      {float a;scanf("%f",&a); 
      PR1(a);PR2(a);PR3(a); 

      9.7設(shè)計(jì)所需的各種各樣的輸出格式(包括整數(shù)、實(shí)屬、字符串等),用一個(gè)文件名"fornat.h",把信息都放到這個(gè)文件內(nèi),另編一個(gè)程序文件,用命令#include "fornat.h"以確保能使用這些格式。 
      分別用函數(shù)和帶參的宏,從三個(gè)數(shù)中找出最大的數(shù)。 
      #define MAX(a,b,c) (a>((b>c)? b:c))? a:((b>c)? b:c) 
      main() 
      {float x,y,z,t; 
      scanf("%f,%f,%f",&x,&y,&z); 
      t=MAX(x,y,z); 
      printf("%.4f\n",t); 
      9.10用條件編譯方法實(shí)現(xiàn)以下功能: 
      輸入一行電報(bào)文字,可以任選兩種輸出,一為原文輸出;一為將字母變成其下一字母(如’a’變成’b’……’z’變成’a’其它字符不變)。用命令來控制是否要譯成密碼。例如: 
      #define CHANGE 1 
      則輸出密碼。若: 
      #define CHANGE 0 
      則不譯為密碼,按原碼輸出。 
      #define CHANGE 1 
      main() 
      {char str[80],c;int i=0; 
      gets(str); 
      while(str[i]!=’\0’) 

      #if CHANGE 
      if(str[i]==90||str[i]==122) str[i]=str[i]-25; 
      else if(str[i]>=65&&str[i]<90||str[i]>=97&&str[i]<122) str[i]=str[i]+1; 
      #endif 
      i++; 

      puts(str); 

      或 
      #define CHANGE 1 
      main() 
      {char str[80],*c;int i=0; 
      gets(str);c=str; 
      while(*c!=’\0’) 

      #if CHANGE 
      if(*c==90||*c==122) *c=*c-25; 
      else if(*c>=65&&*c<90||*c>=97&&*c<122) *c=*c+1; 
      #endif 
      c++; 

      puts(str); 
      第十章 
      第十章 指針 
        
      10.1輸入三個(gè)整數(shù),按由小到大的順序輸出。 
      main() 
      { int a,b,c,*p1,*p2,*p3,t; 
      scanf("%d,%d,%d",&a,&b,&c); 
      p1=&a;p2=&b;p3=&c; 
      if(*p1>*p2)  
      {t=p1;p1=p2;p2=t;} 
      if(*p1>*p3) 
      {t=p1;p1=p3;p3=t;} 
      if(*p2>*p3)  
      {t=p2;p2=p3;p3=t;} 
      printf("%d,%d,%d\n",*p1,*p2,*p3); 

      或 
      main() 
      {int a,b,c,*p1,*p2,*p3,t; 
      scanf("%d,%d,%d",&a,&b,&c); 
      p1=&a;p2=&b;p3=&c; 
      if(a>b)  
      {t=*p1;*p1=*p2;*p2=t;} 
      if(a>c)  
      {t=*p1;*p1=*p3;*p3=t;} 
      if(b>c) 
      {t=*p2;*p2=*p3;*p3=t;} 
      printf("%d,%d,%d\n",a,b,c); 
      10.2輸入三個(gè)字符串,按由小到大的順序輸出 
      #define N 3 
      #define M 20 
      main() 
      {char str0[N][M],str1[M],*p,*q; 
      int i,l,m,n; 
      q=str0; 
      for(;p<q+N;p++) 
      gets(p); 
      l=strcmp(q,q+1); 
      if(l>0) 
       {strcpy(str1,q);strcpy(q,q+1);strcpy(q+1,str1);} 
      m=strcmp(q,q+2); 
      if(m>0)  
      {strcpy(str1,q);strcpy(q,q+2);strcpy(q+2,str1);} 
      n=strcmp(q+1,q+2); 
      if(n>0) 
       {strcpy(str1,q);strcpy(q+1,q+2);strcpy(q+2,q+1);} 
      for(p=q;p<q+N;p++) 
      puts(p); 
      10.3輸入10個(gè)整數(shù),將其中最小的數(shù)與第一個(gè)數(shù)對換,把最大的數(shù)與最后一個(gè)數(shù)對換。寫三個(gè)函數(shù);①輸入10個(gè)數(shù);②進(jìn)行處理;③輸出10個(gè)數(shù)。 
      f(x,n) 
      int x[],n; 
      {int *p0,*p1,i,j,t,y; 
      i=j=x[0];p0=p1=x; 
      for(y=0;y<n;y++) 
      {if(x[y]>i)  
      {i=x[y];p0=&x[y];} 
      else if(x[y]<j) 
      {j=x[y];p1=&x[y];}} 
      t=*p0;*p0=x[n-1];x[n-1]=t; 
      t=*p1;*p1=x[0];x[0]=t; 
      return; 

      main() 
      {int a[10],u,*r; 
      for(u=0;u<10;u++) 
      scanf("%d",&a[u]); 
      f(a,10); 
      for(u=0,r=a;u<10;u++,r++) 
      printf(" %d",a[u]); 
      printf("\n"); 
      10.4有n個(gè)整數(shù),使前面各數(shù)順序向后移m個(gè)位置,最后m個(gè)數(shù)變成前面m個(gè)數(shù),見圖。寫一函數(shù):實(shí)現(xiàn)以上功能,在主函數(shù)中輸入n個(gè)數(shù)和輸出調(diào)整后的n個(gè)數(shù)。 
      #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;i<x;i++) 
      *(o+i)=*(q+N-x+i); 
      for(p=p+N-1;p>=q;p--) 
      *p=*(p-x); 
      for(i=0;i<x;i++) 
      *(q+i)=*(o+i); 
      return;} 
      main() 
      {float shuzhu[N],*u,*v; 
      int h,i;u=v=shuzhu; 
      scanf("%f",&h); 
      for(;u<v+N;u++) 
      scanf("%f",u); 
      shift(v,h); 
      for(u=v;u<v+N;u++) 
      printf("%.2f ",*u); 
      printf("\n"); 
      10.5有n人圍成一圈,順序排號。從第1個(gè)人開始報(bào)數(shù)(從1到3報(bào)數(shù)),凡報(bào)到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個(gè)字符。寫一函數(shù),將此字符串中從第m個(gè)字符開始的全部字符復(fù)制成為另一個(gè)字符串。 
      #include"stdio.h" 
      #define N 10 
      main() 
      {char a[N+1],b[N+1],*p,*q; 
      int m; 
      gets(a); 
      scanf("%d",&m); 
      p=a+m;q=b; 
      strcpy(q,p); 
      puts(q); 

      10.6寫一函數(shù),求一個(gè)字符串的長度。在主函數(shù)種輸入字符串,并輸出其長度。 
      第十一章 
      11.1定義一個(gè)結(jié)構(gòu)體變量(包括年、月、日)。計(jì)算該日在本年中是第幾天,注意閏年問題。 
      解:Struct 
      {int year; 
       int month; 
       int day; 
      }date; 
      main() 
      {int days;  
           printf(“Input  year,month,day:”); 
           scanf(“%d,%D,%d”,&date.year,&date.month,&date.day); 
           switch(date.month) 
       {case 1: days=date.day;         break; 
            case 2: days=date.day+31;      break; 
            case 3: days=date.day+59;      break; 
            case 4: days=date.day+90;      break; 
            case 5: days=date.day+120;     break; 
      case 6: days=date.day+31;      break; 
              case 7: days=date.day+181;      break; 
              case 8: days=date.day+212;      break; 
      case 9: days=date.day+243;      break; 
      case 10: days=date.day+273;      break; 
      case 11: days=date.day+304;      break; 
      case 12: days=date.day+334;      break; 

      if((date.year%4==0&&date.year%100!=0||date.year%400==0)&&date.month>=3)days+=1; 
      printf(“\n%d/%d is the %dth day in%d.”,date.month,data.day,days,date,year); 

      11.2寫一個(gè)函數(shù)days,實(shí)現(xiàn)上面的計(jì)算。由主函數(shù)將年、月、日傳遞給days 函數(shù),計(jì)算后將日數(shù)傳回主函數(shù)輸出。 
      解:struct  y_m_d 
      {int year: 
       int month; 
       int day; 
      }date; 
      intdays(struct  y_m_d  date1) 
      {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+243;         break 
      case 11:sum=date1.day+243;         break 
      case 12:sum=date1.day+243;         break 

      }; 
      11.3編寫一個(gè)函數(shù)print,打印一個(gè)學(xué)生的成績數(shù),該數(shù)組中有5個(gè)學(xué)生的數(shù)據(jù)記錄,每個(gè)記錄包括num、name、sore[3],用主函數(shù)輸入這些記錄,用print函數(shù)輸出這些記錄。 
      解: 
      #define N 5 
      struct student 
      {char num[6]; 
         char name[8]; 
         int score[4]; 
      }stu[N]; 
      main() 
      {int I,j ; 
         for(I=0;I<N;I++) 
      {printf(“\Input score 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]); 

       printf(“\n”); 

      print(stu); 

      print(struct student stu[6]) 
      {int I,j; 
           printf(“%5s%10s”,stu[i].num,stu[i].name); 
           for(j=0;j<3;j++) 
           printf(“%9d”,stu[i].score[j]); 
           print(“\n”); 

      11.4在上題的基礎(chǔ)上,編寫一個(gè)函數(shù)input,用來輸入5個(gè)學(xué)生的數(shù)據(jù)記錄。 
      解: 
            #define  N  5 
         struct student 
         {char num[6]; 
          char name[8]; 
      int score[4] 
      }stu[N]; 
      input(struct student stu[]) 
      {int I,j; 
      for(I=0;I<N;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個(gè)學(xué)生,每個(gè)學(xué)生的數(shù)據(jù)包括學(xué)號、姓名、3門課的成績,從鍵盤輸入10個(gè)學(xué)生的數(shù)據(jù),要求打印出3門課的總平均成績,以及最高分的學(xué)生的數(shù)據(jù)(包括學(xué)號、姓名、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<N;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<N;I++) 
      {printf(“%5s%10s”,stu[i].num, stu[i].name); 
       for(j=0;j<3;j++) 
       printf(“%9d”,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 編寫一個(gè)函數(shù)new,對n個(gè)字符開辟連續(xù)的存儲空間,此函數(shù)應(yīng)返回一個(gè)指針,指向字符串開始的空間。New(n)表示分配n個(gè)字節(jié)的內(nèi)存空間。 
      解:new函數(shù)如下: 
      #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寫一函數(shù)free,將上題用new函數(shù)占用的空間釋放。Free(p)表示將p指向的單元以后的內(nèi)存段釋放。 
      解: 
         #define  Null o 
         #define  NEWSIZE 1000 
      char newbuf[NEWSIZE]; 
      char *newp=newbuf; 
      free(char *p) 
      {if((p>=newbuf)&&(p<newbuf+NEWSIZE)) 
         newp=p; 

      11.8已有a、b亮光鏈表,每個(gè)鏈表中的結(jié)點(diǎn)包括學(xué)好、成績。要求把兩個(gè)鏈表合并,按學(xué)號升序排列。 
      解: 
         #include<stdio.h> 
      #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個(gè)人圍成一圈,從第1個(gè)人開始順序報(bào)號1、2、3。凡報(bào)到“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<N-1) 
        {I=0; 
         while(I!=3) 
         {h=link[h].nextp; 
          if(link[h].number) 
          I++; 
        } 
      printf(“%4d”,link[h].number); 
      link[h].number=0; 
      count++; 

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

      11.10有兩個(gè)鏈表a和b,設(shè)結(jié)點(diǎn)中包含學(xué)號、姓名。從1鏈表中刪去與b鏈表中有相同學(xué)號的那些結(jié)點(diǎn)。 
      解: 
        #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<a=LA;i++) 
      {p=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<b+LB;I++) 
      {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建立一個(gè)鏈表,每個(gè)結(jié)點(diǎn)包括:學(xué)號、姓名、性別、年齡。輸入一個(gè)年齡,如果鏈表中的結(jié)點(diǎn)所包含的年齡等于此年齡,則將此結(jié)點(diǎn)刪去。 
      解:#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; 

      for(I=0;I<lenth;I++) 
         {p=(struct student *)malloc(LEN); 
            if(I==0) 
               head=pt=p; 
            else 
               pt->next=p; 
            pt=p; 
            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%6s%6d\n”,p->num, p->name, p->sex, 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”); 
         while(p!=NULL) 
         { 
      printf(“%4s%8s”,p->num,p->name); 
      printf(“%6s%6d”,p->sex,p->age); 
      p=p->next; 


      11.12將一個(gè)鏈表按逆序排列,即將鏈頭當(dāng)鏈尾,鏈尾當(dāng)鏈頭。 
      解: 
        # 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); 
      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(“%4d”,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; 
       p2->next=NULL; 

      printf(\n\n The new  listL\n); 
      p1=newhead; 
      for(I=0l;I<len;I++) 
      {pritf(“4d,p1->num”); 
       p1=p1->next, 

       printf(“\n”); 
      第12章 
      12.1編寫一個(gè)函數(shù)getbits,從一個(gè)16位的 單元中取出某幾位(即該幾位保留原值,其余位為0)。函數(shù)調(diào)用形式為:getbits(value,n1,n2)其中value為該16位單元中的數(shù)據(jù)值,n1為欲取出的起始位,n2為欲取出的結(jié)束位。 
      解: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寫一個(gè)函數(shù),對一個(gè)16位的二進(jìn)制數(shù)驅(qū)除它的奇數(shù)位。 
      解: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編一程序,檢查一下年一所用的計(jì)算機(jī)系統(tǒng)的C編譯在執(zhí)行有移時(shí)是按照邏輯位移的原則,還有按照算術(shù)右移的原則。如果是邏輯右移,請編一函數(shù)實(shí)現(xiàn)算術(shù)右移。如果是算術(shù)右移,請編寫一函數(shù)實(shí)現(xiàn)邏輯右移。 
      解: 
        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編一函數(shù)用來實(shí)現(xiàn)左右循環(huán)移位。函數(shù)名為move,調(diào)用方法為: 
      move(value,n) 其中value為要循環(huán)位移的數(shù),n為位移的位數(shù)。 
      解:  
      main() 
      {unsigned moveright(unsigned,int); 
       unsigned moveleft(unsigned,int); 
      unsigned a; 
      int n; 
      printf(“\n input an octal number:”); 
      scanf(“%o”,&a); 
      printf(“input n:”); 
      scanf(“%d”,&n); 
      if(n>0) 
      {moveright(a,n); 
        printf(“result:%o\n”,moveright(a,n)); 
        } 
      else 
      {n=-n; 
       moveleft(a,n); 
       printf(“result:%o\n”,moveleft(a,n)); 


      unsigned moveright(unsigned value,int n) 
      {unsigned z; 
       z=(value>>n)|(value<<(16-n)); 
      return(z); 

      unsigned moveleft(unsigned value,int n ) 
      {unsigned z; 
      z=(value>>(16-n))|(value<<n); 
      return(z);   
      }     
      12.5設(shè)計(jì)一個(gè)函數(shù),使給出一個(gè)數(shù)的原碼,能得到該數(shù)的補(bǔ)碼。 
      解: 
        main() 
      {unsigned int a; 
       unsigned int getbits(unsigned); 
       pritf(“\nInput an octal number:”); 
       scanf(“%o”,&a); 
       printf(“result:%o\n”,getbits(a)); 
      }         
      unsigned int getbits(unsigned value) 
      {unsigned int z; 
       z=value&0100000; 
       if(z==0100000) 
         z=~value+1; 
       else  
         z=value; 
       return(z); 
      第十三章 
      13.4從鍵盤輸入一個(gè)字符串,將其中的小寫字母全部轉(zhuǎn)換成大寫字母,然后輸出到一個(gè)磁盤文件“test”中保存。輸入的字符串以“!”結(jié)束。 
      解:#include <stdio.h> 
      main() 

       File *fp; 
       Char str[100]; 
       Int I=0; 
       If((fp=fopen(“test”,”w”)==NULL) 
      {printf(“Can not open the file\n”); 
       exit(0); 

      printf(“Input a string:\n”); 
      gets(str); 
      while(str[i]!=’!’) 
        {if (str[i]>=’a’&&str[i]<=’z’) 
           str[i]=str[I-32]; 
           fputc(str[i],fp); 
           I++; 

       fclose(fp); 
       fp=fopen(“test”,”r”); 
       fgets(str,strlen(str)+1,fp); 
       printf(“%s\n”,str); 
       fclose(fp); 

      13.5有兩個(gè)磁盤文件”A”和”B”,各存放一行字母,要求把這兩個(gè)文件中的信息合并(按字母順序排列),輸出到一個(gè)新文件”C”中。 
      解:#include<stdio.h> 
      main() 

      FILE *fp; 
      Int I,j,n,i1; 
      Char c[100],t ,ch; 
      If((fp=fopen(“a1”,”r”))==NULL) 
      {printf(“can not open the file\n”); 
       exit(0); 

      printf(“\n file A:\n”); 
      for(I=0;(ch=fgetc(fp)!=EOF;I++) 
      {c[i]=ch; 
       putchar(c[i]); 

      fclose(fp); 
      i1=I; 
      if((fp=fopen(“b1”,”r”))==NULL) 
        {printf(“\n can not ipen the file”); 
         exit(0); 

      printf(“\n  file B:\n”); 
      for(I=i1;(ch=fgenc(fp))!=EOF;I++) 
      {c[i]=ch; 
       putchar(c[i]); 

      fclose(fp); 
      n=I; 
      for(i=0;I<n;I++) 
      for(j=I+1;j<n;j++) 
      if(c[i]>c[j]) 
      {t=c[i]; 
       c[i]=c[j]; 
       c[j]=t; 
      printf(“\n file C:\n”); 
      fp=fopen(“c1”,”w”); 
         for(I=0;I<n;I++) 
           {putc(c[i],fp); 
            putchar(c[i]); 

            fclose(fp); 
      13.6有5個(gè)學(xué)生,每個(gè)學(xué)生有3門課的成績,從鍵盤輸入以上數(shù)據(jù)(包括學(xué)生號、姓名、三門課成績),計(jì)算出平均成績,將原有數(shù)據(jù)和計(jì)算出的平均分?jǐn)?shù)存放在磁盤文件stud中。 
      解: 
         #include<stdio.h> 
         struct student 
         {char num[10]; 
       char name[8]; 
       int score[3]; 
       float ave; 
      }stu[5]; 
      main() 
      {int I,j,sum; 
       FILE *fp; 
       For(I=0;I<5;I++) 
        {printf(“\n input score of student%d:\n”,I+1); 
         printf(“NO.:”); 
         scanf(“%s”,stu[i].num); 
         printf(“name:”); 
         scanf(“%s”,stu[i].name); 
         sum=0; 
         for(j=0;j<3;j++) 
           {printf(“score %d :”j+1); 
            scanf(“%d”,&stu[i].score[j]); 
            sum+=stu[i].score[j]; 

           stu[i].ave=sum/3.0 

      fp=fopen(“stud”,”w”); 
      for(I=0;I<5;I++) 
       if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1) 
          printf(“File write error\n”); 
      fclose(fp); 
      fp=fopen(“stud”,”r”); 
      for(I=0;I<5;I++) 
      {fread(&stu[i],sizeof(struct student),1,fp); 
       printf(“%s,%s,%d,%d,%d,%6.2f\n”,stu[i].num,stu[i].name,stu[i].score[0], stu[i].score[1], stu[i].score[2] ,stu[i].ave); 


      13.7將上題stud文件中的學(xué)生數(shù)據(jù)按平均分進(jìn)行排序處理,并將已排序的學(xué)生數(shù)據(jù)存入一個(gè)新文件stu-sort中。 
      解: 
         #include <stdio.h> 
         #define N 10 
         struct student 
         {char num[10]; 
      char name[8]; 
      int score[3]; 
      float ave; 
      }st[N],temp; 
      main() 

      FILE *fp; 
      Int I,j,n; 
      If((fp=fopen(“stud”,”r”))==NULL) 
      {printf(“can not open the file”); 
      exit(0); 

      printf(“\n file ‘stud’:”); 
      for(I=0;fread(&st[i],sizef(struct student),1,fp)!=0;I++) 
       {printf(“\n%8s%8s”,st[i].num,,st[i].name); 
        for(j=0;j<3;j++) 
          printf(“%8d”,st[i].score[j]); 
      printf(“%10.f”,st[i].ave); 
      fclose(fp); 
      n=I; 
      for(I=0;I<n;I++) 
       for(j=I+1;j<n;j++) 
       if(st[i].ave<st[j].ave) 
        {temp=st[i]; 
         st[i]=st[j]; 
         st[j]=temp; 

      printf(“\nnow:”); 
      fp=fopen(“stu-sort”,”w”); 
      for(I=0;I<n;I++) 
        {fwrite(&st[i],sizeof(struct student),1,fp); 
         printf(“\n%8s%8s”,st[i].num,st[i].name); 
         for(j=0;j<3;j++) 
          printf(“%8d”,st[i].score[j]); 
       printf(“%10.2f”,st[i].ave); 
          fclose(fp); 

      13.8將上題以排序的學(xué)生成績文件進(jìn)行插入處理。插入一個(gè)學(xué)生的3門課成績,程序先計(jì)算新插入學(xué)生的平均成績,然后將它按平均成績高低順序插入,插入后建立一個(gè)新文件。 
      解:#include <stdio.h> 
      struct student 
      {char num[10]; 
       char name[8]; 
       int score[3]; 
       float ave; 
      }st[10],s; 
      main() 
      {FILE  *fp, * fp1 ;  
      int I,j,t,n; 
      printf(“\n NO.:”); 
      scanf(“%s”,s.num); 
      printf(“name:”); 
      scanf(“%s”,s.name); 
      printf(“score1,score2,score3:”); 
      scanf(“%d,%d,%d”,&s. score[0], &s. score[1], &s. score[2]); 
      s.ave=(s.score[0]+s.score[1]+s.score[2])/3.0; 
      if((fp=fopen(“stu_sort”,”r”))==NULL) 
      {printf(“can  not open file.”); 
       exit(0); 

         printf(“original data:\n”); 
      for(I=0;fread(&st[i],sizeof(struct student),1,fp)!=0;I++) 
      {printf(“\n%8s%8s”,st[i].num,st[i].name); 
        for(j=0;j<3;j++) 
          printf(“%8d”,st[i].score[j]); 
        printf(“%10.2f”,st[i].ave); 

         n=I; 
         for(t=0;st[t].ave>s.ave&&t<n;t++); 
         printf(“\nnow:\n”); 
         fp1=fopen(“sort1.dat”,”w”); 
         for(I=p;j<t;I++) 
      {fwrite(&st[i],sizeof(stuct student),1,fp1); 
      print(“\n%8s%8s”,st[i],num,st[i].name); 
      for(j=0;j<3;j++) 
      ptintf(“%8d”,st[i].score[j]); 
      printf(“%10.2f”,st[i].ave); 

         fwrite(&s,sizeof(struct student),1,fp1); 
         printf(“\n%8s%7s%7d%7d%7d%10.2f”,s.num,s.name,s.score[0],s.score[1],s.score[2],s.ave); 
         for(I=t;I<n;I++) 
         {fwrite(&st[i],sizeof(struct student),1,fp1); 
      printf(“\n %8s%8s”,st[i].num,st[i].name); 
      for(j=0;j<3;j++) 
      printf(“%8d”,st[i].score[j]); 
      printf(“10.2f”,st[i].ave); 
      fclose(fp); 
      fclose(fp1); 

      13.9上題結(jié)果仍存入原有的stu_sort文件而不另建立新文件。 
      解: #include<stdio.h> 
      struct student 
      {char num[10]; 
      char name[8]; 
      int score[3]; 
      float ave; 
      }st[10],s; 
      main() 
      {FILE  *fp, *fp1; 
       int I ,j,t,n; 
       printf(“\nNO.:”); 
       scanf(“%s’,s.num); 
       printf(“name:”); 
       scanf(“%s’,s.name); 
       printf(“score1,score2,score3:”); 
       scanf(“%d%d%d”,&s.score[0]+&s.score[1]+&s.score[1], &s.score[2]); 
       s.ave=( s.score[0]+ s.score[1]+ s.score[2])/3.0; 
       if((fp=fopen(“stu=sort”,”r”))==NULL) 
        {printf(“can not open the file.”); 
         exit(0); 

       printf(“original data:\n”); 
       for(I=0;fread(&st[i],sizeof(struct student),1,fp)!=0;I++) 
        {printf(“\n%8s%8s”,st[i].num,st[i].name); 
         for(j=0;j<3;j++) 
           printf(“%8d”,st[i].score[j]); 
       printf(“%10.2f”,st[i].ave); 

      fclose(fp); 
      n=I; 
      for(t=0;st[t].ave>s.ave+&&t<n;t++); 
      ptintf(“\nnow:\n”); 
      fp=fopen(“stu_sort”,”w”); 
      for(I=0;I<t;I++) 
        {fwrite(&st[i],sizeof(struct student),1,fp); 
         printf(“\n%9s%8s%8d%8d%8d%10.2f”,s.num,s.name,s.score[0],s.score[1] s.score[2] s.ave); 
      for(I=t;I<n;I++) 
        {fwrit(&sr[i],sizeof(struct srudent),1,fp); 
         printf(“\n %8s%8s”,st[i].num,st[i].name); 
      for(j=0;j<3;j++) 
      printf(“%8d”,st[i].score[j]); 
      printf(“%10.2f”,st[i].ave); 

               fclose(fp); 

      13.10 有一磁盤文件emploee,內(nèi)存放職工的數(shù)據(jù)。每個(gè)職工的數(shù)據(jù)包括:職工姓名、職工號、性別、年齡、住址、工資、健康狀況、文化程度。要求將職工名和工資的信息單獨(dú)抽出來另建一個(gè)簡明的職工工資文件。 
      解:#include<stdio.h> 
      struct emploee  
      {char num[6]; 
       char name[10]; 
       char sex[2]; 
       int age; 
       char addr[20]; 
       int salary; 
       char health[8]; 
       char class[10]; 
      }en[10]; 
      struct emp 
      {char name[10]; 
       int salary; 
      }em-case[10]; 
      main() 
      {FILE *fp1, *fp2; 
       int I,j; 
      if ((fp1=fopen(“emploee”,”r”))==NULL) 
        {printf(“can not open the file.”); 
         exit(0); 

      printf(“\n  NO.  name  sex  age  addr  salary  health  class\n”); 
      for(I=0;fread(&em[i],sizeof(struct emploee),1,fp1)!=p;I++) 
        {printf(“\n%4s%8s%4s%6s%10s%6s%10s%8s”,em[i].num,em[i].name,em[i].sex, em[i].age, em[i].addr, em[i].salary, em[i].health, em[i].class); 
      strcpy(em_case[i].name, em[i].name); 
      em_case[i].salary=en[i].salary; 

      printf(“\n\n*****************************************”); 
      if((fp2=fopen(“emp_salary”,”wb”))==NULL) 
        {printf(“can not open the file.”); 
      exit(0);} 
      for(j=0;j<I;j++) 
      {if(fwrite(&en_case[j],sizeof(struct emp),1,fp2)!=1) 
        printf(“error!”); 
       printf(“\n %12s%10d”,em_case[j].name,em_case[j].salary); 

      printf(“\n*******************************************”); 
      fclose(fp1); 
      fclose(fp2); 

      13.11從上題的“職工工資文件”中刪區(qū)一個(gè)職工的數(shù)據(jù),再存回原文件。 
      解:#incude <stdio.h> 
          #incude <string.h> 
          struct emploee 
          {char name[10]; 
           int salary; 
           }emp[20]; 
          main() 
          {FILE *fp; 
           int I,j,n,flag; 
           char name[10]; 
           int salary; 
           if((fp=fopen(“emp_salary”,”rb”))==NULL) 
              {printf(“can not open file.”); 
      exit(0); 

      printf(“\n original data:”); 
      for(I=0;fead(&emp[i],sizeof(struct emploee),1,fp)!=0;I++) 
        printf(“\n %8s %7d”,emp[i].name,emp[i].salary); 
      fclose(fp); 
      n=I; 
      printf(“\n input name deleted:”); 
      scanf(“%s”,name); 
      for(flag=1,I=0;flag&&I<n;I++) 
       {if(strcmp(name,emp[i].name)==0) 
      {for(j=I;j<n-1;j++) 
        {strcmp(name,emp[i].name)==0 
      {for(j=I;j<n-1;j++) 
      {strcpy(emp[j].name,emp[j+1].name); 
      emp[j].salary=emp[j+1].salary; 

                          flag=0; 

                       } 
      if(!flag) 
                      n=n-1; 
      else 
        printf(“\n Now,the content of file:\n”); 
        fp=fopen(“emp-dalary”,”wb”); 
        for(I=p;I<n;I++) 
           fwrite(&emp[i],sizeof(struct emploee),1,fp); 
        fclose(fp); 
      fp=fopen(“emp_salary”,”r”); 
      for(I=0;fread(&emp[i],sezeof(struct emploee),1,fp)!=0;I++) 
      printf(“\n%8s%7d”,emp[i].name,emp[i].salary); 
      fclose(fp); 

      13.12 從鍵盤輸入若干行字符(每行長度不等),輸入后把它們存儲到一磁盤文件中。再從該文件中讀入這些數(shù)據(jù),將其中小寫字母轉(zhuǎn)換成大寫字母后在顯示屏上輸出。 
      解: #include<stdio.h> 
      main() 
      {int I,flag; 
      char str[80],c; 
      FILE *fp; 
      Fp=fopen(“text”,”w”); 
      Flag=1; 
      While(flag==1) 
       {printf(“\n Input string:\n”); 
        ges(str); 
      fprintf(fp,”%s”,str); 
      printf(“\nContinue?”); 
      c=getchar(); 
      if((c==’N’)||(c==’n’)) 
      flag=0; 
      getchar(); 

      fcolse()fp; 
       fp=fopen(“text”,”r”); 
       while(fscanf(fp,”%s”,str)!=EOF) 
        {for(I=0;str[i]!=’\0’;I++) 
           if((str[i]>=’a’)&& (str[i]<=’z’)) 
               str[i]-=32; 
          printf(“\n%s\n”,str); 

      fclose(fp); 
      }   
       

        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多