C Primer Plus È«ÊéÔ´´úÂ루Áù£©

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

/* compare.c -- this will work */

/*

#include <stdio.h>

#include <string.h>  

#define ANSWER "Grant"

#define MAX 40

int main(void)

{

   char try[MAX];

 

   puts("Who is buried in Grant's tomb?");

   gets(try);

   while (strcmp(try,ANSWER) != 0)                      //here is the critical

   {

       puts("No, that's wrong. Try again.");

       gets(try);

   }

   puts("That's right!");

   

   return 0;

}

 

*/

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

/* compback.c -- strcmp returns */

/*

#include <stdio.h>

#include <string.h>

int main(void)

{

 

   printf("strcmp(\"A\", \"A\") is ");

   printf("%d\n", strcmp("A", "A"));

 

   printf("strcmp(\"A\", \"B\") is ");

   printf("%d\n", strcmp("A", "B"));

 

   printf("strcmp(\"B\", \"A\") is ");

   printf("%d\n", strcmp("B", "A"));

 

   printf("strcmp(\"C\", \"A\") is ");

   printf("%d\n", strcmp("C", "A"));

 

   printf("strcmp(\"Z\", \"a\") is ");

   printf("%d\n", strcmp("Z", "a"));

   

   printf("strcmp(\"apples\", \"apple\") is ");

   printf("%d\n", strcmp("apples", "apple"));

 

   return 0;

}

*/

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

 

/* quit_chk.c -- beginning of some program */

/*

#include <stdio.h>

#include <string.h>

#define SIZE 81

#define LIM 100

#define STOP "quit"

int main(void)

{

   char input[LIM][SIZE];

   int ct = 0;

 

   printf("Enter up to %d lines (type quit to quit):\n", LIM);

  // while (ct < LIM && gets(input[ct]) != NULL && strcmp(input[ct],STOP) != 0)

   while(ct< LIM && gets(input[ct])!=NULL && input[ct][0]!='\0')

      {

       ct++;

   }

   printf("%d strings entered\n", ct);

 

   return 0;

}

*/

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

/* copy2.c -- strcpy() demo */                         // read it carefully

/*

#include <stdio.h>

#include <string.h>   

#define WORDS "beast"

#define SIZE 40

 

int main(void)

{

   const char * rig = WORDS;

   char copy[SIZE] = "Be the best that you can be.";

   char * ps;

 

   puts(orig);

   puts(copy);

   ps = strcpy(copy + 7, orig);

   puts(copy);

   puts(ps);

  

   return 0;

}

*/

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

 

 

/* copy3.c -- strncpy() demo */

/*

#include <stdio.h>

#include <string.h>

#define SIZE 40

#define TARGSIZE 7

#define LIM 5

int main(void)

{

   char qwords[LIM][TARGSIZE];

   char temp[SIZE];

   int i = 0;

  

   printf("Enter %d words beginning with q:\n", LIM);

   while (i < LIM && gets(temp))

   {

       if (temp[0] != 'q')

           printf("%s doesn't begin with q!\n", temp);

       else

       {

           strncpy(qwords[i], temp, TARGSIZE - 1);

           qwords[i][TARGSIZE - 1] = '\0';

           i++;

       }

   }

   puts("Here are the words accepted:");

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

       puts(qwords[i]);

  

   return 0;

}

*/

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

 

/* format.c -- format a string */

/*

#include <stdio.h>

#define MAX 20

 

int main(void)

{

   char first[MAX];

   char last[MAX];

   char formal[2 * MAX + 10];

   double prize;

 

   puts("Enter your first name:");

   gets(first);

   puts("Enter your last name:");

   gets(last);

   puts("Enter your prize money:");

   scanf("%lf", &prize);

   sprintf(formal, "%s, %-19s: $%6.2f\n", last, first, prize);       // sprintf() you should take care of the function

   puts(formal);

   

   return 0;

}

*/

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

 

/* repeat.c -- main() with arguments */

/*

#include <stdio.h>

int main(int argc, char *argv[])

{

   int count;

 

   printf("The command line has %d arguments:\n", argc - 1);       // just test it in WIN-TC

   for (count = 1; count < argc; count++)

       printf("%d: %s\n", count, argv[count]);

   printf("\n");

  

   return 0;

}

*/

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

 

/* strcnvt.c -- try strtol() */

/*

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

   char number[30];

   char * end;

   long value;

 

   puts("Enter a number (empty line to quit):");

   while(gets(number) && number[0] != '\0')

   {

       value = strtol(number, &end, 10); // base 10

       printf("value: %ld, stopped at %s (%d)\n",

                     value, end, *end);

       value = strtol(number, &end, 16); // base 16

       printf("value: %ld, stopped at %s (%d)\n",

                     value, end, *end);

       puts("Next number:");

   }

   puts("Bye!\n");

   

   return 0;

}

*/

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

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

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

// chapter 12´æ´¢Àà¡¢Á´½ÓºÍÄÚ´æ¹ÜÀí

/* hiding.c -- variables in blocks */

/*

#include <stdio.h>

int main()

{

   int x = 30;    

   

   printf("x in outer block: %d\n", x);

   {

       int x = 77;

       printf("x in inner block: %d\n", x);

   }

   printf("x in outer block: %d\n", x);

   while (x++ < 33)

   {

       int x = 100;

       x++;

       printf("x in while loop: %d\n", x);

   }

   printf("x in outer block: %d\n", x);   // will print 34 not 33

   

   return 0;

}

*/

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

 

/* global.c -- uses an external variable */

/*

#include <stdio.h>

int units = 0;        // an external variable   

void critic(void);

int main(void)

{

  // extern int units;     // an optional redeclaration

 

   printf("How many pounds to a firkin of butter?\n");

   scanf("%d", &units);

   while ( units != 56)

       critic();

   printf("You must have looked it up!\n");

  

   return 0;

}

 

void critic(void)

{

   // optional redeclaration omitted

   printf("No luck, chummy. Try again.\n");

   scanf("%d", &units);


TAG:

 

ÆÀ·Ö£º0

ÎÒÀ´ËµÁ½¾ä

Open Toolbar