有人可以向我解释这段代码的作用吗?

时间:2019-12-19 17:15:21

标签: c

最近,我们收到了这段代码片段,我真的不知道它做什么。 有人可以帮我,也许可以向我解释代码的实际作用吗?

#include <stdio.h>
#define N 29
#define C_SPACE 26
#define C_COMMA 27
#define C_STOP 28

int getcc() {
  int c, haveSpace = 0;
  while(isspace(c=getchar())) haveSpace = 1;
  if(haveSpace) return (ungetc(c,stdin),C_SPACE);
  else if(c>=’a’ && c<=’z’) return c-’a’;
  else if(c>=’A’ && c<=’Z’) return c-’A’;
  else if(c==’,’) return C_COMMA;
  else if(c==’.’) return C_STOP;
  else if(c==EOF) return EOF;
  else return getcc();
}

1 个答案:

答案 0 :(得分:1)

它正在返回从stdin中读取的下一个字母字符的代码:

int getcc() {
  int c, haveSpace = 0;
  //Read characters until you get one that is not white space. If any white space read, remember it
  while(isspace(c=getchar())) haveSpace = 1;
  //if a space was read, put the non-space back to stdin, and return 26.
  if(haveSpace) return (ungetc(c,stdin),C_SPACE);
  //if the character is a lower-case letter, return the index into the alphabet: a=0, b=1, etc.
  else if(c>=’a’ && c<=’z’) return c-’a’;
  //if the character is an upper-case letter, return the index into the alphabet: A=0, B=1, etc.
  else if(c>=’A’ && c<=’Z’) return c-’A’;
  //If the character is a comma, return 27
  else if(c==’,’) return C_COMMA;
  //If the character is a period, return 28
  else if(c==’.’) return C_STOP;
  //if at end of file, return EOF
  else if(c==EOF) return EOF;
  //if any other character, skip it by calling the function again and returning the result.
  else return getcc();
}