繁体   English   中英

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

[英]Could someone explain to me what this code does?

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

#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();
}

它返回从标准输入读取的下一个字母字符的代码:

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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM