Chinaunix首页 | 论坛 | 博客
  • 博客访问: 301759
  • 博文数量: 63
  • 博客积分: 1482
  • 博客等级: 上尉
  • 技术积分: 1185
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-12 19:06
个人简介

hello world!

文章分类

全部博文(63)

分类: C/C++

2011-07-16 09:15:49

The C programming language –第三章学习 Chapter 3 – Control Flow

3.1 Statements and blocks:

In C, the semicolon is a statement terminator, rather than a separator as it is in languages like Pascal. Braces { and } are used to group declarations and statements together into a compound statement, or block, so that they are syntactically equivalent to a single statement.

3.2 If-Else:

The if-else statement is used to express decisions. Formally the syntax is

   if (expression)

       statement1

   else

       statement2

where the else part is optional. The expression is evaluated; if it is true (that is, if expression has a non-zero value), statement1 is executed. If it is false (expression is zero) and if there is an else part, statement2 is executed instead.

Since an if tests the numeric value of an expression, certain coding shortcuts are possible. The

most obvious is writing

   if (expression)

instead of

   if (expression != 0)

Sometimes this is natural and clear; at other times it can be cryptic.  

3.3 Else-If:

The construction

   if (expression)

       statement

   else if (expression)

       statement

   else if (expression)

       statement

   else if (expression)

       statement

   else

       statement

Exercise 3-1. Our binary search makes two tests inside the loop, when one would suffice (at the price of more tests outside.) Write a version with only one test inside the loop and measure

 

3.4 Switch:

The switch statement is a multi-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly.

   switch (expression) {

       case const-expr: statements

       case const-expr: statements

       default: statements

   }

Exercise 3-2. Write a function escape(s,t) that converts characters like newline and tab into visible escape sequences like \n and \t as it copies the string t to s. Use a switch. Write a function for the other direction as well, converting escape sequences into the real characters.

//#include   //itoa

 

/*

 *escape(s,t):converts characters like newline and tab into visible

 *                  escape sequences like \n and \t as it copies the string t to s.

 */

int escape(char s[],char t[])

 {

      int i,j;

     

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

              NULL;    

     

       printf("the long of s1:%d!\n",i);

       for(j=0;t[j]!='\0';j++)

       {

              switch(t[j]){

                     case '\n':

                            s[i]='\\';

                            ++i;

                            s[i]='n';

                            ++i;

                            break;                 

                     case '\t':

                            s[i]='\\';

                            ++i;

                            s[i]='t';

                            ++i;

                            break;

                     default:

                            s[i]=t[j];

                            ++i;

                            break;

              }

       }

       s[i]='\0'; 

      

       printf("result:%s \n",s);

      

 }

3.5 Loops- While and For:

We have already encountered the while and for loops. In

   while (expression)

       statement

the expression is evaluated. If it is non-zero, statement is executed and expression is re-evaluated. This cycle continues until expression becomes zero, at which point execution resumes after statement. The for statement

   for (expr1; expr2; expr3)

       statementre

As a larger example, here is another version of atoi for converting a string to its numeric equivalent. This one is slightly more general than the one in Chapter 2; it copes with optional leading white space and an optional + or - sign. (Chapter 4 shows atof, which does the same conversion for floating-point numbers.)

The structure of the program reflects the form of the input:

   skip white space, if any

   get sign, if any

   get integer part and convert it

Each step does its part, and leaves things in a clean state for the next. The whole process terminates on the first character that could not be part of a number.

 

/*

 *   itoa: convert n to characters in s ,occupy x blank

   */

 

 int itoa(int n,char s[],int x)

 {

      int i=0,j,sign,t,z;  // z:char numbers after convert

     

      if((sign = n)<0)

             n = -n;

            

       do{

              s[i++] = n%10+'0';

       }while((n /= 10)>0);

      

       if(sign <0)

              s[i++]='-';

       s[i]='\0';

      

       printf("debug1:%s\n",s);

      

       z=i;

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

       j=i-1;

       //reverse(s);

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

       {

              t=s[i];

              s[i]=s[j-i];

              s[j-i]=t;

       }

       printf("debug2:%s\n",s);

      

       if(z  //0123+1   12345

       {

              s[x]='\0';

              printf("j:%d\n",j);

              for(i=(x-1);i>=(x-z);i--)

              {

                     s[i]=s[j--];

              }

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

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

                     s[i]=' ';

                    

              printf("result:%s\n",s);

              return x;

       }

       else

              return z;

      

 }

Exercise 3-3. Write a function expand(s1,s2) that expands shorthand notations like a-z in the string s1 into the equivalent complete list abc...xyz in s2. Allow for letters of either case and digits, and be prepared to handle cases like a-b-c and a-z0-9 and -a-z. Arrange that a leading or trailing - is taken literally.

 

3.6 loops-Do-While:

The syntax of the do is

   do

       statement

   while (expression);

Exercise 3-4. In a two's complement number representation, our version of itoa does not handle the largest negative number, that is, the value of n equal to -(2wordsize-1). Explain why not. Modify it to print that value correctly, regardless of the machine on which it runs.

/*

 *itoa: convert n to characters in s ,occupy x blank

 */

 

 int itoa(int n,char s[],int x)

 {

   int i=0,j,sign,t,z;  // z:char numbers after convert

  

   if((sign = n)<0)

          n = -n;

         

    do{

           s[i++] = n%10+'0';

    }while((n /= 10)>0);

   

    if(sign <0)

           s[i++]='-';

    s[i]='\0';

   

    printf("debug1:%s\n",s);

   

    z=i;

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

    j=i-1;

    //reverse(s);

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

    {

           t=s[i];

           s[i]=s[j-i];

           s[j-i]=t;

    }

    printf("debug2:%s\n",s);

   

    if(z  //0123+1   12345

    {

           s[x]='\0';

           printf("j:%d\n",j);

           for(i=(x-1);i>=(x-z);i--)

           {

                  s[i]=s[j--];

           }

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

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

                  s[i]=' ';

                 

           printf("result:%s\n",s);

           return x;

    }

    else

           return z;

   

 }

Exercise 3-5. Write the function itob(n,s,b) that converts the integer n into a base b character representation in the string s. In particular, itob(n,s,16) formats s as a hexadecimal integer in s.

 

3.7 Break and Continue:

It is sometimes convenient to be able to exit from a loop other than by testing at the top or bottom. The break statement provides an early exit from for, while, and do, just as from switch. A break causes the innermost enclosing loop or switch to be exited immediately.

The continue statement is related to break, but less often used; it causes the next iteration of the enclosing for, while, or do loop to begin. In the while and do, this means that the test part is executed immediately; in the for, control passes to the increment step. The continue statement applies only to loops, not to switch. A continue inside a switch inside a loop causes the next loop iteration.

 

阅读(1456) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~