°ê¥ß¦¨¥\¤j¾Ç

 

¸ê·½¨t

 

­pºâ¾÷·§½×(C »y¨¥Á¿¸q)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

³¯ ¤é ª@ ½s µÛ

 

¹q  ¤l  ­p  ºâ  ¾÷  ¤¤  ¤ß

 

¤¤  µØ  ¥Á  °ê  1  O  9  ¦~  9  ¤ë

 

 


¤W½Ò¤º®e

  

  1. ±q¤@­Ó²³æµ{¦¡½Í°_

  2. °ò¥»ª¾ÃÑ

  3. ¿é¥X»P¿é¤J

  4. ±±¨î±Ô­z

  5. °j°é±Ô­z

 

  ***  ¥æ²Ä¤@­Óµ{¦¡  ***

 

  6. «e³B²zµ{¦¡

  7. »¼°j¹Bºâ

  8. pointer »P array

  9. ¦r¤¸¦ê 

 10. ¦ì¤¸¹Bºâ

 

  ***  ¥æ²Ä¤G­Óµ{¦¡  ***

 

 11. unix Àô¹Ò¤U³nÅé¤èªk½×

 

  ***  ¥æ²Ä¤T­Óµ{¦¡  ***

 

 


/*   ­pºâ¦¨ÁZ¥­§¡­È   */

 

 

 #include <stdio.h>

 #define quiz_rate 0.1

 #define hw_rate 0.15

 #define proj_rate 0.25

 #define mid_rate 0.2

 #define final_rate 0.3

 main()

 {

     char name[10];

     int quiz_score,hw_score,proj_score,mid_score,final_score;

     float average;

 

     printf("Please enter student name .\n")¡F

     while  (scanf("%s",name)¡×¡×1){

           printf ("Please enter quiz, hw, proj, mid, final.\n");

            if   (scanf("%d %d %d %d %d ", &quiz_score, &hw_score,&proj_score,

                      &mid_score, &final_score)¡×¡×5){

                 average¡×quiz_score¡¯quiz_rate¡Ïhw_score¡¯hw_rate¡Ïproj_score¡¯

                                 proj_rate¡Ïmid_score¡¯mid_rate¡Ïfinal_score¡¯final_rate¡F

                printf ("%s final score is %3.1f\n",name,average)¡F

                break;

            }

    }

 }


 ¡Ga.out

 

 Please enter student name

 

 Peter

 

 Please enter quiz, hw, proj, mid, final.

 

 90 87 95 75 80

 

 Peter final score is 84.8


°ò¥»±`ÃÑ

 

1.¦r¤¸»P»y¨¥­n¯À

 

      ¤p¦r¦r¥À           a b c . . . z

      ¤j¦r¦r¥À           A B C . . . Z

      ¼Æ¦r                   0 1 2 3 4 5 6 7 8 9

      ¨ä¥Lªº¦r¤¸       + - * / = ( ) { } [ ] < > ' " `

                                 # $ % & _ | ^ ~ \ . , ; : ?

      ªÅ¥Õ¦r¤¸(white space characters)¡A¨Ò¦p¡GªÅ®æ(blank)©M©w¦ì¦r¤¸(tab)¡C

 

¢ëªº»y¨¥­n¯À´N¬O¥Ñ³o¨Ç¦r¤¸©Ò²Õ¦X¦Ó¦¨¡A°ò¥»ªº»y¨¥­n¯À¦³ÃѧO¦r(identifiers)

¡AÃöÁä¦r(keywords)¡A±`¼Æ(constants)¡A¹Bºâ²Å¸¹(operators)¥H¤Î¤À¹j²Å¸¹(separators)¡C

 

 

¥H¤@­Ó²³æªºµ{¦¡¬°¨Ò¡G

 

/* Read in two integers and print their sum. */

 

   main()

   {

     int a,b,sum;

 

     printf("Input two integers: ");

     scanf("%d %d",&a,&b);

     sum = a + b ;

     printf("\nThe sum of %d and %d is %d.\n\n",a,b,sum);

   }


/* Read in two integers and print their sum. */

 

¦b"/*"©M"*/"¤§¶¡ªº¤å¦r¬Oµ{¦¡ªºª`¸Ñ¡A½sĶµ{¦¡±Nª`¸Ñµø¬°ªÅ¥Õ¦r¤¸¨Ó³B²z¡C

 

   main()

   {

     int a,b,sum;

 

¨ç¼Æ¦WºÙ main ¬O¤@­ÓÃѧO¦r¡A" ( "¡A" ) "¡A" { "¬O¤À¹j²Å¸¹¡Aint¬OÃöÁä¦r¡Ca¡A

b¡A¤Îsum³£¬OÃѧO¦r¡A¦Ó"¡A"©M";"¤]¬O¤À¹j²Å¸¹¡C

 

     printf("Input two integers: ");

     scanf("%d %d",&a,&b);

 

¨ç¼Æ¦WºÙprintf©Mscanf¤]¬OÃѧO¦r¡A¨ä¥ª¥k¬A©·¥Î¥H§iª¾½sĶµ{¦¡¡A³o¨â­ÓÃѧO

¦r¬O¨ç¼Æ¡A¥¦­Ì³£¦s©ñ¦b¼Ð·Çµ{¦¡®w¤¤¡A§Ú­Ì¤£»Ý­n­«·s©w¸q¥¦­Ì¡C

 

     "Input two integers: "

 

¥ÑÂù¤Þ¸¹¬A°_¨Óªº¤@³s¦ê¦r¤¸¥Nªí¦r¦ê±`¼Æ(string constant )¡C

 

     &a,&b

 

" & " ¬O¤@­Ó¹Bºâ²Å¸¹.

 

     sum = a + b ;

 

" - " ¤Î " + "³£¬O¹Bºâ²Å¸¹¡C¦b¦r¤¸¶¡ªºªÅ®æ¥i¥H©¿²¤¤£­p¡A¦]¦¹¤]¥i¥H¼g¦¨¡G

 

  sum=a+b;

 

  ©Î

 

  sum   =   a   +   b   ;

 

  ¦ý¬O¤£¥i¥H¼g¦¨³o¼Ë :

 

  s  u  m = a + b ;

 


2 ÃѧO¦r

 

      ÃѧO¦r¬O¥Ñ¤@¦ê¦r¥À¡A¼Æ¦r©M¤@­Ó¯S®í¦r¤¸ "©³½u" ( _, underline )©Ò²Õ¦¨¡A¦Ó¥B

²Ä¤@­Ó¦r¤¸¥²¶·¬O¦r¥À©Î¬O©³½u¡C¦Ü©óÃѧO¦rªº¿ï¾Ü¤è¦¡À³¸Ó¬O«K©ó°O¾Ð©M¨ã

¦³¹ê½è·N¸qªº¡A¥H¼W¥[µ{¦¡ªº¥iŪ©Ê©M»¡©ú©Ê¡C¥H¤U¬O¤@¨Ç¦XªkªºÃѧO¦r¡G

 

n

_id

iamanidentifier

i_am_an_identifier

 

¦Ó«D¡G

 

not#me

101_south

-plus

 


 

3 ÃöÁä¦r

 

        ¤£¦PªºÃѧO¦r¨Ï¤£¦Pªº¨Æª«¦bµ{¦¡¤¤¨ã¦³°ß¤@ªº¦WºÙ¡C¢ë»y¨¥«O¯d¤F¤@¨Ç

¯S®íªº¦r·J¡AºÙ¬°ÃöÁä¦r¡C

 

      ÃöÁä¦r¬O©Ò«O¯dªºÃѧO¦r¡A¦Ó¥B¨C¤@­Ó³£¦³¥¦¯S®íªº·N¸q¡A¤£¯à³Q­«·s©w

¸q©Î¬O¨Ï¥Î¦b§OªºÀô¹Ò¡C

 

     ÃöÁä¦r

===================================================================

    auto       default    extern     int          sizeof         union

    break      do         float      long         static         uigned

    case       double     for        register     struct         void

    char       else       goto       return       switch         while

    continue   enum       if         short        typedef

 

 

 

 

 

4 ¹Bºâ²Å¸¹

 

        ³\¦h¯S®í¦r¤¸¨ã¦³¯S§Oªº·N¸q¡A¤U¦C¬O´X­Óºâ³N¹Bºâ²Å¸¹¡G

 

+      -      *      /       %

 

        ¥¦­Ì¤À§O¥Nªí¥[ªk¡B´îªk¡B­¼ªk¡B°£ªk¥H¤Î¼Ò¼Æªººâ³N¹Bºâ¡C

 


5 ¹Bºâ²Å¸¹ªºÀu¥ý¶¶§Ç»Pµ²¦X«ß

 

         ¹Bºâ¦¡ªº­pºâ­n¨Ì¾Ú¹Bºâ²Å¸¹ªºÀu¥ý¶¶§Ç(precedure)»Pµ²¦X«ß(associativity)ªº

³W«h¡A¦ý¬O¦b¬A©·¤ºªº¹Bºâ¦¡«h¥²¶·¥ý­pºâ¡C¬A©·¨Ï¹Bºâ¬Ý¨Ó§ó©ú¥Õ¡A¤]¯à§ï

ÅܹBºâªº¥ý«á¦¸§Ç¡A¥H¤U¦Cªº¹Bºâ¦¡¬°¨Ò¡G

 

1 + 2 * 3

 

  ¥Ñ©ó "*"ªºÀu¥ý¶¶§Ç°ª©ó "+"¡A¥ç§Y­n¥ý§@­¼ªk¦A§@¥[ªk¡A¦]¦¹­pºâ«á©Ò±oµ²

  ªG¬° 7 ¡C³o¦¡¤l¤]¥i¼g¦¨³o¼Ë¡G

 

1 + ( 2 * 3 )

 

  ¥t¤@¤è­±¡A¥Ñ©ó¬A©·¤¤ªº¹Bºâ¦¡­n¥ý­pºâ¡A¦]¦¹¤U¦C©Ò¥Ü

 

( 1 + 2 ) * 3

 

  ªº¹Bºâµ²ªG´N¤£¤@¼Ë¤F¡F¨äµª®×¬° 9 ¡C¦A¬Ý¤U­±³o­Ó¹Bºâ¦¡

 

1 + 2 - 3 + 4 - 5

 

  ¦]¬° "+"©M "-"¨ã¦³¬Û¦PªºÀu¥ý¶¶§Ç¡A®Ú¾Úµ²¦X«ß¡A³o¦¡¤l­n¥Ñ¥ª¦Ó¥k­pºâ¡A

  ³o´N¬Û·í©ó

 

( ( ( 1 + 2 ) - 3 ) + 4 ) - 5

 

      ¤U­±ªºªí®æ¬O¤@¨Ç¹Bºâ²Å¸¹ªºÀu¥ý¶¶§Ç»Pµ²¦X«ß¡C

 

 

      ¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢s¢w¢w¢w¢w¢w¢w¢w¢w¢w¢{

      ¢x           ¹B ºâ ²Å ¸¹    ¢x  µ²  ¦X  «ß      ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x     - (³æ¤¸)   ++   --   ¢x ¥Ñ ¥k ¦Ü ¥ª      ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x             *    /     %      ¢x  ¥Ñ ¥ª ¦Ü ¥k     ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x                 +      -       ¢x  ¥Ñ ¥ª ¦Ü ¥k     ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x     =  +=  -=  *=  /= ¢x  ¥Ñ ¥k ¦Ü ¥ª     ¢x

      ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢r¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}


6 »¼¼W»P»¼´î¹Bºâ²Å¸¹

 

        " ++ "»¼¼W©M" -- "»¼´î³£¬O³æ¤¸¹Bºâ²Å¸¹¡A¨ä»P³æ¤¸¹Bºâªº­t¸¹¦³µÛ¬Û¦Pªº

Àu¥ý¶¶§Ç¥H¤Î "¥Ñ¥k¦Ü¥ª" ªºµ²¦X«ß¡A¦ý¶È¾A¥Î©óÅܼƦӤ£¯à¥Î©ó±`¼Æ©Î´¶³qªº

¹Bºâ¦¡¡C¦¹¥~¡A»¼¼W»P»¼´î¹Bºâ²Å¸¹¥i¥H¬O«e¸m¦¡ ( prefix )¡A¤]¥i¥H¬O«á¸m¦¡

(postfix)¡A¦Ó¨âªÌ¥i¯à¦³¤£¦Pªº®ÄªG¡C¥H¤U¬O¦Xªkªº¨Ò¤l¡G

 

++j

j++

--cnt

cnt--

 

  ¦Ó«D¡G

777++        /* ±`¼Æ¤£¯à³Q»¼¼W */

++ (a * b - 1)    /* ¤£¯à¹ï¹Bºâ¦¡§@»¼¼W */

 

 

         ++i ©Mi++ ³£¨ã¦³¤@­Ó­È¡A¦Ó¥B¨Ï±o°O¾ÐÅ餤 i ªº¤º§t­È¼W¥[ 1¡A¥u¬O++i

¦b­pºâ i ¤§«e´N¼W¥[ i ªº¤º§t­È¡Ai++«h¬O¥ý­pºâ i ­È¦A¼W¥[ i ªº¤º§t­È¡C

 

½Ð¬Ýµ{¦¡ªº»¡©ú¡G

 

      int a,b,c = 0 ;

 

      a = ++c ;

      printf ("%d %d\n",a,c);                                        /* ¦L¥X 1  1 */

      b = c++ ;

      printf ("%d %d\n",b,c);                                       /* ¦L¥X 1  2 */

 

        --i ¤Îi-- ªº¹Bºâ¤è¦¡Ãþ¦ü¤G­Ó»¼¼Wªº¦¡¤l¡A¤£¦Pªº¬O°O¾ÐÅ餤 i ªº¤º§t­È´î

¤Ö1¡C¤£¦P©ó¨ä¥Lªº¹Bºâ²Å¸¹¡A++©M--·|§ïÅÜ°O¾ÐÅ餤Åܼƪº¤º§t­È¡C¦Ó¦p "+"

³o­Ó¹Bºâ²Å¸¹«h¨Ã¤£·|³o»ò°µ¡C¨Ò¦p a + b ªº¹Bºâ¦¡·|²£¥Í¤@­Ó­È¡A¦ý a ©M b Åé

¤º§t­È¨Ã¤£·|§ïÅÜ¡C§Ú­Ì¥i¥H»¡++©M--¬O¨ã¦³°Æ§@¥Î (side effect)ªº¹Bºâ²Å¸¹¡C


7 ³]©w¹Bºâ²Å¸¹

 

        §Ú­Ì¥i¥H§Q¥Î³]©w±Ô­z¨Ó§ïÅܤ@­ÓÅܼƪº¤º§t­È¡A¨Ò¦p¡G

a = b + c¡F

 

        »P¨ä¥Lªº»y¨¥¤£¦P¡A¢ëµø"="¬°¤@­Ó¹Bºâ²Å¸¹¡A¨äÀu¥ý¶¶§Ç§C©ó©Ò¦³§Ú­Ì°Q

½×¹Lªº¹Bºâ²Å¸¹¡A¦Ó¥B¥¦ªºµ²¦X«ß¬O"¥Ñ¥k¦Ü¥ª"¡A½Ð¬Ý¤U¦Cªº»¡©ú¡G

 

      int x,y,z;

 

      y = 2 ;

      z = 3 ;

      x = y + z ;

 

        «e¤G­Ó³]©w±Ô­z¤À§O±N 2 ©M 3 «ü©wµ¹ y ©M z ,µM«á­pºâ y + z ªº­È¨Ã¥B«ü©wµ¹

x¡C§Q¥Î³]©w¹Bºâ¦¡¡A¤W­z¦¡¤l¥i¥H¿@ÁY¦¨

 

      int x,y,z;

 

      x = ( y = 2 ) + ( z = 3 );

 

        y = 2 ³]©w y ­È¬° 2 ¡A¦P®É¹Bºâ¦¡¥»¨­ªº­È¤]¬O 2 ¡A¦P¼Ëªº¡Az = 3 ³]©w z ­È

¬° 3¡A¹Bºâ¦¡¥»¨­ªº­È¤]¬O 3¡C³Ì«á¡A¨â­Ó¦¡¤lªº­È¬Û¥[¡A¨äµ²ªG«ü©wµ¹ x ¡CÁö

µM¤W­z¨ÒÃD¬O¤H¬°ªº¡A¦ýÁÙ¬O¦³³\¦h±¡§Î¡A³]©w¹Bºâ¦¡¬O¤@­Ó¹Bºâ¦¡ªº¤@³¡¤À

¡C³Ì±`¨£ªº´N¬O¦h­«³]©w¡C(multiple assignment)¦p¤U©Ò¥Ü¡G

 

x = y = z = 0 ;

 

        ¦]¬°"=" ªºµ²¦X«ß¬O¥Ñ¥k¦Ü¥ª¡A©Ò¥H¤W­z¦¡¤l¥i¥H¼g¦¨

 

x = ( y = ( z = 0 ) ) ;

 

        ­º¥ý¡Az ³Q³]©w¬° 0¡A¦]¦Ó z = 0 ¹Bºâ¦¡ªº­È¤]¬O 0¡C±µµÛ¡A y³Q³]©w¬° 0¡A

¦P®É y = ( z = 0 ) ªº­È¤]¬O 0¡C³Ì«á¡A x³Q³]©w¬° 0 ,x = (y = (z = 0))ªº­È¤]¬O 0 ¡C

°£¤F"=" ,"+=" ©M"-="¤]¬O³]©w¹Bºâ²Å¸¹¡A¦p¤U©Ò¥Ü¡G

 

k = k + 2 ;

 

        k ªº­ì­È¥[ 2¡A¨äµ²ªG«ü©wµ¹ k¡A¦P®É¾ã­Ó¹Bºâ¦¡¤]¨ã¦³¬Û¦Pªº­È¡C

¤U¦C¹Bºâ¦¡

k += 2 ;


8 ª` ¸Ñ

 

        ª`¸Ñ(comment)¬O"/*"©M"*/"¤G­Ó¤À¬É²Å¸¹(delimiter)¤§¶¡¡A¥ô·Nªø«×ªº²Å¸¹¦r¦ê

¡C¥H¤U¬O´X­Óª`¸Ñªº¨Ò¤l¡G

/* a comment */

 

/*** another comment ***/

 

/*****/

 

 

 

9 ±` ¼Æ

 

        §Ú­Ìª¾¹D¢ë¥i¥H³B²z¤£¦PºØÃþªº­È¡A¦³¨Ç¬O±`¼Æ¡A¦³¨Ç¬OÅܼơC¾ã¼Æªº 7

©M¯BÂI¼Æªº 3.14159³£¬O±`¼Æªº½d¨Ò¡A¦p "a","b"«h¬O¦r¤¸±`¼Æ¡C" i am a string "

«h¬O¦r¦ê±`¼Æ¡C½Ðª`·N¡A¦r¤¸±`¼Æ'a'ªº©M¦r¦ê±`¼Æªº"a"¬O¨âºØ¤£¦Pªº±`¼Æ¡C

 

·s¦r¤¸¡A¦p'\n'¡A«h¬O¤@¯S®íªº¦r¤¸±`¼Æ¡C¤Ï±×½u¦b¦¹¨ã¦³¯S®íªº¥Î·N¡A¦Ó¨äÁö

§t¦³"\"©M"n"¤G­Ó¦r¤¸¦ý¤´³Qµø¬°³æ¤@ªº¦r¤¸±`¼Æ¡C

 

 

 

10 ¦r ¦ê

 

         ¦b¤@¹ïÂù¤Þ¸¹¤§¶¡ªº¤@§Ç¦C¦r¤¸´N¬O¦r¦ê(string)¡C½Ðª`·N¡AÂù¤Þ¸¹"¬O¤@­Ó

¦r¤¸¦Ó«D¤G­Ó¦r¤¸¡C¦pªG­n¦b¦r¦ê¤¤©ñ¤J"¡A«h¥²¶·­n¦b\¤§«á¡F¦pªG­n©ñ¤J\¡A¦P

¼Ë­n¸ò¦b¤Ï±×½u¤§«á¡C


main()

{

int i,j,k;

float r, s, q;

  i = 7;

  j = 2;

  s = i /  j;

  printf("s = i / j = %f\n",s);

 

  s = (float) i /  j;

  printf("s = (float) i / j = %f\n",s);

 

  s = (float) (i /  j);

  printf("s = (float) (i / j) = %f\n",s);

 

  s = (float) i / (float) j;

  printf("s = (float) i / (float) j = %f\n",s);

 

  k = (float) i / (float) j;

  printf("k = (float) i / (float) j = %d\n",k);

 

  r = 7.0;

  s = 2.0;

  k = r / s;

  printf("k = r / s = %d\n", k);

 

  k = (int)  r / s;

  printf("k = (int) r / s = %d\n", k);

 

  k = (int)  r / (int) s;

  printf("k = (int) r / (int) s = %d\n", k);

}

 

s = i / j = 3.000000

s = (float) i / j = 3.500000

s = (float) (i / j) = 3.000000

s = (float) i / (float) j = 3.500000

k = (float) i / (float) j = 3

k = r / s = 3

k = (int) r / s = 3

k = (int) r / (int) s = 3


¿é¥X¡þ¿é¤J

 

       ¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢{

       ¢xprintf()                                                                                          ¢x

       ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

       ¢x                                                                                                     ¢x

       ¢xÂà ´« ¦r ¤¸                  ¥H¦óºØ§Î¦¡¦C¦L¹ïÀ³ªº¤Þ¼Æ                  ¢x

       ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

       ¢x        c                                  ¦r¤¸                                                  ¢x

       ¢x        d                                  ¢°¢¯¶i¦ì¾ã¼Æ                                  ¢x

       ¢x        o                                  µL¥¿­t¸¹¢·¶i¦ì¾ã¼Æ                      ¢x

       ¢x        x                                  µL¥¿­t¸¹¢°¢µ¶i¦ì¾ã¼Æ                  ¢x

       ¢x        f                                  ¯BÂI¼Æ¡A¨Ò¦p :7.123000                  ¢x

       ¢x        s                                  ¦r¦ê                                                  ¢x

       ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}


 ¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢{

 ¢xÅܼƫŧi¤Î³]©wªì­È                                                ¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢xchar          c = 'w';                                            ¢x

 ¢xint           i = 1, j = 29;                                      ¢x

 ¢xfloat         x = 333.12345678901234567890;                       ¢x

 ¢xdouble        y = 333.12345678901234567890;                       ¢x

 ¢xstatic char   s1[] = "she sells sea shells"                       ¢x

 ¢xstatic char   s2[] = "by the seashore"                            ¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢x ®æ¦¡    ¹Bºâ¦¡    ¦C¦L¦bÄæ¦ì¤Wªºµ²ªG      ª` ¸Ñ                  ¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢x  %c       c       "w"                     ¹w³]ªºÄæ«×ªø«×¬°¢°     ¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢x  %2c      c       " w"                    ¦V¥k¹ï»ô               ¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢x %-3c      c       "w  "                   ¦V¥ª¹ï»ô               ¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢x  %d      -j       "-29"                   ¹w³]ªºÄæ«×ªø«×¬°¢²     ¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢x%010d      i       "0000000001"            ¥H¢¯¶ñ¸É               ¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢x%-12d      j       "29          "          ¦V¥ª¹ï»ô               ¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢x %12o      j       "          35"          ¢·¶i¦ì ,¦V¥k¹ï»ô       ¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢x%-12x      j       "1d          "          ¢°¢µ¶i¦ì ,¦V¥ª¹ï»ô     ¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢x  %f       x       "333.123456"            ¹w³]ªººë½T«×¬°¢µ       ¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢x %.1f      x       "333.1"                 ºë½T«×¬°¢°             ¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢x%20.3f     x       "             333.123"  ¦V¥k¹ï»ô               ¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢x %.9f      y       "333.123456789"         ºë½T«×¬°¢¸             ¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢x  %s       s1      "she sells sea shells"  ¹w³]ªºÄæ«×ªø«×¬°¢±¢¯ ¡@¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢x %7s       s1      "she sells sea shells"  »Ý­n§ó¦hªºªÅ¶¡         ¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢x%.5s       s2      "by th"                 ºë½T«×¬°¢´             ¢x

 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

 ¢x%-15.12s   s2      "by the seash   "       ºë½T«×¬°¢°¢± ,¦V¥ª¹ï»ô ¢x

 ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}


±±¨î¬yµ{

 

1 Ãö«Y¡B¬Ûµ¥©MÅÞ¿è¹Bºâ²Å¸¹

 

        ¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢{

        ¢xÃö«Y¡B¬Ûµ¥©MÅÞ¿è¹Bºâ²Å¸¹                      ¢x

        ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

        ¢xÃö«Y¹Bºâ²Å¸¹ :                     ¤p©ó : <   ¢x

        ¢x                                   ¤j©ó : >   ¢x

        ¢x                             ¤p©ó©Îµ¥©ó : <=  ¢x

        ¢x                             ¤j©ó©Îµ¥©ó : >=  ¢x

        ¢x                                              ¢x

        ¢x¬Ûµ¥¹Bºâ²Å¸¹ :                     µ¥©ó : ==  ¢x

        ¢x                                 ¤£µ¥©ó : !=  ¢x

        ¢x                                              ¢x

        ¢xÅÞ¿è¹Bºâ²Å¸¹ :               (¤@¤¸)§_©w : !   ¢x

        ¢x                               Å޿誺©M : &&  ¢x

        ¢x                               Å޿誺©Î : ||  ¢x

        ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

 

        °£¤F" ! " ¬O³æ¤¸ªº¤§¥~ ,©Ò¦³ªºÃö«Y¡B¬Ûµ¥©MÅÞ¿è¹Bºâ²Å¸¹³£¬O¤G¤¸¹Bºâ²Å

¸¹¡C³o¤TÃþ¹Bºâ²Å¸¹ªºÀu¥ý¶¶§Ç³£§C©óºâ³N¹Bºâ²Å¸¹ ,¦Ó°ª©ó³]©w¹Bºâ²Å¸¹¡C§Ú

­Ì¯à°÷ª½Ä±ªº§PÂ_a < b ¬O¯u¬O°²¡A¢ë«h¬O¥H¾ã¼Æ­È 1¥Nªí¬°¯u¡A0¥Nªí¬°°²¡C¨Æ

¹ê¤W¡A©Ò¦³«D¹s­È³£¥i¥H¥Nªí¬°¯u¡C

 

 

2 Ãö«Y¹Bºâ²Å¸¹©M¹Bºâ¦¡

 

        °²³]e1©Me2¬O¥ô·Nªººâ³N¹Bºâ¦¡¡A¤U¦Cªº¯u­Èªí¥He1-e2 ªº­È¨Ó¨M©wÃö«Y¹B

ºâ¦¡ªºÅÞ¿è­È¡G

 

      ¹Bºâ¦¡ªº­È¡G

 

      e1-e2    e1 < e2    e1 > e2     e1 <= e2    e1 >= e2

 ¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w

        ¥¿        F          T           F           T

        ¹s        F          F           T           T

        ­t        T          F           T           F

 


3 ¬Ûµ¥¹Bºâ²Å¸¹©M¹Bºâ¦¡

 

        ¬Ûµ¥¹Bºâ²Å¸¹==¤Î!=¬O¹Bºâ¦¡ªº¤G¤¸¹Bºâ²Å¸¹¡A²£¥Í¾ã¼Æ­È 0 ©Î 1

 

      ¹Bºâ¦¡ªº­È¡G

 

      exp1 - exp2        exp1 == exp2        exp1 != exp2

      ¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w

          ¹s                         1                              0

          «D¹s                     0                              1

 

 

4 ÅÞ¿è¹Bºâ²Å¸¹©M¹Bºâ¦¡

 

        !¡B&& ©M||¬O¤T­ÓÅÞ¿è¹Bºâ²Å¸¹¡A¨ä¤¤ !¬O³æ¤¸¹Bºâ²Å¸¹¡A¦Ó&&©M||¬O¤G¤¸

¹Bºâ²Å¸¹¡A¨Ï¥Î¦¹ºØ¹Bºâ²Å¸¹ªº¹Bºâ¦¡¡A¨ä­È¤£¬O 0 ´N¬O 1¡C

 

        ÅÞ¿è§_©w(logical negation)¯à°÷¦b¥ô¦óºâ³N¹Bºâ¦¡¤¤¨Ï¥Î¡A­Y¹Bºâ¦¡ªº­È¬° 0

¡A¨ä§_©w¦¡«h²£¥Í¾ã¼Æ­È 1¡F¬Û¤Ïªº¡A­Y¹Bºâ¦¡ªº­È¬°¤@«D¹sªº¼Æ­È¡A¨ä§_©w¦¡

«h²£¥Í¾ã¼Æ­È 0¡C

 

      ¤Uªí¬°¹Bºâ²Å¸¹! ªº»y·N¡G

 

               ¹Bºâ¦¡ªº­È¡G

 

                                                   exp         !exp

                                                   ¢w¢w¢w¢w¢w¢w¢w¢w

                                                    ¹s             1

                                                   «D¹s          0

 

 

      ¤Uªí¬°ÅÞ¿è¹Bºâ²Å¸¹ªº»y·N¡G

 

      ¹Bºâ¦¡ªº­È¡G

 

                       exp1      exp2          exp1 && exp2         exp1 || exp2

                      ¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w

                        F             F                      F                            F

                        F             T                      F                            T

                        T             F                      F                            T

                        T             T                      T                            T


        &&ªºÀu¥ý¶¶§Ç°ª©ó||¡A¦ý¬O¨âªÌ³£§C©ó³æ¤¸¡Bºâ³N©MÃö«Y¹Bºâ²Å¸¹¡A¨äµ²

¦X«ß¬°"¥Ñ¥ª¦Ü¥k"¡C¤U¦Cªí®æ¥HÀu¥ý¶¶§Ç©Mµ²¦X«ß¨Ó­pºâ´X­ÓÅÞ¿è¹Bºâ¦¡ªº­È¡G

 

¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢{

¢xÅܼƫŧi¤Î³]©wªì­È                                                ¢x

¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

¢xint           i = 7, j = 8, k = 9;                                ¢x

¢xfloat         x = 0.0;                                            ¢x

¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

¢x        ¹Bºâ¦¡                   ¬Ûµ¥¹Bºâ¦¡                  µ² ªG¢x

¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

¢x       i && j && k              (i && j) && k                  1  ¢x

¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

¢x      i || j-3 && 0           i || ((j-3) && 0)                1  ¢x

¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

¢x     i < j && 2 >= k          (i < j) && (2 >= k)              0  ¢x

¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

¢x     i < j || 2 >= k          (i < j) || (2 >= k)              1  ¢x

¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

¢xi ==2 || j ==4 || k ==6   ((i ==2) || (j ==4)) || (k ==6)      0  ¢x

¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

¢xi =2 || j ==4 || k ==6    i =((2 || (j ==4)) || (k ==6))       1  ¢x

¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

¢xx <=5.0 && x !=1.0 ||i>j  ((x <=5.0) && (x !=1.0)) ||(i>j)     1  ¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

 

        §Ú­Ì­pºâ && ©M || ªº¹Bºâ¤¸ªº¹Bºâ¦¡­È¡A¨äµ{§Ç¬O¥Ñ¥ª¦Ü¥k¡A¤@¥¹±oª¾µ²

ªG¬O¯u©Î°²¡A¦¹µ{§Ç´N¥ß§Y°±¤î¡C°²³] exp1 ©M exp2 ¬O¹Bºâ¦¡¡A¥B exp1 ªº­È¬°

0¡A«h¤U¦CÅÞ¿è¹Bºâ¦¡¡G

 

exp1 && exp2

 

¤£·|­pºâ exp2 ªº­È¡A¦]¬°¾ã­ÓÅÞ¿è¹Bºâ¦¡ªºµ²ªG¤w¸g¬O 0¡C¦P¼Ëªº¡A­Yexp1

¬°«D¹s­È¡A«h

 

exp1 || exp2

 

¤£·|­pºâ exp2 ªº­È¡A¦]¬°¾ã­ÓÅÞ¿è¹Bºâ¦¡ªºµ²ªG¤w¸g¬O 1¡C½Ð¨£¤U¦Cµ{¦¡ªº

»¡©ú¡G

 

      int  ij = 2 ;

 

      i = 0 && (j = 3) ;

      printf( "%d %d\n", i, j) ;                     /* ¦L¥X 0 2 */

      i = 4 || (j = 5) ;

      printf( "%d %d\n", i, j) ;                     /* ¦L¥X 1 2 */

      i = 0 || (j = 6) ;

      printf( "%d %d\n", i, j) ;                     /* ¦L¥X 1 6 */

 


main()

{

   int a,b=14;

   if ((a=1) && (b=10))

       printf("TRUE a=%d,b=%d\n", a,b);

   else

       printf("FALSE a=%d,b=%d\n", a,b);

}

 

TRUE a=1,b=10

 

main()

{

   int a,b=14;

   if ((a=0) && (b=10))

       printf("TRUE a=%d,b=%d\n", a,b);

   else

       printf("FALSE a=%d,b=%d\n", a,b);

}

 

FALSE a=0,b=14

 

main()

{

   int a,b=14;

   if ((a=1) || (b=10))

       printf("TRUE a=%d,b=%d\n", a,b);

   else

       printf("FALSE a=%d,b=%d\n", a,b);

}

 

TRUE a=1,b=14

 

main()

{

   int a,b=14;

   if ((a=0) || (b=10))

       printf("TRUE a=%d,b=%d\n", a,b);

   else

       printf("FALSE a=%d,b=%d\n", a,b);

}

 

TRUE  a=0,b=10


5 ½Æ¦X±Ô­z

 

        ½Æ¦X±Ô­z¬O«ü'{ '©M'} '¤§¶¡ªº¤@³s¦ê±Ô­z¡A¨ä¥D­n¥Î³~¬O±N¼Æ­Ó±Ô­z¶°¦X¦b

¤@°_¡A¦¨¬°³æ¤@ªº¥i°õ¦æ±Ô­z¡C

 

 


±±¨î±Ô­z

(Control Statement)

 

µ²ºc¤Æµ{¦¡³]­p©Ò¨Ï¥Î¬yµ{¤j­P¤£¥~¤TºØ¡G

 

1. ¶¶§Ç¦¡µ²ºc(sequence structure)

2. ¿ï¾Ü¦¡µ²ºc(selection structure)

3. °j°é¦¡µ²ºc(loop structure)

 

¶¶§Ç¦¡µ²ºc

 

¥Ñ¤@³s¦ê²³æ±Ô­z²Õ¦X¡A¨ä°õ¦æ¶¶§Ç¥ç«ö³o¨Ç±Ô­zªº¥ý«á¦¸§Ç¡G

 

                                ¡õ

                             ¢z¢w¢w¢w¢{

                             ¢x ±Ô­z1¢x

                             ¢|¢w¢w¢w¢}

                                ¡õ

                             ¢z¢w¢w¢w¢{

                             ¢x ±Ô­z2¢x

                             ¢|¢w¢w¢w¢}

                                ¡õ

                             ¢z¢w¢w¢w¢{

                             ¢x ±Ô­z3¢x

                             ¢|¢w¢w¢w¢}

                                ¡õ

                                ¡G

                                ¡G


¿ï¾Üµ²ºc

 

±ø¥ó´ú¸Õ

 

¿ï¾Üµ²ºc¥Î¦b§@¨Mµ¦§PÂ_®É¡A¥¦¯à¨M©wµ{¦¡Å޿誺¨«¦V¡C¡ã(NOT)¡B&&(AND)¡B

»P || (OR)¬O C¤T­Ó°ò¥»ªºÅÞ¿è¹Bºâ¤l¡C

 

  ¡ã  ¢x T   F      && ¢x T    F       || ¢x T   F

  ¢w¢w¢q¢w¢w¢w¢w   ¢w¢w¢q¢w¢w¢w¢w     ¢w¢w¢q¢w¢w¢w¢w

  µ²ªG¢x F   T       T ¢x T    F        T ¢x T   T

                     F ¢x F    F        F ¢x T   F

 

T (TRUE)¥H 1 ªí¥Ü¡AF (FALSE)«h¬° 0¡C«e³¹¤]´£¨ì¡A©Ò¦³«D¹sªº­È¡A¦b´ú¸Õ

®É³£³Q»{¬°¬O TRUE¡C

 

           i = j * k ;

           if  (i)

               printf("TRUE");


IF ±Ô­z

 

IF ±Ô­z¨Ì¨äºc³yªº½ÆÂø©Ê¤S¥i¤À¬°¤TºØµ²ºc:

 

¤@¡B²³æIF±Ô­z

 

IF  (expression)             ¡õ

{                                   =0

   ±Ô ­z ¤@¡F

   ±Ô ­z ¤G¡F           expression  ¢w¢{

   :                                  ¢x

   :                                  ¢x

}                          ¡õ¡Ú0      ¢x

                        ¢z¢w¢w¢w¢{    ¢x

                        ¢x±Ô­z¤@¢x    ¢x

                        ¢|¢w¢w¢w¢}    ¢x

                           ¡õ         ¢x

                        ¢z¢w¢w¢w¢{    ¢x

                        ¢x±Ô­z¤G¢x    ¢x

                        ¢|¢w¢w¢w¢}    ¢x

                           ¡õ ¡ö¢w¢w¢w¢}

 

¨Ò:

if  (a-2)

    printf("a is not equal to 2\n");

 

¦P¸q©ó¤U¦¡

 

if  (a-2 !=0)

    printf("a is not equal to 2 \n");


¤G¡BIF....ELSE

 

³o­Óµ²ºc»P²Ä¤@ºØªº®t²§¦b©ó±ø¥ó¤£¦¨¥ß®É¡A¦³¥t¤@²Õ¥²¶·§¹¦¨ªº¤u§@¡C

 

 IF  (expression)    ¡õ

   {                      =0 ¢z¢w¢w¢w¢{  ¢z¢w¢w¢w¢{   ¢z¢w¢w¢w¢{

     ±Ô­z 1¡F   expression ¡÷¢x±Ô­z 1¢x¡÷¢x±Ô­z 2¢x...¢x±Ô­z  ¢x¢{

       ¡G                    ¢|¢w¢w¢w¢}  ¢|¢w¢w¢w¢}   ¢|¢w¢w¢w¢}¢x

     ±Ô­z m        ¡õ¡Ú0                                        ¢x

   }            ¢z¢w¢w¢w¢{                                      ¢x

ELSE            ¢x±Ô­z 1¢x                                      ¢x

   {            ¢|¢w¢w¢w¢}                                      ¢x

     ±Ô­z 1¡F      ¡õ                                           ¢x

       ¡G       ¢z¢w¢w¢w¢{                                      ¢x

     ±Ô­z n     ¢x±Ô­z 2¢x                                      ¢x

   }            ¢|¢w¢w¢w¢}                                      ¢x

                   ¡õ                                           ¢x

                   ¡G                                           ¢x

                ¢z¢w¢w¢w¢{                                      ¢x

                ¢x±Ô­z m¢x                                      ¢x

                ¢|¢w¢w¢w¢}                                      ¢x

                   ¡õ ¡ö¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

 


¤T¡B±_ª¬¦¡ IF ... ELSE

 

        A                             B

IF  (expression1)             IF  (expression1)

    IF  (expression2)             ±Ô­z 1¡F

        ±Ô­z 1¡F              ELSE IF  (expression2)

    ELSE                               ±Ô­z 2¡F

        ±Ô­z 2¡F                   ELSE

ELSE                                   ±Ô­z 3¡F

    ±Ô­z3¡F                             

 

                                                 ¡Ú0

       ¡õ    =0                                     ¢z¢w¢w¢w¢{

                 ¢z¢w¢w¢w¢{           expression1 ¡÷¢x±Ô­z 1¢x¢w¢{

   expression1 ¡÷¢x±Ô­z 3¢x ¢{                      ¢|¢w¢w¢w¢}  ¢x

                 ¢|¢w¢w¢w¢} ¢x             ¡õ =0                ¢x

       ¡õ¡Ú0                ¢x                      ¢z¢w¢w¢w¢{  ¢x

                 ¢z¢w¢w¢w¢{ ¢x        expression2 ¡÷¢x±Ô­z 3¢x  ¢x

   expression2 ¡÷¢x±Ô­z 1¢x ¢x                      ¢|¢w¢w¢w¢}  ¢x

                 ¢|¢w¢w¢w¢} ¢x             ¡õ  ¡Ú0              ¢x

       ¡õ                   ¢x         ¢z¢w¢w¢w¢{               ¢x

   ¢z¢w¢w¢w¢{               ¢x         ¢x±Ô­z 2¢x               ¢x

   ¢x±Ô­z 2¢x               ¢x         ¢|¢w¢w¢w¢}               ¢x

   ¢|¢w¢w¢w¢}               ¢x             ¡õ ¡ö¢w¢w¢w¢w¢w¢w¢w¢w¢}

       ¡õ ¡ö¢w¢w¢w¢w¢w¢w¢w¢w¢}

 


¡±¨Ò¡G

    if  (x > value)

        high = mid - 1;

    else if  (x < value)

             low = mid + 1;

         else

             return (mid);

 

·í else ªº³¡¥÷ªº½T¤£¶·¦³¥ô¦ó±Ô­z®É¡A ¥i¥H¥Î¡u¡F¡v¥N´À¡C

 

¡±¨Ò¡G

    if  (A == B) {

        printf("correct");

        if  (B == C)

            printf("right");

        else

            ;

    }

    else

        printf("wrong");


¡¹¡¸¡¹ ¨Ò ¡¹¡¸¡¹

 

main()

{

  float a,b,c;          // ¤T¨¤§Î¤TÃäªø

  float s1, s2, temp;   //¼È®ÉÅܼÆ

 

  printf("¿é¤J¤TÃäªø:\n");

  scanf("%f%f%f",&a,&b,&c);

  if  (b > a){

      temp = a;

      a = b;

      b = temp;

  }

  if  (c > a) {

       temp = a;

       a = c;

       c = temp;

  }

  s1 = a * a;

  s2 = b * b + c * c;

  if  (a >= b+c)

      printf("\n¿é¤J¤§¤TÃäµLªkºc¦¨¤T¨¤§Î");

  else if  (s1 == s2)

           printf("\n¿é¤J¤§¤TÃä¥iºc¦¨ª½¨¤¤T¨¤§Î");

       else if  (s1 > s2)

                printf("\n¿é¤J¤§¤TÃä¥iºc¦¨¶w¨¤¤T¨¤§Î");

            else

                printf("\n¿é¤J¤§¤TÃä¥iºc¦¨¾U¨¤¤T¨¤§Î");

}

 

¿é¥Xµ²ªG :         

 

¿é¤J¤TÃäªø:3 4 5

¿é¤J¤§¤TÃä¥iºc¦¨ª½¨¤¤T¨¤§Î¿é¤J¤TÃäªø:

 

¿é¤J¤TÃäªø:3 2 1

¿é¤J¤§¤TÃä¥iºc¦¨¶w¨¤¤T¨¤§Î¿é¤J¤TÃäªø:

 

¿é¤J¤TÃäªø:7 5 8

¿é¤J¤§¤TÃä¥iºc¦¨¾U¨¤¤T¨¤§Î¿é¤J¤TÃäªø:

 

¿é¤J¤TÃäªø:10 5 3

¿é¤J¤§¤TÃäµLªkºc¦¨¤T¨¤§Î


¡±¨Ò¡G

 

main ()

{

  if  (10)

      printf ("  10 is true.\n");

  if  (-10)

      printf (" -10 is true.\n");

  if  (0)

      printf ("   0 is true.\n");

  else

      printf ("   0 is false.\n");

}

 

¿é¥Xµ²ªG :

 

 1  is true.

-10 is true.

  0 is false.


±ø¥óªí¥Ü¦¡¡G±ø¥ó¹Bºâ¤l¬°¡H¡G

 

  ¼Ð·Ç®æ¦¡¬°

 

expression1¡Hexpression2¡Gexpression3

 

³o­Ó¹Bºâ¦¡µ¥©ó¼g¦¨¡G

 

        if  (expression 1)

            expression 2;

        else

            expression 3;

 

        if  (a > b)

            z = a ;    ¦P¸q©ó z = ( a > b ) ? a : b ;

        else

       ¡@   z = b ;

 


switch ±Ô­z

 

´£¨Ñ¤F¦h­«¿ï¾Üªº¥\¯à¡G

 

»yªk¡G

          switch (¹Bºâ¦¡) {

 

              case (±`¼Æ¹Bºâ¦¡ 1 )¡G±Ô­z 1¡F

                                    ±Ô­z 2¡F

                                     .

                                     .

                                     .

                                    break¡F

 

              case (±`¼Æ¹Bºâ¦¡ 2 )¡G±Ô­z 1¡F

                                    ±Ô­z 2¡F

                                     .

                                     .

                                     .

                                    break¡F

                .

                .

                .

              default             : ±Ô­z 1¡F

                                    ±Ô­z 2¡F

                                     .

                                     .

                                     .

          }


 

¨Ï¥Î switch ®É¦³¤U¦C´XÂIÀ³¯S§Oª`·N¡G

 

1¡Dcase «á­±µù©ú¬°±`¼Æ¹Bºâ¦¡¡Aªí¥Ü¥²¶·¬O¤@­Ó­È©Î¤@­Ó±`¼Æ²Å¸¹¡A¤£¥i

   ¬OÅܼƩΨä¥L¹Bºâ¦¡¡C

 

2¡Dcase ¤ºªº±Ô­z¦h©ó¤@¥y®É¡A¤£¥²¥H{ } §@°Ï¹j¡C

 

3¡D¦b case ¤¤§ä¨ì²Ä¤@­Ó¥¿½Tªº­È®É«K°õ¦æ¨ä«á­±ªº±Ô­z¡Abreak¥i¥H¬Ù²¤¡A

   ¦ýµ{¦¡¦]¦¹µLªk¸õÂ÷ switch¡A·|Ä~Äò°õ¦æ¤U­±¨ä¥Lªº case¡C

 

4¡D·í©Ò¦³ case ³£¤£¦¨¥ß®É¡A·|°õ¦æ default ³¡¥÷¡A¦ý­Y¨S¦³ default ±Ô

   ­z¡A«h³o¤@³¡¥÷¤]¥i¬Ù²¤¡C

 

5¡Dcase ¤º¤]¥i¥H¬O¡yªÅ±Ô­z¡z(Null Statement)¡C

 

   ¡±¨Ò¡G

 

   switch ( ch ) {

       case 'F' :             /* Null Statement */

       case 'f' : printf(...)

         :

   }

 

   ­Y¹J¨ìªÅ±Ô­z¡A«h°õ¦æ¬yµ{Ä~Äò©¹¤U¤@­Ó case °õ¦æ¡C

 

6¡Dbreak ±Ô­z¡A«ü¥Üµ{¦¡¬yµ{¥ß§Y¸õ¥X switch body¡A¥Ñ switch «áªº²Ä¤@

   ­Ó±Ô­z©¹¤U°õ¦æ¡C­Y¨S¦³ break ±Ô­z¡A«hµ{¦¡·|Ä~Äò°õ¦æ¤U¤@­Ó case

   ±Ô­zª½¨ì¹J¨£ break ©Î switch ªº§À³¡¤~°±¤î¡C


¡±¨Ò¡G

main()

{

  int i;

  scanf("%d",&i);

  switch (i) {

      case  1: printf("i equals to %d\n",i);

               i++;

      case  2: printf("i equals to %d\n",i);

               i++;

      default: printf("Now i becomes %d\n",i);

  }

}

 

¿é¥Xµ²ªG :         

 

1    <--- ¿é¤J¸ê®Æ 1

 

i equals to 1

i equals to 2

Now i becomes 3


¡±¨Ò¡G

main()

{

   int i;

   scanf(%d",i);

   switch(i) {

      case  1: printf("i equals to %d\n",i);

               i++;

                break;

      case  2: printf("i equals to %d\n",i);

               i++;

                break;

      default: printf("Now i becomes %d\n",i);

   }

}

¿é¥Xµ²ªG :         

 

1  <--- ¿é¤J¸ê®Æ 1

i equals to 1


¡±¨Ò¡G ¿é¤J¥ô·N¦r¦ê¡A­p¼Æ¼Æ¦r¡BªÅ¥Õ¡B·s¦æ(new line)¡B¸õ®æ(Tab)ªº­Ó¼Æ¡G

 

#include <stdio.h>

main()

{

   int c;  // ¿é¤J¦r¤¸

   int nspace = 0;    /* number of space */

   int ntab = 0;      /* number of tab */

   int nnewline = 0;  /* number of new line */

   int nothers = 0;

   int ndigit = 0;    /* number of digit */

   printf("¿é¤J¸ê®Æ:");

   while((c=getchar()) != EOF)

       switch(c){

           case '0':

           case '1':

           case '2':

           case '3':

           case '4':

           case '5':

           case 36':

           case '7':

           case '8':

           case '9': ++ndigit;

                     break;

           case ' ': ++nspace;

                     break;

           case '¢@n': ++nnewline;

                      break;

           case '¢@t': ++ntab;

                      break;

           default: ++nothers;

                    break;

       }

   printf("¢@n¢@n ** output result **");

   printf("¢@n¼Æ¦r­Ó¼Æ:&d",ndigit);

   printf("¢@n<new line>­Ó¼Æ:%d",nnewline);

   printf("¢@nªÅ¥Õ­Ó¼Æ:%d",nspace);

   printf("¢@n<Tab>ªº­Ó¼Æ:,%d",ntab);

   printf("¢@n¨ä¥¦¦r¤¸ªº­Ó¼Æ:&d",others);

}


°j°é¦¡µ²ºc

 

for ±Ô­z

 

»yªk:

      for(expr1;expr2;expr3)

      {

         ±Ô­z;

         :

         :

      }

expr1: °j°é°O¼Æ¾¹ªºªì­È¡C

expr2: ´ú¸Õ°j°é¬O§_°±¤î¡C

expr3: °j°éªº¼W­È¡C

expression¤§¶¡¥H¡y;¡z¤À³Î¡C

 

­Y expression ¤º¦³¦h­Ó¹Bºâ¦¡«h¥H¡y,¡z¹j¶},³r¸¹ªº¹Bºâ¦¸§Ç¥Ñ¥ª¦Ü¥k¨Ì¦¸

¹Bºâ¡C

 

    for(oun=1,cost=FIRST; oun <= 16; oun++,cost+=next)

 

for ©Ò¨ç¬Aªº±Ô­z¥i¥H¬OªÅ±Ô­z

 

¨Ò¡Gfor (t=0; t < some-value; t++)

        ;

expression 1~3 ¬Ò¥i¬Ù²¤ ¦ý expression ¶¡ªº¡y;¡z¤£¥i¬Ù²¤¡C

 

¨Ò¡Gfor (n=3; ans <= 25;)

        for (;;){

           :

           :

        }

 

for ¨S¦³­­¨î initialize counter ¤@©w¬O¤@­Óºâ¦¡¡C

 

¨Ò¡Gfor (printf("keep entering numbers!\n"); num==6;){

         ¡K

         ¡K

    }


¡±¨Ò¡G/*  Add 1 + 2 + ..... + 100    */

 

main()

{

    int sum = 0;

    int i = 1;

 

    for ( ; i <= 100; )

        sum += i++;

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

}

 

 

¡±¨Ò¡G/*  Print 9 * 9 table using double for loop.  */

 

main()

{

    int i,j,result;

 

    for (i = 1; i <= 9; i++){

        for (j = 1; j <= 9; j++){

            result = i * j;

            printf("%d*%d=%-3d",i,j,result);

        }

        printf("\n");

    }

}


while ±Ô­z

 

»yªk:

       while(expression)       while(expression)

       {                           ±Ô­z;

          ±Ô­z1;

          :

          :

       }

­Y expression ªº­È¤£µ¥©ó¹s«h°õ¦æ°j°éªº±Ô­z¡C

 

­Y°j°é¤º±Ô­z¥u¦³¤@¥y®É¥i¬Ù²¤ {}¡C

 

¡±¨Ò¡G/*  add 1 + 2 + ..... + 10  */

 

main()

{

    int i,sum;

 

    i = 1;

    sum = 0;

    while (i <= 10)

        sum += i++;

    printf("The sum is %d \n",sum);

}

 

¡±¨Ò¡G/*   find the greatest common divisor.  */

main()

{

    int i,j,tmp;

 

    printf("Please input 2 nonnegative numbers \n==> ");

    scanf("%d %d",&i,&j);

    while (j != 0) {

        tmp = i % j;

        i = j;

        j = tmp;

    }

    printf("The gcd is %d \n",i);

}

 


do ~ while ±Ô­z

 

»yªk:  do                        do

       {                             ±Ô­z;

           ±Ô­z;                     :

           :                     while(expression);

           :

       }while(expression);

 

expression ªº­È¬° true ®É¦^¨ì°j°é¤º°õ¦æ¡A¦Ü¤Ö°õ¦æ°j°é±Ô­z¤@¦¸¡C

 

¡±¨Ò¡G/*  add 1 + 2 + .... + 100   */

main()

{

    int i,sum;

    i = 1;

    sum = 0;

    do {

       sum += i++;

    } while ( i <= 100 );

    printf("\1: The sum is %d \n",sum);

}

 

¡±¨Ò¡G¿é¤J¤@¦æ¦r¦ê­p¼Æ¦@¦³´X­Ó¦r²Õ(word)¡C¥HªÅ¥Õ¡B³r¸¹©Î¥yÂIªí¤@¦r²Õ

      ªºµ²§ô¡C

 

main()

 

  char ch;

  int punch;

  int cnt = 0;

  printf("¿é¤J¸ê®Æ:");

  do

  {

     do

       ch=getchar();

     while(ch!=' ' && ch!=',' && ch!='.' && ch!='¢@n')

         ;

     while(ch == ' ' || ch == '.' || ch == ',')

         ch = getchar();

     cnt++;

  } while(ch != '\n');

  printf("¦@¿é¤J %d ­Ó¦r",cnt);

}


break »P continue

 

¨Ï¸õ¥X°j°é¥DÅé¦Ó°õ¦æ¤U¤@±Ô­z

 

break ¤§¹ê¨ÒÀ³¥Î¡G

¡±¨Ò:

 

do {

   scanf("%d",i);

   if  (i < 0)

       break;

         :

} while(i != 0);

printf("End");

 

¡±¨Ò¡G/* for and break application.  */

main()

{

    int i;

 

    for (i = 1; i <= 100; i++){

        printf("loop index %d \n",i);

        if  (i >= 5)

             break;

    }

}

 

¡±¨Ò¡G/* for loop with forever loop  */

main()

{

    int i;

    int count = 1;

    for ( ; ; ) {

        printf("guess number here: ");

        scanf("%d",&i);

        if  (i == 5)

             break;

        count++;

    }

    printf("You take %d times to get it.\n",count);

}


continue ¤§¹ê¨ÒÀ³¥Î:

 

¡±¨Ò¡G ¨D1¨ì100¤¤5ªº­¿¼Æ¤§©M

 

#include <stdio.h>

main()

{

   float sum = 0.0;

   int i;

   printf("¥Ñ 1 ¦Ü 100,©Ò¦³ 5 ªº­¿¼Æ¬°:\n");

   for (i = 1;i <= 100; i++){

       if  (i % 5 != 0)

           continue;

       printf("&d",i);

       (i % 25 == 0) ? printf("\n") : printf(" ");

       sum += i;

   }

   printf("¨ä©M¬°:&d",sum);

}

 

¡±¨Ò¡G/* add 2 + 4 + .... + 100  */

 

main()

{

    int i,sum;

 

    sum = 0;

    for (i = 2; i <= 100; i++){

        if  ((i % 2) != 0)

             continue;

        sum += i;

    }

    printf("The sum is %d \n",sum);

}


2-5 µL±ø¥ó¸õÅD -- goto

 

»yªk®æ¦¡:

          goto label-name;

                  ....

                  ....

       label-name:

                  statements;

                  .....

 

label name »PÅܼƪº©R¦W¬Û¦P¡A¨ä«á»Ý¸ò«_¸¹¡A±`¥Î¦b¥Ñ±_ª¬°j°é¤¤¸õ¥X¡C

¨Ò¡G

for (...)

    for (...){

        ...

        if  (...)

            goto error;

            ...;

    }

error:

      ¡K

      ¡K

 

¡±¨Ò¡G/* goto command application  */

 

main()

{

    int i,j;

 

    start: printf("Please input 2 number here \n==> ");

           scanf("%d%d",&i,&j);

           if  (i > j)

               goto stop;

           goto start;

     stop: printf("program stop. \n");

}

 


¶|¦~ªº§PÂ_¡G(1)¯à°÷³Q 4¾ã°£¦Ó¥B¤£¬° 100©Ò¾ã°£

                        (2)¯à³Q 400©Ò¾ã°£

              ·í (1)»P (2)¨ä¤¤¦³¤@¦¨¥ß§Y¬°¶|¦~

¡±¨Ò

 

 /* This program is to test logical use */

 main ()

 {

  int year ;

  printf("This program is to test wether the input is leap year :\n\n");

  printf("Which year :");

  scanf("%d",&year);

  if   ((year%4==0 && year%100 !=0) || (year%400==0))

       printf ("%4d is a leap year\n",year);

   else

       printf ("%4d is not a leap year\n",year);

  }

  ¿é¥Xµ²ªG

 

          This program is to test wether the input is leap year :

          Which year : 1984

          1984 is a leap year

 

          This program is to test wether the input is leap year :

          Which year : 1911

          1911 is not a leap year

 

          This program is to test wether the input is leap year :

          Which year : 2000

          2000 is a leap year


strlen(s)   /* return length of s */

  char s[];

 

{

  int i;

 

  i = 0;

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

      i++;

  return (i);

}

 

 

 

strcmp(s, t) /* return < 0 if s<t, 0 if s==t, >0 if s>t */

{

  int  i;

 

  i = 0;

  while (s[i] == t[i])

      if  (s[i++] == '\0')

           return (0);

  return (s[i] - t[i]);

}


strcat(s, t)      /* concatenate t to s */

  char s[], t[];  /* s must be big enough */

 

{

  int i, j;

 

  i = j = 0;

  while (s[i] != '\0')  /* find end of s */

      i++;

  while ((s[i++] = t[j++]) != '\0')   /* copy t */

      ;

}

 

 

 

reverse(s)   /* reverse string s in place */

  char s[];

{

  int c, i, j;

 

  for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {

      c    = s[i];

      s[i] = s[j];

      s[j] = c;

  }

}


main()  /* count lines in input  */

{

  int  c, nl;

 

  nl = 0;

  while ((c=getchar()) != EOF)

      if  (c == '\n')

             nl++;

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

}

 

 

 

 

 

 

 

#define YES 1

#define NO  0   

main() /* count lines, words, chars in input */

{

  int  c, nl, nw, nc, inword;

  inword = NO;

  nl = nw = nc = 0;

  while ((c = getchar()) != EOF) {

      nc++;

      if  (c == '\n')

             nl++;

      if  (c == ' ' || c == '\n' || c == '\t')

             inword = NO;

      else if  (inword == NO) {

                  inword = YES;

                  nw++;

            }

  }

  printf("%d %d %d \n", nl, nw, nc);

}

 

 


index(s,t) /* return index of t in s, -1 if none */

char s[], t[];

{

  int i, j, k;

 

  for (i=0; s[i]!='\0'; i++) {

      for (j=i,k=0; t[k]!='\0' && s[j]==t[k]; j++,k++)

          ;

      if  (t[k] == '\0')

             return (i);

  }

  return (-1);

}

 

 

 

 

 

 

 

main()

{

  int  i, num;

  printf("Prime Number ...\n\n");

  printf("Please enter number ('0' to Quit)\n");

  do

     {

       printf("number = ");

       scanf("%d", &num);

       i = 2;

       if  (num == 1)

              i = 1;

       else

              while ((num % i) != 0)

                  i++;

       if  (i == num)

           printf("%d Is a Prime Number.\n", num);

       else

           printf("%d Is Not a Prime Number.\n", num);

     } while (num != 0);

}

 

 


  1. ©w¸q¤@ int array P, P[i]=0   2<i<n

 

  2. i=2

 

  3. ¦L¥X i ­È(¨ä¬°½è¼Æ)

 

  4. ¹ï i*j < n ªº j ­È, ±N P[i*j] ³]©w¬° 1

 

  5. §ä¥X¤U¤@­Ó²Å¦X P[i]=0 ªÌ=> 3,§ä¤£¨ì=> µ²§ô

 

 

 


 

 

#define N 1000

main()

{

   int p[N],i,j,k,prime,n=0;

 

   for (i = 2; i < N; i++)

       p[i] = 0;

   printf("... find prime number ....\n");

   printf("input your data : ");

   scanf("%d", &prime);

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

   i = 2;

   while (i < prime) {

       printf("%5d%c", i, (n++%7==6 || i==prime-1) ? '\n' : ' ');

       for (j = 1; (i * j) < prime ; j++)

              p[i*j] = 1;

       for (k = i + 1; k < prime+1; k++) {

            if  (p[k] == 0) {

                  i = k;

                  break;

           }

       }

   }

   printf("\n");

}

 

... find prime number ....

input your data : 200

 

    2     3     5     7    11    13    17

   19    23    29    31    37    41    43

   47    53    59    61    67    71    73

   79    83    89    97   101   103   107

  109   113   127   131   137   139   149

  151   157   163   167   173   179   181

  191   193   197   199

 


 

 

main()

{

  char str[20];

  int  i, len, sum = 0;

 

  printf("This program is to test your data is divisible by 9\n");

  printf("Please  input your data (digits) ");

  scanf("%s", str);

  len = strlen(str);

  for (i = 0; i <= len-1; i++)

       sum += str[i] - '0';           /*    sum += str[i]- 48;  */

  if  (sum % 9 == 0)

      printf("%s is divisible by 9\n",str);

  else

      printf("%s is not divisible by 9\n",str);

}

 


binary(x, v, n)  /* find x in v[0] ... v[n-1]  */

  int x, v[], n;

{

  int low, high, mid;

 

  low  = 0;

  high = n-1;

  while (low <= high) {

      mid = (low + high) / 2;

      if  (x < v[mid])

             high = mid - 1;

      else if  (x > v[mid])

                  low = mid + 1;

           else  /* found match  */

                  return (mid);

  }

  return (-1)

}

      


«eºÝ³B²zµ{¦¡

(preprocessor)

 

¡­define      ¡­include

 

 

      ¢ë´£¨Ñ«eºÝ³B²zµ{¦¡¡A¤Z¬O«eºÝ¦³¡­ªÌ¬ÒºÙ¤§¡A¦p§Q¥Î¡­include§â¸ê®Æ±q¬Y

¤@Àɮפ¤¤Þ§t¶i¨Ó¡C«eºÝ³B²zµ{¦¡¤]¥i¥Î¨Ó©w¸q±`¼Æ¡C¦p¡­define PI 3.1416 ªí

¥Ü 3.1416¥H PI ²Å¸¹¥N¤§¡A¥H«á¥X²{ PI ªº¦a¤è´N¬Oµ¥©ó 3.1416¡Aª`·N¡­define «á

­±¤£­n¦³¤À¸¹¡]¡F)¡C¦³®É¬°¤F¨Ïµ{¦¡©öŪ¡A©¹©¹§â©w¸q²Å¸¹ PI ¤j¼g¡A¦p¦¹¦bµ{

¦¡¤¤¸I¨ì¤j¼gªº¦r¡A«K¥iª¾¹D¨ä¬°±`¼Æ¦Ó¤£¬OÅܼơC

 


¨ÒÃD ¢°

 

 

#define  a  10

#define  b  20

#define  c  30

 

main()

{

  int sum;

  float average;

  sum = a + b + c;

  average = sum / 3;

  printf("a=%d, b=%d, c=%d, sum=%d, average=%f\n",a,b,c,sum,average);

}

 

 

¿é¥Xµ²ªG

 

a=10¡Ab=20¡Ac=30¡Asum=60¡Aaverage=20.000000

 

¦b¨ÒÃD¢°¤¤§Q¥Î #define ©w¸q¤F a¡Ab¡Ac ¤À§O¬° 10¡A20¡A30 ¦]¦¹µ{¦¡¤¤¤Z¬O¥X

²{ a¡Ab¡Ac ªº¦a¤è¡A¨ä­È¬Û·í©ó 10¡A20¡A30¡C

 

°²­Yµ{¦¡¥Î¨ì«Ü¦h¡­defineªº²Å¸¹¡A«h§Ú­Ì¥i¥H¥ý§â¥¦¦s¦b¤@­ÓÀÉ®×ùØ¡A­n¥Î®É

¦A§Q¥Î¡­include§â¥¦¤Þ§t¶i¨Ó¡C

 

 


¨ÒÃD ¢±

 

#include "def.h"

 

main()

{

  int sum;

  float average;

  sum = a + b + c;

  average = sum / 3;

  printf("a=%d, b=%d, c=%d, sum=%d, average=%f\n",a,b,c,sum,average);

}

 

 


 ¨ÒÃD ¢²

 

#define square(x) x*x

 

main()

{

  int result;

  result = square(3);

  printf("The square(3) is %d.\n",square(3));

  result = square(3+4);

  printf("The square(3+4)is %d.\n",square(3+4));

}

 

¿é¥Xµ²ªG

 

The square(3) is 9.

 

The square(3+4) is 19.

 


¨ÒÃD ¢³

 

#define square(x) (x)*(x)

 

main()

{

  int i=1,result;

  result = square(++i);

  printf("The square(++%d) is %d.\n",i,square(++i));

  result = square(i);

  printf("The square(%d) is %d.\n",i,square(i));

}

 

¿é¥Xµ²ªG

 

The square(++5) is 20.

The square(5) is 25.

 

¨ÒÃD¢³§i¶D§Ú­Ì¤£­n¦b¨â­ÓÅܼƤ@¼Ëªº±¡ªp¤U¨Ï¥Î ++¡C

 

ª`·N¤£¥i¥H§â #define square(x) (x)*(x) ¼g¦¨

 

#define square (x) (x)*(x) ©Î

#define square(x) (x)*(x);


#define square(x) (x)*(x)

main()

{

  int i = 1, result;

  result = square(i++);

  printf("The square(++%d) is %d \n",i, square(i++));

  result = square(i);

  printf("The square(%d) is %d \n",i, square(i));

}

 

¿é¥Xµ²ªG

 

The square(++3) is 9

The square(5) is 25

 


¨ÒÃD ¢´  §Q¥Î #define ¨D¨â¼Æ¤§³Ì¤p­È

 

#define MIN(x,y) ( ( (x) < (y) ) ? (x) : (y) )

 

main()

{

  int x, y, min;

  printf("The program is to find the minimun of x & y\n");

  printf("Please input your data (x,y)\n");

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

  printf("The minimun of %d & %d is %d\n",x,y,MIN(x,y));

}

 

¿é¥Xµ²ªG

 

The program is to find the minimun of x & y

Please input your data (x,y)

17 13

The minimun of 17 & 13 is 13

 


¨ÒÃD ¢µ  §Q¥Î #define ¨D¤T¼Æ¤§³Ì¤j­È

 

#define MAX(x,y) ( ( (x) > (y) ) ? (x) : (y) )

#define MAX3(x,y,z) ( (MAX(x,y)) > (z) ? (MAX(x,y)) : (z) )

 

main()

{

  int x, y, z;

  printf("The program is to find the maximun of three number\n");

  printf("Please input your data (x,y,z)\n");

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

  printf("The maximun of %d & %d & %d is %d\n",x,y,z,MAX3(x,y,z));

}

 

¿é¥Xµ²ªG

 

The program is to find the minimun of three number

Please input your data (x,y,z)

27 39  15

The maximun of 27 & 39 & 15 is 39

 


¨ÒÃD ¢¶

 

§Q¥Î #define ¨Ì¿é¤J data ªº ¥¿­t¸¹±N data ¦V¥ª©Î¦V¥k²¾­Y¤z¦ì¤¸¡C

¦p 10 ¦V¥ª²¾¤G¦ì¤¸¡A 10 = (1010) => (101000)  => 40

                                                                                        2                               2

#define SHIFT(a,b) ((b) > 0 ? (a << b) : (a >> -b) )

 

main()

{

  int value,n;

  printf("This program is to shift your data\n");

  printf("IF 'n' > 0 shift left ELSE¡@shift right\n");

  printf("Please input your data(value n)\n");

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

  if    (n >= 0)

        printf("%d after shift left %d bits ===> %d\n",value,n,SHIFT(value,n));

  else

        printf("%d after shift right %d bits ===> %d\n",value,-n,SHIFT(value,n));

}

 

¿é¥Xµ²ªG

 

This program is to shift your data

IF 'n' > 0 shift left ELSE¡@shift right

Please input your data(value n)

10    2

10 after shift left 2 bits ===> 40

 


 

 

/*   call by value */

 

swap(x,y)

  int x, y;

{ 

  int temp;

 

  temp = x;

  x    = y;

  y    = temp;

}

main()

{

  int a = 1, b = 2;

  swap(a,b);

  printf("a is now %d, b is now %d\n",a,b);

}

 

a is now 1, b is now 2

 


 

 

/*   call by address */

 

swap(x,y)

  int *x, *y;

{ 

  int temp;

  temp = *x;

  *x   = *y;

  *y   = temp;

}

main()

{

  int a = 1, b = 2;

  swap(&a,&b);

  printf("a is now %d, b is now %d\n",a,b);

}

 

a is now 2, b is now 1

 


/*   call by name */

 

swap(x,y)

  int x, y;

{ 

  int a;

 

  a = x;

  x = y;

  y = a;

}

main()

{

  int a = 1, b = 2;

  swap(a,b);

  printf("a is now %d, b is now %d\n",a,b);

}

 

swap(a,b)

  int a, b;

{ 

  int a;

 

  a = a;

  a = b;

  b = a;

}

 

a is now 2, b is now 2


»¼Âk¹Bºâ

 

¥ô¦ó¨ç¼Æ³£¯à©I¥s¥¦¥»¨­

 

 

sum (n)

int   n;

{

    if  (n <= 1)

        return  (n);

    else

        return  (n + sum (n-1));

}

 

 

 

      Function  call           Value  returned

  ===============================================================================

    

      sum (1)                  1

      sum (2)                  2 + sum (1) is 2 + 1

      sum (3)                  3 + sum (2) is 3 + 2 + 1

      sum (4)                  4 + sum (3) is 4 + 3 + 2 + 1

      sum (5)                  5 + sum (4) is 5 + 4 + 3 + 2 + 1


    sum = 5 + sum (5-1) ; {first}

               ¢x

              sum = 4 + sum (4-1) ; {second}

                         ¢x

                        sum = 3 + sum (3-1) ; {third}

                                   ¢x

                                  sum = 2 + sum (2-1) ; {fourth}

                                                   ¢x

                                                  sum = 1 ; {fifth}

 

 

                                                  sum = 1 ; {fifth}

                                                  ¢x

                                        sum = 2 + 1 ; {fourth}

                                         ¢x

                               sum = 3 + 3 ; {third}

                               ¢x

                      sum = 4 + 6 ; {second}

                       ¢x

             sum = 5 + 10 ; {first}


/*  Write a line backwards. */

 

main ()

{

    printf (" Input a line :");

    prn_it();

    printf ("\n\n");

}

 

 

prn_it()

{

    char   c;

 

    if  ((c = getchar()) != '\n')

        prn_it();

    putchar(c);

}

 

 

 

Input a line : sing a song of sixpence

 

 

ecnepxis fo gnos a gnis

 


#define     MAXWORD     100

 

main ()

{

    printf (" Input a line :");

    prn_it_by_word();

    printf ("\n\n");

}

 

prn_it_by_word()

{

    char   w[MAXWORD];

 

    get_word(w);

    if (w[0] != '\n')

       prn_it_by_word();

    printf ("%s",w);

}

 

get_word(s)

char  *s;

{

    static char c = '\0';

 

    if  (c == '\n')

        *s++ = c;

    else

        while (( c =getchar()) != '' && c != '\t' && c != '\n')

              *s++ = c ;

              *s = '\0';

}

 

Input a line : deep in the heart of texas

texas of heart the in deep


/* Reverse the character between s[j] and s[k] recursively. */

 

reverse (s,j,k)

char  *s;

int j,k ;

{

  if  (j<k) {

      swap (&s[j], &s[k]);

      reverse (s ,++j, --k);

  }

}

 

 

swap (p ,q )

char  *p,*q;

{

  char    temp ;

 

  temp = *p ;

  *p = *q ;

  *q = temp ;

}


/* The best possible minmax algorithm - Pohl , 1972 */

 

minmax (a,n,min_ptr,max_ptr)  /* n is the size of a[] */

int a[], n,*min_ptr ,*max_ptr ;  /* n must be a power of 2 */

{

   int min1,max1,min2, max2;

 

   if  (n ==2)

     if  (a[0] < a[1]) {

         *min_ptr = a[0] ;

         *max_ptr = a[1] ;

     }

     else {

         *min_ptr = a[1] ;

         *max_ptr = a[0] ;

     }

   else {

        minmax (a,n/2,&min1,&max1);

        minmax (a+n/2,n/2,&min2,&max2);

        if  (min1 < min2)

            *min_ptr = min1 ;

        else

            *min_ptr = min2 ;

        if  (max1 < max2)

            *max_ptr = max2 ;

        else

            *max_ptr = max1 ;

   }

}


±`¨£ªºµ{¦¡¿ù»~

 

 

factorial_forever(n)

int  n ;

{

   return  (n * factorial_forever(n-1)) ;

}

 

 

factorial_error(n)

int  n ;

{

   if  (n == 1)

       return  (1) ;

   else

       return  (n * factorial_positive(n-1)) ;

}

 

 

factorial (n)

int  n ;

{

   if  (n <= 1)

       return  (1) ;

   else

       return  (n * factorial(n-1)) ;

}

 

 


ªe¤º¶ð°ÝÃD  (Tower of Hanoi)

 

    ©Ò¿×ªe¤º¶ð°ÝÃD¡A¬O«ü¦³ n ­Ó½L¤l¡A¤w¸g«ö·Ó¥Ñ¤j¦Ü¤pªº¶¶§Ç¡A¤jªº¦b

¤U¡A¤pªº¦b¤W©ñ¦b¤@®Ú¬W¤l¤W¡A¥t¥~¦³¨â®Ú¬W¤l¬OªÅªº¡A²{¦b­n±N©Ò¦³ªº½L¤l

·h¨ì¥t¥~¤@®Ú¬W¤l¤W¡A²Ä¤T®Ú¬W¤l¥i¥H°µ¬°¼È®É©ñ¸m½L¤l¤§¥Î¡A¦b·h½L¤lªº¹L

µ{¤¤¥²¶·¿í¦u¤U¦Cªº¤T¶µ³W«h¡G

 

1. ¤@¦¸¥u¯à·h°Ê¤@­Ó½L¤l¡C

2. ¥ô¦ó¤@­Ó½L¤l¥i¥H±q¤@®Ú¬W¤l·h¨ì¥ô¦ó¤@®Ú¬W¤l¤W¡C

3. ¦b¥ô¦ó®É¶¡¤§¤º ,µ´¤£¯à¦³¤j½L¤l¦b¤W ,¦Ó¤p½L¤l¦b¤Uªº±¡ªpµo¥Í¡C

 

  (¤@)·í n = 1 ®É :

      ¥u¦³¤@­Ó½L¤l®É ,¥i¤£ÂǧU©ó C ,ª½±µ A ¥Ñ·h¦Ü B¡C

 


  (¤G)·í n = 2 ®É :

     1. ¥¼·h°Ê«e ,1 ¤Î 2©ñ¦b A ¬W , B ¬W¤Î C ¬WªÅµÛ¡C

     

               ¢z¢{                   ¢z¢{                   ¢z¢{

               ¢x¢x                   ¢x¢x                   ¢x¢x

               ¢x¢x                   ¢x¢x                   ¢x¢x

             ¢z¢r¢r¢{                 ¢x¢x                   ¢x¢x

             ¢x 1  ¢x                 ¢x¢x                   ¢x¢x

           ¢z¢r¢w¢w¢r¢{               ¢x¢x                   ¢x¢x

           ¢x   2    ¢x               ¢x¢x                   ¢x¢x

       ¢z¢w¢r¢w¢w¢w¢w¢r¢w¢{   ¢z¢w¢w¢w¢r¢r¢w¢w¢w¢{   ¢z¢w¢w¢w¢r¢r¢w¢w¢w¢{

       ¢x       A        ¢x   ¢x       B        ¢x   ¢x       C        ¢x

       ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢}   ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢}   ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢}

     2. ±N½L 1 ¥Ñ A ·h¦Ü C¡C( A -> C )

               ¢z¢{                   ¢z¢{                   ¢z¢{

               ¢x¢x                   ¢x¢x                   ¢x¢x

               ¢x¢x                   ¢x¢x                   ¢x¢x

               ¢x¢x                   ¢x¢x                   ¢x¢x

               ¢x¢x                   ¢x¢x                   ¢x¢x

           ¢z¢w¢r¢r¢w¢{               ¢x¢x                 ¢z¢r¢r¢{

           ¢x   2    ¢x               ¢x¢x                 ¢x 1  ¢x

       ¢z¢w¢r¢w¢w¢w¢w¢r¢w¢{   ¢z¢w¢w¢w¢r¢r¢w¢w¢w¢{   ¢z¢w¢w¢r¢w¢w¢r¢w¢w¢{

       ¢x       A        ¢x   ¢x       B        ¢x   ¢x       C        ¢x

       ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢}   ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢}   ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢}

     3. ±N½L 2 ¥Ñ A ·h¦Ü B¡C( A -> B )

               ¢z¢{                   ¢z¢{                   ¢z¢{

               ¢x¢x                   ¢x¢x                   ¢x¢x

               ¢x¢x                   ¢x¢x                   ¢x¢x

               ¢x¢x                   ¢x¢x                   ¢x¢x

               ¢x¢x                   ¢x¢x                   ¢x¢x

               ¢x¢x               ¢z¢w¢r¢r¢w¢{             ¢z¢r¢r¢{

               ¢x¢x               ¢x   2    ¢x             ¢x 1  ¢x

       ¢z¢w¢w¢w¢r¢r¢w¢w¢w¢{   ¢z¢w¢r¢w¢w¢w¢w¢r¢w¢{   ¢z¢w¢w¢r¢w¢w¢r¢w¢w¢{

       ¢x       A        ¢x   ¢x       B        ¢x   ¢x       C        ¢x

       ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢}   ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢}   ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢}

     4. ±N½L 1 ¥Ñ C ·h¦Ü B¡C( C -> B )

              ¢z¢{                   ¢z¢{                    ¢z¢{

              ¢x¢x                   ¢x¢x                    ¢x¢x

              ¢x¢x                   ¢x¢x                    ¢x¢x

              ¢x¢x                 ¢z¢r¢r¢{                  ¢x¢x

              ¢x¢x                 ¢x 1  ¢x                  ¢x¢x

              ¢x¢x               ¢z¢r¢w¢w¢r¢{                ¢x¢x

              ¢x¢x               ¢x   2    ¢x                ¢x¢x

      ¢z¢w¢w¢w¢r¢r¢w¢w¢w¢{   ¢z¢w¢r¢w¢w¢w¢w¢r¢w¢{    ¢z¢w¢w¢w¢r¢r¢w¢w¢w¢{

      ¢x       A        ¢x   ¢x       B        ¢x    ¢x       C        ¢x

      ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢}   ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢}    ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢}

 


(¤T)·ín = 3 ®É :

     1. ¥¼·h°Ê«e ,1¡B2¡B3 §¡©ñ¦b A ¬W , B ¬W¤Î C ¬WªÅµÛ¡C

     

            ¢z¢{                        ¢z¢{                      ¢z¢{

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

          ¢z¢r¢r¢{                      ¢x¢x                      ¢x¢x

          ¢x 1  ¢x                      ¢x¢x                      ¢x¢x

        ¢z¢r¢w¢w¢r¢{                    ¢x¢x                      ¢x¢x

        ¢x   2    ¢x                    ¢x¢x                      ¢x¢x

    ¢z¢w¢r¢w¢w¢w¢w¢r¢w¢{                ¢x¢x                      ¢x¢x

    ¢x       3        ¢x                ¢x¢x                      ¢x¢x

¢z¢w¢r¢w¢w¢w¢w¢w¢w¢w¢w¢r¢w¢{¢z¢w¢w¢w¢w¢w¢r¢r¢w¢w¢w¢w¢w¢{¢z¢w¢w¢w¢w¢r¢r¢w¢w¢w¢w¢{

¢x           A            ¢x¢x           B            ¢x¢x         C          ¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

     2. ±N½L 1 ¥Ñ A ·h¦Ü B¡C( A -> B )

            ¢z¢{                        ¢z¢{                      ¢z¢{

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

        ¢z¢w¢r¢r¢w¢{                    ¢x¢x                      ¢x¢x

        ¢x   2    ¢x                    ¢x¢x                      ¢x¢x

    ¢z¢w¢r¢w¢w¢w¢w¢r¢w¢{              ¢z¢r¢r¢{                    ¢x¢x

    ¢x       3        ¢x              ¢x 1  ¢x                    ¢x¢x

¢z¢w¢r¢w¢w¢w¢w¢w¢w¢w¢w¢r¢w¢{¢z¢w¢w¢w¢w¢r¢w¢w¢r¢w¢w¢w¢w¢{¢z¢w¢w¢w¢w¢r¢r¢w¢w¢w¢w¢{

¢x           A            ¢x¢x           B            ¢x¢x         C          ¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

     3. ±N½L 2 ¥Ñ A ·h¦Ü C¡C( A -> C )

            ¢z¢{                        ¢z¢{                      ¢z¢{

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

    ¢z¢w¢w¢w¢r¢r¢w¢w¢w¢{              ¢z¢r¢r¢{                ¢z¢w¢r¢r¢w¢{

    ¢x       3        ¢x              ¢x 1  ¢x                ¢x   2    ¢x

¢z¢w¢r¢w¢w¢w¢w¢w¢w¢w¢w¢r¢w¢{¢z¢w¢w¢w¢w¢r¢w¢w¢r¢w¢w¢w¢w¢{¢z¢w¢w¢r¢w¢w¢w¢w¢r¢w¢w¢{

¢x           A            ¢x¢x           B            ¢x¢x         C          ¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}


4. ±N½L 1 ¥Ñ B ·h¦Ü C¡C( B -> C )

            ¢z¢{                        ¢z¢{                      ¢z¢{

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                    ¢z¢r¢r¢{

            ¢x¢x                        ¢x¢x                    ¢x 1  ¢x

    ¢z¢w¢w¢w¢r¢r¢w¢w¢w¢{                ¢x¢x                  ¢z¢r¢w¢w¢r¢{

    ¢x       3        ¢x                ¢x¢x                  ¢x   2    ¢x

¢z¢w¢r¢w¢w¢w¢w¢w¢w¢w¢w¢r¢w¢{¢z¢w¢w¢w¢w¢w¢r¢r¢w¢w¢w¢w¢w¢{¢z¢w¢w¢r¢w¢w¢w¢w¢r¢w¢w¢{

¢x           A            ¢x¢x           B            ¢x¢x         C          ¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

     5. ±N½L 3 ¥Ñ A ·h¦Ü B¡C( A -> B )

            ¢z¢{                        ¢z¢{                      ¢z¢{

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                    ¢z¢r¢r¢{

            ¢x¢x                        ¢x¢x                    ¢x 1  ¢x

            ¢x¢x                ¢z¢w¢w¢w¢r¢r¢w¢w¢w¢{          ¢z¢r¢w¢w¢r¢{

            ¢x¢x                ¢x       3        ¢x          ¢x   2    ¢x

¢z¢w¢w¢w¢w¢w¢r¢r¢w¢w¢w¢w¢w¢{¢z¢w¢r¢w¢w¢w¢w¢w¢w¢w¢w¢r¢w¢{¢z¢w¢w¢r¢w¢w¢w¢w¢r¢w¢w¢{

¢x           A            ¢x¢x           B            ¢x¢x         C          ¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

6. ±N½L 1 ¥Ñ C ·h¦Ü A¡C( C -> A )

            ¢z¢{                        ¢z¢{                      ¢z¢{

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

          ¢z¢r¢r¢{              ¢z¢w¢w¢w¢r¢r¢w¢w¢w¢{          ¢z¢w¢r¢r¢w¢{

          ¢x 1  ¢x              ¢x       3        ¢x          ¢x   2    ¢x

¢z¢w¢w¢w¢w¢r¢w¢w¢r¢w¢w¢w¢w¢{¢z¢w¢r¢w¢w¢w¢w¢w¢w¢w¢w¢r¢w¢{¢z¢w¢w¢r¢w¢w¢w¢w¢r¢w¢w¢{

¢x           A            ¢x¢x           B            ¢x¢x         C          ¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

 


7. ±N½L 2 ¥Ñ C ·h¦Ü B¡C( C -> B )

            ¢z¢{                        ¢z¢{                      ¢z¢{

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                    ¢z¢w¢r¢r¢w¢{                  ¢x¢x

            ¢x¢x                    ¢x   2    ¢x                  ¢x¢x

          ¢z¢r¢r¢{              ¢z¢w¢r¢w¢w¢w¢w¢r¢w¢{              ¢x¢x

          ¢x 1  ¢x              ¢x       3        ¢x              ¢x¢x

¢z¢w¢w¢w¢w¢r¢w¢w¢r¢w¢w¢w¢w¢{¢z¢w¢r¢w¢w¢w¢w¢w¢w¢w¢w¢r¢w¢{¢z¢w¢w¢w¢w¢r¢r¢w¢w¢w¢w¢{

¢x           A            ¢x¢x           B            ¢x¢x         C          ¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

8. ±N½L 1 ¥Ñ A ·h¦Ü B¡C( A -> B )

            ¢z¢{                        ¢z¢{                      ¢z¢{

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                        ¢x¢x                      ¢x¢x

            ¢x¢x                      ¢z¢r¢r¢{                    ¢x¢x

            ¢x¢x                      ¢x 1  ¢x                    ¢x¢x

            ¢x¢x                    ¢z¢r¢w¢w¢r¢{                  ¢x¢x

            ¢x¢x                    ¢x   2    ¢x                  ¢x¢x

            ¢x¢x                ¢z¢w¢r¢w¢w¢w¢w¢r¢w¢{              ¢x¢x

            ¢x¢x                ¢x       3        ¢x              ¢x¢x

¢z¢w¢w¢w¢w¢w¢r¢r¢w¢w¢w¢w¢w¢{¢z¢w¢r¢w¢w¢w¢w¢w¢w¢w¢w¢r¢w¢{¢z¢w¢w¢w¢w¢r¢r¢w¢w¢w¢w¢{

¢x           A            ¢x¢x           B            ¢x¢x         C          ¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

 

      Æ[¹î¤W­±·h½L¤lªº¹Lµ{¤¤¡A§Ú­ÌÂk¯Ç¥X¤@­Ó»¼°j³B²zªº­ì«h¨Ó¤F¡A¥i¥HÆ[¹î

n = 3 ®Éªº¨BÆJ 2¦Ü 4  ©Mn = 2 ®Éªº¨BÆJ 2¦Ü 4§¹¥þ¬Û¦P¡A¥u¬O¬W¤lªº¦WºÙ¤£¤@¼Ë

½}¤F¡A¦P²z¦b¨BÆJ 6¦Ü 8¤¤¤]¬O©M¨BÆJ 2¦Ü 4¬Û¦P¡A¤]ºû¦³¬W¤l¦WºÙ¤£¤@¼Ë¦Ó¤w

¡A³o­Ó»¼°j³B²zªº¹Lµ{¥i¥H´y­z¦p¤U¡G

 

     1. ·h°Ê n - 1 ­Ó½L¤l, ¥Ñ A¬W, ÂÇµÛ B¬W, ·h¦Ü C¬W¡C

     2. ·h°Ê²Ä n ­Ó½L¤l, ¥Ñ A¬W¨ì B¬W¡C

     3. ·h°Ê n - 1 ­Ó½L¤l, ¥Ñ C¬W, ÂÇµÛ A¬W, ·h¦Ü B¬W¡C

 

®Ú¾Ú³o­Ó»¼°j³B²z¹Lµ{ ,§Ú­Ì´N«Ü®e©ö¥i¥H§¹¦¨¸Ñ¨Mªe¤º¶ð°ÝÃDªºµ{¦¡¤F¡C


int n = 0;

main()

{

  int height;

 

  printf("How many disks are you going to start with? \n");

  scanf("%d", &height);

  move(height, 'A', 'B', 'C');

  printf("\n\nTotal have %d moves\n", n);

}

move(height, from, to, using)

  int height;

  char  from, to, using;

{

  if  (height == 1) {

      printf("Move a disk from %c to %c\n", from, to);

      n++;

  }

  else {

        move(height-1, from, using, to);

        printf("Move a disk from %c to %c\n", from, to);

        n++;

        move(height-1, using, to, from);

  }

}

 

¿é¥Xµ²ªG

 

How many disks are you going to start with?

Move a disk from A to B

Move a disk from A to C

Move a disk from B to C

Move a disk from A to B

Move a disk from C to A

Move a disk from C to B

Move a disk from A to B

 

 

Total have 7 moves


  ®ðªw±Æ§Çªk

 

  bubble (a,n)                          order (p,q)

  int    a[], n;                        int  *p,*q;

  {                                     {

     int i , j ;                        int temp;

 

     for ( i=0 ; i < n -1 ;++i)             if  (*p>*q) {

       for ( j= n-1 ; i<j ;--j)                 temp = *p;

          order (&a[j-1],&a[j]);                *p = *q ;

  }                                             *q = temp;

                                            }

                                        }

 

  °²³]§Ú­Ì«Å§i

 

       static int a[] = { 7,3,66,3,-5,22,-77,2};

 

  µM«á©I¥s bubble ( a,8 ) ¡C¤U¦C  ¥Ü¨C¦¸¥~  ´¿°j°é°õ¦æ¤§«á,a[]°}¦C¤¸¯À

  ªº±Æ¦C±¡§Î :

 

      ¥¼±Æ¦Cªº¸ê®Æ :   7   3  66   3  -5  22  -77   2

 

            ²Ä¤@¦¸ : -77   7   3  66   3  -5   22   2

 

            ²Ä¤G¦¸ : -77  -5   7   3  66   3    2  22

 

            ²Ä¤T¦¸ : -77  -5   2   7   3  66    3  22

 

            ²Ä¥|¦¸ : -77  -5   2   3   7   3   66  22

 

            ²Ä¤­¦¸ : -77  -5   2   3   3   7   22  66

 

            ²Ä¤»¦¸ : -77  -5   2   3   3   7   22  66

 

            ²Ä¤C¦¸ : -77  -5   2   3   3   7   22  66

 


§Ö³t±Æ§Çªk ( »¼°jªk )

¤@¡B¤ÀªR :

 

    ©Ò¿×§Ö³t±Æ§Çªk¡A¬O§Q¥Î°Ï¶¡¹º¤ÀªºÆ[©À¨Ó§¹¦¨¡C¦Ó©Ò¿×¹º¤À«h¬O¥ý¦b

¦¹°Ï¶¡ªº¥ª¥k¨âºÝ³]©w¨â­Óµù¼Ð¡A¨Ã¦b¨ä¤¤¿ï¾Ü¤@­ÓÁä­È¡A¦¹Áä­È¥iÀH¾÷¿ï

¾Ü¡A¦b¦¹§Ú­Ì¥H¤¤¶¡¤¸¯À·íÁä­È¡AÁä­È x = a [(l+r)/2] ,¦¹ l ©M r ¬°°}¦C

ªº¥ªºÝ¤Î¥kºÝ¡C

 

      °²³] l = 1,r = 8 , ¨Ã¥H 48 ¬°Áä­È¡A¦Ò¼{¥H¤U¨Ò¤l¡G

 

          79  -10    3   48   90    7   42   11

           i              x                   j

 

  µM«á i¥Ñ¥ª¦V¥kÀˬd¡A¦Ó j¥Ñ¥k¦V¥ªÀˬd¡Aª½¨ì§ä¥X¤ñ x¤j¡]¤p¡^ªº¸ê®Æ¡A­Y

  i <= j¡A«h¤¬´« i¤Î j¦ì¸mªº¸ê®Æ¡A¨Ã ±N i ¥[ 1¡Aj ´î 1¡C

 

          79  -10    3   48   90    7   42   11

           i              x                   j

 

          11  -10    3   48   90    7   42   79

                          i              j

 

          11  -10    3   42   90    7   48   79

                          i              j

 

          11  -10    3   42   48    7   90   79

                               i    j

     

                                 ¢z¢w¢{

          11  -10    3   42    7 ¢x48¢x 90   79

          i1                  j1 ¢x x¢x i2   j2

                                 ¢|¢w¢}                            

 

  ¨ì¦¹¥i¬Ý¥X¡A¤ñÁä­È 48 ¤pªº¼Æ³£³Q©ñ¨ì 48 ªº¥ªÃä¡A¦Ó 48 ¤ñ¤jªº«h¦b¨ä

¥kÃä¡A¦¹®É­«·s¹ï·sªº¨â­Ó°Ï¶¡¡] 11 - 7 ¥H¤Î 90 - 78¡^°µ±Æ§Ç¡A³Ì«á¾ã­Ó

¦C³£±N±Æ§Ç§¹¦¨¡C


§Ö³t±Æ§Çªk (»¼°jªk)

 

 1  quicksort(left, right, s)

 2  int left, right, s[];

 3  {

 4    int i, j, x, k,temp;

 5

 6    i = left;

 7    j = right;

 8    x = s[(left+right)/2];

 9    do {

10       while (s[i] < x)

11           i++;

12       while (s[j] > x)

13           j--;

14       if  (i <= j) {

15           if  (s[i] != s[j]) {

16           }

17               temp = s[j];

18               s[j] = s[i];

19               s[i] = temp;

20           }

21           i++;

22           j--;

23       }

24

25    } while (i <= j)

26          ;

27    if (left <= j)

28       quicksort(left, j, s);

29    if (i < right)

30       quicksort(i, right, s);

31  }

 


¿é¥Xµ²ªG

 

There are 8 intergers to sort.

 

    7     3    66     3    -5    22   -77     2

 

    2     3    66     3    -5    22   -77     7

    2   -77    66     3    -5    22     3     7

    2   -77    -5     3    66    22     3     7

    2   -77    -5     3    66    22     3     7

  -77     2    -5     3    66    22     3     7

  -77     2    -5     3    66    22     3     7

  -77    -5     2     3    66    22     3     7

  -77    -5     2     3    66    22     3     7

  -77    -5     2     3     7    22     3    66

  -77    -5     2     3     7     3    22    66

  -77    -5     2     3     3     7    22    66

  -77    -5     2     3     3     7    22    66

  -77    -5     2     3     3     7    22    66

 

 

 ... Quick Sort ...

 

  -77    -5     2     3     3     7    22    66

 

 


 1  quicksort(left, right, s)

 2  int left, right, s[];

 3  {

 4    int i, j, x, k,temp;

 5

 6    i = left;

 7    j = right;

 8    x = s[(left+right)/2];

 9    do {

10       while (s[i] < x)

11           i++;

12       while (s[j] > x)

13           j--;

14       if  (i <= j) {

15           if  (s[i] != s[j]) {

16           }

17               temp = s[j];

18               s[j] = s[i];

19               s[i] = temp;

20           for (k = left; k <= right; k++) {

21               printf("%5d%c", s[k],((k%10 == 9) ? '\n' : ' '));

22           }

23           printf("\n");

24           i++;

25           j--;

26       }

27

28    } while (i <= j)

29          ;

30    if (left <= j)

31       quicksort(left, j, s);

32    if (i < right)

33       quicksort(i, right, s);

34  }

 


 

¿é¥Xµ²ªG

 

There are 8 intergers to sort.

 

    7     4    66     3     5    22    77     2

 

    2     4    66     3     5    22    77     7

    2     3    66     4     5    22    77     7

    2     3

    5     4    66    22    77     7

    4     5

    4

    7    22    77    66

    7    22    77    66

    7

   66    77

   66

 

 

 ... Quick Sort ...

 

    2     3     4     5     7    22    66    77

  

 


                 fputc , fgetc , fprintc , fscanf

 

      ³o¥|­Ó¨ç¼Æ¬O°w¹ïÀɮק@¾Þ§@¡A¬G­n¥ý«Å§i

     

              FILE *data, *out ,*append;

                      ¢x

                      ¢|¢w¢w¢w>¦WºÙ¥i¦Û©w

              ¥B°õ¦æ

 

              data = fopen ( "filename" , " r ");

                                           ¢x

                                           ¢|¢w¢w¢w>  ¥u¥iŪ

               out = fopen ( "filename" , " w ");

                                           ¢x

                                           ¢|¢w¢w¢w>  ¥u¥i¼g

            append = fopen ( "filename" , " a ");

                                           ¢x

                                           ¢|¢w¢w¢w>  ¥i¼g,¥iŪ

  ªí¥Ü data ¬O¿é¤JÀɤ§Àɮ׫ü¼Ð¡A out ¬O¿é¥XÀɤ§Àɮ׫ü¼Ð¡]ÀɦW¦Û­q¡^

¡C¥H«á´N°w¹ï³o¨Ç«ü¼Ð§@¾Þ§@¡A¦Ó¦bµ{¦¡µ²§ô¥H«e¡A°O±o­n

 

              fclose ( data );

              fclose ( out );

 

  ¥H±NÀÉ®×Ãö±¼¡A¥H§K¸ê®Æ¬y¥¢¡C ®æ¦¡¦p¤U¡G

 

  fgetc ( data ) :

 

      ±q data Àɤ¤Åª¨ú¤@¦r¤¸¶Ç¦^¡C

 

  fputc ( c, out) :

 

      ±N¦r¤¸ c ¿é¥X¦ÜÀÉ®× out¡C

 

  fscanf (data, format_string [ ,argument......])  :

 

      ±q data Àɨ̮榡¤Æ¿é¤J ¡C

 

  fprintf (out , format_string [ ,argument......])  :

 

      ¨Ì®æ¦¡¥~¿é¥X¦ÜÀÉ®× out¤¤¡C

 


«ü¼Ð

(pointer)

 

        «ü¼Ð(pointer)¦b C »y¨¥¬O·¥¨ã«Â¤Oªº¡A¥i¥H²`¤Î°O¾ÐÅ骺¦ì§}(memory addres)

¡A¤]¥i¥H°µ¬°°ÊºA°O¾ÐÅéºÞ²z(dynamic memory management)¦³®Ä§Q¾¹¡CÅܼƦb°O¾Ð

Åé¬Ò¦³¤@¦ì§}¡A´N¹³¨C­Ó®a®x¦³¦a§}¤@¯ë¡A¦p¤U¹Ï©Ò¥Ü¡C

 

 

 ÅܼƠ     ¦ì§}

 ¢w¢w      ¢w¢w

 

                 ¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢{

count       0    ¢x                            ¢x¢{

                 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t¢x

            1    ¢x                            ¢x¢x

                 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t¢x

 sum        2    ¢x                            ¢x¢x

                 ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t¢u  °O¾ÐÅé²Ó­M

            3    ¢x                            ¢x¢x  (memory cell)

            .    ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}¢x

            .                .                   ¢x

            .                .                   ¢x

                 ¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢{¢x

          65535  ¢x                            ¢x¢}

                 ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}      

 

 

 

          count ¦b²Ä 0 ¸¹ªº¦ì§}, sum ¦b²Ä 2 ¸¹¦ì§}............¡C

 

 

 

 

 


¤@¡B«ü¼Ðªí¥Üªk

 

¦p¦ó«Å§i«ü¼Ð©O¡H¤@¯ë§Î¦¡¬°¡G

 

                    ¢z¢w¢w¢w¢w¢w¢w¢w¢w¢{      type ¡G ¸ê®Æ«¬ºA

                    ¢x type     *name ¢x

                     ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢}      name ¡G ÅܼƦWºÙ

 

 

    ¦p int *ptr ³o¼Ë«Å§i«á¨ã¦³¤G­Ó·N¸q¡G

(¤@)ptr¬°¤@«ü¼ÐÅܼơA¦¹ÅܼƦb°O¾ÐÅ馳¦Û¤vªº¦ì§}(¥i¥Î &ptr ¦L¥X)¡C

(¤G)ptrÅܼƩҦs©ñªº¼Æ­È¬O¬Y¤@ÅܼƦb°O¾ÐÅ骺§}¡C

 


 

 

#define print(x) printf("x = %d, *x = %d, &x = %d\n",x, *x, &x)

main()

{

    static int num[] = {1,2,3};

    int *ptr1, *ptr2;

 

    ptr1 = num;

    ptr2 = &num[2];

    print(ptr1);

    ptr1++;

    print(ptr1);

    print(ptr2);

    ptr2++;

    print(ptr2);

    printf("ptr2 - ptr1 = %d\n", ptr2 - ptr1);

}

 

¿é¥Xµ²ªG

 

ptr1 = 268436528, *ptr1 = 1, &ptr1 = 2147466868

ptr1 = 268436532, *ptr1 = 2, &ptr1 = 2147466868

ptr2 = 268436536, *ptr2 = 3, &ptr2 = 2147466864

ptr2 = 268436540, *ptr2 = ?, &ptr2 = 2147466864

ptr2 - ptr1 = 2

 


     ÅܼƠ   ¦ì  §}        ¤º®e        ÅܼƠ   ¦ì  §}        ¤º®e

     ¢w¢w    ¢w¢w¢w        ¢w¢w        ¢w¢w    ¢w¢w¢w        ¢w¢w

                      ¢z¢w¢w¢w¢w¢w¢{                     ¢z¢w¢w¢w¢w¢w¢{

     ptr1  2147466868 ¢x 268436528¢x   ptr2  2147466864¢z¢t268436532 ¢x

                      ¢|¢w¢w¢s¢w¢w¢}                   ¢x¢|¢w¢w¢w¢w¢w¢}

                            ¢x ptr1=num                ¢x ptr2=&num[2]

                       <¢w¢w¢}                    ¢z<¢w¢}

                      ¢x                          ¢x

                      ¢x

                      ¢|>  268436528 268436532 268436536 268436540

                         ¢z¢w¢w¢w¢w¢s¢w¢w¢w¢w¢s¢w¢w¢w¢w¢s¢w¢w¢w¢w¢{

                         ¢x   1    ¢x    2   ¢x   3    ¢x    ?   ¢x

                         ¢|¢w¢w¢w¢w¢r¢w¢w¢w¢w¢r¢w¢w¢w¢w¢r¢w¢w¢w¢w¢}    

                            &num[0]   &num[1]   &num[2]

                             num       num+1     num+2

 

 

¥»¨Óptr1ÅܼƩҦsªº¼Æ­È¬O268436528¡A¦¹268436528¬Onum°}¦C²Ä1­Ó¤¸¯À¡A¦b

°O¾ÐÅ骺¦ì§}¡A·í°õ¦æptr1++«áptr1«ü¦V268436532¦ì§}¡Cptr2«ü¦V268436536ªº

¦ì§}¡A­Y¦A¦V«á°lÂÜ¡A«h«ü¦V268436540ªº¦ì§}¦Ó268436540¦ì§}©Ò¦sªº­È¬O¦h

¤Ö´N¤£±o¦Óª¾¡A¦]¬°num°}¦C¥u¦³3­Ó¤¸ ¯À¡C½Ðª`·N¡A&ptr1©M&ptr2©Ò¦b°O¾ÐÅé

¦ì¸m¥Ã»·¦b2147466868©M2147466864¡C

 

ptr2 - ptr1ªí¥Ü¨âªÌ¤§¶¡¶ZÂ÷¡A¥i¥H³o¼Ë·Q¡A·í°õ¦æ«á¡Aptr2¬Û·í©ónum+3¡A¦Ó

ptr1¬Û·í©ónum+1¡A©Ò¥Hnum+3 - (num+1)=2¡C

 

¥²»Ýª¾¹DÁöµM°}¦C¤]¨ã¦³«ü¼Ð¤§¥\¯à¡A¦ý¤£¯à¥Î«e¸m¥[¡A´î©Î«á¸m¥[¡A´î¨Ó¹B

ºâ¦p¨ÒÃD­Y¼gnum++¨º¬O¿ù»~ªº¼gªk¦Ó¥²»Ý¼g¦¨num+1¡A¦Ó«ü¼ÐÅܼƥi¥H¥Î«e¸m

©Î«á¸m¤§¥[,´î¦pptr++¡A©Îptr+1¬Ò¥i¡A³o¬O°}¦CÅܼƻP«ü¼ÐÅܼƳ̤j¤§¤£¦P¡C


¤G¡B°}¦Cªí¥Üªk

 

(A) ¤@ºû°}¦C(one dimension array)

 

¤@ºû°}¦Cªº«Å§i¤èªk«Ü²³æ¦p int number[5]¡A·N«ä¬Onumber¬°¤@ºû°}¦C¡A¦³5­Ó

¤¸¯À¡A¨C¤@¤¸¯À¬O¾ã¼Æ¡A½Ðª`·Nªº¬O°}¦Cªºµù¼Ð(subscript)¬O±q0¶}©l¡C©Ò¥H¦³

number [0]¡Anumber[1]¡Anumber[2]¡A number[3]¡A number[4]µ¥5­Ó¤¸¯À¡C

 

                   ¢z¢w¢w¢w¢w¢w¢{

          number[0]¢x    4     ¢x

                   ¢u¢w¢w¢w¢w¢w¢t

          number[1]¢x    7     ¢x

                   ¢u¢w¢w¢w¢w¢w¢t

          number[2]¢x    5     ¢x

                   ¢u¢w¢w¢w¢w¢w¢t

          number[3]¢x    1     ¢x

                   ¢u¢w¢w¢w¢w¢w¢t

          number[4]¢x    3     ¢x

                   ¢|¢w¢w¢w¢w¢w¢}

(B) ¤Gºû°}¦C(two dimension array)

                   0     1     2     3

               ¢z¢w¢w¢s¢w¢w¢s¢w¢w¢s¢w¢w¢{

             0 ¢x    ¢x    ¢x    ¢x    ¢x

               ¢u¢w¢w¢q¢w¢w¢q¢w¢w¢q¢w¢w¢t

             1 ¢x    ¢x¢x  ¢x    ¢x    ¢x

               ¢u¢w¢w¢q¢q¢w¢q¢w¢w¢q¢w¢w¢t

             2 ¢x    ¢x¢x  ¢x    ¢x¢x  ¢x

               ¢|¢w¢w¢r¢q¢w¢r¢w¢w¢r¢q¢w¢}

                       ¢x          ¢x

                matrix[1][1]    matrix[2][3]

 

¤Gºû°}¦C»P¤@ºû°}¦Cªº«Å§i¬Û¦ü¥u¬O¦h¤F¤@­Ó¤¤¬A©·¡C¦pªG°}¦C¦³°_©l­È«h¥²

»Ý¬OstaticÅܼƤ~¥i¡C²Ä¤»¦æ¬O³]©wªì­È¡A¥ç¥i¥H

static int matrix[3][4]={12,8,5,10,23,7,17,9,45,16,8,2}ªí¥Ü¡C

°²­Y static int matrix[3][4] = {{12,8,5},{23,7,17},{45,16,8,2}}«hªí¥Ü

 matrix[0][3] ¤Î matrix[1][3] ¤§­È¬° 0 ¡C

 


#define constant_value 2

main()

{

  int row,column;

  static int matrix[3][4] = {

        {12,  8,  5, 10},

        {23,  7, 17,  9},

        {45, 16,  8,  2},

                            };

 

  printf("The original matrix[3][4] is:\n");

  for (row = 0; row < 3; row++) {

      for (column = 0; column < 4; column++)

           printf("%4d",matrix[row][column]);

      printf("\n");

  };

 

  printf("The original matrix[3][4] * %d is:\n", constant_value);

  for (row = 0; row < 3; row++) {

      for (column = 0; column < 4; column++)

           printf("%4d",matrix[row][column] * constant_value);

      printf("\n");

  };

}

 

¿é¥Xµ²ªG

 

The original matrix[3][4] is:

  12   8   5  10

  23   7  17   9

  45  16   8   2

The original matrix[3][4] * 2 is:

  24  16  10  20

  46  14  34  18

  90  32  16   4


 

     

¤T. °}¦C»P«ü¼Ð    

 

main()

{

  int a, b, *ptr;

 

  a = 100;

  ptr = &a;

  b = *ptr;

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

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

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

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

}

 

¿é¥Xµ²ªG

 

&ptr = 2147466860

 

ptr  = 2147466868

&a   = 2147466868    a = 100

&b   = 2147466864    b = 100


¦b¨ÒÃD¤¤«Å§iptr¬°¤@«ü¼ÐÅܼƨåB«ü©waÅܼƩҦb°O¾ÐÅé¦a§}µ¹ptr¡AµM«á§âaªº

­È«ü©wµ¹b¡C

 

   ÅܼƠ   ¦ì  §}    ¤º®e             ÅܼƠ  ¦ì  §}    ¤º®e

   ¢w¢w    ¢w¢w¢w    ¢w¢w             ¢w¢w   ¢w¢w¢w    ¢w¢w

                    ¢z¢w¢w¢w¢w¢w¢{                    ¢z¢w¢w¢{

   ptr  2147466860  ¢x2747466868¢u¢w>  a   2147466868 ¢x100 ¢x

                    ¢|¢w¢w¢w¢w¢w¢}(1)                 ¢|¢s¢w¢}

                                                        ¢x

                                                        ¢x(2)

                                                        ¢x

                                                      ¢z¢r¢w¢{

      (1) : ptr = &a                   b   2147466864 ¢x100 ¢x

                                                      ¢|¢w¢w¢}    

      (2) : b = *ptr

 

 

±q¤W­±¨ÒÃD¡A¥i¥Hª¾¹D¡®¬Oªí¥Ü¤@¦ì¸m(address)¡A¦Ó¡¯¬°¨äÅܼƭÈ(value)¡C¦p¡®a

¡A¡®b¦Uªí¥ÜaÅܼơAbÅܼƩҦb°O¾ÐÅ骺¦ì§}¡Cptr=¡®a¡A·N«ä¬O±NaÅܼƪº¦ì§}

«ü©wµ¹ptr¡A*ptr´N¬Û·í©óa¤§­È¡A©Ò¥Hb=*ptr ¤D§âa¤§­È«ü©wµ¹b¡C


main()

{

  static int a[10] = {0,1,2,3,4,5,6,7,8,9};

  int *ptr, i;

 

  ptr = a;

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

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

      printf("ptr+%d=%d  a+%d=%d  &a[%d]=%d ",i,ptr+i,i,a+i,i,&a[i]);

      printf(" *(ptr+%d)=%d  a[%d]=%d\n",i,*(ptr+i),i,a[i]);

  }

}

 

¿é¥Xµ²ªG

 

&ptr = 2147466868

 

ptr+0=268436528  a+0=268436528  &a[0]=268436528  *(ptr+0)=0  a[0]=0

ptr+1=268436532  a+1=268436532  &a[1]=268436532  *(ptr+1)=1  a[1]=1

ptr+2=268436536  a+2=268436536  &a[2]=268436536  *(ptr+2)=2  a[2]=2

ptr+3=268436540  a+3=268436540  &a[3]=268436540  *(ptr+3)=3  a[3]=3

ptr+4=268436544  a+4=268436544  &a[4]=268436544  *(ptr+4)=4  a[4]=4

ptr+5=268436548  a+5=268436548  &a[5]=268436548  *(ptr+5)=5  a[5]=5

ptr+6=268436552  a+6=268436552  &a[6]=268436552  *(ptr+6)=6  a[6]=6

ptr+7=268436556  a+7=268436556  &a[7]=268436556  *(ptr+7)=7  a[7]=7

ptr+8=268436560  a+8=268436560  &a[8]=268436560  *(ptr+8)=8  a[8]=8

ptr+9=268436564  a+9=268436564  &a[9]=268436564  *(ptr+9)=9  a[9]=9


³]©wa¤§ªì­È¨Ã«Å§i¬°¤@ºû°}¦C¦³¤Q­Ó¤¸¯À¡Aª`·N°}¦C¦WºÙa¥Nªí²Ä¤@­Ó¤¸¯Àªº¦ì

§}©M&a[0]·N«ä¤@¼Ë¡C¨ÒÃDªí¥Ü°}¦C¤¸¯À»P«ü¼Ð¤§Ãö«Y¡A§Æ±æ°È¥²­n¤F¸Ñ¡C¿é¥X

µ²ªG¥H¤U¹Ïªí¥Ü¤§¡G

 

     ÅܼƠ   ¦ì  §}      ¤º®e

     ¢w¢w    ¢w¢w¢w      ¢w¢w

                     ¢z¢w¢w¢w¢w¢w¢{

     ptr  2147466868 ¢x268436528 ¢x

                     ¢|¢w¢w¢s¢w¢w¢}

                           ¢x

                           ¢x

  ¢z<¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

  ¢|>268436528               268436540

             268436532                 ....

                     268436536                             268436564

    ¢z¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢{

    ¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x

    ¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x

    ¢|¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢}

     ptr     ptr+1   ptr+2   ptr+3   ptr+4       ...       ptr+9

     a       a+1     a+2     a+3     a+4         ...       a+9

     &a[0]   &a[1]   &a[2]   &a[3]   &a[4]       ...       &a[9]    

 

 

*ptr=a[0], *(ptr+1)=a[1], *(ptr+2)=a[2],... *(ptr+9)=a[9]¥Ñ©ó int¦û 4 byte¡A©Ò¥H

&a[0] ¤Î &a[1]¬Û®t 4¡A¥H¦¹Ãþ±À¡C


main()

{

  int i,j;

  static int a[3][3] = {

     {1,2,3},

     {4,5,6},

     {7,8,9}};

  int *ptr;

 

  ptr =  &a[0][0];

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

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

      printf("ptr + %d = %d   *(ptr + %d) = %d\n",i,ptr+i,i,*(ptr+i));

 

  printf("\n");

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

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

           printf("&a[%d][%d] = %d a[%d][%d] = %d\n",i,j,&a[i][j],i,j,a[i][j]);

 

  printf("\n");

 

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

      printf("a[%d] = %d    a + %d = %d\n",i,a[i],i,a+i);

}


 

¿é¥Xµ²ªG

 

&ptr = 2147466860

 

ptr + 0 = 268436528   *(ptr + 0) = 1

ptr + 1 = 268436532   *(ptr + 1) = 2

ptr + 2 = 268436536   *(ptr + 2) = 3

ptr + 3 = 268436540   *(ptr + 3) = 4

ptr + 4 = 268436544   *(ptr + 4) = 5

ptr + 5 = 268436548   *(ptr + 5) = 6

ptr + 6 = 268436552   *(ptr + 6) = 7

ptr + 7 = 268436556   *(ptr + 7) = 8

ptr + 8 = 268436560   *(ptr + 8) = 9

 

&a[0][0] = 268436528 a[0][0] = 1

&a[0][1] = 268436532 a[0][1] = 2

&a[0][2] = 268436536 a[0][2] = 3

&a[1][0] = 268436540 a[1][0] = 4

&a[1][1] = 268436544 a[1][1] = 5

&a[1][2] = 268436548 a[1][2] = 6

&a[2][0] = 268436552 a[2][0] = 7

&a[2][1] = 268436556 a[2][1] = 8

&a[2][2] = 268436560 a[2][2] = 9

 

a[0] = 268436528    a + 0 = 268436528

a[1] = 268436540    a + 1 = 268436540

a[2] = 268436552    a + 2 = 268436552

 

 


     ÅܼƠ   ¦ì  §}       ¤º®e

     ¢w¢w    ¢w¢w¢w       ¢w¢w

                      ¢z¢w¢w¢w¢w¢w¢{

     ptr  2147466860  ¢x268436528 ¢x

                      ¢|¢w¢w¢s¢w¢w¢}

                            ¢x ptr=&a[0][0]

¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

¢x

¢|>268436528               268436540

             268436532                 ....

                     268436536                             268436560

  ¢z¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢s¢{

  ¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x

  ¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x¢x

  ¢|¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢r¢}

  ptr      ptr+1   ptr+2   ptr+3   ptr+4   ptr+5   prt+6 ...   ptr+8

  &a[0][0] &a[0][1]        &a[1][0]        &a[1][2]      ...   &a[2][2]

  a[0]                     a[1]                    a[2]

  a                        a+1                     a+2

 

¨ÒÃD¬O¤Gºû°}¦C»P«ü¼ÐÃö«Y ,&a[0][0]¡Ba ©Ma[0]³£«ü¦V¬Û¦Pªº¦ì§}¡A¦P¼Ë&a[1][0]

¡Ba+1 ©M a[1]¡B&a[2][0]¡Ba+2 ©Ma[2]¤]«ü¦V¬Û¦Pªº¦ì§}¡C

¯u¥¿ÅܼƭÈa[0][0]=1¡Aa[0][1]=2¡A...a[2][2]=9¡C¦]¬° a °}¦C¬°¾ã¼Æ«¬ºA¬G¬Û¾F¤¸

¯À©Ò¦bªº¦ì§}µó¬Û®t4¡C


 

 

main()

{

  int i,j;

  static int a[3][3] = {

     {1,2,3},

     {4,5,6},

     {7,8,9}};

 

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

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

           printf("&a[%d][%d] = %d \n", i,j,&a[i][j]);

 

  printf("\n");

 

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

      printf("a + %d = %d\n",i,a+i);

}

 

 

¿é¥Xµ²ªG

 

&a[0][0] = 268436528

&a[0][1] = 268436532

&a[0][2] = 268436536

&a[1][0] = 268436540

&a[1][1] = 268436544

&a[1][2] = 268436548

&a[2][0] = 268436552

&a[2][1] = 268436556

&a[2][2] = 268436560

 

a + 0 = 268436528

a + 1 = 268436540

a + 2 = 268436552

 


main()

{

  int i,j,k;

  static int a[2][3][4] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24 };

 

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

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

           for (k = 0; k < 4; k++)

           printf("&a[%d][%d][%d] = %d \n", i,j,k,&a[i][j][k]);

 

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

      printf("a + %d = %d\n",i,a+i);

 

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

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

           printf("a[%d] + %d = %d\n",i,j,a[i]+j);

 

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

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

           for (k = 0; k < 4; k++)

                printf("a[%d][%d]+%d = %d\n",i,j,k,a[i][j]+k);

 

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

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

           for (k = 0; k < 4; k++)

                printf("a[%d][%d][%d]+%d = %d\n",i,j,k,k,a[i][j][k]+k);

}

 


¿é¥Xµ²ªG

 

&a[0][0][0] = 268436528 a + 0 = 268436528     a[0][0]+0 = 268436528

&a[0][0][1] = 268436532 a + 1 = 268436576     a[0][0]+1 = 268436532

&a[0][0][2] = 268436536 a[0] + 0 = 268436528  a[0][0]+2 = 268436536

&a[0][0][3] = 268436540 a[0] + 1 = 268436544  a[0][0]+3 = 268436540

&a[0][1][0] = 268436544 a[0] + 2 = 268436560  a[0][1]+0 = 268436544

&a[0][1][1] = 268436548 a[1] + 0 = 268436576  a[0][1]+1 = 268436548

&a[0][1][2] = 268436552 a[1] + 1 = 268436592  a[0][1]+2 = 268436552

&a[0][1][3] = 268436556 a[1] + 2 = 268436608  a[0][1]+3 = 268436556

&a[0][2][0] = 268436560                       a[0][2]+0 = 268436560

&a[0][2][1] = 268436564                       a[0][2]+1 = 268436564

&a[0][2][2] = 268436568                       a[0][2]+2 = 268436568

&a[0][2][3] = 268436572                       a[0][2]+3 = 268436572

&a[1][0][0] = 268436576                       a[1][0]+0 = 268436576

&a[1][0][1] = 268436580                       a[1][0]+1 = 268436580

&a[1][0][2] = 268436584                       a[1][0]+2 = 268436584

&a[1][0][3] = 268436588                       a[1][0]+3 = 268436588

&a[1][1][0] = 268436592                       a[1][1]+0 = 268436592

&a[1][1][1] = 268436596                       a[1][1]+1 = 268436596

&a[1][1][2] = 268436600                       a[1][1]+2 = 268436600

&a[1][1][3] = 268436604                       a[1][1]+3 = 268436604

&a[1][2][0] = 268436608                       a[1][2]+0 = 268436608

&a[1][2][1] = 268436612                       a[1][2]+1 = 268436612

&a[1][2][2] = 268436616                       a[1][2]+2 = 268436616

&a[1][2][3] = 268436620                       a[1][2]+3 = 268436620

                                              a[0][0][0]+0 = 1

                                              a[0][0][1]+1 = 3

                                              a[0][0][2]+2 = 5

                                              a[0][0][3]+3 = 7

                                              a[0][1][0]+0 = 5

                                              a[0][1][1]+1 = 7

                                              a[0][1][2]+2 = 9

                                              a[0][1][3]+3 = 11

                                              a[0][2][0]+0 = 9

                                              a[0][2][1]+1 = 11

                                              a[0][2][2]+2 = 13

                                              a[0][2][3]+3 = 15

                                              a[1][0][0]+0 = 13

                                              a[1][0][1]+1 = 15

                                              a[1][0][2]+2 = 17

                                              a[1][0][3]+3 = 19

                         a[1][2][0]+0 = 21    a[1][1][0]+0 = 17

                         a[1][2][1]+1 = 23    a[1][1][1]+1 = 19

                         a[1][2][2]+2 = 25    a[1][1][2]+2 = 21

                         a[1][2][3]+3 = 27    a[1][1][3]+3 = 23


/* ¦r¤¸³B²z¡G§ïÅܤ@¦C¤å¦r¡A±N¦r¦ê¤¤ªºe§ï¦¨E¡A¨Ã±N¨C­ÓªÅ®æ§ï¦¨¤@­Ó·s¦C

    ¦r¤¸©M©w¦ì¦r¤¸ */

          #define      MAXLINE       100

          #include  <stdio.h>

          main()

          {

             char line[MAXLINE], *change();

             printf ("\nWhat is your favorite line? ");

             read_in(line);

             printf ("\n%s\n\n%s\n\n",

                     "Here it is after being changed :",change(line));

          }

          read_in(s)

          char s[];

          {

             int   c,i=0;

             while ((c = getchar()) != EOF && c != '\n')

                  s[i++] = c ;

             s[i] =  '\0';

          }

          char  *change(s)

          char   *s;

          {

              static char   new_string[MAXLINE];

              char           *p=new_string;

              *p++ = '\t';

              for ( ; *s != '\0' ; ++s)

                  if (*s == 'e')

                    *p++ = 'E' ;

                  else if (*s == ' ') {

                          *p++ = '\n' ;

                          *p++ = '\t' ;

                       }

                       else

                          *p++ = *s ;

              *p = '\0';

              return ( new_string );

          }

 

°²³]¿é¤J : she sells sea shells

 

¿é¥Xµ²ªG

                shE

                sElls

                sEa

                shElls


main()

{

  char *p, *q;

 

  p = q = " RICH";

  printf("%s%s%s\n", p,p,p);

  *++q = 'p';

  *++q = 'o';

  *++q = 'o';

  *++q = 'r';

  printf("%s%s%s\n", p,p,p);

}

 

¿é¥Xµ²ªG

 

 RICH RICH RICH

 poor poor poor


                                                              ascii(7)

 

     | 00 nul| 01 soh| 02 stx| 03 etx| 04 eot| 05 enq| 06 ack| 07 bel|

     | 08 bs | 09 ht | 0a nl | 0b vt | 0c np | 0d cr | 0e so | 0f si |

     | 10 dle| 11 dc1| 12 dc2| 13 dc3| 14 dc4| 15 nak| 16 syn| 17 etb|

     | 18 can| 19 em | 1a sub| 1b esc| 1c fs | 1d gs | 1e rs | 1f us |

     | 20 sp | 21  ! | 22  " | 23  # | 24  $ | 25  % | 26  & | 27  ' |

     | 28  ( | 29  ) | 2a  * | 2b  + | 2c  , | 2d  - | 2e  . | 2f  / |

     | 30  0 | 31  1 | 32  2 | 33  3 | 34  4 | 35  5 | 36  6 | 37  7 |

     | 38  8 | 39  9 | 3a  : | 3b  ; | 3c  < | 3d  = | 3e  > | 3f  ? |

     | 40  @ | 41  A | 42  B | 43  C | 44  D | 45  E | 46  F | 47  G |

     | 48  H | 49  I | 4a  J | 4b  K | 4c  L | 4d  M | 4e  N | 4f  O |

     | 50  P | 51  Q | 52  R | 53  S | 54  T | 55  U | 56  V | 57  W |

     | 58  X | 59  Y | 5a  Z | 5b  [ | 5c  \ | 5d  ] | 5e  ^ | 5f  _ |

     | 60  ` | 61  a | 62  b | 63  c | 64  d | 65  e | 66  f | 67  g |

     | 68  h | 69  i | 6a  j | 6b  k | 6c  l | 6d  m | 6e  n | 6f  o |

     | 70  p | 71  q | 72  r | 73  s | 74  t | 75  u | 76  v | 77  w |

     | 78  x | 79  y | 7a  z | 7b  { | 7c  | | 7d  } | 7e    | 7f del|


main()

{

  int i;

  static char *p[6]={"abc","def","hi","jklmo","pqrstuvw","xyz"};

 

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

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

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

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

      printf("p[%d] = %d\n", i,p[i]);

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

      printf("*p[%d] = %d\n", i,*p[i]);

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

      printf("&(*p[%d]) = %d\n", i,&(*p[i]));

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

      printf("&(p[%d]) = %d\n", i,&p[i]);

}

¿é¥Xµ²ªG

p = 268436528

*p = 268440000

**p = 97

 

p[0] = 268440000

p[1] = 268440004

p[2] = 268440008

p[3] = 268440012

p[4] = 268436552

p[5] = 268440020

 

*p[0] = 97

*p[1] = 100

*p[2] = 104

*p[3] = 106

*p[4] = 112

*p[5] = 120

 

&(*p[0]) = 268440000

&(*p[1]) = 268440004

&(*p[2]) = 268440008

&(*p[3]) = 268440012

&(*p[4]) = 268436552

&(*p[5]) = 268440020

&(p[0]) = 268436528

&(p[1]) = 268436532

&(p[2]) = 268436536

&(p[3]) = 268436540

&(p[4]) = 268436544

&(p[5]) = 268436548


main()

{

  int i;

  static char *p[6]={"abc","def","hi","jklmo","pqrstuvw","xyz"};

 

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

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

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

 

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

      printf("p[%d] = %d\n", i,p[i]);

  printf("\n");

 

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

      printf("p[0]+%d = %d\n",i, p[0]+i);

      printf("*(p[0]+%d) = %d\n", i,*(p[0]+i));

  }

  printf("\n");

 

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

      printf("p[1]+%d = %d\n",i, p[1]+i);

      printf("*(p[1]+%d) = %d\n", i,*(p[1]+i));

  }

  printf("\n");

 

  for (i = 0; i < 2; i++) {

      printf("p[2]+%d = %d\n",i, p[2]+i);

      printf("*(p[2]+%d) = %d\n", i,*(p[2]+i));

  }

  printf("\n");

 

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

      printf("p[3]+%d = %d\n",i, p[3]+i);

      printf("*(p[3]+%d) = %d\n", i,*(p[3]+i));

  }

  printf("\n");

 

  for (i = 0; i < 8; i++) {

      printf("p[4]+%d = %d\n",i, p[4]+i);

      printf("*(p[4]+%d) = %d\n", i,*(p[4]+i));

  }

  printf("\n");

 

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

      printf("p[5]+%d = %d\n",i, p[5]+i);

      printf("*(p[5]+%d) = %d\n", i,*(p[5]+i));

  }

}


¿é¥Xµ²ªG

p = 268436528

*p = 268440160

**p = 97

 

p[0] = 268440160

p[1] = 268440164

p[2] = 268440168

p[3] = 268440172

p[4] = 268436552

p[5] = 268440180

p[0]+0 = 268440160

 

*(p[0]+0) = 97

p[0]+1 = 268440161

*(p[0]+1) = 98

p[0]+2 = 268440162

*(p[0]+2) = 99

p[1]+0 = 268440164

*(p[1]+0) = 100

p[1]+1 = 268440165

*(p[1]+1) = 101

p[1]+2 = 268440166

*(p[1]+2) = 102

p[2]+0 = 268440168

*(p[2]+0) = 104

p[2]+1 = 268440169

*(p[2]+1) = 105

p[3]+0 = 268440172

*(p[3]+0) = 106

p[3]+1 = 268440173

*(p[3]+1) = 107

p[3]+2 = 268440174

*(p[3]+2) = 108

p[3]+3 = 268440175

*(p[3]+3) = 109

p[3]+4 = 268440176

*(p[3]+4) = 111

p[4]+0 = 268436552

*(p[4]+0) = 112

p[4]+1 = 268436553

*(p[4]+1) = 113

p[4]+2 = 268436554

*(p[4]+2) = 114

p[4]+3 = 268436555

*(p[4]+3) = 115

p[4]+4 = 268436556

*(p[4]+4) = 116

p[4]+5 = 268436557

*(p[4]+5) = 117

p[4]+6 = 268436558

*(p[4]+6) = 118

p[4]+7 = 268436559

*(p[4]+7) = 119

p[5]+0 = 268440180

*(p[5]+0) = 120

p[5]+1 = 268440181

*(p[5]+1) = 121

p[5]+2 = 268440182

*(p[5]+2) = 122


main()

{

  int i,j;

  static char *p[2][3]={"abc","def","hi","jklmo","pqrstuvw","xyz"};

 

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

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

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

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

 

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

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

           printf("p[%d][%d] = %d\n", i,j,p[i][j]);

 

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

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

           printf("*p[%d][%d] = %d\n", i,j,*p[i][j]);

 

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

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

           printf("&(*p[%d][%d]) = %d\n", i,j,&(*p[i][j]));

 

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

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

           printf("&(p[%d][%d]) = %d\n", i,j,&p[i][j]);

}

 

¿é¥Xµ²ªG

p = 268436528

*p = 268436528

**p = 268440032

***p = 97

 

p[0][0] = 268440032

p[0][1] = 268440036

p[0][2] = 268440040

p[1][0] = 268440044

p[1][1] = 268436552

p[1][2] = 268440052

 

*p[0][0] = 97

*p[0][1] = 100

*p[0][2] = 104

*p[1][0] = 106

*p[1][1] = 112

*p[1][2] = 120

 

&(*p[0][0]) = 268440032

&(*p[0][1]) = 268440036

&(*p[0][2]) = 268440040

&(*p[1][0]) = 268440044

&(*p[1][1]) = 268436552

&(*p[1][2]) = 268440052

&(p[0][0]) = 268436528

&(p[0][1]) = 268436532

&(p[0][2]) = 268436536

&(p[1][0]) = 268436540

&(p[1][1]) = 268436544

&(p[1][2]) = 268436548

 


      ¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢s¢w¢w¢w¢w¢w¢w¢w¢w¢w¢{

      ¢x  Operator                                ¢x  Associativity   ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x  ()    []     ->                         ¢x   left to right  ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x  !     ++  --  - (type)  *  &  sizeof    ¢x   right to left  ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x  *     /      %                          ¢x   left to right  ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x  +     -                                 ¢x   left to right  ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x  <<    >>                                ¢x   left to right  ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x  <     <=     >      >=                  ¢x   left to right  ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x  ==    !=                                ¢x   left to right  ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x  &                                       ¢x   left to right  ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x  ^                                       ¢x   left to right  ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x  |                                       ¢x   left to right  ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x  &&                                      ¢x   left to right  ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x  ||                                      ¢x   left to right  ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x  ¡H:                                     ¢x   right to left  ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x  =     +=     -=      etc.               ¢x   right to left  ¢x

      ¢u¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢t

      ¢x  ,                                       ¢x   left to right  ¢x

      ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢r¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}     

 


¦ì¤¸¹Bºâ

( bits operation )

 

 

 

 

   ¢Ñ»y¨¥´£¨Ñ¤»ºØ¹Bºâ¡A¦ì¤¸¹Bºâ¤l(bit operators)¦pªí 1¡C»¡©ú¦p¤U¡G

 

 

     

                           ªí  ¤@¡G

 

 

               ¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w

                 ²Å  ¸¹                  ·N  ¸q

               ¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w

                   &                ¥B ( AND )

 

                   |                ©Î (inclusive OR )

 

                   ^                ©Î (exclusive OR )

 

                   ~                1 ¸É¼Æ

 

                   <<               ¥ª²¾

 

                   >>               ¥k²¾

               ¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w                 


 

 

 

 

  ¤@¡B¥B (AND)¡÷ & ¡G¥u¦³¦b¨â­Ó¦ì¤¸¬Ò¬° 1 ®É¤~¬° 1¡A¨ä¾l¬Ò¬°¹s¡C

 

 

 

 

main ()

{

   int num1 = 067, num2 = 025, num3 = 046;

 

   printf (" ..... bitwise AND (&) operator .....\n\n");

   printf ("\t %d & %d = %o \n\n", num1 ,num1 ,num1 & num1 );

   printf ("\t %d & %d = %o \n\n", num2 ,num1 ,num1 & num2 );

   printf ("\t %d & %d = %o \n\n", num3 ,num1 ,num1 & num3 );

   printf ("\t %d & %d = %o \n\n", num2 ,num3 ,num2 & num3 );

   printf ("\t %d & %d & %d = %o \n\n", num1 ,num2 ,num3 ,num1 & num2 & num3 );

}

 

    ..... bitwise AND (&) operator .....

 

 

    55 & 55 = 67

 

    21 & 55 = 25

 

    38 & 55 = 46

 

    21 & 38 = 4

 

    55 & 21 & 38 = 4

 


½Ðª`·N¡A«Å§i num1¡Anum2¡Anum3¬O¥H¤K¶i¦ìªí¥Ü¡A¦]¬°«e­±¬O¥H0¬°¶}©l¡C

 

      num1 = (067) = (55) = (110111)

                        8        10             2

 

      num2 = (025) = (21) = (010101)

                        8        10             2

 

      num3 = (046) = (38) = (100110)

                        8        10             2

 

  ¹Bºâ¹Lµ{¤Îµ²ªG¦p¤U¡G

 

     (55) & (55) = (110111) & (110111) = (110111) = (067)

            10       10             2               2              2        8

 

     (55) & (21) = (110111) & (010101) = (010101) = (25)

            10       10             2               2              2       8

 

     (55) & (38) = (110111) & (100110) = (100110) = (46)

            10       10             2               2              2       8

 

     (21) & (38) = (010101) & (100110) = (000100) = (4)

            10       10             2               2              2      8

 

     (55) & (21) & (38) = (110111) & (000100) = (000100) = (4)

            10       10       10             2                2             2       8

 
  ¤G¡B©Î (OR)¡÷ | ¡G·í¨â­Ó¦ì¤¸¨ä¤¤¦³¤@¬° 1 ®É´N¬° 1¡C

 

main ()

{

   int num1 = 067, num2 = 025, num3 = 046;

 

   printf (" ..... bitwise OR (|) operator .....\n\n");

   printf ("\t %d | %d = %o \n\n", num1 ,num1 ,num1 | num1 );

   printf ("\t %d | %d = %o \n\n", num2 ,num1 ,num1 | num2 );

   printf ("\t %d | %d = %o \n\n", num3 ,num1 ,num1 | num3 );

   printf ("\t %d | %d = %o \n\n", num2 ,num3 ,num2 | num3 );

   printf ("\t %d | %d & %d = %o \n\n", num1 ,num2 ,num3 ,num1 | num2 | num3 );

}

 

    ..... bitwise OR (|) operator .....

 

 

    55 | 55 = 67

 

    21 | 55 = 67

 

    38 | 55 = 67

 

    21 | 38 = 67

 

    55 | 21 | 38 = 67

 

 

     (55) | (55) = (110111) | (110111) = (110111) = (067)

            10        10            2              2              2         8

 

     (55) | (21) = (110111) | (010101) = (110111) = (067)

            10        10            2              2              2         8

 

     (55) | (38) = (110111) | (100110) = (110111) = (067)

            10        10            2              2              2         8


¤T¡B 1 ¸É¼Æ (~)¡A»Pexclusive OR(^)¡G1¸É¼Æ¬O§â1Åܦ¨0¡A0Åܦ¨1¡A¦Óexclusive¦p

ªí 2¡C

 

                           ªí  2 ¡G

 

               ¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w

                 A     B           A (exclusive OR ) B

               ¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w

 

                 0     0                   0

 

                 0     1                   1

 

                 1     0                   1

 

                 1     1                   0

               ¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w    

 

main ()

{

   int num1 = 067, num2 = 025, num3 = 046;

 

   printf (" ..... bitwise exclusive (^) operator .....\n\n");

   printf ("\t %d ^ %d = %o \n\n", num1 ,num1 ,num1 ^ num1 );

   printf ("\t %d ^ %d = %o \n\n", num2 ,num1 ,num1 ^ num2 );

   printf ("\t %d ^ %d = %o \n\n", num3 ,num1 ,num1 ^ num3 );

   printf ("\t %d ^ %d = %o \n\n", num2 ,num3 ,num2 ^ num3 );

   printf ("\t %d ^ %d ^ %d = %o \n\n", num1 ,num2 ,num3 ,num1 ^ num2 ^ num3 );

}

 

..... bitwise exclusive (^) operator .....

 

         55 ^ 55 = 0

 

         21 ^ 55 = 42

 

         38 ^ 55 = 21

 

         21 ^ 38 = 63

 

         55 ^ 21 ^ 38 = 4

 


main()

{

  int num1 = 067, num2 = 025, num3 = 046;

 

  printf("..... Before exclusive OR (^) operation .....\n\n");

  printf("\t num1 = %d \t num2 = %d\n\n",num1, num2);

 

  /*   exchange num1, num2    */

       num1 ^= num2;

       num2 ^= num1;

       num1 ^= num2;

 

  printf("..... After  exclusive OR (^) operation .....\n\n");

  printf("\t num1 = %d \t num2 = %d\n\n",num1, num2);

}

 

 

..... Before exclusive OR (^) operation .....

 

         num1 = 55       num2 = 21

 

..... After  exclusive OR (^) operation .....

 

         num1 = 21       num2 = 55

 


¥|¡B¦ì¤¸¥ª²¾(<<)»P¦ì¤¸¥k²¾(>>)¡G>>n©M<<n¤À§Oªí¦V¥k²¾©Î¦V¥ª²¾n­Óbits¡C

 

main()

{

  int num1 = 017777, num2 = 0444;

 

  printf("..... bitwise shift (>>, <<) operator .....\n\n");

  printf("\t %o >> 1 = %o \n\n", num1, num1 >> 1);

  printf("\t %o >> 2 = %o \n\n", num1, num1 >> 2);

  printf("\t %o >> 6 = %o \n\n", num1, num1 >> 6);

  printf("\t %o >> 1 = %o \n\n", num2, num2 >> 1);

  printf("\t %o >> 3 = %o \n\n", num2, num2 >> 3);

  printf("\t %o << 5 = %o \n\n", num1, num1 << 5);

  printf("\t %o << 2 = %o \n\n", num2, num2 << 2);

}

 

 

..... bitwise shift (>>, <<) operator .....

 

         17777 >> 1 = 7777

 

         17777 >> 2 = 3777

 

         17777 >> 6 = 177

 

         444 >> 1 = 222

 

         444 >> 3 = 44

 

         17777 << 5 = 777740

 

         444 << 2 = 2220

 


int_size()

{

  int n, c, j;

 

  n = 0;

  n = ~n  

  c = 1;

  for (j=0; n & c; j++, c <<= 1)

      ;

  return (j);

}

 


 

µ²ºcÅé

( struct )

 

 

 

       

                      ¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢{

                      ¢x µ²ºcÅé ( structure ) ¢x

                      ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

      

                      ¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢{

                      ¢x¥i¥Ñ User ¦Û©wªº¸ê®Æ«¬§O¢x

                      ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

 

¤é±`¥Í¬¡¤¤©Ò¹J¨ìªº¸ê®ÆºØÃþ¤Ó¦h¡A¥ô¦ó°ª¶¥»y¨¥¡A³]·Q¦A©P¨ì¤]µLªk¥]¬A¦U

¦¡¦U¼Ëªº¸ê®Æ§ÎºA¡C¬°¤F¸Ñ¨M³o­Ó°ÝÃD¡A¢ë»y¨¥´£¨Ñ¤F "¥i¥Ñ User ¦Û«Øªº" ¸ê®Æ

«¬§O¡C¥Ñ User ©w¸qªº¸ê®Æ«¬§O¡A«h¬O¥Ñ°ò¥»¸ê®Æ«¬§Oºc¦¨ªº½Æ¦X¦¡¸ê®Æ«¬§O¡A

»y¨¥§â³oºØ½Æ¦X¦¡¸ê®Æ«¬§O¥s°µµ²ºcÅé¡C


°²³]¾Ç®Õªº¹qºâ¤¤¤ß­n±N¥þ®Õ¨C­Ó¾Ç¥Íªº°ò¥»¸ê®Æ«Ø¦¨Àɮ׮ɡA·|¦³­þ¨Ç¸ê®Æ

­n¿é¤J©O¡H§Ú­Ì°²³]¥H¤U´N¬O©Ò»Ýªº¸ê®Æ©M¨ä«¬§O¡G

 

     ¸ê®Æ¶µ¥Ø           ¸ê®Æ«¬§O                    ¨Ò¦p

  ¢w¢w¢w¢w¢w¢w¢w    ¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w    ¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w

  ¢w¢w¢w¢w¢w¢w¢w    ¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w    ¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w

       ©m¦W           ¦r¦ê char name[10]        ±i¤j¥\

 

       ¦a§}           ¦r¦ê char addr[60]        ¥x«n¥«¤j¾Ç¸ô 1¸¹

 

       ¦~ÄÖ           ¾ã¼Æ int age              27       

 

 

¨C­Ó¾Ç¥Í´N¦³¤T¶µ°ò¥»¸ê®Æ­n¿é¤J¡A¦Ó¥B¸ê®Æ«¬§O»Pªø«×¦U¦³¤£¦P¡A§Ú­Ì¥i¥H§â¾Ç¥Íªº¸ê®Æ«Ø¥ß¦¨¤@­Óµ²ºcÅ髬§O¡A¥H¤è«K³B²z¡C§Ú­Ì§â³o·s«Øªº¦æ§O¥s

person ¡A¦¹«¬§O¬O¤@½Æ¦XÅé¡A¦@¥Ñ¤H¦W¡B¦a§}¡B¦~ÄÖ¤T³¡¥÷²Õ¦¨¡A¦¹¤T­Ó

³¡¥÷¨Ì¦¸¬°¦r¦ê¡B¦r¦ê¡B¾ã¼Æµ¥°ò¥»¸ê®Æ«¬§O¡G

 

               ¢z¢w¢w¢w¢w¢w¢s¢w¢w¢w¢w¢w¢s¢w¢w¢w¢w¢{

     person :  ¢x   name   ¢x   addr   ¢x   age  ¢x¡]¦U¸ê®Æ¦Vªø«×¤£¦P¡^

               ¢|¢w¢w¢s¢w¢w¢r¢w¢w¢s¢w¢w¢r¢w¢s¢w¢w¢}

  ¡]½Æ¦X¦¡«¬§O¡^     ¢x          ¢x        ¢|¢w¢w¢w  ¾ã¼Æ«¬§O

                     ¢x          ¢x

                     ¢x          ¢|¢w¢w¢w¢w¢w¢w¢w¢w  ¦r¦ê«¬§O

                     ¢x

                     ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w  ¦r¦ê«¬§O      


¢z¢w¢w¢w¢w¢w¢w¢w¢{

¢x µ²ºcÅ骺«Å§i ¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢}

 

 

µ²ºcÅ骺«Å§i»P¤@¯ëÅܼƪº«Å§i¦³¼h¦¸¤Wªº®t²§¡C¦]¬°µ²ºcÅ饻¨­¬O¤@ºØ«¬§O

¦Ó¤£¬OÅܼơA©Ò¥Hµ²ºcÅ骺«Å§i¬O«Ø¥ß¤@­Ó·sªº«¬§O¡A¦Ó¤£¬O«Å§i¤@­Ó·sªºÅÜ

¼Æ¡C¥²¶·¦¹µ²ºcÅ髬§O«Ø¦n¤§«á¡A¤~¯à¥Î¨Ó«Å§iÅܼơA³o¬O¤£¦Pªº¨â­Ó¼h¦¸¡C

µ²ºcÅ骺«Å§i¤è¦¡¦³¨âºØ¡G

 


  ¤@¡B¤ÀÂ÷¦¡«Å§i

  ¢w¢w¢w¢w¢w¢w¢w

 

¥ý¦bµ{¦¡³Ì«e­±«Å§iµ²ºcÅ骺²Õ¦¨¤º®e¡AµM«á¦A§âÅܼƫŧi¦¨¸Óµ²ºcÅ髬§O¡C

¤W­z¾Ç¥Íªº°ò¥»¸ê®Æ´N¥i³o¼Ë«Å§i¡G

 

        ¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w struct «ü¥O

        ¢x

        ¢x    ¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w µ²ºcÅ髬§O¦W

        ¢x    ¢x

        ¢x    ¢x          ¢z¢w¢w¢w µ²ºcÅ餺®e

        ¢x    ¢x          ¢x

      struct person {     ¢x

                      char name[10] ;   /* ©m¦W */ ¢w¢w¢{

                                                       ¢x

                      char addr[60] ;   /* ¦a§} */     ¢u¢w ³]©w¸ê®Æ¶µ

                                                       ¢x

                      int age ;         /* ¦~ÄÖ */ ¢w¢w¢}

 

                    };  ¢w¢w¢w¢w¢w¢w¢w¢w  ¦¹¶¥¬q«Ø¥ß¤@­Ó person «¬§O

    

 

 

      main ()

      {

        struct person student ; ¢w¢w¢w¢w¢w¢w  ¦¹¶¥¬q«Å§i student ÅܼÆ

        ................                      ¬° person «¬§O

      }

 

¦¹³B struct ¬O¢ëªº«O¯d¦r«ü¥O¡A¥Î¨Ó«Å§iµ²ºcÅ髬§O¡C¦b struct ¤§«á§Ú­Ì¥i¥H©ñ

¤@­Ó«¬§O¦W (tag)¡A¦¹³B person §Y¬°§Ú­Ì©Ò«Øªº«¬§O¦W¡C¦Óperson «á­±{ }¤º³¡©Ò

¦C§Y¬°µ²ºcÅ髬§Oªº²Õ¦¨³¡¥÷¡C¦¹³B§Ú­Ì«Å§i person³o­Óµ²ºcÅ髬§O¦@¥Ñ name ¤¸°}¦C¡Baddr¦r¤¸°}¦C¡B¥H¤Î age¾ã¼ÆÅܼƤT³¡¥÷²Õ¦¨¡A³o¨Ç²Õ¦¨³¡¥÷§Ú­ÌºÙ¬°

¸ê®Æ¶µ (member) ¡C«Å§i¤F person «¬§O¡A¥H«á¥u­n´£¨ì person ¡A¢ë½sĶ¾¹«Kª¾¹D

³o¬O¤@­Ó¥Ñ name ¦r¤¸°}¦C¡Baddr¦r¤¸°}¦C¡B¥H¤Î age¾ã¼ÆÅܼƲզ¨ªºµ²ºcÅ髬

§O¡C

 

      «Å§i¤F person «¬§O«á¡A±µµÛ§Ú­Ì´N¥i¥H§Q¥Î person ¨Ó«Å§iÅܼơG

 

        ¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w struct «ü¥O

        ¢x

        ¢x    ¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w µ²ºcÅ髬§O¦W

        ¢x    ¢x

      struct person student ¢w¢w¢w ÅܼƦW              

 

´Nªí¥Ü student³o­ÓÅܼƬO person ³oºØµ²ºcÅ髬§O¡C


  ¤G¡Bµ²¦X«¬«Å§i

  ¢w¢w¢w¢w¢w¢w¢w

 

³oºØ«Å§i¤è¦¡¤£¶È«Å§i¤Fµ²ºcÅ餺ªº¸ê®Æ¶µ¡A¦P®É¤]«Å§i¤Fµ²ºcÅéÅܼƦWºÙ¡A

¤]´N¬O§â¤ÀÂ÷«¬«Å§iªº¨â­Ó¶¥¬q¦X¦Ó¬°¤@¡G

 

        ¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w struct «ü¥O

        ¢x

        ¢x    ¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w µ²ºcÅ髬§O¦W

        ¢x    ¢x

        ¢x    ¢x

      struct person {

                      char name[10] ;    ¢w¢w¢{

                                             ¢x

                      char addr[30] ;        ¢u¢w ¸ê®Æ¶µ

                                             ¢x

                      int age ;          ¢w¢w¢}

 

                    };  student ; /* «Å§i student ¬O person «¬§O */

                         ¢x

                         ¢|¢w¢w¢w¢w  ÅܼƦW                 

 

¦³®É­Ô¡A¦pªG§Ú­Ì½T©w±µ¤U¨Ó¤£·|¦A«Å§i¸Óµ²ºcÅ髬§OªºÅܼơA«h¤W¦C«Å§i¤¤

ªºµ²ºcÅ髬§O¦W¡]§Y person ¡^¤]¥i¥H¬Ù²¤¡A¦]¥H«á¤£¨Ï¥Î¡C


 

 

/* compute the size of the fundamental types */

 

main()

{

  printf("The size of the fundamental types is computed.\n\n");

  printf("    char:%3d bytes\n", sizeof(char));

  printf("   short:%3d bytes\n", sizeof(short));

  printf("     int:%3d bytes\n", sizeof(int));

  printf("unsigned:%3d bytes\n", sizeof(unsigned));

  printf("    long:%3d bytes\n", sizeof(long));

  printf("   float:%3d bytes\n", sizeof(float));

  printf("  double:%3d bytes\n", sizeof(double));

}

 

¿é¥Xµ²ªG

 

The size of the fundamental types is computed.

 

    char:  1 bytes

   short:  2 bytes

     int:  4 bytes

unsigned:  4 bytes

    long:  4 bytes

   float:  4 bytes

  double:  8 bytes

 


struct  test {

    char a;

    int  i;

    char b;

             };

main ()

{

 struct test test_size;

 printf ("size of struct test = %d byte \n",

          sizeof (struct test ));

 printf ("size of variable test = %d byte \n",sizeof (test_size));

}

 

¿é¥Xµ²ªG

size of struct test = 12 byte

size of variable test = 12 byte

 


struct  test {

    char a;

    char b;

    int  i;

             };

 

main ()

{

 struct test test_size;

 printf ("size of struct test = %d byte \n",

          sizeof (struct test ));

 printf ("size of variable test = %d byte \n",sizeof (test_size));

}

 

¿é¥Xµ²ªG

 

size of struct test = 8 byte

size of variable test = 8 byte

 


§Ú­Ì¥i¥H¥Î sizeof ºâ²Å¨Ó¨D¥Xµ²ºcÅé¦b¹q¸£¤¤¹ê»Ú©Ò¦ûªºªÅ¶¡¡C

 

 

 struct  person  {

                 char name[30] ;

                 char addr[20] ;

                 int age ;

                 };

 

 main ()

 {

  struct person student;

  printf ("size of struct person = %d byte \n",sizeof (struct person ));

  printf ("size of variable student = %d byte \n",sizeof (student ));

 }

 

 

      °õ¦æµ²ªG¡G

 

 size of struct person = 56 byte

 size of variable student = 56 byte

 


¦b¹q¸£°O¾ÐÅ餤µ²ºcÅ骺¸ê®Æ¶µ¬O³s¦ê±Æ¦C¦b¤@°_ªº¡G

 

               char       char           int

            ¢z¢w¢w¢w¢w¢s¢w¢w¢w¢w¢w¢s¢w¢s¢w¢w¢w¢{

   student  ¢xname[20]¢x addr[30] ¢x  ¢x age  ¢x

            ¢|¢w¢w¢w¢w¢r¢w¢w¢w¢w¢w¢r¢s¢r¢w¢w¢w¢}

             ¢|¢w¢s¢w¢}¢|¢w¢s¢w¢w¢} ¢x¢|¢w¢s¢w¢}                  

           ¦û 20 bytes ¦û 30 bytes  ¢x ¦û 4 bytes

                                    ¢x

                                ¦û 2 bytes


¢z¢w¢w¢w¢w¢w¢w¢w¢{

¢x µ²ºcÅ骺°}¦C ¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢}

 

 

µ²ºcÅé¤]¥i¥H²Õ¦¨°}¦C¡A¨ä«Å§i¤èªk¦p¤U¡G

 

      strust  µ²ºcÅé¦W  °}¦C¦W  ¡e ¤¸¯À­Ó¼Æ ¡f;

 

¦pªG¾Ç®Õ¤¤¤@¯Z¯Å¦³ 20 ­Ó¾Ç¥Íªº¸Ü¡A§Ú­Ì¥i¥H«Å§i¤@­Óµ²ºcÅé°}¦C¨Ó®e¯Ç³o¨Ç

¸ê®Æ¡C


struct  person  {

                char name[30] ;

                char addr[20] ;

                int age ;

};

 

main ()

{

 struct person student[20]; ¢w¢w¢w¢w¢w¢w¢w¢w¢w  «Å§iµ²ºcÅé°}¦C

 printf ("size of student array = %d byte \n",sizeof (student ));

}

 

°õ¦æµ²ªG¡G

 

size of student array = 1120 byte

 

¦b°O¾ÐÅ餤¡A student ªºµ²ºcÅé°}¦C¤]¬O¥H³sÄò±Æ¦Cªº¤èªkÀx¦sªº¡G

 

                    char       char       int

                ¢z¢w¢w¢w¢w¢s¢w¢w¢w¢w¢w¢s¢w¢s¢w¢w¢w¢w¢{

   student [0]  ¢xname[20]¢x addr[30] ¢x  ¢x  age   ¢x

                ¢u¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢q¢w¢q¢w¢w¢w¢w¢t

   student [1]  ¢xname[20]¢x addr[30] ¢x  ¢x  age   ¢x

                ¢u¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢q¢w¢q¢w¢w¢w¢w¢t

   student [2]  ¢xname[20]¢x addr[30] ¢x  ¢x  age   ¢x

                ¢u¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢q¢w¢q¢w¢w¢w¢w¢t

       .        ¢x  ...   ¢x   ...    ¢x  ¢x  ...   ¢x

       .        ¢u¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢q¢w¢q¢w¢w¢w¢w¢t

   student [18] ¢xname[20]¢x addr[30] ¢x  ¢x  age   ¢x

                ¢u¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢q¢w¢q¢w¢w¢w¢w¢t

   student [19] ¢xname[20]¢x addr[30] ¢x  ¢x  age   ¢x

                ¢u¢w¢w¢w¢w¢q¢w¢w¢w¢w¢w¢q¢w¢q¢w¢w¢w¢w¢t

                ¢|¢w¢w¢w¢w¢r¢w¢w¢w¢w¢w¢r¢w¢r¢w¢w¢w¢w¢}

                 ¢|¢w¢s¢w¢} ¢|¢w¢s¢w¢}      ¢|¢s¢}               

               ¦û 20 bytes ¦û 30 bytes     ¦û 4 bytes


¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢{

¢x µ²ºcÅ骺ªì©l³]©w ¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

 

 

¥~¦b©Î¬OÀRºAªºµ²ºcÅéÅܼƥi¥H¦b«Å§iªº¦P®É°µªì­È³]©w¡A¨ä¤èªk¦p¤U¡G

 

 

struct  person  {

                char *name ;

                char *addr ;

                int age ;

} a = {"liu","london",25} ;


¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢{

¢x    ¦s¨úµ²ºcÅé¸ê®Æ    ¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

      

¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢{

¢x µ²ºcÅé¸ê®Æªº¦s¨úªk¤@¡G`.`ºâ²Å¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

 

 

«Å§i¦nµ²ºcÅéÅܼƤ§«á¡A±µ¤U¨Ó´N¬O­n±N¾Ç¥Í¸ê®Æ¿é¶i¨ìµ²ºcÅ骺¸ê®Æ¶µ¤W¡A

¦³¨âºØ¤èªk¥i¥H¦s¨ú¨ìµ²ºcÅé¤Wªº¸ê®Æ¶µ¡G¨Ï¥Îºâ²Å¡D©Î«ü¦ì¾¹ºâ²Å¸¹¡Ð¡Ö(´î

¸¹¤Î¤j©óªº²Å¸¹)¡C§Ú­Ì¥ý¥Îºâ²Å¡D°µ¥Ü½d¡A¨ä®æ¦¡¬°¡G

 

 

µ²ºcÅéÅܼƦW¡D¸ê®Æ¶µ¦W


¨Ò¦p¦b student «á¥[ .name ®É¡A§Y¥i¦s¨ú¨ì©m¦W³o­Ó¸ê®Æ¶µ¡A¥H¤U³o­Ó¨Ò¤l§Y§Q¥Îºâ²Å'.'¡A§â¤@­Ó¾Ç¥Íªº©m¦W¡B¦a§}©M¦~ÄÖ¿é¤J¨ìµ²ºcÅ餤¡G

 

struct  person  {

                      char name[30] ;

                      char addr[20] ;

                      int age ;

};

 

main ()

 

{

 struct person student;

                             ¢z¢w¢w¢w¢w¢w  °}¦C¦W¤£¥²¥[ &

        printf ("name = :"); ¢x

 

       scanf ("%s",student.name); ¢w¢w¢w  ¦s¤J student.name

 

       printf ("addr = :");

 

       scanf ("%s",student.addr);

 

       printf ("age = :");

 

       scanf ("%d",&student.age); ¢w¢w¢w  ¤@¯ëÅܼƭn¥[ &

 

       /*  input 3 data then display  */

 

       printf ("%-30s %-20s %3d", student.name ,

 

       student.addr , student.age );

                       ¢x

                       ¢x

}                      ¢|¢w¢w¢w¢w¢w¢w  ¦L student ªº¦U¸ê®Æ¶µ

 

 

      °õ¦æµ²ªG¡G

 

name = : lee

 

addr = : taipei_taiwan

 

age = :  30

 

lee             taipei_taiwan             30

 


¢z¢w¢w¢w¢w¢w¢w¢w¢w¢{

¢x  µ²ºcÅé«ü¦ì¾¹  ¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢w¢}

 

 

µ²ºcÅé«ü¦ì¾¹ªº¨Ï¥Î¤èªk©M¤@¯ë«ü¦ì¾¹¬O¤@¼Ëªº¡C¥Ñ©óµ²ºcÅ餤§t¦³³\¦h¸ê®Æ

¶µ¡A¦]¦¹µ²ºcÅé«ü¦ì¾¹¥[ 1 ®É¡A¨ä­Èµ´¹ï¤£·|¬O¥u¥[ 1 ¦Ó¤w¡A¦Ó¬O¥[¾ã­Óµ²ºc

Åé©Ò¦ûªº byte ¼Æ¡C

 

 

struct  person  {

                char name[30] ;

                char addr[20] ;

                int age ;

                };

 

main ()

{

  struct person student [20];

  struct person *p;     /* pointer of structure */

  int i;

 

      p = student; ¢w¢w¢w  §â p «ü¦Vµ²ºcÅé student

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

      printf (" address of person [%d] = %Nx\n",i,p);

}

 

 

°õ¦æµ²ªG¡G

 

  address of person [0] = 2147465752 ¢w¢s¢w  ¬Û®t 56 bytes¡Aµ¥©ó person

                                       ¢x    ³o­Óµ²ºcÅ骺¤j¤p¡C

  address of person [1] = 2147465808 ¢w¢}

 

  address of person [2] = 2147465864

 

  address of person [3] = 2147465920

 

  address of person [4] = 2147465976


¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢{

¢x µ²ºcÅ鶵¥Øªº¦s¨úªk¤G¡G-> ºâ²Å¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

 

­n¦s¨úµ²ºcÅ餺ªº¸ê®Æ¶µ¡AÁÙ¦³¥t¤@ºØ¤èªk¥i¥H¿ìªº¨ì¡A¨º´N¬O±Ä¥Î¡Ð¡Ö

²Å¸¹¡]´î¸¹»P¤j©ó²Å¸¹¡^¡A¦¹ºâ²Å¶·°t¦X«ü¦ì¾¹¨Ó¨Ï¥Î¡A¨ä¼gªk¬°¡G

 

 

µ²ºcÅé«ü¦ì¾¹¡Ð¡Ö¸ê®Æ¶µ

 

 

§Ú­Ì¥i¥H¥Î¤@­Ó«ü¦ì¾¹¨Ó«ü¦V¤@­Óªºµ²ºcÅé¡AµM«á§Q¥Î¦¹«ü¦ì¾¹¨Ó¦s¨úµ²ºcÅé

¤¤ªº­Ó§O¸ê®Æ¶µ¡C¨Ò¦p p->age ´N¨ú¨ìµ²ºcÅ餤ªº¦~ÄÖ³oÄæ¸ê®Æ¶µ¡A§Ú­ÌºÙ³oºØ

¦s¨úªk¬°¶¡±µ¦s¨úªk¡A¦Ó`.` ºâ²Åªº¤èªk¬°ª½±µ¦s¨úªk¡C


§Q¥Î«ü¦ì¾¹¦s¨ú¸ê®Æ¶µªº¤èªk¡G`->`ºâ²Å

 

struct  person  {

                char name[30] ;

                char addr[20] ;

                int age ;

                };

 

 

main ()

{

 struct person student [20];

 struct person *p;     /* pointer of structure */

        p = &student;

        printf ("Your name :");

        scanf ("%s",p->name);

        printf ("Your address :");

        scanf ("%s",p->addr);

        printf ("Your age :");

        scanf ("%s",p->age);

        printf ("%-30s%-20s%3d\n",p->name,p->addr,p->age);

}

 

 

°õ¦æµ²ªG¡G

 

Your name : Lee

 

Your address : Taipei

 

Your age : 34

 

Lee       Taipei        34


¥H¤@­Óµ²ºcÅé¾ã­Ó³]µ¹¥t¤@­Óµ²ºcÅ骺¤èªk

 

 

struct data {

              char *name;

              int n;

            } x,y;           /* x and y are structure */

 

main ()

{

     x.name = "Lin S.M" ;  ¢w¢w¢s¢w¢w ³]©w x ¦U¸ê®Æ¶µ

                               ¢x

     x.n = 124 ; ¢w¢w¢w¢w¢w¢w¢w¢}

 

     y = x ;

 

     printf ("y.name = %s, y.n = %d\n",y.name, y.n); <¢w¢w¢{

                                                          ¢x

                              ¦L¥Xµ²ºcÅé y ªº¤º®e   ¢w¢w¢w¢}

 

 

°õ¦æµ²ªG¡G

 

y.name = Lin S.M, y.n = 124

 

µ{¦¡¤¤ªº y=x¦¡¤l§Yµ¥©ó y.name=x.name©M y.n=x¨â¦¡¡A¦³¤F³oºØ¥\¯à«á¡A§Ú

­Ì¥H«á¦b³]©wµ²ºcÅé®É¡A´N¤£¶·³v¤@¦C¥X¨C¤@­Ó¸ê®Æ¶µ¤F¡C


¢z¢w¢w¢w¢w¢w¢w¢w¢w¢{

¢x µ²ºcÅ骺µ²ºcÅé ¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢w¢}

 

µ²ºcÅ骺¸ê®Æ¶µ¤]¥i¥H¬O¤@­Óµ²ºcÅé¡G

 

struct person {

              char  name[30];

              int age;

};

struct card {

              struct person p;

              char addr [30];

} student, *stp;

 

·í§Ú­Ì­n³]©w student ªº¦W¦r®É¡A¥i¥H¼g¡G

 

                         ¢z¢w¢w  ¦h­«µ²ºcÅ骺¸ê®Æ¶µªí¥Üªk

                         ¢x

    scanf(" %s ", student.p.name);

 

©Î  scanf(" %s ", stp->p.name);

 


¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢{

¢x »¼°jªºµ²ºcÅé«Å§i ¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

 

µ²ºcÅé¥i¥H°µ»¼°j¦¡«Å§i¡C¤ñ¦p¡A¤@­Ó¦ê¦Cµ²ºc¡A¨ä¨C­Ó¸`ÂI(node)¥]§t¨â³¡

¥÷¡G¸ê®Æ¤Î¥u¦V¤U­Ó¸`ÂIªº«ü¦ì¾¹¡A§Ú­Ì¥i¥H«Å§i¦¨¡G

 

struct node {

       char *info;

       struct node *next;¢w¢w¢w»¼°jªº«¬§O«Å§i¡]node«Å§i¸Ì¤S¦³node¡^

 

         node               node                          node

    ¢z¢w¢w¢s¢w¢w¢{     ¢z¢w¢w¢s¢w¢w¢{                ¢z¢w¢w¢s¢w¢w¢{

¢w¡÷¢xinfo¢xnext¢q¢w¡÷ ¢xinfo¢xnext¢q¢w¡÷ .......... ¢xinfo¢xnext¢x

    ¢|¢w¢w¢r¢w¢w¢}     ¢|¢w¢w¢r¢w¢w¢}                ¢|¢w¢w¢r¢q¢w¢}

                                                             ¡õ

                                                             NULL

 

¨C­Ó¸`ÂI³£¦³¤@­Ó¸ê®Æ¶µ¬O¤@­Óªº«ü¦ì¾¹(next)¥u¦V¤U¤@¸`ÂI¡A¦ý³o¼Ë¤@ª½«ü¤U

¥h¡A³Ì«á¸`ÂI¬O¤°»ò©O¡H³Ì«á¤@­Ónext¬O«ü¦VNULL¡A³o¼Ë»¼°j¦¡«Å§i«K¦³µ²ªG

¤F¡C


¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢{

¢x «¬§O¦WºÙ©w¸q typedef ¢x

¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

 

structure Åý user ²Õ¦X¦Û¤vªº¸ê®Æ«¬§O¡A¦Ó typedef «h¥i¥H¥Î¨Ó³Ð·s¦Û¤vªº«¬§O¦W

ºÙ (type name)¡C¤ñ¦p§Ú­Ì¥i¥H«Å§i¡G

 

typedef char *STRING;

 

«h§Ú­Ì´N¥i¥H¥Î STRING §@¬°«¬§O¦WºÙ¨Ó§@Åܼƫŧi¤F¡G

 

STRING  s ,lineptr[MAX];

 

 

³o¼Ë´N§â s ¤Î lineptr «Å§i¦¨¦r¤¸«ü¦ì¾¹¦r¤¸«ü¦ì¾¹°}¦C¡C³q±`¥Ñ typedef©w¸q¥X

¨Óªº·s«¬§O¦WºÙ³£¥Î¤j¼g¦r¥À¡A¥H«K»P¤@¯ë¦WºÙ°Ï§O¡C

 

typedef ªº¥Î³~»P #define ¤@¼Ë¡A¥i¥H¨Ïµ{¦¡§ó²M·¡§ó®e©öºûÅ@¡C


struct list {

            int           data;

            struct list *next;

};

 

¨C¤@­Ó struct list «¬§OªºÅܼƳ£§t¦³ data ©M next ¨â­Ó¦¨­û¡Cnext «ü¼ÐÅܼƺ٬°³s

µ²¶µ¡A¨C­Óµ²ºc«¬§O¸ê®Æ³£¥iÂÇµÛ next ¦¨­û¦Ó³sµ²¦Ü¤@­Ó³sÄòªºµ²ºc¤W¡C

 

   µ²ºc¦ê¦C

¢z¢w¢w¢s¢w¢w¢{

¢x    ¢x  ¢w¢q¢w¡÷

¢|¢w¢w¢r¢w¢w¢}

  data  next

 

struct list a,b,c;

 

a.data = 1;

b.data = 2;

c.data = 3;

a.next = b.next = c.next = NULL;

 

¦¹µ{¦¡½Xªºµ²ªG¥i¥H§Q¥Î¹Ï§Î¨Ó´y­z¡G

 

 

a                  b                   c

¢z¢w¢w¢s¢w¢w¢{     ¢z¢w¢w¢s¢w¢w¢{      ¢z¢w¢w¢s¢w¢w¢{

¢x  1 ¢xNULL¢x     ¢x  2 ¢xNULL¢x      ¢x  3 ¢xNULL¢x

¢|¢w¢w¢r¢w¢w¢}     ¢|¢w¢w¢r¢w¢w¢}      ¢|¢w¢w¢r¢w¢w¢}

 

¨ä¦¸¡A§Ú­Ì­n¦ê³s³o¤T­Óµ²ºc¡G

 

a.next = &b;

b.next = &c;

 

³o¨Ç«ü¼Ð¹Bºâ¨Ï±o a ³sµ²¦Ü b , b ³sµ²¦Ü c¡G

 

      a                  b                 c

      ¢z¢w¢w¢s¢w¢w¢{     ¢z¢w¢w¢s¢w¢w¢{    ¢z¢w¢w¢s¢w¢w¢{

      ¢x  1 ¢x  ¢w¢q¢w¡÷ ¢x  2 ¢x  ¢w¢q¢w¡÷¢x  3 ¢xNULL¢x

      ¢|¢w¢w¢r¢w¢w¢}     ¢|¢w¢w¢r¢w¢w¢}    ¢|¢w¢w¢r¢w¢w¢}         


½u©Ê³sµ²¦ê¦C

 

 

½u©Ê³sµ²¦ê¦C(linear linked list)´N¹³¤@±øÅΦç÷¡A¤W­±³sÄòªº±¾µÛ¸ê®Æµ²ºc¡C¨ä¤¤

ªº¼ÐÀY«ü¼Ð(header pointer)«üµÛ¦ê¦Cªº²Ä¤@­Ó¤¸¯À¡AµM«á¨C­Ó¤¸¯À³£«ü¦V¤U¤@­Ó¤¸

¯À¡A¦Ó³Ì«á¤@­Ó¤¸¯Àªº³sµ²¶µ­È¬°NULL¡C

 

­º¥ý§Ú­Ì«Ø¥ß¤@­Ó¼ÐÀYÀÉ¡A¦¹ÀÉ¥i³Q¤U¦Cªº¦ê¦C³B²z¨ç¼Æ¥]®e¡C¦¹¼ÐÀYÀÉ¥]¬A

stdio.hÀÉ¡A¦]¬°¥¦©w¸q¤FNULL¡C

 

stdio.h ÀÉ¡G

 

      #include   <stdio.h>

 

      typedef char DATA ;    /* ¨ÒÃD¤¤±N¨Ï¥Î char */

 

      struct  linked_list {

               DATA                    d;

               struct linked_list  *next;

      };

 

      typedef  struct linked_list  ELEMENT;

 

      typedef  ELEMENT*            LINK;


°ÊºAªºÀx¦sÅé°t¸m

 

list.h Àɪº struct linked_list «Å§i¡A¤£·|°t¸mÀx¦sÅé¡A¦Ó·í«Å§i³oÃþ«¬ªºÅܼƩM°}

¦C®É¡A¥¦´N°µ¬°¨t²Î©¹«á°t¸mÀx¦sÅé®É©Ò¥Îªº¼Ëª©¡A¦]¬°§Ú­Ì§Æ±æ±N³o­Ó«¬§O

µø¬°¦ê¦Cªº¤@­Ó¤¸¯À¡A©Ò¥H§Ú­Ì§Q¥Î typedef ¥\¯à±N¨ä¦WºÙ§ï¬° ELEMENT¡C¦]

¬°¢ë¨ã¦³¥Î©ó°ÊºA¦a°t¸m°O¾ÐÅ骺¤½¥Îµ{¦¡¡A¦]¦Ó¨Ï±oELEMENT³oºØ¦Û§Ú°Ñ¦Ò

¦¡µ²ºc«¬§OÅܱo®æ¥~¦³¥Î¡Cmalloc()¨ç¼Æ¥Ñ¼Ð·Çµ{¦¡®w´£¨Ñ¡C¤U¦C§Î¦¡ªº¨ç¼Æ©I

¥s¡G

 

malloc(size)

 

·|¶Ç¦^¤@­Ó«ü¼Ð¡A¨ä«ü¦V¤@¬q¥R¨¬ªº°O¾ÐÅé¡A¥HÀx¦s»Ý­n size ­Ó¦ì¤¸²Õªºª«Åé

¡C malloc() ¨ç¼Æ¶Ç¦^­È¬O¤@­Ó«ü¦V char ªº«ü¼Ð¡A«ü¦V¦¹¨ç¼Æ©Ò°t¸mÀx¦sÅ骺°ò

©³¦ì§}¡C­Y head ¬O LINK «¬§OÅܼơA«h

 

head = (LINK) malloc(sizeof(ELEMENT));

 

±N¨ú±o¤@¬q¨¬¥HÀx¦s¤@­Ó ELEMENT ªº°O¾ÐÅé¡A¨Ã¥B±N¥¦ªº°ò©³¦ì§}«ü©wµ¹

head«ü¼Ð¡C

 


°²³]§Ú­Ì­n°ÊºAªº«Ø¥ß¤@­Ó½u©Ê³sµ²¦ê¦C¥HÀx¦s n¡Be ©M w¤T­Ó¦r¤¸¡A¨ä

  µ{¦¡½X¦p¤U¡G

 

head = (LINK) malloc(sizeof(ELEMENT));

 

head -> d = `n`;

 

head -> next = NULL ;

 

³o¬qµ{¦¡½X«Ø¥ß¤F¤@­Ó³æ¤@¤¸¯Àªº¦ê¦C¡C

 

    °ÊºAªº«Ø¥ß¤@­Ó¦ê¦C

              ¢z¢w¢w¢s¢w¢w¢{

head ¡ó¢w¢w¡÷ ¢x  n ¢xNULL¢x

              ¢|¢w¢w¢r¢w¢w¢}

 

¤U¦C³]©w¹Bºâ±N¼W¥[²Ä¤G­Ó¤¸¯À¡G

 

head -> next = (LINK) malloc(sizeof(ELEMENT));

 

head -> next -> d = `e`;

 

head -> next -> next = NULL ;

 

²{¦b¤w¸g¬O¨ã¦³¤G­Ó¤¸¯Àªº¦ê¦C¡G

 

              ¢z¢w¢w¢s¢w¢w¢{    ¢z¢w¢w¢s¢w¢w¢{

head ¡ó¢w¢w¡÷ ¢x  n ¢x  ¢w¢q¢w¡÷¢x  e ¢xNULL¢x

              ¢|¢w¢w¢r¢w¢w¢}    ¢|¢w¢w¢r¢w¢w¢}

³Ì«á­n¼W¥[²Ä¤T­Ó¤¸¯À¡G

 

head -> next -> next= (LINK) malloc(sizeof(ELEMENT));

head -> next -> next-> d = `w`;

head -> next -> next-> next = NULL ;

 

²{¦b§Ú­Ì¦³¤F¤@­Ó¤T¤¸¯Àªº¦ê¦C¡Ahead¬°¼ÐÀY«ü¼Ð¨Ã¥H NULL ¼Ð»x°µ¬°²×¤î¡C

 

                ¢z¢w¢w¢s¢w¢w¢{     ¢z¢w¢w¢s¢w¢w¢{    ¢z¢w¢w¢s¢w¢w¢{

  head ¡ó¢w¢w¡÷ ¢x  n ¢x  ¢w¢q¢w¡÷ ¢x  e ¢x  ¢w¢q¢w¡÷¢x  w ¢xNULL¢x

                ¢|¢w¢w¢r¢w¢w¢}     ¢|¢w¢w¢r¢w¢w¢}    ¢|¢w¢w¢r¢w¢w¢}


¦ê¦C¹Bºâ

 

 

¤U¦C¬O¤@¨Ç½u©Ê³sµ²¦ê¦Cªº°ò¥»¹Bºâ¡G

 

 

1.  «Ø¥ß¦ê¦C

 

2.  ­pºâ¤¸¯Àªº¼Æ¥Ø

 

3.  ¬d¸ß¬Y¤@¤¸¯À

 

4.  ´¡¤J¬Y¤@¤¸¯À

 

5.  §R°£¬Y¤@¤¸¯À

 


§Ú­Ì­n³]­pªº²Ä¤@­Óµ{¦¡·|¥Ñ¤@­Ó¦r¦ê¦Ó²£¥Í¤@­Ó¦ê¦C¡A¥¦ªº¶Ç¦^­È¬O¤@­Ó«ü

¦V¦ê¦C¼ÐÀYªº«ü¼Ð¡C

 

 

/* §Q¥Î»¼°j¹Bºâ«Ø¥ß¦ê¦C */

 

# include  "list.h"

 

LINK string_to_list(s)

char s[];

{

    char  *malloc() ;

    LINK  head ;

 

    if  (s[0] == '\0')     /* base case */

        return  (NULL);

    else{

        head = (LINK) malloc(sizeof(ELEMENT));

        head -> d = s[0];

        head -> next = string_to_list(s+1) ;

        return (head);

    }

}


­p  ¼Æ

 

¥¦·|ªuµÛ¦ê¦C¤@ª½°õ¦æ¡Aª½¨ìµo²{NULL«ü¼Ð¬°¤î¡A¦pªG¬OªÅ¦ê¦C´N¶Ç¦^0¡C

 

/* »¼°j¦a­pºâ¦ê¦C¤¸¯Àªº¼Æ¥Ø */

 

# include  "list.h"

 

count(head)

LINK  head ;

{

    if  (head == NULL)

        return (0);

    else

        return(1 + count(head -> next ));

}


¬d  ¸ß

 

¨ç¼Æ¬O¦b¦ê¦C¤¤·j´M¤@­Ó¯S©wªº¤¸¯À¡A¦pªG§ä¨ì³o­Ó¤¸¯À«h¶Ç¦^¤@­Ó«ü¦V¸Ó¤¸

¯Àªº«ü¼Ð¡A§_«h¶Ç¦^NULL«ü¼Ð¡C

 

/* ¦b¦ê¦C¤¤¬d¸ß c¡A¨Ã¥Ñ head «ü¥X¨ä¦ì¸m */

 

# include  "list.h"

 

LINK lookup (c,head)

DATA  c;

LINK  head;

{

   if  (head == NULL)

       return(NULL);

   else if  (c == head ->d)

            return(head);

        else

            return(lookup(c,head -> next));

}


´¡  ¤J

 

¦ê¦C³Ì¦³¥Îªº¯S©Ê¤§¤@¬O·í§Ú­Ì¦b¦ê¦C¤¤§ä¨ì¾A·íªº¦ì¸m®É¡A¥u»Ý¤@¬q©T©wªº

®É¶¡§Y¥i§¹¦¨´¡¤Jªº°Ê§@¡C¬Û¹ï¦a¡A¦pªG±z­n¦b¤j«¬°}¦C¤¤©ñ¤J¤@­Ó­È¨Ã¥B«O

¯d¨ä¥L°}¦C­Èªº¶¶§Ç¡A«h´¡¤Jªº°Ê§@©Ò»Ý­nªº®É¶¡±N¥¿¤ñ©ó°}¦Cªºªø«×¡A¦Ó¦b

·s¥[¤Jªº­È¤§«áªº©Ò¦³°}¦C¤¸¯À­È³£¥²¶·¦V«á²¾¤@¦ì¡C

 

§Ú­Ì¥H¦b ¦ê¦C¤¤ªº´¡¤J°Ê§@¬°¨Ò¡Ap1©Mp2«ü¦V¨â­Ó¬Û¾Fªº¤¸¯À¡Aq«ü¦V­n´¡¤Jªº

¤¸¯À¡C

 

´¡¤J¤§«e¡G

                              ´¡ ¤J

     

                 ¢z¢w¢w¢s¢w¢w¢{     ¢z¢w¢w¢s¢w¢w¢{

           . . . ¢x  A ¢x  ¢w¢q¢w¡÷ ¢x  C ¢x  ¢w¢q¢w¡÷  . . .

           ¢z¢w¡÷¢|¢w¢w¢r¢w¢w¢} ¢z¡÷¢|¢w¢w¢r¢w¢w¢}

           ¢x                   ¢x

           ¢x                   ¢x

        p1 ¡ó                p2 ¡ó

 

                        ¢z¢w¢w¢s¢w¢w¢{

               q¡ó¢w¢w¡÷¢x  B ¢xNULL¢x

                        ¢|¢w¢w¢r¢w¢w¢}

 

        ´¡¤J¤§«á¡G

 

                 ¢z¢w¢w¢s¢w¢w¢{               ¢z¢w¢w¢s¢w¢w¢{

           . . . ¢x  A ¢x  ¢x¢x    ¢z¢w¢w¢w¡÷ ¢x  C ¢x  ¢w¢q¢w¡÷  . . .

           ¢z¢w¡÷¢|¢w¢w¢r¢w¢q¢}    ¢x   ¢z¢w¡÷¢|¢w¢w¢r¢w¢w¢}

           ¢x              ¢x      ¢x   ¢x

           ¢x              ¢x      ¢x   ¢x

        p1 ¡ó              ¢x   p2 ¡ó   ¢x

                           ¢x           ¢x

                           ¢|¡÷ ¢z¢w¢w¢s¢q¢w¢{

                      q ¡ó¢w¢w¡÷¢x  B ¢x¢x  ¢x

                                ¢|¢w¢w¢r¢w¢w¢}


insert () ¨ç¼Æ±N q©Ò«üªº¤¸¯À©ñ¦b p1 ©M p2 ©Ò«üªº¤¸¯À¤§¶¡¡G

 

/*  ¦b³sµ²¦ê¦C¤¤´¡¤J¤@­Ó¤¸¯À */

 

#include  "list.h"

 

insert (p1,p2,q)

LINK    p1,p2,q;

{

    p1 -> next = q;  /* ´¡  ¤J */

    q -> next = p2;

}

 

 
§R  °£

 

¦b½u©Ê³sµ²¦ê¦C¤¤§R°£¤@­Ó¤¸¯À«D±`²³æ¡A¥u­n±N³Q§R°£¤¸¯À¤§«eªº¤¸¯À³sµ²

¶µ¦¨­û³]©w¬°³Q§R°£¤¸¯À¤§«áªº¤¸¯À¦ì§}§Y¥i¡C§Ú­Ì¥H¹Ï§Î¨Ó»¡©ú§R°£ªº°Ê§@¡G

 

§R°£¤§«e¡G

 

                              §R °£

     

           ¢z¢w¢w¢s¢w¢w¢{     ¢z¢w¢w¢s¢w¢w¢{    ¢z¢w¢w¢s¢w¢w¢{

     . . . ¢x  A ¢x  ¢w¢q¢w¡÷ ¢x  B ¢x  ¢w¢q¢w¡÷¢x  C ¢x  ¢w¢q¢w¡÷. . .

     ¢z¢w¡÷¢|¢w¢w¢r¢w¢w¢}     ¢|¢w¢w¢r¢w¢w¢}    ¢|¢w¢w¢r¢w¢w¢}

     ¢x

     ¢x

  p  ¡ó

 

²{¦b°õ¦æ¤U¦Cªºµ{¦¡½X¡G

 

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

 

´¡¤J¤§«á¡G

                   ¢z¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢{

           ¢z¢w¢w¢s¢q¢w¢{ ¢z¢w¢w¢s¢w¢w¢{ ¢|¡÷ ¢z¢w¢w¢s¢w¢w¢{

     . . . ¢x  A ¢x¢x  ¢x ¢x  B ¢x  ¢w¢q¢w¡÷  ¢x  C ¢x  ¢w¢q¢w¡÷. . .

     ¢z¢w¡÷¢|¢w¢w¢r¢w¢w¢} ¢|¢w¢w¢r¢w¢w¢}      ¢|¢w¢w¢r¢w¢w¢}

     ¢x

     ¢x

  p  ¡ó

 


/* »¼°j¦a§R°£¤@­Ó¦ê¦C */

 

# include  "list.h"

 

delete_list(head)

LINK  head;

{

   if  (head != NULL)  {

       delete_list (head -> next);

       free ((char * ) head);

    }

}

 


¾ðª¬µ²ºc

°ò¥»Æ[©À»P©w¸q

 

¾ðª¬µ²ºc(Tree structure)¬O¹Ï§Îµ²ºc¤¤¤@ºØ¯S®íªºµ²ºc¡A¤]¬O¥Ñ¸`ÂI(Node)¤ÎÃä

(Edge)©Òºc¦¨¡A¦ý¾ðª¬µ²ºc¤¤¡A¦³¤@­Ó¯S©wªº¸`ÂI¥s¾ð®Ú(Root)¡A¦Ó¤@¯ëªº¹Ï§Îµ²

ºc¡A¨Ã¨S¦³¯S©wªº®Ú¸`ÂI¡A³o¬O¼Æ©M¹Ïªº¤£¦P¤§³B¡C¨Ò¦p¹Ï¤@©Ò¥Ü¡A´N¬O¤@­Ó

¾ðª¬µ²ºc¡C

 

                           A ¡ó

                           ¡þ¢@

                         ¡þ    ¢@

                       ¡ú        ¡û

                    B ¡ó         C ¡ó

                    ¡þ¢@          ¡þ

                  ¡ú    ¡û      ¡ú

              D  ¡ó    E ¡ó  F ¡ó   

 

                          ¹Ï  ¤@

 

 

 

¤G¤¸¾ð¡]Binary tree¡^

 

¤G¤¸¾ð¬O¾ðª¬µ²ºc¤¤¯S®íªº¤@ºØ¾ð¡A¦b¹q¸£¬ì¾ÇªºÀ³¥Î¤W«D±`¼sªx¡A­º¥ý§Ú­Ì

¬Ý¬Ý¤G¤¸¾ð¦WºÙ±o¥Ñ¨Ó¡A°²¦p¦b¤@Áû¤è¦V¾ð¤W¡A¸`ÂIªº¥~¤À¤ä«×¦pªG¤p©ó©Îµ¥

©ó m ®É¡A«hºÙ¦¹¾ð¬° m ¤¸¾ð(m-ary tree)¡A°²¦p¦b¤@Áû¾ðªº¥~¤À¤ä«×¤p©ó©Îµ¥©ó2

®É¡A´N¥s¤G¤¸¾ð¡C


¤G¤¸¾ð¸ê®Æ¦b¹q¸£¤º³¡ªºªí¥Üªk

 

¤G¤¸¾ð¸ê®Æ¦b¹q¸£¤º³¡ªí¥Ü®É¡A¥i¥Î¨âºØ¤èªk¡C

 

¡]¤@¡^¥H§tÃìµ²Ä椧°}¦Cªí¥Üªk

 

¥Ñ©ó¤G¤¸¾ðªº¨C¤@­Ó¸`ÂI³Ì¦h¦³¨â­Ó¤l¾ð¡A¦]¦¹§Ú­Ì¥i¥H«ÅºÙ¤@­Ó°O¿ý¡A¨ä¤¤

¦³¨â­Ó«ü¼Ð¡A¤À§O«ü¦V¥ª¸`ÂI¤Î¥k¸`ÂIªº¦ì¸m¡A¥H¢ë°µ«ÅºÙ®É¡A¥i¥Î¤U¦C¤è¦¡¡G

typedef struct treenode

{

   char info;

   int  left;

   int  right;

} NODE;

NODE  tree[20];

¦Ü©ó¸ê®Æ¦p¦ó¦w±Æ©O¡H§Ú­Ì¥i¥H±N¤G¤¸¾ð¥Ñ¤W¦Ü¤U¡A¥Ñ¥ª¦Ü¥kµ¹¤©½s¸¹¡A©Ò½s

ªº¸¹½X¡A´N¬O¸`ÂIÀx¦s¦b°}¦C¤¤ªº¦ì¸m¡A¦pªG¨S¦³¤l¾ðªº¸`ÂI¡A«h¨ä«ü¼Ð¤º®e

¥H 0ªí¥Ü¡A¥H¹Ï10¬°¨Ò¡A¨ä¸ê®ÆÀx¦s¦b°}¦C¤¤¥H¹Ï11ªí¥Ü¡C

 

                          A ¡ó 1

                           ¡þ¢@

                         ¡þ    ¢@

                       ¡ú        ¡û

                    B ¡ó 2       C ¡ó 3

                    ¡þ¢@             ¢@

                  ¡ú    ¡û             ¡û

              D ¡ó 4   E ¡ó 5         F ¡ó 6

                         ¡þ

                       ¡ú

                    G ¡ó 7

                     ¹Ï  10

     

               info  left  right

             ¢z¢w¢w¢s¢w¢w¢s¢w¢w¢{

            1¢x A  ¢x 2  ¢x 3  ¢x

             ¢u¢w¢w¢q¢w¢w¢q¢w¢w¢t

            2¢x B  ¢x 4  ¢x 5  ¢x

             ¢u¢w¢w¢q¢w¢w¢q¢w¢w¢t

            3¢x C  ¢x 0  ¢x 6  ¢x

             ¢u¢w¢w¢q¢w¢w¢q¢w¢w¢t

            4¢x D  ¢x 0  ¢x 0  ¢x                  root

             ¢u¢w¢w¢q¢w¢w¢q¢w¢w¢t                ¢z¢w¢w¢{

            5¢x E  ¢x 7  ¢x 0  ¢x                ¢x 1  ¢x

             ¢u¢w¢w¢q¢w¢w¢q¢w¢w¢t                ¢|¢w¢w¢}

            6¢x F  ¢x 0  ¢x 0  ¢x

             ¢u¢w¢w¢q¢w¢w¢q¢w¢w¢t

            7¢x G  ¢x 0  ¢x 0  ¢x

             ¢|¢w¢w¢r¢w¢w¢r¢w¢w¢}     

              ¹Ï  11  °}¦Cªí¥Üªk


¡]¤G¡^Ãìµ²ªí¥Üªk¡]Linked represention¡^¡G

 

Ãìµ²ªí¥Üªk¡A´N¬O§Q¥Î°ÊºA¸ê®Æµ²ºc¡A±N¤l¾ðªº«ü¼Ð©ñ¨ì¥ª¡B¥k¨âÃ䪺«ü¼ÐÅÜ

¼ÆÄ椺¡C§Ú­Ì¥i¥H¥Î¢ë«Å§i¨C¤@­Ó¸`ÂIªºµ²ºc¦p¤U¡G

 

 

typedef struct treenode

{

   struct treenode *left;

   char info;

   struct treenode *right;

} NODE;

NODE  *root;

 

¦¹³B root ¬OÀx¦s®Ú¸`ÂI«ü¼Ð¡C

 

¥H¹Ï10¬°¨Ò¡A¨äÃìµ²ªí¥Üªk¦p¹Ï12©Ò¥Ü¡A¨ä¤¤NULLªí¥Ü¤£¦b«ü¦V¨ä¥L¸`ÂI¡C

                                    ¢z¢w¢w¢w root

                           left info¡õ right

                         ¢z¢w¢w¢s¢w¢w¢s¢w¢w¢{

                         ¢x    ¢x A  ¢x    ¢x

                         ¢|¢q¢w¢r¢w¢w¢r¢w¢q¢}

                       ¢z¢w¢}            ¢|¢w¢{

                       ¡õ                    ¡õ

             ¢z¢w¢w¢s¢w¢w¢s¢w¢w¢{  ¢z¢w¢w¢s¢w¢w¢s¢w¢w¢{

             ¢x    ¢x B  ¢x    ¢x  ¢xNULL¢x C  ¢x    ¢x

             ¢|¢q¢w¢r¢w¢w¢r¢w¢q¢}  ¢|¢w¢w¢r¢w¢w¢r¢w¢q¢}

           ¢z¢w¢}            ¢|¢w¢{                ¢|¢w¢{

           ¡õ                    ¡õ                    ¡õ

   ¢z¢w¢w¢s¢w¢w¢s¢w¢w¢{  ¢z¢w¢w¢s¢w¢w¢s¢w¢w¢{  ¢z¢w¢w¢s¢w¢w¢s¢w¢w¢{

   ¢xNULL¢x D  ¢xNULL¢x  ¢x    ¢x E  ¢xNULL¢x  ¢xNULL¢x F  ¢xNULL¢x

   ¢|¢w¢w¢r¢w¢w¢r¢w¢w¢}  ¢|¢w¢w¢r¢w¢w¢r¢w¢w¢}  ¢|¢w¢w¢r¢w¢w¢r¢w¢w¢}

                       ¢z¢w¢}

                       ¡õ

                    ¢z¢w¢w¢s¢w¢w¢s¢w¢w¢{

                    ¢xNULL¢x G  ¢xNULL¢x

                    ¢|¢w¢w¢r¢w¢w¢r¢w¢w¢}

 

¹Ï  12  Ãìµ²ªí¥Üªk


¤G¤¸¾ð¡]Binary tree¡^±Æ§Ç

 

§Q¥Î¤G¤¸¾ð(Binary tree)±Æ§Ç®É¡A¬O±N¿é¤Jªº¸ê®Æ´¡¤J¼Æ¤¤ªº¥ª¤ä©Î¥k¤ä¡A¦Ó²Ä¤@

­Ó¸ê®Æ³Q©ñ¦b®Ú¸`ÂI(root)¡A­Y¦³¤@¤G¤¸¾ð¥Ø«e±¡ªp¦p¤U¡G

     

                            ¢z¢w¢w¢w¢{

                            ¢x  37  ¢x

                            ¢|¢w¢s¢w¢}

                                ¢x

                      ¢z¢w¢w¢w¢w¢r¢w¢w¢w¢{

                      ¢x                ¢x

                  ¢z¢w¢r¢w¢{        ¢z¢w¢r¢w¢{

                  ¢x  24  ¢x        ¢x  99  ¢x

                  ¢|¢w¢s¢w¢}        ¢|¢w¢s¢w¢}

                      ¢x                ¢x

                ¢z¢w¢w¢r¢w¢w¢{    ¢z¢w¢w¢r¢w¢w¢{

                ¢x          ¢x    ¢x          ¢x

                ¡Ý      ¢z¢w¢r¢w¢{¡Ý      ¢z¢w¢r¢w¢{

                        ¢x  31  ¢x        ¢x 105  ¢x

                        ¢|¢w¢s¢w¢}        ¢|¢w¢s¢w¢}

                            ¢x                ¢x

                      ¢z¢w¢w¢r¢w¢w¢{    ¢z¢w¢w¢r¢w¢w¢{

                      ¡Ý          ¡Ý    ¡Ý          ¡Ý            

 

¦Ó§Ú­Ì­n±N48´¡¤J¾ð¤¤¡A«h¥ý±qRoot§ä°_¡A¦]48¡Ö37¡A©Ò¥H¤ñ¸û37ªº¥k¸`ÂI¡A¨ä¤¤ªº¸ê®Æ¬O99¡A¦Ó48¡Õ99¡A¬G§ä¥X99ªº¥ª¸`ÂI¡A¦ý99ªº¥ª¸`ÂI¨ÃµL¸ê®Æ¡A©Ò

¥H±N48«Ø¥ß©ó¦¹¡A·sªº¤G¤¸¾ð¦p¤U¡G

                              ¢z¢w¢w¢w¢{

                              ¢x  37  ¢x

                              ¢|¢w¢s¢w¢}

                                  ¢x

                      ¢z¢w¢w¢w¢w¢w¢r¢w¢w¢w¢w¢w¢{

                      ¢x                      ¢x

                  ¢z¢w¢r¢w¢{              ¢z¢w¢r¢w¢{

                  ¢x  24  ¢x              ¢x  99  ¢x

                  ¢|¢w¢s¢w¢}              ¢|¢w¢s¢w¢}

                      ¢x                      ¢x

                ¢z¢w¢w¢r¢w¢w¢{          ¢z¢w¢w¢r¢w¢w¢{

                ¢x          ¢x          ¢x          ¢x

                ¡Ý      ¢z¢w¢r¢w¢{  ¢z¢w¢r¢w¢{  ¢z¢w¢r¢w¢{

                        ¢x  31  ¢x  ¢x  48  ¢x  ¢x 105  ¢x

                        ¢|¢w¢s¢w¢}  ¢|¢w¢s¢w¢}  ¢|¢w¢s¢w¢}

                            ¢x          ¢x          ¢x

                        ¢z¢w¢r¢w¢{  ¢z¢w¢r¢w¢{  ¢z¢w¢r¢w¢{

                        ¢x      ¢x  ¢x      ¢x  ¢x      ¢x

                        ¡Ý      ¡Ý  ¡Ý      ¡Ý  ¡Ý      ¡Ý


/*     makefile     */

 

SRC = print.c tree.c node.c

OBJ = tree.o print.o node.o

 

a.out: ${OBJ}

       cc ${OBJ} -o p4

 

clean:

       rm -f core a.out ${OBJ} p4

 

tree.o : tree.c

print.o : print.c

node.o : node.c


/*   tree.h    */

 

# include   <stdio.h>

typedef struct node  *TREE

struct node  {

     int info ;

     TREE left , right ;

};

 

int t;

FILE  *fopen() , *fp ;

 

TREE   root , CreatNode() ;

/*     tree.c    */

#include  "tree.h"

 

main()

{

 

     int  n,i,k ;

 

     fp = fopen ("treesort.data " , "r") ;

     fscanf (fp , "%d" , &n) ;

     printf ("\nThere are %d integers to sort.\n\n",n);

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

          fscanf (fp , "%d" , &k ) ;

          root = CreatNode (root, k) ;

          printf ("%5d%c",k,((i%9 == 0) ? '\n' : ' ' )) ;

     }

            printf ("\n\n...tree sort...\n\n") ;

            t = 1 ;

            print (root) ;

            printf ("\n\n") ;

            fclose(fp) ;

       }


/*         node.c       */

#include "tree.h"

TREE CreatNode(ptr, k)

  TREE        ptr;

  int         k;

{

  TREE p;

  p = ptr;

  if  (ptr == NULL) {

      p = (TREE) malloc(sizeof(struct node));

      p->info = k;

      p->left = NULL;

      p->right = NULL;

  }

  else if  (k > ptr->info)

           ptr->right = CreatNode(ptr->right, k);

       else

           ptr->left = CreatNode(ptr->left, k);

  return(p);

}

 

 

/*    print.c     */

 

#include "tree.h"

print(ptr)

  TREE ptr;

{

  if  (ptr != NULL) {

      print(ptr->left);

      printf("%5d%c", ptr->info, ((t++%9 == 0) ? '\n' : ' '));

      print(ptr->right);

  }

}

 


/*   treesort.data    */

12

 

12  -7  11  999  7  3  66  3  -5  22  -77 2

 

 

¿é¥Xµ²ªG

 

 

There are 12 integers to sort.

 

12     -7    11     999     7     3     66     3     -5

 

22    -77     2

 

 

... tree sort ...

 

-77     -7     -5     2     3     3     7     11     12

 

 22     66    999

 


Ãìµ²¦ê¦C

 

¬°¤FÀ±¸É¥H°}¦C»s§@¦ê¦C¤§¤£¨¬¡A¹E¦³Ãìµ²¦ê¦C(linked list)ªº²£¥Í¡A¨ä»P°}¦Cªº¥D

­n¤£¦PÂI¦b©ó¡G¡uÃìµ²¦ê¦C¡v¤¤¬Û¾F¨â¶µ¥Ø¦b¥D°O¾ÐÅ骺¦ì¸m¨Ã¤£»Ý­n¬Û¾F¡A

¨ä¨C¤@¸ê®Æ¶µ³£¦³¤@Ãìµ²Äæ¡AÃìµ²Äæ¸Ì¦sªº¬O¤U¤@¶µ¥Øªº¦ì¸m¡A¦]¦¹¡A´N¥i»´

©ö¨ú±o¤U¤@­Ó¶µ¥Øªº¸ê®Æ¡FÃìµ²¦ê¦C¥i¥Ñ¹Ï¤@ªí¥Ü¡A¦b¹Ï¤@¤¤¡AÅÜ¼Æ list ºÙ¬°

                                info    next

                             ¢z¢w¢w¢w¢s¢w¢w¢w¢{

                           1 ¢x Yang ¢x   0  ¢x

                             ¢u¢w¢w¢w¢q¢w¢w¢w¢t

                           2 ¢x Mang ¢x   1  ¢x

                             ¢u¢w¢w¢w¢q¢w¢w¢w¢t

                           3 ¢x Lai  ¢x  11  ¢x

                             ¢u¢w¢w¢w¢q¢w¢w¢w¢t

                           4 ¢x      ¢x      ¢x

                             ¢u¢w¢w¢w¢q¢w¢w¢w¢t

                           5 ¢x Huang¢x   3  ¢x

                             ¢u¢w¢w¢w¢q¢w¢w¢w¢t

                           6 ¢x Liu  ¢x   9  ¢x

                             ¢u¢w¢w¢w¢q¢w¢w¢w¢t

                           7 ¢x      ¢x      ¢x

                             ¢u¢w¢w¢w¢q¢w¢w¢w¢t

                           8 ¢x Cheng¢x   5  ¢x

                             ¢u¢w¢w¢w¢q¢w¢w¢w¢t

                           9 ¢x Pon  ¢x  12  ¢x

                             ¢u¢w¢w¢w¢q¢w¢w¢w¢t

                          10 ¢x      ¢x      ¢x

                             ¢u¢w¢w¢w¢q¢w¢w¢w¢t

                          11 ¢x Liang¢x   6  ¢x

                             ¢u¢w¢w¢w¢q¢w¢w¢w¢t

                          12 ¢x Tasi ¢x   2  ¢x

                             ¢|¢w¢w¢w¢r¢w¢w¢w¢}

¹Ï  ¤@ Ãìµ²¦ê¦C¦b¥D°O¾ÐÅ餧Àx¦s§ÎºA


Ãìµ²¦ê¦C¤§«ü¼ÐÅܼơ]¨ä­È¬°8¡^¡A¥Ñlist«ü¨ì¦ê¦C­º¡A¤]´N¬O¦ê¦C¥Ñ¦ì¸m8¶}©l

¡A¨C¤@­Ó¶µ¥Ø¥]§t¨âÄæ¡A¤@­Ó¸ê®ÆÄæ¡]info¡^¡A¥t¤@­ÓÃìµ²Äæ¡]next¡^¡A¨Ò¦p¦ì

¸m8¤§Ãìµ²Ä欰5¡A¥Nªí¤U¤@­Ó¶µ¥Ø¬°¦ì¸m5¡F¦ì¸m5¤§Ãìµ²Ä欰3¡A¦]¦Ó¥iÃìµ²¦Ü

¦ì¸m3¡F¦p¦¹Ä~Äò¤U¥h­È¨ì¦ì¸m1¡A¨äÃìµ²Ä欰0¡A¥Nªí¦ê¦Cµ²§ô¡C

 

²ßºD¤W¡A§Ú­Ì³£¥H¹Ï2ªí¥ÜÃìµ²¦ê¦C¡A¦Ó¥H«ü¼ÐÅܼÆlist·í§@¾ã­Ó¦ê¦Cªº¦WºÙ¡F¦]

¦¹¡A¹Ï2©Ò¥Ü¤§¦ê¦CºÙ¬°¦ê¦Clist¡C

             8                5                        1

          ¢z¢w¢w¢w¢s¢w¢{   ¢z¢w¢w¢w¢s¢w¢{            ¢z¢w¢w¢w¢s¢w¢{

 list ¢w¡÷¢xCheng ¢x 5¢u¡÷ ¢xHuang ¢x 3¢u¡÷ .... ¢w¡÷¢xYang  ¢x 0¢x

          ¢|¢w¢w¢w¢r¢w¢}   ¢|¢w¢w¢w¢r¢w¢}            ¢|¢w¢w¢w¢r¢w¢}

 

¹Ï  2 Ãìµ²¦ê¦Cªº¹Ï¥Üªk

 

¢ë»y¨¥´£¨Ñ¨âºØ¤èªk»s°µ¤W­zÃìµ²¦ê¦C¡A¤@¬°§tÃìµ²Ä椧°}¦Cªí¥Üªk¡A¥t¤@¬°

«ü¼Ð§ÎºAÅܼơ]Pointer type variable¡^ªí¥Üªk¡C


¥H§tÃìµ²Ä椧°}¦Cªí¥ÜÃìµ²¦ê¦C

 

­n«Ø¥ß¹Ï 2 ¤§µ²ºcÅéÃìµ²¦ê¦C¡A¥²¶·«Å§i¤@°}¦CÅÜ¼Æ a ¦p¤U¡G

 

#define  N 100

typedef  struct listnode {

                int info;

                int next;

         } NODE;

 

NODE a[N] ;

 

¦p¦¹¤@¨Óµ¥©ó«Å§i¤F100­Ó¤¸¯Àªº°}¦CÅܼÆa¡A¦Óa¤§¨C¤@¤¸¯À¥]§t¨âÄæ¡G¸ê®ÆÄæ

info»PÃìµ²Äænext¡C³q±`§Ú­ÌºÙa¤§¨C¤@¤¸¯À¬°¸`ÂI¡]node¡^¡C

 


¨â¦h¶µ¦¡¬Û¥[

 

¼Æ¾Ç¤W¨â¦h¶µ¦¡¬Û¥[ªº°ÝÃD¡A¦p¥H¹q¸£µ{¦¡³B²z¡A¥»¨Ó¥i¥H¥Î°}¦Cªí¥Ü¡A¦ý¦]

¬°¦³®É¦h¶µ¦¡¦³«Ü¦h¯Ê¶µ¡A¦p¥Î°}¦Cªí¥Ü¦]·|®ö¶O¥D°O¾ÐÅéªÅ¶¡¡F¦]¦¹¡A¨â¦h

¶µ¦¡¬Û¥[ªº°ÝÃD¡A«Ü¾A©y¥ÎÃìµ²¦ê¦Cªí¥Ü¦h¶µ¦¡¡A­º¥ý¡A«Å§i¤T­Ó¦h¶µ¦¡«ü¼Ð

¦p¤U¡G

     

             typedef struct listnode  *NODE;

             struct listnode {

                  float  info ;

                  NODE   next;

             }

             NODE  a,b,c;

  

«h¦h¶µ¦¡¸`ÂI¥iø¥Ü¦p¤U¡G

     

             ¢z¢w¢w¢w¢w¢s¢w¢w¢w¢w¢s¢w¢w¢w¢w¢{

             ¢xa ¡÷coef¢xa ¡÷exp ¢xa ¡÷next¢x

             ¢|¢w¢w¢w¢w¢r¢w¢w¢w¢w¢r¢w¢w¢w¢w¢}

      ¨ä¤¤ a ¡÷ coef  ¥Nªí«Y¼Æ

           a ¡÷ exp   ¥Nªí­¼¾­¡A

           a ¡÷ next  ¬°Ãìµ²Äæ¡C


¨Ò¡G

 

¦h¶µ¦¡

A(x) = 2.5 x ** 10 + 12 x ** 6 + x ** 3

¥iø¥Ü¦p¤U¡G

           ¢z¢w¢w¢s¢w¢s¢{    ¢z¢w¢w¢s¢w¢s¢{    ¢z¢w¢w¢s¢w¢s¢{

     a ¢w¡÷¢x2.5 ¢x10¢x¢q¢w¡÷¢x 12 ¢x6 ¢x¢q¢w¡÷¢x  1 ¢x3 ¢x¢q¢w¡÷ NULL

           ¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}

¦A¦p¡A¦h¶µ¦¡

B(x) = 8.5 x ** 8 + 10 x ** 6 + 5.5

¥iø¥Ü¦p¤U¡G

           ¢z¢w¢w¢s¢w¢s¢{    ¢z¢w¢w¢s¢w¢s¢{    ¢z¢w¢w¢s¢w¢s¢{

     b ¢w¡÷¢x8.5 ¢x 8¢x¢q¢w¡÷¢x 10 ¢x6 ¢x¢q¢w¡÷¢x5.5 ¢x0 ¢x¢q¢w¡÷ NULL

           ¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}

¨â¦h¶µ¦¡A(x)»PB(x)¬Û¥[¡A©M¬°C(x)¤§ºtºâªk¥i¼g¦p¤U¡G

 

1.¥O«ü¼ÐÅܼÆp«ü¦V¦ê¦Ca¤§²Ä¤@­Ó¸`ÂI¡A¦P®É¥O«ü¼ÐÅܼÆq«ü¦V¦ê¦Cb¤§²Ä¤@­Ó¸`

   ÂI¡C

 

2.¤ñ¸û­¼¾­¤j¤p¡A§Y¤ñ¸ûp¡÷exp»Pq¡÷exp¤§¤j¤p¡A­Y

p¡÷exp¡Öq¡÷exp

   «h±Np¸`ÂI¤§¤º®e§Û¿ý¦Ü¦ê¦Cc¡A¨Ã±Np²¾¦Ü¤U¤@¸`ÂI¦ì¸m¡F­Y

p¡÷exp == q¡÷exp

   «h±N¸`ÂIp»Pq«Y¼ÆÄ椧©M¡A»P­¼¾­Ä椧­È¤@°_§Û¿ý¦Ü¦ê¦Cc¡AµM«áp»Pq§¡²¾¦Ü

   ¤U¤@¸`ÂI¦ì¸m¡F­Y

p¡÷exp ¡Õ q¡÷exp

 

    «h±N¸`ÂIq¤§¤º®e§Û¿ý¦Ü¦ê¦Cc¡AµM«á±Nq²¾¦Ü¤U¤@¸`ÂI¦ì¸m¡C

3.­«½Æ¨BÆJ2¡Aª½¨ì

p¡×NULL©Îq¡×NULL

4.­Yp¡×NULL¡A«h±N¦ê¦Cb³Ñ¾l³¡¥÷§Û¿ý¦Ü¦ê¦Cc¡C

 

5.­Yq¡×NULL¡A«h±N¦ê¦Ca³Ñ¾l³¡¥÷§Û¿ý¦Ü¦ê¦Cc¡C


¤U¹Ï¦C¥X A(x) »P B(x) ¬Û¥[ªº«e¤T¶µ¹Lµ{¡G

   ¢z p ¢w¡÷¢z¢w¢w¢s¢w¢s¢{    ¢z¢w¢w¢s¢w¢s¢{    ¢z¢w¢w¢s¢w¢s¢{

   ¢x       ¢x2.5 ¢x10¢x¢q¢w¡÷¢x 12 ¢x6 ¢x¢q¢w¡÷¢x 1  ¢x3 ¢x¢q¢w¡÷ NULL

   ¢x a ¢w¡÷¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}

   ¢x

   ¢x

   ¢x q ¢w¡÷¢z¢w¢w¢s¢w¢s¢{    ¢z¢w¢w¢s¢w¢s¢{    ¢z¢w¢w¢s¢w¢s¢{

 Æ«¢t       ¢x8.5 ¢x 8¢x¢q¢w¡÷¢x 10 ¢x6 ¢x¢q¢w¡÷¢x5.5 ¢x0 ¢x¢q¢w¡÷ NULL

   ¢x b ¢w¡÷¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}

   ¢x

   ¢x

   ¢x       ¢z¢w¢w¢s¢w¢s¢{

   ¢| c ¢w¡÷¢x2.5 ¢x10¢x¢q¢w¡÷ NULL   ¡] p¡÷exp ¡Ö q¡÷exp ¡^

            ¢|¢w¢w¢r¢w¢r¢}

 

                       p¢w¢{

   ¢z ¡@¡@¡@¢z¢w¢w¢s¢w¢s¢{¢|¡÷¢z¢w¢w¢s¢w¢s¢{    ¢z¢w¢w¢s¢w¢s¢{

   ¢x       ¢x2.5 ¢x10¢x¢q¢w¡÷¢x 12 ¢x6 ¢x¢q¢w¡÷¢x 1  ¢x3 ¢x¢q¢w¡÷ NULL

   ¢x a ¢w¡÷¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}

   ¢x

   ¢x

   ¢x q ¢w¡÷¢z¢w¢w¢s¢w¢s¢{    ¢z¢w¢w¢s¢w¢s¢{    ¢z¢w¢w¢s¢w¢s¢{

 Æ¬¢t       ¢x8.5 ¢x 8¢x¢q¢w¡÷¢x 10 ¢x6 ¢x¢q¢w¡÷¢x5.5 ¢x0 ¢x¢q¢w¡÷ NULL

   ¢x b ¢w¡÷¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}

   ¢x

   ¢x

   ¢x       ¢z¢w¢w¢s¢w¢s¢{     ¢z¢w¢w¢s¢w¢s¢{

   ¢| c ¢w¡÷¢x2.5 ¢x10¢x¢q¢w¡÷ ¢x8.5 ¢x 8¢x¢q¢w¡÷ NULL¡@

            ¢|¢w¢w¢r¢w¢r¢}     ¢|¢w¢w¢r¢w¢r¢}

 

            ¡]p¡÷exp ¡Õ q¡÷exp ¡^

 

                       p¢w¢{

   ¢z       ¢z¢w¢w¢s¢w¢s¢{¢|¡÷¢z¢w¢w¢s¢w¢s¢{    ¢z¢w¢w¢s¢w¢s¢{

   ¢x       ¢x2.5 ¢x10¢x¢q¢w¡÷¢x 12 ¢x6 ¢x¢q¢w¡÷¢x 1  ¢x3 ¢x¢q¢w¡÷ NULL

   ¢x a ¢w¡÷¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}

   ¢x

   ¢x                  q¢w¢{

   ¢x       ¢z¢w¢w¢s¢w¢s¢{¢|¡÷¢z¢w¢w¢s¢w¢s¢{    ¢z¢w¢w¢s¢w¢s¢{

 Æ­¢t       ¢x8.5 ¢x 8¢x¢q¢w¡÷¢x 10 ¢x6 ¢x¢q¢w¡÷¢x5.5 ¢x0 ¢x¢q¢w¡÷ NULL

   ¢x b ¢w¡÷¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}

   ¢x

   ¢x

   ¢x       ¢z¢w¢w¢s¢w¢s¢{    ¢z¢w¢w¢s¢w¢s¢{    ¢z¢w¢w¢s¢w¢s¢{

   ¢| c ¢w¡÷¢x2.5 ¢x10¢x¢q¢w¡÷¢x8.5 ¢x 8¢x¢q¢w¡÷¢x 22 ¢x 6¢x¢q¢w¡÷ NULL

            ¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}    ¢|¢w¢w¢r¢w¢r¢}

 

            ¡] p¡÷exp == q¡÷exp ¡^              


¨â¦h¶µ¬O¬Û¥[¤§¹Lµ{¤¤¡A¨C·í¦³¤@·s¸`ÂI²£¥Í¡A³£¬Oªþ¥[¦b¦ê¦Cc¤§«áºÝ¡A¦]¦¹

¦pªG¦³¤@«ü¼ÐÅܼÆÀH®É«ü¨ìc¤§³Ì«á¤@­Ó¦ê¦C¸`ÂI¦]±N¥i¬Ù¨Æ¤£¤Ö¡F¦]¦¹¤£§«¤@

¶}©l´NÅýc»Ptail¦P®É«ü¨ì¤@ªÅ¸`ÂI¡AµM«á¨Cªþ¥[¤@¸`ÂI¦Ü¦ê¦Cc¡A´NÅýtail«ü¨ì¸Ó

¸`ÂI¡]§Ytail¥Ã»·«ü¨ìc¤§³Ì«á¸`ÂI¡^¡A³Ì«á¦b±N«eºÝ¤§ªÅ¸`ÂI§R±¼§Y¥i¡C

 

 

 

   typedef struct listnode *NODE;

   struct listnode ¡a

          float info;

          NODE  next;

   ¡b


NODE polyadd(a, b)

   NODE a, b;

 ¡a

   NODE p, q, tail, c, ptr;

   p = a;

   q = b;

   tail = c = (NODE) malloc(sizeof(struct listnode));

   while (p != NULL && q!=NULL) ¡a

        ptr = (NODE)malloc(sizeof(struct listnode));

        if (p->exp > q->exp) ¡a

           ptr->coef = p->coef;

           ptr->exp  = p->exp;

           p = p->next;

        ¡b

        else if (p->exp < q->exp) ¡a

                ptr->coef = q->coef;

                ptr->exp  = q->exp;

                q = q->next;

        ¡b

        else ¡a

                ptr->coef = p->coef + q->coef;

                ptr->exp  = p->exp;

                p = p->next;

                q = q->next;

        ¡b

        tail->next = ptr;

        tail = ptr;

  ¡b

   if (p !=NULL || q!=NULL) ¡a

      if (p == NULL)

         p = q;

      while (p !=NULL) ¡a

          ptr = (NODE) malloc(sizeof(struct listnode));

          ptr->coef = p->coef;

          ptr->exp  = p->exp;

          p = p->next;

          tail->next = ptr;

          tail = ptr;

      ¡b

  ¡b

  tail->next = NULL;

  ptr = c;

  c = c->next;

  free(ptr);

  retrun(c);

¡b
¦A½ÍÃìµ²¦ê¦C

Ãìµ²¦ê¦C¦b¹ê»ÚÀ³¥Î¤è­±¡A¦³¤@¨Ç¯S§Oªº¹Bºâ¡A§Ú­Ì¤@¤@¤¶²Ð¡C

¤@¡B²M°£¤@­ÓÃìµ²¦ê¦C¡G

 

¨Ò¦p´£¤Î¤§¨â¦h¶µ¦¡¬Û¥[¡A¦b¨D¥X¤@²Õµª®×¤§«á¡A¦p±µµÛ¦A³B²z¥t¤@²Õ¦h¶µ¦¡

¬Û¥[¡A³Ì¦n±N¤w¤£¥Î¤§Ãìµ²¦ê¦C¸`ÂIÂkÁÙµ¹¹q¸£¨t²Î¡A¤~¯à§Q¥Î³o¨Ç¸`ÂIªÅ¶¡

¡Ferase()§Y¬°²M°£¨ÃÂkÁÙ¦ê¦C¸`ÂI¤§°Æµ{¦¡¡G

 

NODE erase(list)

  NODE list;

 

  NODE p;

 

  p = (NODE)malloc(sizeof(struct listnode));

 

  while (list != NULL) ¡a

      p = list;

      list = list->next;

      free(p);

  ¡b

¡b                     

 

 


¤G¡B±N¦ê¦C¤ÏÂà¡G

 

§Ú­Ì¥u­n§Q¥Î¤T­Ó«ü¼ÐÅܼơA´N¥i¥H»´©öªº±N¤@Ãìµ²¦ê¦C¤ÏÂà¡A¦Ó¤£¶·¥Î¤Î¥ô

¦ó¨ä¥L¸`ÂI©Î¦ê¦CªºÀ°¦£¡Ainvert() §Y¬°±N¦ê¦C¤ÏÂध°Æµ{¦¡¡G

 

NODE invert(list)

  NODE list;

 

  NODE p, q, r;

 

  p = (NODE)malloc(sizeof(struct listnode));

  q = (NODE)malloc(sizeof(struct listnode));

  r = (NODE)malloc(sizeof(struct listnode));

 

  p = list;

 

  while (p != NULL) ¡a

      r = q;

      q = p;

      p = p->next;

      q->next = r;

  ¡b

  return(q);

¡b

 


¤T¡B±N¨â¦ê¦CÃìµ²¦¨¤@¦ê¦C¡G

 

   NODE concatenate(x, y)

     NODE x, y;

  ¡a

     NODE p, z;

 

       p = (NODE)malloc(sizeof(struct listnode));

       z = (NODE)malloc(sizeof(struct listnode));

 

       if  (x == NULL)

            z = y;

       else¡a

            z = x;

            if  (y != NULL)¡a

                p = x;

                while (p->next != NULL)

                    p = p->next;

                p->next = y;

            ¡b

       ¡b

       return(z);

   ¡b

 

concatenate() ±N¦ê¦C x »P y¡AÃìµ²¦¨¦ê¦C z¡A¨äµ{§Ç¬°¡G

 

       1.­Y x ¬°ªÅ¦ê¦C¡A«h¦ê¦C y §Y¦¨¬°¦ê¦C z¡C

       2.­Y x ¬°«DªÅ¦ê¦C¡A«h z «ü¨ì x ¤§²Ä¤@­Ó¸`ÂI¡FµM«á¦A´ú¸Õ y ¬O§_¬°ªÅ¦ê

       ¦C¡A­Y¬O¡A«hµ²§ô¡F­Y¤£¬O¡A«h¦A´M§ä x¤§³Ì«á¸`ÂI¡A±N¨äÃìµ²¦Ü y ¤§²Ä¤@

       ­Ó¸`ÂI¡C¦]¦¹¡A¦b°õ¦æ§¹µ{¦¡¤§«á¡A·s¦ê¦C z ¤§«e¥b³¡¬° x¡A«á¥b³¡¬° y¡C


­º¸`ÂI

 

Ãìµ²¦ê¦C¦b¹ê»ÚÀ³¥Î¤è­±¡A¦³®É¶·¦b¦ê¦C­ºªþ¥[¤@ÃB¥~¸`ÂI¡A¥H¨ó§U¦ê¦C³B²z

¡A³oºØ¸`ÂI¨Ã¤£ÄÝ©ó¦ê¦C¤¤ªº¶µ¥Ø¡AºÙ¬°­º¸`ÂI (Head node)©Î¦ê¦C­º(List head)¡C¨Ò¦p¡A¦b«Ø¥ß¤@Ãìµ²¦ê¦C®É¡A¶}©l®É´N¥Î¤F¤@­ÓªÅ¥Õªº­º¸`ÂI¡Aµ¥«Ø¥ß¦n¤@¦ê

¦C«á¡A¦b±N­º¸`ÂI§R°£¡C

 

­º¸`ÂI¤§¨ä¥L¥Î³~¡A¨Ò¦p¹Ï 2¡]a¡^¡A­º¸`ÂI¤§±×½u³¡¥÷¡]¸ê®ÆÄæ¡^¥i¥Î¨ÓÀx¦s

»P¸Ó¦ê¦C¦³Ãö¤§°T®§¡F¨Ò¦p¹Ï 2¡]b¡^¡A­º¸`ÂI¸ê®ÆÄæÀx¦s¦ê¦C¸`ÂI¼Æ¥Ø¡]¤£§t

­º¸`ÂI¡^¡A¦p¦¹¤@¨Ó¡A¥u­n¦b¦ê¦C§R±¼©Î´¡¤J¤@¸`ÂI®É¡AÀH®É§ó§ï­º¸`ÂI¤§¼Æ

¥Ø¡A§Y¥iª¾¨ì¾ã­Ó¦ê¦C¤§¸`ÂI¼Æ¡C

           ¢z¢w¢w¢s¢{    ¢z¢w¢w¢s¢{    ¢z¢w¢w¢s¢{

  list ¢w¡÷¢x¢¬¢¬¢x¢q¢w¡÷¢x  7 ¢x¢q¢w¡÷¢x 10 ¢x¢q¢w¡÷ NULL

           ¢|¢w¢w¢r¢}    ¢|¢w¢w¢r¢}    ¢|¢w¢w¢r¢}

 

¡]a¡^±×½u³¡¥÷¥i¦s¦³Ãö¦ê¦C¤§¦UºØ°T®§

 

           ¢z¢w¢w¢s¢{    ¢z¢w¢w¢s¢{    ¢z¢w¢w¢s¢{    ¢z¢w¢w¢s¢{

  list ¢w¡÷¢x 3  ¢x¢q¢w¡÷¢x 55 ¢x¢q¢w¡÷¢x 50 ¢x¢q¢w¡÷¢x 30 ¢x¢q¢w¡÷ NULL

           ¢|¢w¢w¢r¢}    ¢|¢w¢w¢r¢}    ¢|¢w¢w¢r¢}    ¢|¢w¢w¢r¢}

 

¡]b¡^3¬°¦ê¦Clist¤§¸`ÂI¼Æ

 

           ¢z¢w¢w¢s¢{

  list ¢w¡÷¢x    ¢x¢q¢w¡÷ NULL

           ¢|¢w¢w¢r¢}

 

¡]c¡^¥u¦³­º¸`ÂI¤§ªÅ¦ê¦C

 

           ¢z¢w¢w¢s¢{    ¢z¢w¢w¢s¢{    ¢z¢w¢w¢s¢{    ¢z¢w¢w¢s¢{

  list ¢w¡÷¢x    ¢x¢q¢w¡÷¢x 55 ¢x¢q¢w¡÷¢x 50 ¢x¢q¢w¡÷¢x 30 ¢x¢q¢w¡÷ NULL

           ¢|¢q¢w¢r¢}    ¢|¢w¢w¢r¢}    ¢|¢w¢w¢r¢}    ¢|¢w¢w¢r¢}

             ¢x                                        ¡ô

             ¢|¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

 

¡]d¡^­º¸`ÂI¤§¨âÄ槡¬°«ü¼Ð§ÎºA

 

¹Ï 2    §t­º¸`ÂI¤§¦ê¦C


¹Ï 2¡]c¡^¥Nªí¤@ªÅ¦ê¦C¡]¦]¬°­º¸`ÂI¨Ã«D¦ê¦C¤§¶µ¥Ø¡^¡F¹Ï 2¡]d¡^¹ê¬°¤@Ãìµ²

¦î¦Cµ²ºc¡A¨ä­º¸`ÂI¤§¨âÄ槡¬°«ü¼Ð§ÎºA¡F¥Ñ¦¹¥iª¾¡A­º¸`ÂI»P¨ä«á¦ê¦C¸`ÂI

¡A¨Ã¤£¶·¦³¬Û¦Pªº¸ê®Æ§ÎºA¡F´«¨¥¤§¡A­º¸`ÂI¤]¥i«Å§i¦¨¿W¥ßÅܼơA»P¦ê¦C¸`

ÂI¤À¶}¡A¨Ò¦p¡G

 

         typedef struct  queue  *QUEUE ;

         struct queue  {

                       int info ;

                       QUEUE next ;

                       }

 

         typedef struct  quehead  QUEHEAD ;

         struct quehead  {

                          QUEUE front, rear ;

                         }

         QUEHEAD  head ;

         QUEUE p, q ;

  

 

      ¤W­zÅÜ¼Æ head, p,»P q ¨ä¹ê§¡¬°«ü¼Ð§ÎºAÅܼơA¦]¬° head ¤§¨âÄæ

 

            head.front »P head.rear

 

      »P p, q¤§§ÎºA¦P¬° QUEUE¡A¥L­Ì¤§Ãö«Y¥i¥Î¤U¹Ïªí¥Ü¡G

                             p              q

               ¢z¢w¢{        ¡õ             ¡õ

               ¢x  ¢x      ¢z¢w¢s¢{       ¢z¢w¢s¢{

    head.front ¢x  ¢q¢w¢w¡÷¢x5 ¢x¢q¢w¢w¡÷ ¢x10¢x¢q¢w¢w¡÷ NULL

               ¢x  ¢x      ¢|¢w¢r¢}       ¢|¢w¢r¢}

               ¢u¢w¢t                        ¡ô

               ¢x  ¢x                        ¢x

    head.rear  ¢x  ¢q¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢}

               ¢x  ¢x

               ¢|¢w¢}

 


¤W¹Ï¥i¥H¤U¦Cµ{¦¡¤ù¬q¨Ó¹F¦¨¡G

 

p = (QUEUE) malloc (sizeof (sruct queue)) ;

head = (QUEHEAD) malloc (sizeof (QUEHEAD)) ;

head.front = p ;

p -> info = 5 ;

q = (QUEUE) malloc (sizeof (sruct queue)) ;

q -> info = 10;

q -> next = NULL ;

p -> next = q ;

head.rear = q ;

 

¥H­º¸`ÂI¤§Æ[©À»s§@¦ê¦C¡A¦³§Q©ó§R°£¦ê¦C²Ä¤@­Ó¸`ÂI¡A©Î¦b¦ê¦C«eºÝ¥[¤J¤@

·s¸`ÂI¡A¬O¤@ºØ±`¥Îªº³nÅé§Þ¥©¡C