[27无验证]程序注释统计-字节跳动2018秋

2018-10-28  本文已影响29人  jdzhangxin

1.题目描述

C 语言有两种注释,单行注释 // 和多行注释 /* */。请编写程序,统计注释数量。注意引号中的不要统计,以及引号中可能出现的转义字符影响。

2.题目解析

需要熟悉C++语法

3.参考答案

#include <bits/stdc++.h>
using namespace std;

int main(){
    // 单行注释//开始,回车换行结束
    // 多行注释/*开始,*/结束
    // 字符串"开始,"结束
    
    string s = 
    "// line comment\n"
    "// line comment2\n"
    "/* block comment */ /* block comment 2 */\n"
    "// /* block comment */ /* block comment 2 */\n"
    "/* // block comment */ /* // block comment 2 */\n"
    "int main(){\n"
    "char[] s = \"/* string */\";"
    "return 0;\n"
    "}\n";
    int count = 0;// 统计注释个数
    bool inString = false; // 表示字符串中
    bool sinlgeComment = false; // 表示在单行注释中
    bool multiComment = false; // 表示在多行注释中
    for(int i=0;i<s.size();++i){
        if(s[i] == '"'){// 字符串开始与结束
            if(inString == false){ // 如果前面没有在字符串中
                inString = true;// 标记开始字符串
            }else{
                inString = false;// 标记在字符串中
            }
            // inString = !inString;
        }
        if(!inString){
            if(s[i] == '/'){
                if(s[i+1] == '/'){ // 单行注释开始
                    if(!multiComment){// 确定//不是多行注释中
                        sinlgeComment = true;
                    }
                }else if(s[i+1] == '*'){ // 多行注释开始
                    if(!sinlgeComment){// 确定/*不是单行注释中
                        multiComment = true;
                    }
                }
            }
            if(s[i] == '\n'){// 单行注释结束
                if(sinlgeComment){
                    ++count;
                    sinlgeComment = false;
                }
            }
            if(s[i] == '*' && s[i+1] == '/'){// 多行注释结束
                if(multiComment){
                    ++count;
                    multiComment = false;
                }
            }
        }
    }
    printf("%d\n",count);    
}
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  string code;// 代码
  // 获取输入,直至Ctrl+Z
  char c = getchar(); // 获取输入字符
  while (c != EOF){
    code += c;
    c = getchar();
  }
  
  // 解析代码
  int res = 0;// 注释统计个数
  bool c1 = false;// 第一种注释方式(单行注释)
  bool c2 = false; // 第二种注释方式(多行注释)
  bool qs = false;// 双引号处理

  // 遍历代码中的每一字符
  for (int i = 0; i < code.size(); i++) {
    if (code[i] == '\n') { // 遇到换行
      c1 = false;// 单行注释结束
    } else if (c1) {
    } else if (c2) {
      // 统计
      if (i + 1 < code.size() 
          && code[i] == '*'
          && code[i + 1] == '/') { // 多行注释结尾判断
        c2 = false;
        i++;
      }
    } else if (qs) {
      if (code[i] == '"'){ // 双引号结束判断
        int t = false;
        for (int j = i - 1; j >= 0; j--)
          if (code[j] == '\\') {
            t++;
          } else {
            break;
          }
          if (t % 2 == 0) qs = 0;
        }
    } else if (code[i] == '"') { // 双引号开始
      qs = true;
    } else if (i + 1 < code.size() && code[i] == '/' && code[i + 1] == '/') { // 单行注释开始
      c1 = true;
      res++;
      i++;
    } else if (i + 1 < code.size() && code[i] == '/' && code[i + 1] == '*') { // 多行注释开始
      c2 = true;
      res++;
      i++;
    } else if (code[i] == '\'') {// 转义字符
      if (code[i + 1] == '\\')
        i += 3;
      else
        i += 2;
    }
  }
  printf("%d",res);
  return 0;
}
上一篇下一篇

猜你喜欢

热点阅读