zoukankan      html  css  js  c++  java
  • C程序设计语言笔记-第一章

     The C Programming language notes

    一 基础变量类型、运算符和判断循环

            char                 字符型  character               --> char c = ' ' ;            [ASCII码 0-255] 

      short int long    整数形  interger             --> int num = 120;         [ unsigned 无符号表示]

           float double       浮点型(单精度,双精度)--> float celsius = 12.3;

           加减乘除取余, 与或非位运算, 赋值大于小于等于, if else,switch case, for do  while continue break goto

    二 习题代码

       1.1 摄氏转华氏,华氏转摄氏以及逆序输出代码

    #include <stdio.h>
    
    #define LOWER 0   /* 下限 */
    #define UPPER 300 /* 上限  */
    #define STEP 20   /* 步长 */
    typedef float ElementType;
    
    void fahrToCelsius()
    {
        ElementType fahr, celsius;
    
        fahr = LOWER;
        // %o oct-8 | %x hex-16 | %c %s | %f %e(科学) | %d %i   
        printf("
    fahr to celsius:
    ");
        while ( fahr <= UPPER     ) {
            celsius = (5.0 / 9.0) * (fahr- 32.0);
            // %6.2f 6个字符宽  
            printf("%6.2f	%6.2f
    ", fahr, celsius);
            fahr = fahr + STEP;
        }
    }
    
    void celsiusToFahr()
    {       /* 1.4 摄氏温度转华氏 */
        ElementType fahr, celsius;
        celsius = UPPER;    
        printf("
    celsius to fahr:
    ");    
        for ( ; celsius >= LOWER ; celsius -= STEP ) {
            fahr = (9.0 / 5.0)*celsius + 32.0;
            printf("%6.2f	%6.2f
    ", celsius, fahr);
        }    
    }
    
    int main(int argc, char *argv[])
    {    
        fahrToCelsius();
        celsiusToFahr();
        return 0;
    }

    1.2 标准输入输出库的单个字符获取和输出

      getchar()从STDIN中获取单个字符并返回其值,如果到达文件尾则返回EOF, putchar( cha ) 将单个字符cha输出到STDOUT

        printf("This is:%d
    ", EOF);  /* EOF define as -1 */

      gets()可获取包含空格的字符串,并返回 char *指针, puts()则将字符串输出

         printf()和scanf()函数原型如下,分别以特定格式输出和获取输入

      int printf( const char *format, ... );
      int scanf( const char *format, ... );

       字符获取输出程序如下: getchar()直到在WIndows上按下CTRL+Z产生EOF输入才停止

    void getPutChar()
    {   /* 1.5 字符输入输出  copy input to output */
        int c; /* c要足够大,能存储任何getchar()的返回值 */
        while ( (c = getchar())!= EOF ) {
            // 赋值表达式的值为赋值后左边变量的值 
            putchar(c); /* EOF end of file */
        }
    } 

    1.3 标准输入输出库的单个字符获取和输出

          统计字符流中的空白、换行和制表符

    void cntSpaceTable()
    {   /* 1-8 count space table and newline chars */
        int c; /* big volume */
        int nl, nt, ns;
        nl = ns = nt = 0;
        while ( (c = getchar()) != EOF ) {
            if ( c == '
    ' ) {
                nl++;
            } else if ( c == '	' ) {
                nt++;
            } else if ( c == ' ' ) {
                ns++;
            }
        }
        printf("num of space, table, newline: %d, %d, %d
    " 
              ,  ns, nt, nl);
    }

          1-9将字符流中的多个空格替换为一个空格,  1-10将字符进行替换

    void spaceChange()
    {   /* 1-9 alterante mutilple sapces into one space */
        int c, cs = 0;  /* cs -> count space */
        while ( (c = getchar()) != EOF ) {
            if ( c == ' ' ) {
                // print the first space, omit other space
                if ( cs == 0 ) {
                    putchar( c );
                    cs++;
                }
            } else {
                cs = 0;  // 恢复计数 
                putchar( c );
            }
        }    
    }
    
    void charChange()
    {   /* 1-10 alterante table back and slash */
        int c;
        while ( (c = getchar()) != EOF ) {
            switch( c ) {
                case '	': printf( "\t" );
                              break;
                case '': printf( "\b" );
                              break;    
                            case '\': printf( "\\ ");
                                          break;      
                default:   putchar(c);
                              break;
            }
        }
        printf("End
    ");    
    }

    1.4 统计单词数量

    /* 1.5.4 count lines, words, and chars in input */
    void countWords()
    {    /* 单词计数,单词不包含空格、制表、换行符 */
        int c, nl, nw, nc, state;
        
        state = OUT;
        nl = nw = nc = 0;
        /* 每当遇到单词的第一个字符,就作为一个新单词计数 */
        while ( (c = getchar()) != EOF ) {
            ++nc;  /* count char */
            if ( c == '
    ' ) {
                ++nl;
            } 
            if ( c == ' '  ||
                 c == '
    ' ||
                 c == '	' ) {
                state = OUT; /* not a word */
            } else if ( state == OUT ) {
                state = IN;
                ++nw;   /* start count word */
            }
        } /* end of while */ 
        printf("chars:%d, lines:%d, words:%d
    ", nc, nl, nw);
    }
    
    void showSingleWord()
    {   /* 1-12 每个单词一行 */
        int c, state;
        
        state = OUT;
        printf("Print Single Word:
    ");
        while ( (c = getchar()) != EOF ) {
            if ( c == ' ' || c == '
    ' || c == '	' ) {
                 if ( state == IN ) {
                     printf("
    "); /* end of a word */
                 }
                state = OUT;      /* not a word */
            } else if ( state == OUT ) {
                state = IN;     /* start count word */
            }
            if ( state == IN ) {
                putchar(c); /* print each char of word */
            }
        } /* end of while */     
    }

    1.5统计字符打印次数直方图

    void printGraph( int num )
    {   /* Histograph  subprogram*/
        int i;
        for ( i = 0; i < num; i++ ) {
            printf("|");
        }    
        printf("
    ");
    }
    
    void countOtherChars()
    {   /* 统计数字和字母以及空格和其他字符,打印直方图 */
        int c, i, nwhite ,nother;
        int ndigit[10];    /* 0-9 */
        int nalpha[26];  /* A-Z */
        
        nwhite = nother = 0;
        for ( i = 0; i < 26; ++i ) {
            if ( i < 10) {
                ndigit[i] = 0; /* init */    
            }
            nalpha[i] = 0;
        }
    
        while ( (c = getchar()) != EOF ) {
            if ( c >= '0' && c <= '9' ) {
                ++ndigit[c - '0'];            
            } else if( c >='A' && c <= 'Z' ) {
                ++nalpha[c - 'A']; /* upper */
            } else if( c >='a' && c <= 'z' ) {
                ++nalpha[c - 'a']; /* lower */    
            } 
            else if ( c == ' ' || c == '
    ' || c == '	' ) {
                /* space */
                ++nwhite;
            } else {
                ++nother;                
            }
        } /* end of while */
    
        for ( i = 0; i < 10; ++i ) {
            printf("%d->", i); //printf(" %d", ndigit[i]);
            printGraph( ndigit[i] );        
        }
        printf("
     ");
        for ( i = 0; i < 26; ++i ) {
            printf("%c->", i+'a');//printf(" %d", nalpha[i]);
            printGraph( nalpha[i] );        
        }
        printf("
    white space->");
        printGraph(nwhite);
        printf("
    other->");
        printGraph(nother);    
    }

     参考资料: 《C程序设计语言》

    转载请注明:https://www.cnblogs.com/justLittleStar/p/10487055.html

  • 相关阅读:
    反转链表 16
    CodeForces 701A Cards
    hdu 1087 Super Jumping! Jumping! Jumping!(动态规划)
    hdu 1241 Oil Deposits(水一发,自我的DFS)
    CodeForces 703B(容斥定理)
    poj 1067 取石子游戏(威佐夫博奕(Wythoff Game))
    ACM 马拦过河卒(动态规划)
    hdu 1005 Number Sequence
    51nod 1170 1770 数数字(数学技巧)
    hdu 2160 母猪的故事(睡前随机水一发)(斐波那契数列)
  • 原文地址:https://www.cnblogs.com/justLittleStar/p/10487055.html
Copyright © 2011-2022 走看看