第一章-例子以及习题 (The c programming language)

1. hello world

#include <stdio.h>

main() {
  printf("hello, world\n");
}

在代码中如果加入了不合法的转意字符如printf("\c");则会出现编译错误,并且在运行过程中直接无视"\"的存在

2. 温度变换

#include <stdio.h>

float fahr_to_celsius(float fahr) {
  return 5.0/9.0*(fahr-32);
}

float celsius_to_fahr(float celsius) {
  return 9.0/5.0*celsius + 32;
}

int main(int argc, char *argv[])
{
  float fahr, celsius;
  float lower, upper, step;

  lower = 0;
  upper = 300;
  step = 20;

  // transform the Fahrenheit to Celsius
  fahr = lower;
  printf("%s %s\n", "Fahrenheit", "Celsius");
  while (fahr <= upper) {
    celsius = fahr_to_celsius(fahr);
    printf("%10.0f %6.1f\n", fahr, celsius);
    fahr = fahr + step;
  }

  lower = -10;
  upper = 40;
  step = 5;
  printf("=======================")
  // Transform the Celsius to Fahrenheit
  printf("%s %s\n", "Celsius", "Fahrenheit");
  for (celsius = upper; celsius >= lower; celsius -= step) {
    fahr = celsius_to_fahr(celsius);
    printf("%7.0f %10.0f\n", celsius, fahr);
  }
  return 0;
}

3. 文本文档复制

//用很简单的方式就能够实现文本文档的复制,代码如下
#include <stdio.h>

int main() {
  int c;

  while ((c = getchar()) != EOF) {
    putchar(c);
  }
}

利用gcc filename.c编译改文件,然后在控制台上用:(a.exe < in.txt) > out.txt就能够实现文本文档的复制

4. 文本中字符、行、单词计数

/**
 * @file tmp.c
 * @brief this file for counting the word, 
 *        chars, lines
 * @author stupidgrass
 * @version 0.0
 * @date 2013-10-10
 */

#include <stdio.h>

void print_message() {
  printf("\nPlease masure the command style and try again:\n");
  printf("\tsupercount filename\n");
}

int cnt(FILE* fl, int *nc, int *nl, int *nw) {
  char c;
  int outword;

  if (fl) {
    while ((c=fgetc(fl)) != EOF) {
      (*nc)++;
      if (c == '\n')
        (*nl)++;
      if ((c == ' ') || (c == '\t') || (c == '\n')) {
        outword = 1;
      }
      else if (outword == 1) {
        outword = 0;
        (*nw)++;
      }
    }
    return 1;
  }
  else {
    return 0;
  }
}

int main(int argc, char* argv[]) {
  // here assume or the input is right
  int success;
  int nw = 0;
  int nl = 0;
  int nc = 0;

  char *filename = argv[1];

  FILE *fl = fopen(filename, "r");
  success = cnt(fl, &nc, &nl, &nw);
  fclose(fl);

  if (success) {
    printf("%d chars in the file %s", nc, filename);
    printf("\n%d lines in the file %s", nl, filename);
    printf("\n%d words in the file %s", nw, filename);
  } else {
    print_message();
  }
  return 0;
}

5. 频数直方图

#include <stdio.h>

int main() {
  int c, i, j;
  int ndigit[10];

  for (i=0; i<10; ++i) {
    ndigit[i] = 0;
  }

  while ((c = getchar()) != EOF) {
    if (c >= '0' && c <= '9') {
      ++ndigit[c-'0'];
    }
  }

  for (i=0; i<10; ++i) {
    printf("%d:",i);
    for (j=0; j<ndigit[i]; ++j) {
      printf(" *");
    }
    printf("\n");
  }
}

上面给出的是从0到9的数字的频数直方图,那么如何检测文件中单词长度的直方图呢?

#include <stdio.h>

#define MAX_WORD_LENGTH 50 

int main() 
{
  enum {INWORD, OUTWORD};
  int state = OUTWORD;
  int cnt = 0;
  int counts[MAX_WORD_LENGTH];
  int i, j;
  char c;

  for (i=0; i<MAX_WORD_LENGTH; ++i) {
    counts[i] = 0;
  }

  while ((c=getchar()) != EOF) {
    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
      state = INWORD;
      cnt++;
    }
    else {
      if (state == INWORD) {
        counts[cnt]++;
      }
      state = OUTWORD;
      cnt = 0;

    }
  }

  for (i=0; i<MAX_WORD_LENGTH; ++i) {
    if (counts[i] != 0) {
      printf("Length %2d:", i);
      for (j=0; j<counts[i]; ++j) {
        printf(" *");
      }
      printf("\n");
    }
  }

  return 0;
}

你可能感兴趣的:(c,programming,language,example,练习,the)