fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //#include <conio.h>
  4. int main() {
  5. //clrscr();
  6. FILE *fp;
  7. int c;
  8. int chars = 0, total = 0, multi = 0, single = 0;
  9.  
  10. enum states { TEXT, SAW_SLASH, SAW_STAR, SINGLE_COMMENT, MULTI_COMMENT } state = TEXT;
  11.  
  12. fp = fopen( "comments.txt", "r" );
  13.  
  14. while ( (c = fgetc( fp )) != EOF ){
  15. chars++;
  16. switch( state ){
  17. case TEXT : switch( c ){
  18. case '/' : state = SAW_SLASH; break;
  19. case '\n' : total++; // fall-through
  20. default : break;
  21. }
  22. break;
  23.  
  24. case SAW_SLASH : switch( c ) {
  25. case '/' : state = SINGLE_COMMENT; break;
  26. case '*' : state = MULTI_COMMENT; break;
  27. case '\n' : total++; // fall through
  28. default : state = TEXT; break;
  29. }
  30. break;
  31.  
  32. case SAW_STAR : switch( c ) {
  33. case '/' : state = TEXT; multi++; break;
  34. case '*' : break;
  35. case '\n' : total++; //multi++; // fall through
  36. default : state = MULTI_COMMENT; break;
  37. }
  38. break;
  39.  
  40. case SINGLE_COMMENT : switch( c ) {
  41. case '\n' : state = TEXT; total++; single++; // fall through
  42. default : break;
  43. }
  44. break;
  45.  
  46. case MULTI_COMMENT : switch( c ) {
  47. case '*' : state = SAW_STAR; break;
  48. case '\n' : total++; //multi++; // fall through
  49. default : break;
  50. }
  51. break;
  52.  
  53. default: // NOT REACHABLE
  54. break;
  55. }
  56. }
  57.  
  58. fclose(fp);
  59. printf( "Total lines : %8u\n", total );
  60. printf( "Single-comment lines : %8u\n", single );
  61. printf( "Multi-comment lines : %8u\n", multi );
  62. //getch();
  63. return 0;
  64. }
Runtime error #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
Standard output is empty