Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdio.h>
  3.  
  4. //Automaton states
  5. enum Automata {
  6.     Start,
  7.     Short_com,
  8.     Long_com,
  9.     String
  10. };
  11.  
  12. int main(int agrc, char *argv[]) {
  13.     enum Automata cur_state;
  14.     cur_state = Start;
  15.  
  16.     char symbol;
  17.     int counter = 0;
  18.  
  19.     while (EOF != (symbol = getchar())) {
  20.         //Check if we can go in some not Start state
  21.         if (symbol == '/' && cur_state == Start) {
  22.             char next_symbol = getchar();
  23.             if (next_symbol == '/') {
  24.                 cur_state = Short_com;
  25.             }
  26.             else if (next_symbol == '*') {
  27.                 cur_state = Long_com;
  28.             }
  29.             else {  //because of EOF
  30.                 break;
  31.             }
  32.             continue;   //No need to count this symbols
  33.         }
  34.         if (symbol == '"' && cur_state == Start) {
  35.             cur_state = String;
  36.             ++counter;
  37.             continue;   //Already count symbol
  38.         }
  39.         //Trying to go to the Starting state
  40.         if (cur_state == Short_com &&  symbol == '\n') {
  41.             cur_state = Start;
  42.             continue;   //No need to count this symbol
  43.         }
  44.         if (cur_state == Long_com && symbol == '*') {
  45.             char next_symbol = getchar();
  46.             if (next_symbol == '/') {
  47.                 cur_state = Start;
  48.             }
  49.             continue; //No need to count this symbols
  50.         }
  51.         if (cur_state == String && symbol == '"') {
  52.             cur_state = Start;
  53.             ++counter;
  54.             continue; //Already count symbol
  55.         }
  56.         //Counting if in String or Start
  57.         if (cur_state == Start || cur_state == String) {
  58.             if (!isspace(symbol)) {
  59.                 ++counter;
  60.             }
  61.         }
  62.         //printf("%d", cur_state);
  63.     }
  64.  
  65.     printf("%d", counter);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement