task27.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. int other(char sym);
  4. int ConvertForNum(char sym);
  5. int sign(char sym);
  6. void printnum3(int num);
  7. void printnum7(int num);
  8. typedef enum {
  9. S01, S02, S03
  10. } state;
  11. int main(void)
  12. {
  13. int result = 0, tmp, check = 0, num1 = 0, num2 = 0, num3tmp = 0;
  14. state sx = S01;
  15. char symbol;
  16. while ((symbol = getchar()) != EOF) {
  17. switch(sx) {
  18. case S01:
  19. result = 0;
  20. if (isspace(symbol))
  21. sx = S01;
  22. else if (isdigit(symbol)) {
  23. if (check == 2) {
  24. num3tmp = num1;
  25. num1 = num2;
  26. }
  27. sx = S02;
  28. tmp = ConvertForNum(symbol);
  29. result = result * 10 + tmp;
  30. } else if (sign(symbol))
  31. sx = S02;
  32. else if (!isdigit(symbol))
  33. sx = S03;
  34. break;
  35. case S02:
  36. if (isdigit(symbol) && !check) {
  37. sx = S02;
  38. tmp = ConvertForNum(symbol);
  39. result = result * 10 + tmp;
  40. } else if ((isspace(symbol) || other(symbol)) && !check) {
  41. check = 1;
  42. sx = S01;
  43. num1 = result;
  44. result = 0;
  45. } else if (isdigit(symbol) && check) {
  46. sx = S02;
  47. tmp = ConvertForNum(symbol);
  48. result = result * 10 + tmp;
  49. } else if ((isspace(symbol) || other(symbol)) && check) {
  50. check = 2;
  51. sx = S01;
  52. num2 = result;
  53. result = 0;
  54. } else if (!isdigit(symbol)) {
  55. sx = S03;
  56. if (check == 2)
  57. num1 = num3tmp;
  58. }
  59. break;
  60. case S03:
  61. if (isspace(symbol) || other(symbol))
  62. sx = S01;
  63. break;
  64. }
  65. }
  66. if (check == 2) {
  67. printf("number3 : ");
  68. printnum3(num1);
  69. putchar('\n');
  70. printf("number7 : ");
  71. printnum7(num1);
  72. putchar('\n');
  73. printf("number11 : %d\n", num1);
  74. }
  75. if (check == 1)
  76. printf("You enter one number\n");
  77. if (!check)
  78. printf("Number no found!\n");
  79. return 0;
  80. }
  81. int other(char sym) {
  82. return sym == ',' ? 1 : 0;
  83. }
  84. int ConvertForNum(char sym) {
  85. return (int) sym - 48;
  86. }
  87. int sign(char sym) {
  88. return (sym == '-' || sym == '+') ? 1 : 0;
  89. }
  90. void printnum3(int num) {
  91. if (num / 10)
  92. printnum3(num / 10);
  93. if (num % 10 == 0)
  94. printf("00");
  95. if (num % 10 == 1)
  96. printf("01");
  97. if (num % 10 == 2)
  98. printf("02");
  99. if (num % 10 == 3)
  100. printf("10");
  101. if (num % 10 == 4)
  102. printf("11");
  103. if (num % 10 == 5)
  104. printf("12");
  105. if (num % 10 == 6)
  106. printf("20");
  107. if (num % 10 == 7)
  108. printf("21");
  109. if (num % 10 == 8)
  110. printf("22");
  111. if (num % 10 == 9)
  112. printf("30");
  113. }
  114. void printnum7(int num) {
  115. if (num / 10)
  116. printnum7(num / 10);
  117. if (num % 10 == 0)
  118. printf("00");
  119. if (num % 10 == 1)
  120. printf("01");
  121. if (num % 10 == 2)
  122. printf("02");
  123. if (num % 10 == 3)
  124. printf("03");
  125. if (num % 10 == 4)
  126. printf("04");
  127. if (num % 10 == 5)
  128. printf("05");
  129. if (num % 10 == 6)
  130. printf("06");
  131. if (num % 10 == 7)
  132. printf("07");
  133. if (num % 10 == 8)
  134. printf("10");
  135. if (num % 10 == 9)
  136. printf("11");
  137. }