《The C Programming Language》部分代码

第一章

/*******************************
*第一个c程序
*******************************/
/*
#include <stdio.h> //标准输入输出库

int main()
{
    printf("hello , world\n");
    printf("test\c");  //测试转义字符\c
    return 0;
}
*/
/**********************************
*打印华氏温度与摄氏温度的对照表
*********************************/
/*
#include <stdio.h>
int main()
{
    int fahr,celsius;  // 华氏度degree fahrenheit摄氏度
    int lower ,upper , step;

    lower = 0; //温度表的下限
    upper = 300; //温度表的上限
    step = 20; //歩长

    fahr = lower;
    while(fahr <= upper)
    {
        celsius = 5 * (fahr - 32) / 9; //华氏度转摄氏度的公式
        printf("%d\t%d\n",fahr, celsius);
        fahr = fahr + step;
    }
    return 0;
}
*/
/*
#include <stdio.h>
int main()
{
    float fahr,celsius;  // 华氏度degree fahrenheit摄氏度 [ˈselsiəs]
    float lower ,upper , step;

    lower = 0; //温度表的下限
    upper = 300; //温度表的上限
    step = 20; //歩长

    fahr = lower;
    while(fahr <= upper)
    {
        celsius = (5.0 / 9.0) * (fahr - 32.0); //华氏度转摄氏度的公式
        printf("%3.0f %6.1f\n",fahr, celsius);
        fahr = fahr + step;
    }
    return 0;
}
*/
/**********************************
*文件复制
*********************************/
/*
#include <stdio.h>

int main()
{
    int c;
    c = getchar();
    while(c != EOF )
    {
        putchar(c);
        c = getchar();
    }
    return 0;
}
*/
/**********************************
*单词计数
*********************************/
/*
#include <stdio.h>

#define IN 1 //inside a word
#define OUT 0 //outside a word

//cout  lines ,words, and characters in input
int main()
{
    int state , nl , nw , nc , c;
    state = OUT;
    nl = nw = nc = 0;
    while(( c = getchar() )!= '#'){
        ++nc; //字符计数
        if(c == '\n')
            ++nl;  //行计数
        if(c == ' ' || c == '\n' || c == '\t')
            state = OUT;
        else if( state == OUT)
        {
            state = IN;//进入下一个单词
            ++nw; // 单词数加一
        }
    }

    printf(" %d %d %d\n",nl,nw,nc);
    return 0;
}

*/
/**********************************
**数组
*********************************/
/*
#include <stdio.h>

//cout digits数字 white space ,others
int main()
{
    int c , i , nwhite ,nothers;
    int ndigit[10];

    nwhite = nothers = 0;
    for(i = 0; i < 10; ++i) //初始化
        ndigit[i] = 0;

    while((c = getchar()) != '#')
    {
        if(c >= '0' && c <= '9')
            ++ndigit[c-'0'];
        else if (c == ' ' || c == '\n' || c == '\t')
            ++nwhite;
        else
            ++nothers;
    }

    printf("digits = ");
    for(i = 0; i < 10 ;++i)
        printf (" %d " ,ndigit[i] );
    printf(", white space = %d , other = %d\n" ,nwhite ,nothers);
    return 0;
}
*/
/**********************************
**字符数组
*********************************/
/*
#include <stdio.h>

#define MAXLINE 100 //最大的行数
void copy( char to[] , char from[]);
int getline (char line[] , int maxline );

// print the longest input line
int main()
{
    int len; // current line length
    int max; // maximum length seen so far
    char line[MAXLINE];  //current input line
    char longest[MAXLINE]; //longest line saved here

    max = 0;
    while((len = getline(line , MAXLINE)) > 0)
    {
        if(len > max) {
            max = len;
            copy(longest , line);
        }

    }

    if(max > 0 )
    {
        printf("%s",longest);
    }
    return 0;
}

int getline(char s[], int lim)
{
    int c , i ;
    for(i = 0; i < lim-1 &&(c = getchar())!='#' && c != '\n';++i  )
        s[i] = c;
    if( c == '\n'){
        s[i] = c;
        ++ i;
    }
    s[i] = '\0';

    return i;
}
void copy(char to[] ,char from[])
{
    int i;
    i = 0;
    while((to[i] = from[i]) != '\0')
        ++i;
}

第二章  类型、运算符与表达式

任何变量的声明都可以使用 const 限定符限定。该限定符指定变量的值不能被修改。对数组而言,const 限定符指定数组所有元素的值都不能被修改:

const double e = 2.71828182845905;

const char msg[] = "warning: ";

const 限定符也可配合数组参数使用,它表明函数不能修改数组元素的值:

int strlen(const char[]);

3章 控制流

4章 函数与程序结构

5章 指针与数组

第6章 结构

7章 输入与输出

/**********************************
*第二章:变长参数
**miniprintf
*********************************/
/*
#include <stdio.h>
#include <stdarg.h>
void miniprintf(char *fmt , ...)
{
    va_list ap;//points to each unnamed arg in turn
    char *p , *sval;
    int ival;
    double dval;

    va_start(ap,fmt);//make ap point to lst unnamed arg
    for( p = fmt ; *p ; p++)
    {
        if(*p != '%')
        {
            putchar(*p);
            continue;
        }

    switch (*++p)
    {
    case 'd':
        ival = va_arg(ap,int);
        printf("%d",ival);
        break;
    case 'f':
        dval = va_arg(ap,double);
        printf("%f",dval);
        break;
    case 's':
        for(sval = va_arg(ap,char *) ; *sval; sval++)
            putchar(*sval);
        break;
    default :
        putchar(*p);
        break;
    }
     }
     va_end(ap);
}
int main()
{
    miniprintf("aaa%dddd" , 123);
    return 0;
}
*/

8UNIX 系统接口

猜你喜欢

转载自blog.csdn.net/qq_41543888/article/details/96437742