gpt4 book ai didi

使用 else-if 的 C 代码解析器

转载 作者:行者123 更新时间:2023-11-30 15:05:02 25 4
gpt4 key购买 nike

对于以下question ,

Exercise 12347 - Write a program which will read in the source of a C program from its standard input, and print out all the starred items in the following statistics for the program (all as integers). (Note the comment on tab characters at the end of this specification.)

Print out the following values:

  Lines:
* The total number of lines
* The total number of blank lines
(Any lines consisting entirely of white space should be
considered as blank lines.)
* The percentage of blank lines (100 * blank_lines / lines)

Characters:
* The total number of characters after tab expansion
* The total number of spaces after tab expansion
* The total number of leading spaces after tab expansion
(These are the spaces at the start of a line, before any visible
character; ignore them if there are no visible characters.)
* The average number of characters per line
* characters per line ignoring leading spaces
* leading spaces per line
* spaces per line ignoring leading spaces

Comments:
* The total number of comments in the program
* The total number of characters in the comments in the program
excluding the "/*" and "*/" thenselves
* The percentage of number of comments to total lines
* The percentage of characters in comments to characters

Identifiers:
* We are concerned with all the occurrences of "identifiers" in the
program where each part of the text starting with a letter,
and continuing with letter, digits and underscores is considered
to be an identifier, provided that it is not in a comment, or in a string, or within primes.
Note that "abc\"def"
the internal escaped quote does not close the string.
Also, the representation of the escape character is '\\'
and of prime is '\''
Do not attempt to exclude the fixed words of the language,
treat them as identifiers. Print
* The total number of identifier occurrences.
* The total number of characters in them.
* The average identifier length.

Indenting:
* The total number of times either of the following occurs:
a line containing a "}" is more indented than the preceding line
a line is preceded by a line containing a "{" and is less
indented than it.
The "{" and "}" must be ignored if in a comment or string or
primes, or if the other line involved is entirely comment.
* A single count of the sum of both types of error is required.
NOTE: All tab characters ('') on input should be interpreted as multiple spaces using the rule:
"move to the next modulo 8 column"
where the first column is numbered column 0.

col before tab | col after tab
---------------+--------------
0 | 8
1 | 8
7 | 8
8 | 16
9 | 16
15 | 16
16 | 24
To read input a character at a time the skeleton has code incorporated to read a line at a time for you using
char ch;
ch = getchar();
Which will deliver each character exactly as read. The "getline" function then puts the line just read in the global array of characters "linec", null terminated, and delivers the length of the line, or a negative value if end of data has been encountered.
You can then look at the characters just read with (for example)

switch( linec[0] ) {
case ' ': /* space ..... */
break;
case '\t': /* tab character .... */
break;
case '\n': /* newline ... */
break;
....
} /* end switch */
End of data is indicated by scanf NOT delivering the value 1.

Your output should be in the following style:

Total lines 126
Total blank lines 3
Total characters 3897
Total spaces 1844
Total leading spaces 1180
Total comments 7
Total chars in comments 234
Total number of identifiers 132
Total length of identifiers 606
Total indenting errors 2
You may gather that the above program (together with the unstarred items) forms the basis of part of your marking system! Do the easy bits first, and leave it at that if some aspects worry you. Come back to me if you think my solution (or the specification) is wrong! That is quite possible!
<小时/>

这是我不完整的解决方案:

#include<stdio.h>

typedef int bool;
#define true 1;
#define false 0;

int main(void){
int ch;
int numOfLines = 0;
int numOfBlankLines = 0;
int numOfCharAfterTab = 0;
int numOfSpacesAfterTab = 0;

bool isTabNow = false;
bool isNewLine = false;
bool isSpace = false;
bool isChar = false;

while(ch = getchar(), ch >= 0){

if(ch == '\t')
{
isTabNow = true;
}
else if(ch == ' ')
{
if(isTabNow)
{
numOfSpacesAfterTab++; continue;
} /* 4. Number of spaces after tab expansion*/
}
else if(ch == '\n')
{
isTabNow = false;
numOfLines++; /* 1. Total number of Lines*/
if(!isChar)
{
numOfBlankLines++;
} /* 2. Total number of blank lines*/
}
else if((ch >= 'a' && ch <= 'z') || (ch >= 0 && ch <= 9)||
(ch >= '!' && ch <= '/') || (ch >= ':' && ch <= '@')||
/* Referred ascii chart and compared 'ch' with ascii values of printable characters*/
(ch >= 'A' && ch <= 'Z') || (ch >= '[' && ch <= '`') ||
(ch >= '{' && ch <= '~'))
{
isChar=true;
if(isTabNow){ /* 3. Total number of characters after tab expansion*/
numOfCharAfterTab++;
}
}
else
{
printf("\n Invaid character %c", ch);
}

}// end while
}
<小时/>

问题:

以上问题建议使用switch-case ,但无法管理值的范围,与 else-if 不同语法。

根据上述解决方案,我无法解决制表符扩展后的字符总数。如何解决这个问题?

最佳答案

要安装开关,有半程和全程之分。中途是您切换单字符选择的位置,然后在默认中包含其余部分的if测试

switch( ch ) {
case '\t' : ...
case '\n' : ...
case ' ': ...
default:
if (isdigit(ch)) ....
else ....
}

在完整方式中,我们定义了所有相关字符的表并指出它是什么。 枚举用于提高可读性。

在处理文件之前,表会构建一次。

制作表格(为了清楚起见,NONE 设置为 0)

typedef enum { NONE=0, CR, TAB, LETTER, DIGIT, QUOTE, DQUOTE /* ... */ } Kind;

Kind table[256] = { NONE }; // default is invalid

int c;
table[ '\t' ] = TAB;
table[ '\n' ] = CR;
for(c='a' ; c<='z' ; c++) table[ c ] = LETTER;
/* ... */

然后在程序中

switch(table[ ch ]) {
case TAB: ....
case LETTER: ....
case ...
...
}

关于使用 else-if 的 C 代码解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40037446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com