C Primer Plus È«ÊéÔ´´úÂ루¶þ£©

ÉÏһƪ / ÏÂһƪ  2012-03-19 09:26:45 / ¸öÈË·ÖÀࣺѧϰÈÕÖ¾

/* width.c -- field widths */

/*

#include <stdio.h>

#define PAGES 931

int main(void)

{

   printf("*%d*\n", PAGES);          // still will print 931

   printf("*%2d*\n", PAGES);           // still will print 931

   printf("*%10d*\n", PAGES);

   printf("*%-10d*\n", PAGES);

 

   return 0;

}

 

*/

//////////////////////////////////////////

/////////////////////////////////////////////////////////////

//chapter 05        ÔËËã·û¡¢±í´ïʽºÍÓï¾ä

/* add_one.c -- incrementing: prefix and postfix */

/*

#include <stdio.h>

int main(void)

{

   int ultra = 0, super = 0;

 

   while (super < 5)

   {

       super++;

       ++ultra;

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

   }

   

   return 0;

}

*/

///////////////////////////////////////

/* addemup.c -- four kinds of statements */

/*

#include <stdio.h>

int main(void)               // finds sum of first 20 integers

{

   int count, sum;          

   int num=0;

   count = 0;               

   sum = 0;                 

   while (count++ < 20)                 // you should take care of

   {

             num++;

             sum = sum + count;         

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

   }

      printf(" the time of loop is %d\n",num);

   return 0;

}

*/

//////////////////////////////////////////////////////////////////////

 

/* bottles.c -- counting down */

/*

#include <stdio.h>

#define MAX 10

int main(void)

{

   int count = MAX + 1;

 

   while (--count > 0) {

       printf("%d bottles of spring water on the wall, "

              "%d bottles of spring water!\n", count, count);

       printf("Take one down and pass it around,\n");

       printf("%d bottles of spring water!\n\n", count - 1);

   }

 

   return 0;

}

 

*/

//////////////////////////////////////////////////////////////////////

 

 

/* shoes1.c -- converts a shoe size to inches */

/*

#include <stdio.h>

#define ADJUST 7.64

#define SCALE 0.325

int main(void)

{

   double shoe, foot;

 

   shoe = 9.0;

   foot = SCALE * shoe + ADJUST;

   printf("Shoe size (men's)   foot length\n");

   printf("%10.1f %15.2f inches\n", shoe, foot);

 

   return 0;

}

*/

///////////////////////////////////////////////////////

/* shoes2.c -- calculates foot lengths for several sizes */

/*

#include <stdio.h>

#define ADJUST 7.64

#define SCALE 0.325

int main(void)

{

   double shoe, foot;

 

   printf("Shoe size (men's)   foot length\n");

   shoe = 3.0;

   while (shoe < 18.5)     

   {

       foot = SCALE*shoe + ADJUST;

       printf("%10.1f %15.2f inches\n", shoe, foot);

       shoe = shoe + 1.0;

   }                    

   printf("If the shoe fits, wear it.\n");

 

   return 0;

}*/

//////////////////////////////////////////////////////////////////////

/* golf.c -- golf tournament scorecard */

/*

#include <stdio.h>

int main(void)

{

   int jane, tarzan, cheeta;

 

   cheeta = tarzan = jane = 68;

   printf("                 cheeta  tarzan   jane\n");

   printf("First round score %4d %8d %8d\n",cheeta,tarzan,jane);

 

   return 0;

}

*/

///////////////////////////////////////////////

/* squares.c -- produces a table of first 20 squares */

/*                                                // have fun ,you can learn much

#include <stdio.h>

int main(void)

{

   int num = 1;

 

   while (num < 21)

   {

 

       printf("%4d %6d\n", num, num * num);

       num = num + 1;

   }

 

   return 0;

}

*/

///////////////////////////////////////////////

/* wheat.c -- exponential growth */

/*                                                      //ÆåÅÌÔù´óÃ×£¬¹úÍõɵÑÛÀ²

#include <stdio.h>

#define SQUARES 64   // squares on a checkerboard  

#define CROP 1E15    // US wheat crop in grains    

int main(void)

{

   double current, total;

   int count = 1;

 

   printf("square    grains      total    ");

   printf("fraction of \n");

   printf("          added       grains     ");

   printf("US total\n");

   total = current = 1.0; // start with one grain  

   printf("%4d %13.2e %12.2e %12.2e\n", count, current,

          total, total/CROP);

   while (count < SQUARES)

   {

       count = count + 1;

       current = 2.0 * current;

                    // double grains on next square

       total = total + current;    // update total

       printf("%4d %13.2e %12.2e %12.2e\n", count, current,

              total, total/CROP);

    }

    printf("That's all.\n");

  

    return 0;

}

*/

///////////////////////////////////////////////

/* divide.c -- divisions we have known */

/*

#include <stdio.h>

int main(void)

{

    printf("integer division: 5/4  is %d \n", 5/4);

    printf("integer division: 6/3  is %d \n", 6/3);

    printf("integer division: 7/4  is %d \n", 7/4);

       printf("integer division: 7/-4  is %d \n", 7/-4);//print -1

       printf("integer division: -7/4  is %d \n", -7/4);//print -1

    printf("floating division: 7./4. is %1.2f \n", 7./4.);

    printf("mixed division:   7./4 is %1.2f \n", 7./4);

   printf("mixed division:   -7./4 is %1.2f \n", -7./4); // print -1.75

       printf("mixed division:   7./-4 is %1.2f \n", 7./-4); //print -1.75

       printf("mixed division:   -7./-4 is %1.2f \n", -7./-4);

    return 0;

}

*/

///////////////////////////////////////////////

// sizeof.c -- uses sizeof operator

// uses C99 %z modifier -- try %u or %lu if you lack %zd              //%z don't work

/*

#include <stdio.h>

int main(void)

{

   int n = 0;

   size_t intsize;

 

   intsize = sizeof (int);

   printf("n = %d, n has %u bytes; all ints have %u bytes.\n",

        n, sizeof n, intsize );

        

   return 0;

}

*/

///////////////////////////////////////////////

/* post_pre.c -- postfix vs prefix */

/*

#include <stdio.h>

int main(void)

{

   int a = 1, b = 1;

   int aplus, plusb;

 

   aplus = a++;   

   plusb = ++b;   

      printf("a  aplus  b  plusb \n");

   printf("%1d %5d %5d %5d\n", a, aplus, b, plusb);   // 2  1 2 2

  

   return 0;

}

*/

///////////////////////////////////////////////

/* convert.c -- automatic type conversions */

/*

#include <stdio.h>

int main(void)

{

   char ch;

   int i;

   float fl;

TAG:

 

ÆÀ·Ö£º0

ÎÒÀ´ËµÁ½¾ä

Open Toolbar