Advertisement
vallec

Untitled

Mar 5th, 2025
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. // Function to check if a character is a valid digit for a given base
  7. bool is_valid_digit(char c, int base) {
  8.     if (base == 10) {
  9.         return isdigit(c);
  10.     } else if (base == 8) {
  11.         return c >= '0' && c <= '7';
  12.     } else if (base == 16) {
  13.         return isdigit(c) || (tolower(c) >= 'a' && tolower(c) <= 'f');
  14.     }
  15.     return false;
  16. }
  17.  
  18. // Function to check if a string is a valid integer constant
  19. bool is_valid_integer(const char *str, int *type) {
  20.     int len = strlen(str);
  21.     int i = 0;
  22.     int base = 10;
  23.  
  24.     // Check for sign
  25.     if (str[i] == '+' || str[i] == '-') {
  26.         i++;
  27.     }
  28.  
  29.     // Check for hexadecimal prefix
  30.     if (str[i] == '0' && tolower(str[i+1]) == 'x') {
  31.         base = 16;
  32.         i += 2;
  33.         *type = 3; // Hexadecimal
  34.     }
  35.     // Check for octal prefix
  36.     else if (str[i] == '0') {
  37.         base = 8;
  38.         i++;
  39.         *type = 2; // Octal
  40.     }
  41.     else {
  42.         *type = 1; // Decimal
  43.     }
  44.  
  45.     // Check digits
  46.     for (; i < len; i++) {
  47.         if (!is_valid_digit(str[i], base)) {
  48.             return false;
  49.         }
  50.     }
  51.  
  52.     return true;
  53. }
  54.  
  55. // Function to check if a string is a valid floating-point constant
  56. bool is_valid_float(const char *str, int *type) {
  57.     int len = strlen(str);
  58.     int i = 0;
  59.     bool has_digit = false;
  60.     bool has_dot = false;
  61.     bool has_exp = false;
  62.  
  63.     // Check for sign
  64.     if (str[i] == '+' || str[i] == '-') {
  65.         i++;
  66.     }
  67.  
  68.     // Check digits before dot
  69.     while (i < len && isdigit(str[i])) {
  70.         has_digit = true;
  71.         i++;
  72.     }
  73.  
  74.     // Check for dot
  75.     if (i < len && str[i] == '.') {
  76.         has_dot = true;
  77.         i++;
  78.     }
  79.  
  80.     // Check digits after dot
  81.     while (i < len && isdigit(str[i])) {
  82.         has_digit = true;
  83.         i++;
  84.     }
  85.  
  86.     // Check for exponent
  87.     if (i < len && tolower(str[i]) == 'e') {
  88.         has_exp = true;
  89.         i++;
  90.  
  91.         // Check for exponent sign
  92.         if (i < len && (str[i] == '+' || str[i] == '-')) {
  93.             i++;
  94.         }
  95.  
  96.         // Check exponent digits
  97.         while (i < len && isdigit(str[i])) {
  98.             i++;
  99.         }
  100.     }
  101.  
  102.     // Check for floating suffix
  103.     if (i < len && tolower(str[i]) == 'f') {
  104.         i++;
  105.     }
  106.  
  107.     // Check if we reached the end of the string
  108.     if (i == len && has_digit) {
  109.         *type = 4; // Floating-point
  110.         return true;
  111.     }
  112.  
  113.     return false;
  114. }
  115.  
  116. // Function to count numbers in the input text
  117. void count_numbers(const char *input, int *decimal_count, int *octal_count, int *hex_count, int *float_count, int *total_count) {
  118.     char buffer[256];
  119.     int buffer_index = 0;
  120.     int input_length = strlen(input);
  121.  
  122.     for (int i = 0; i < input_length; i++) {
  123.         if (input[i] == ' ' || input[i] == ',' || input[i] == '\n') {
  124.             if (buffer_index > 0) {
  125.                 buffer[buffer_index] = '\0';
  126.                 int type = 0;
  127.                 if (is_valid_integer(buffer, &type)) {
  128.                     if (type == 1) (*decimal_count)++;
  129.                     else if (type == 2) (*octal_count)++;
  130.                     else if (type == 3) (*hex_count)++;
  131.                     (*total_count)++;
  132.                 } else if (is_valid_float(buffer, &type)) {
  133.                     (*float_count)++;
  134.                     (*total_count)++;
  135.                 }
  136.                 buffer_index = 0;
  137.             }
  138.         } else {
  139.             buffer[buffer_index++] = input[i];
  140.         }
  141.     }
  142.  
  143.     // Check the last number in the buffer
  144.     if (buffer_index > 0) {
  145.         buffer[buffer_index] = '\0';
  146.         int type = 0;
  147.         if (is_valid_integer(buffer, &type)) {
  148.             if (type == 1) (*decimal_count)++;
  149.             else if (type == 2) (*octal_count)++;
  150.             else if (type == 3) (*hex_count)++;
  151.             (*total_count)++;
  152.         } else if (is_valid_float(buffer, &type)) {
  153.             (*float_count)++;
  154.             (*total_count)++;
  155.         }
  156.     }
  157. }
  158.  
  159. int main() {
  160.     const char *input = "1 11 111,1a 11a 11a _1 _11 _111,_1b _11b _111b 0 0a 1234567890 "
  161.                         "+1 +11,+111 +1a +11a +11a +_1 +_11 +_111 _+1 _+11 _+111 +_1b +_11b +_111b _+1b _+11b _+111b +0 _+0 +0a +1234567890, "
  162.                         "-1 -11 -111 -1a -11a -11a -_1 -_11 -_111 _-1 _-11 _-111 -_1b -_11b -_111b _-1b _-11b _-111b -0 _-0 -0a -1234567890, "
  163.                         "-1U 1U +1U -1L +1L 1L -1LU +1LU 1LU -1UL +1UL 1UL -12U 12U +12U -12L +12L 12L -12LU +12LU 12LU -12UL +12UL 12UL -123U 123U +123U -123L +123L 123L -123LU +123LU 123LU -123UL +123UL 123UL  +1234567890U  +1234567890L  +1234567890UL  +1234567890LU "
  164.                         "-1Uc 1Uc +1Uc -1Lc +1Lc 1Lc -1LUc +1LUc 1LUc -1ULc +1ULc 1ULc -12Uc 12Uc +12Uc -12Lc +12Lc 12Lc -12LUc +12LUc 12LUc -12ULc +12ULc 12ULc -123Uc 123Uc +123Uc -123Lc +123Lc 123Lc -123LUc +123LUc 123LUc -123ULc +123ULc 123ULc "
  165.                         "-1cU 1cU +1cU -1cL +1cL 1cL -1cLU +1cLU 1cLU -1cUL +1cUL 1cUL -12cU 12cU +12cU -12cL +12cL 12cL -12cLU +12cLU 12cLU -12cUL +12cUL 12cUL -123cU 123cU +123cU -123cL +123cL 123cL -123cLU +123cLU 123cLU -123cUL +123cUL 123cUL "
  166.                         "-1LcU +1LcU 1LcU -1UcL +1UcL 1UcL -12LcU +12LcU 12LcU -12UcL +12UcL 12UcL -123LcU +123LcU 123LcU -123UcL +123UcL 123UcL "
  167.                         "01 -01,+01 01234567 012345678 00,01a a01 012U 012L 012UL a012U a012L,a012UL 012aU 012aL 012aUL,012UaL  012ULa  012Ua  012La "
  168.                         "0x0 -0x0 +0x0,0x1234567890ABCDEF 0x1234567890ABCDEFG 0x1234567890ABCGDE, "
  169.                         "00e5 100e5 1010e5 1231234567e5,-00e5 -100e5 -1010e5,-1231234567e5 +00e5 +100e5,+1010e5 +1231234567e5 "
  170.                         "0e5 10e5 34567e5 -0e5 -10e5 -123e5 +0e5 +10e5 +123e5 "
  171.                         "0e5 01e5,1234567e5 -0e5 -02e5 -10234e5,-1234567e5 +0e5 +02e5 +1234567e5 "
  172.                         "0.0 10.0 10.10 123.1234567 -0.0 -10.0 -10.10,,-123.1234567 +0.0 +10.0,+10.10,+123.1234567 "
  173.                         "0. 10.,34567. -0. -10. -123. +0. +10. +123., "
  174.                         ".0 .01 .1234567 -.0 -.02,-.10234 -.1234567 +.0 +.02,+.1234567 "
  175.                         "0.0a 10.0a 10.10a 123.1234567a -0.0a -10.0a -10.10a,-123.1234567a +0.0a +10.0a +10.10a +123.1234567a "
  176.                         "0.a 10.a 34567.a -0.a -10.a -123.a +0.a +10.a +123.a "
  177.                         ".0a .01a .1234567a -.0a,-.02a -.10234a -.1234567a +.0a +.02a +.1234567a "
  178.                         "a0.0 a10.0,a10.10 a123.1234567 -a0.0 -a10.0 -a10.10 -a123.1234567 +a0.0 +a10.0 +a10.10 +a123.1234567 "
  179.                         "a0. a10. a34567. -a0. -a10.,-a123. +a0. +a10. +a123. "
  180.                         ".a0 .a01 .a1234567 -.a0 -.a02 -.a10234 -.a1234567 +.a0 +.a02 +.a1234567  "
  181.                         "0.0f 10.0f 10.10f,123.1234567f -0.0f -10.0f -10.10f,,-123.1234567f +0.0f +10.0f +10.10f +123.1234567f "
  182.                         "0.f 10.f 34567.f -0.f -10.f -123.f +0.f +10.f +123.f "
  183.                         ".0f .01f .1234567f -.0f -.02f,-.10234f -.1234567f +.0f +.02f +.1234567f, "
  184.                         "0.0e5 10.0e5 10.10e5 123.1234567e5 -0.0e5 -10.0e5 -10.10e5 -123.1234567e5 +0.0e5 +10.0e5 +10.10e5 +123.1234567e5 "
  185.                         "0.e5 10.e5 34567.e5 -0.e5 -10.e5,,-123.e5 +0.e5,+10.e5 +123.e5, "
  186.                         ".0e5 .01e5 .1234567e5,-.0e5 -.02e5 -.10234e5 -.1234567e5 +.0e5 +.02e5,+.1234567e5  "
  187.                         "0.0e+5 10.0e+5 10.10e+5 123.1234567e+5 -0.0e+5 -10.0e+5 -10.10e+5,,-123.1234567e+5 +0.0e+5,+10.0e+5 +10.10e+5 +123.1234567e+5 "
  188.                         "0.e+5 10.e+5 34567.e+5 -0.e+5,-10.e+5 -123.e+5 +0.e+5 +10.e+5 +123.e+5, "
  189.                         ".0e+5 .01e+5 .1234567e+5 -.0e+5 -.02e+5 -.10234e+5 -.1234567e+5 +.0e+5 +.02e+5 +.1234567e+5 "
  190.                         "0.0e-5 10.0e-5 10.10e-5 123.1234567e-5 -0.0e-5 -10.0e-5 -10.10e-5 -123.1234567e-5 +0.0e-5 +10.0e-5 +10.10e-5 +123.1234567e-5 "
  191.                         "0.e-5 10.e-5 34567.e-5 -0.e-5 -10.e-5,-123.e-5 +0.e-5,+10.e-5 +123.e-5 "
  192.                         ".0e-5 .01e-5 .1234567e-5,-.0e-5 -.02e-5 -.10234e-5 -.1234567e-5,,+.0e-5 +.02e-5 +.1234567e-5, "
  193.                         "0.0e-534 10.0e-534 10.10e-534,123.1234567e-534 -0.0e-534 -10.0e-534 -10.10e-534 -123.1234567e-534,+0.0e-534 +10.0e-534 +10.10e-534 +123.1234567e-534 "
  194.                         "0.e-534 10.e-534 34567.e-534 -0.e-534 -10.e-534 -123.e-534 +0.e-534 +10.e-534 +123.e-534 "
  195.                         ".0e-534 .01e-534,.1234567e-534 -.0e-534 -.02e-534,,-.10234e-534 -.1234567e-534,+.0e-534 +.02e-534 +.1234567e-534, "
  196.                         "0.0e534 10.0e534 10.10e534,123.1234567e534 -0.0e534 -10.0e534 -10.10e534 -123.1234567e534 +0.0e534,+10.0e534 +10.10e534 +123.1234567e534 "
  197.                         "0.e534,10.e534 34567.e534 -0.e534 -10.e534 -123.e534 +0.e534 +10.e534 +123.e534 "
  198.                         ".0e534 .01e534 .1234567e534 -.0e534,-.02e534 -.10234e534 -.1234567e534 +.0e534,,+.02e534 +.1234567e534 "
  199.                         "0.0e+534 10.0e+534,10.10e+534 123.1234567e+534 -0.0e+534 -10.0e+534 -10.10e+534 -123.1234567e+534 +0.0e+534 +10.0e+534 +10.10e+534 +123.1234567e+534 "
  200.                         "0.e+534 10.e+534 34567.e+534 -0.e+534 -10.e+534 -123.e+534 +0.e+534 +10.e+534 +123.e+534 "
  201.                         ".0e+534 .01e+534 .1234567e+534 -.0e+534 -.02e+534,,-.10234e+534 -.1234567e+534 +.0e+534,+.02e+534 +.1234567e+534 "
  202.                         "0.0e534f 10.0e534f 10.10e534f 123.1234567e534f -0.0e534f,-10.0e534f -10.10e534f -123.1234567e534f +0.0e534f +10.0e534f,+10.10e534f +123.1234567e534f "
  203.                         "0.e534f 10.e534f 34567.e534f -0.e534f -10.e534f -123.e534f +0.e534f +10.e534f +123.e534f "
  204.                         ".0e534f .01e534f .1234567e534f,-.0e534f -.02e534f,-.10234e534f -.1234567e534f +.0e534f +.02e534f +.1234567e534f "
  205.                         "0.0e53ULf 10.0e53ULf 10.10e53ULf 123.1234567e53ULf -0.0e53ULf -10.0e53ULf -10.10e53ULf -123.1234567e53ULf +0.0e53ULf +10.0e53ULf +10.10e53ULf +123.1234567e53ULf "
  206.                         "0.e53ULf 10.e53ULf,,34567.e53ULf -0.e53ULf -10.e53ULf,-123.e53ULf,+0.e53ULf,+10.e53ULf +123.e53ULf "
  207.                         ".0e53ULf .01e53ULf .1234567e53ULf -.0e53ULf -.02e53ULf -.10234e53ULf -.1234567e53ULf +.0e53ULf +.02e53ULf +.1234567e53ULf "
  208.                         "0.0e53UL 10.0e53UL 10.10e53UL 123.1234567e53UL -0.0e53UL,,-10.0e53UL -10.10e53UL,-123.1234567e53UL +0.0e53UL +10.0e53UL +10.10e53UL +123.1234567e53UL "
  209.                         "0.e53UL 10.e53UL 34567.e53UL -0.e53UL -10.e53UL -123.e53UL +0.e53UL +10.e53UL +123.e53UL "
  210.                         ".0e53UL .01e53UL,.1234567e53UL -.0e53UL -.02e53UL,-.10234e53UL,,-.1234567e53UL +.0e53UL +.02e53UL +.1234567e53UL, ";
  211.                        
  212.     int decimal_count = 0;
  213.     int octal_count = 0;
  214.     int hex_count = 0;
  215.     int float_count = 0;
  216.     int total_count = 0;
  217.  
  218.     count_numbers(input, &decimal_count, &octal_count, &hex_count, &float_count, &total_count);
  219.  
  220.     printf("Total decimal numbers: %d\n", decimal_count);
  221.     printf("Total octal numbers: %d\n", octal_count);
  222.     printf("Total hexadecimal numbers: %d\n", hex_count);
  223.     printf("Total floating-point numbers: %d\n", float_count);
  224.     printf("Total numbers: %d\n", total_count);
  225.  
  226.     return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement