簡體   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