fork(1) download
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. unsigned int SLOC_1(std::fstream &inFile);
  5. unsigned int SLOC_2(std::fstream &inFile);
  6.  
  7. int main()
  8. {
  9. std::fstream filestream ("input.txt", std::fstream::in | std::fstream::out);
  10. if(filestream.is_open()){
  11. std::cout << SLOC_2(filestream) << '\n';
  12. }
  13.  
  14. filestream.close();
  15. return 0;
  16. }
  17. unsigned int SLOC_1( std::fstream &inFile)
  18. {
  19. unsigned int sloc = 0;
  20.  
  21.  
  22. std::string loc;
  23.  
  24. inFile >> loc;
  25. while(!inFile.eof()){
  26. if (loc.length() > 0){
  27. if(loc.at(loc.length()-1) == ';')
  28. ++sloc;
  29. }
  30. inFile >> loc;
  31. };
  32.  
  33. if (loc.length() > 0){
  34. if(loc.at(loc.length()-1) == ';')
  35. ++sloc;
  36. }
  37.  
  38. return sloc;
  39. }
  40.  
  41. unsigned int SLOC_2( std::fstream &inFile)
  42. {
  43. unsigned loc = 0;
  44. bool commentBlock = false;
  45. bool singleLineComment = false;
  46. bool multiLineComment = false;
  47. unsigned int stringLen = 0;
  48. char c;
  49. char aux;
  50.  
  51. do{
  52. inFile.get(c);
  53.  
  54. if(c == ';'){
  55. if (commentBlock == false){
  56. if(stringLen != 0){
  57. ++loc;
  58. stringLen = 0;
  59. }
  60.  
  61. }
  62. else{
  63. if (singleLineComment == true)
  64. singleLineComment = false;
  65. }
  66. }
  67.  
  68. else if (c == '/'){
  69. aux = inFile.peek();
  70. if (aux == '/'){
  71. commentBlock = true;
  72. singleLineComment = true;
  73. inFile.get(c);
  74. }
  75. else if(aux == '*'){
  76. commentBlock = true;
  77. multiLineComment = true;
  78. inFile.get(c);
  79. }
  80. }
  81. else if ((c == '*') && (multiLineComment)){
  82. aux = inFile.peek();
  83. if(aux == '/'){
  84. commentBlock = false;
  85. multiLineComment = false;
  86. inFile.get(c);
  87. }
  88. }
  89. else
  90. ++stringLen;
  91.  
  92.  
  93. }while(!inFile.eof());
  94.  
  95. return loc;
  96. }
  97.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Standard output is empty