task1.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <stdio.h>
  2. int isspace(char c);
  3. int isnext(char c);
  4. typedef enum _State {
  5. S01, S02, S03, S04, S05
  6. } State;
  7. int main(void)
  8. {
  9. int count = 0;
  10. char sym;
  11. State num = S01;
  12. while ((sym = getchar()) != EOF) {
  13. switch (num) {
  14. case S01:
  15. if (sym == '/')
  16. num = S02;
  17. break;
  18. case S02:
  19. if (sym == '/')
  20. num = S03;
  21. else num = S01;
  22. break;
  23. case S03:
  24. if (isnext(sym))
  25. num = S01;
  26. else if (isspace(sym))
  27. num = S04;
  28. else num = S05;
  29. break;
  30. case S04:
  31. if (isnext(sym))
  32. num = S01;
  33. else if (isspace(sym))
  34. continue;
  35. else num = S05;
  36. break;
  37. case S05:
  38. if (isnext(sym)) {
  39. num = S01;
  40. count++;
  41. }
  42. if (isspace(sym)) {
  43. count++;
  44. num = S03;
  45. }
  46. break;
  47. }
  48. }
  49. if (num == S05)
  50. count++;
  51. printf("words = %d\n", count);
  52. return 0;
  53. }
  54. int isspace(char c) {
  55. if (c == ' ' || c == '\t' || c == ',')
  56. return 1;
  57. return 0;
  58. }
  59. int isnext(char c) {
  60. if (c == '\n' || c == '\0')
  61. return 1;
  62. return 0;
  63. }