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

hello world!

文章分类

全部博文(63)

分类: C/C++

2011-05-26 20:18:46

The C programming Language-第一章学习

First point:

The next program uses the formula C=(5/9)(F-32)  to print  the  following  table of Fahrenheit temperatures  and their centigrade or Celsius equivalents:

1  -17

20  -6

40  4

60  15

80  26

100  37

120  48

140  60

160  71

180  82

200  93

220  104

240  115

260  126

280  137

300  148

Width and precision may be omitted from a specification:%6f says that the number is to be at least six characters wide;%.2f specifies two characters after the decimal point, but the width is not constrained; and %f merely says to print the number as floating point.

%d    print  as decimal  integer

%6d   print  as decimal  integer,  at least 6 characters wide

%f     print  as floating  point

%6f    print  as floating  point,  at least 6 characters wide

%.2f   print  as floating  point, 2 characters  after decimal  point

%6.2f  print  as floating  point,  at least 6 wide  and 2 after decimal  point

Among others, printf also recognizes %o for octal , %x  for hexadecimal , %c  for character, %s for character string and %% for itself.

1.2 my code:( Exercise 1-3. Modify the temperature conversion program to print a heading above the table.)

void formula(float lower,float upper,float step)

{

       float fat,i;

       float cent;

      

       printf("*****float test*****\n");      

      

       for(i=lower;i<=upper;i+=step)

       {

              fat=i;

              cent=(5*(fat-32))/9;

              printf("  %3.1f\t    %6.1f   \n",fat,cent); //f浮点数float, x.x输出格式

       }

      

}

 

Next point:

       The simplest example is a program that copies its input to its output one character at a time:

read a character

    while (charater is not end-of-file indicator)

        output the character just read

        read a character

1.5 my code:()

void copy_input_to_output()

{

       long nc;

       int c;

      

       printf("*****getchar and ++i test*****\n"); 

      

       for(nc=0;((c=getchar())!=EOF);){  //XPctrl+zEOFLinux下为组合键Ctrl+D

              putchar(c);

              printf("\n%ld\n",++nc);   //++i:先将i增加1后再在此语句中使用,

                                                        //i++:先在此语句中使用了i后,再令其增加1

       }

}

 

Next point:

1.5 my code:(Exercise 1-8. Write a program to count blanks, tabs, and newlines.)

void calculate_tab_space_line()

{

       int c;

       long space=0;

       long tab=0;

       long line=0;

      

       printf("*****calculate_tab_space_line test*****\n"); 

       c=getchar();

      

       while(c!=EOF){  //XPctrl+zEOFLinux下为组合键Ctrl+D

              if(c==' ')

                     ++space;   //++i:先将i增加1后再在此语句中使

              if(c=='\t')

                     ++tab;   //++i:先将i增加1后再在此语句中使  

              if(c=='\n')

                     ++line;   //++i:先将i增加1后再在此语句中使                                                 

              printf("space Num: %ld\n",++space);

              printf("tab   Num: %ld\n",++tab);

              printf("line  Num: %ld\n",++line);

              c=getchar();

       }

}

 

Next point:

1.5 my code:( Exercise 1-9. Write a program to copy its input to its output, replacing each string of one ormore blanks by a single blank.)

void nspace_to_space() 

{

       int c,s=0;

      

       printf("***** N space to 1 space*****\n");  

       c=getchar();

      

       while(c!=EOF){  //XPctrl+zEOFLinux下为组合键Ctrl+D      

             

              if(c==' ')

                     ++s;

                    

              if(s==0)

                     putchar(c);

              if(s>=1){           //为空格时不做输出,直到非空时输出一个空格加输入的stream

                     if(c!=' '){

                            putchar(' ');

                            putchar(c);

                            s=0;

                     }

              }

             

              c=getchar();

       }     

}

 

Next point:

1.5 my code:( Exercise 1-10. Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b, and each backslash by \\. This makes tabs and backspaces visible in an unambiguous way.)

void copy_show()

{

       int c;

       long space=0;

       long tab=0;

       long line=0;

      

       printf("*****copy show test*****\n");   

       c=getchar();

      

       while(c!=EOF){  //XPalt+zEOFLinux下为组合键alt+D

              printf("\n****\n");

              if(c=='\t'){

                     putchar('\\');      

                     putchar('t');

              }

              else {

                     if(c=='\n'){

                            putchar('\\');      

                            putchar('n');

                     }

                     else

                            putchar(c);                                     

              }

              c=getchar();

              //printf("\n");

       }

}

 

Next point:

The next in our series of useful programs counts lines, words, and characters, with the loose definition that a word is any sequence of characters that does not contain a blank, tab or newline. This is a bare-bones version of the UNIX program wc.

1.5 my code: ()

#define IN_WORD  1     //inside a word

#define OUT_WORD 0            //outside a word

void word_count()

{

       int c,nl,nw,nc,state; //line word character

 

       printf("*****word counting test*****\n");

      

       state = OUT_WORD;

       nl=nw=nc=0;

      

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

              ++nc;

              if(c=='\n')

                     ++nl;

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

                     state = OUT_WORD;

              }

              else if(state==OUT_WORD){

                            state=IN_WORD;

                            ++nw;                

                     }

       }

       printf("line Num :%d\nword Num :%d\nchar Num :%d\n",nl,nw,nc);

}

 

Next point:

1.5 my code:( Exercise 1-12. Write a program that prints its input one word per line.)

void print_word()

{

       int c,state;

      

       printf("*****print one word per line*****\n");   

       state=OUT_WORD;

      

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

             

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

                     state = OUT_WORD; 

                     putchar('\n');

              }     

              else {

                            state=IN_WORD;

                            putchar(c);

                     }

       }

}

 

Next point:

Let is write a program to count the number of occurrences of each digit, of white space characters (blank, tab, newline), and of all other characters. This is artificial, but it permits us to illustrate several aspects of C in one program.

There are twelve categories of input, so it is convenient to use an array to hold the number of occurrences of each digit, rather than ten individual variables. Here is one version of the

program:

1.6 my code:()

 

/*

 *count digits,white,nother

 */

void arry()

{

       int c,i,nwhite,mother;

       int ndigit[10];

 

       printf("*****arry test*****\n");

       nwhite=mother=0;

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

              ndigit[i]=0;

             

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

              if(c>='0' && c<='9')

                     ++ndigit[c-'0'];

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

                            ++nwhite;

                     else

                            ++mother;

              printf("digits=");

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

                     printf(" %d",ndigit[i]);

              printf(",whie space =%d,other=%d\n",nwhite,mother);

             

       }

}

 

Next point:

       In getline, the arguments are declared by the line

int getline(char s[], int lim);

which specifies that the first argument, s, is an array, and the second, lim, is an integer. The purpose of supplying the size of an array in a declaration is to set aside storage. The length of an array s is not necessary in getline since its size is set in main. getline uses return to send a value back to the caller, just as the function power did. This line also declares that getline returns an int; since int is the default return type, it could be omitted.

Some functions return a useful value; others, like copy, are used only for their effect and return no value. The return type of copy is void, which states explicitly that no value is returned. getline puts the character '\0' (the null character, whose value is zero) at the end of the array it is creating, to mark the end of the string of characters. This conversion is also used by the C language: when a string constant like

   "hello\n"

appears in a C program, it is stored as an array of characters containing the characters in the string and terminated with a '\0' to mark the end.

The %s format specification in printf expects the corresponding argument to be a string represented in this form. copy also relies on the fact that its input argument is terminated with a '\0', and copies this character into the output. It is worth mentioning in passing that even a program as small as this one presents some sticky design problems. For example, what should main do if it encounters a line which is bigger than its limit? getline works safely, in that it stops collecting when the array is full, even if no newline has been seen. By testing the length and the last character returned, main can determine whether the line was too long, and then cope as it wishes. In the interests of brevity, we have ignored this issue.

1.9 my code:()

/**

getline(s,lim) : get a line form input.

       s          : the string of input.

      lim        : the maximum of a line length.

       returns    : chars of the string.

 **/

int getline(char s[],int lim)   //得到一行

{

       int c,i;

      

       for(i=0;(i

              s[i]=c;

       }

       if(c=='\n'){

              s[i++]=c;

       }

       s[i]='\0';

       //printf("line: %s\n",s);

       return i;

}

 

Next point:

1.9 my code:()

/**

copy(to,from) : copy a line.

 **/

void copy(char to[],char from[])

{

       int i=0;

       while((to[i]=from[i])!='\0')

              ++i;

       printf("copy over\n");

}

 

Next point:

1.9 my code:(the simplest implement of copy() and getline())

void char_arrays()

{

       int len,max;

       char line[MAXLINE],longest[MAXLINE];

 

       printf("*****char arrays*****\n");

      

       max=0;

       if((len=getline(line,MAXLINE))>0)

              if(len>max){

                     max=len;

                     copy(longest,line);

              }

       printf("while over\n");

       if(max>0)

              printf("%s\n",longest);

      

}

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