Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <stdbool.h>
- // Function to check if a character is a valid digit for a given base
- bool is_valid_digit(char c, int base) {
- if (base == 10) {
- return isdigit(c);
- } else if (base == 8) {
- return c >= '0' && c <= '7';
- } else if (base == 16) {
- return isdigit(c) || (tolower(c) >= 'a' && tolower(c) <= 'f');
- }
- return false;
- }
- // Function to check if a string is a valid integer constant
- bool is_valid_integer(const char *str, int *type) {
- int len = strlen(str);
- int i = 0;
- int base = 10;
- // Check for sign
- if (str[i] == '+' || str[i] == '-') {
- i++;
- }
- // Check for hexadecimal prefix
- if (str[i] == '0' && tolower(str[i+1]) == 'x') {
- base = 16;
- i += 2;
- *type = 3; // Hexadecimal
- }
- // Check for octal prefix
- else if (str[i] == '0') {
- base = 8;
- i++;
- *type = 2; // Octal
- }
- else {
- *type = 1; // Decimal
- }
- // Check digits
- for (; i < len; i++) {
- if (!is_valid_digit(str[i], base)) {
- return false;
- }
- }
- return true;
- }
- // Function to check if a string is a valid floating-point constant
- bool is_valid_float(const char *str, int *type) {
- int len = strlen(str);
- int i = 0;
- bool has_digit = false;
- bool has_dot = false;
- bool has_exp = false;
- // Check for sign
- if (str[i] == '+' || str[i] == '-') {
- i++;
- }
- // Check digits before dot
- while (i < len && isdigit(str[i])) {
- has_digit = true;
- i++;
- }
- // Check for dot
- if (i < len && str[i] == '.') {
- has_dot = true;
- i++;
- }
- // Check digits after dot
- while (i < len && isdigit(str[i])) {
- has_digit = true;
- i++;
- }
- // Check for exponent
- if (i < len && tolower(str[i]) == 'e') {
- has_exp = true;
- i++;
- // Check for exponent sign
- if (i < len && (str[i] == '+' || str[i] == '-')) {
- i++;
- }
- // Check exponent digits
- while (i < len && isdigit(str[i])) {
- i++;
- }
- }
- // Check for floating suffix
- if (i < len && tolower(str[i]) == 'f') {
- i++;
- }
- // Check if we reached the end of the string
- if (i == len && has_digit) {
- *type = 4; // Floating-point
- return true;
- }
- return false;
- }
- // Function to count numbers in the input text
- void count_numbers(const char *input, int *decimal_count, int *octal_count, int *hex_count, int *float_count, int *total_count) {
- char buffer[256];
- int buffer_index = 0;
- int input_length = strlen(input);
- for (int i = 0; i < input_length; i++) {
- if (input[i] == ' ' || input[i] == ',' || input[i] == '\n') {
- if (buffer_index > 0) {
- buffer[buffer_index] = '\0';
- int type = 0;
- if (is_valid_integer(buffer, &type)) {
- if (type == 1) (*decimal_count)++;
- else if (type == 2) (*octal_count)++;
- else if (type == 3) (*hex_count)++;
- (*total_count)++;
- } else if (is_valid_float(buffer, &type)) {
- (*float_count)++;
- (*total_count)++;
- }
- buffer_index = 0;
- }
- } else {
- buffer[buffer_index++] = input[i];
- }
- }
- // Check the last number in the buffer
- if (buffer_index > 0) {
- buffer[buffer_index] = '\0';
- int type = 0;
- if (is_valid_integer(buffer, &type)) {
- if (type == 1) (*decimal_count)++;
- else if (type == 2) (*octal_count)++;
- else if (type == 3) (*hex_count)++;
- (*total_count)++;
- } else if (is_valid_float(buffer, &type)) {
- (*float_count)++;
- (*total_count)++;
- }
- }
- }
- int main() {
- const char *input = "1 11 111,1a 11a 11a _1 _11 _111,_1b _11b _111b 0 0a 1234567890 "
- "+1 +11,+111 +1a +11a +11a +_1 +_11 +_111 _+1 _+11 _+111 +_1b +_11b +_111b _+1b _+11b _+111b +0 _+0 +0a +1234567890, "
- "-1 -11 -111 -1a -11a -11a -_1 -_11 -_111 _-1 _-11 _-111 -_1b -_11b -_111b _-1b _-11b _-111b -0 _-0 -0a -1234567890, "
- "-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 "
- "-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 "
- "-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 "
- "-1LcU +1LcU 1LcU -1UcL +1UcL 1UcL -12LcU +12LcU 12LcU -12UcL +12UcL 12UcL -123LcU +123LcU 123LcU -123UcL +123UcL 123UcL "
- "01 -01,+01 01234567 012345678 00,01a a01 012U 012L 012UL a012U a012L,a012UL 012aU 012aL 012aUL,012UaL 012ULa 012Ua 012La "
- "0x0 -0x0 +0x0,0x1234567890ABCDEF 0x1234567890ABCDEFG 0x1234567890ABCGDE, "
- "00e5 100e5 1010e5 1231234567e5,-00e5 -100e5 -1010e5,-1231234567e5 +00e5 +100e5,+1010e5 +1231234567e5 "
- "0e5 10e5 34567e5 -0e5 -10e5 -123e5 +0e5 +10e5 +123e5 "
- "0e5 01e5,1234567e5 -0e5 -02e5 -10234e5,-1234567e5 +0e5 +02e5 +1234567e5 "
- "0.0 10.0 10.10 123.1234567 -0.0 -10.0 -10.10,,-123.1234567 +0.0 +10.0,+10.10,+123.1234567 "
- "0. 10.,34567. -0. -10. -123. +0. +10. +123., "
- ".0 .01 .1234567 -.0 -.02,-.10234 -.1234567 +.0 +.02,+.1234567 "
- "0.0a 10.0a 10.10a 123.1234567a -0.0a -10.0a -10.10a,-123.1234567a +0.0a +10.0a +10.10a +123.1234567a "
- "0.a 10.a 34567.a -0.a -10.a -123.a +0.a +10.a +123.a "
- ".0a .01a .1234567a -.0a,-.02a -.10234a -.1234567a +.0a +.02a +.1234567a "
- "a0.0 a10.0,a10.10 a123.1234567 -a0.0 -a10.0 -a10.10 -a123.1234567 +a0.0 +a10.0 +a10.10 +a123.1234567 "
- "a0. a10. a34567. -a0. -a10.,-a123. +a0. +a10. +a123. "
- ".a0 .a01 .a1234567 -.a0 -.a02 -.a10234 -.a1234567 +.a0 +.a02 +.a1234567 "
- "0.0f 10.0f 10.10f,123.1234567f -0.0f -10.0f -10.10f,,-123.1234567f +0.0f +10.0f +10.10f +123.1234567f "
- "0.f 10.f 34567.f -0.f -10.f -123.f +0.f +10.f +123.f "
- ".0f .01f .1234567f -.0f -.02f,-.10234f -.1234567f +.0f +.02f +.1234567f, "
- "0.0e5 10.0e5 10.10e5 123.1234567e5 -0.0e5 -10.0e5 -10.10e5 -123.1234567e5 +0.0e5 +10.0e5 +10.10e5 +123.1234567e5 "
- "0.e5 10.e5 34567.e5 -0.e5 -10.e5,,-123.e5 +0.e5,+10.e5 +123.e5, "
- ".0e5 .01e5 .1234567e5,-.0e5 -.02e5 -.10234e5 -.1234567e5 +.0e5 +.02e5,+.1234567e5 "
- "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 "
- "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, "
- ".0e+5 .01e+5 .1234567e+5 -.0e+5 -.02e+5 -.10234e+5 -.1234567e+5 +.0e+5 +.02e+5 +.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 +0.0e-5 +10.0e-5 +10.10e-5 +123.1234567e-5 "
- "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 "
- ".0e-5 .01e-5 .1234567e-5,-.0e-5 -.02e-5 -.10234e-5 -.1234567e-5,,+.0e-5 +.02e-5 +.1234567e-5, "
- "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 "
- "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 "
- ".0e-534 .01e-534,.1234567e-534 -.0e-534 -.02e-534,,-.10234e-534 -.1234567e-534,+.0e-534 +.02e-534 +.1234567e-534, "
- "0.0e534 10.0e534 10.10e534,123.1234567e534 -0.0e534 -10.0e534 -10.10e534 -123.1234567e534 +0.0e534,+10.0e534 +10.10e534 +123.1234567e534 "
- "0.e534,10.e534 34567.e534 -0.e534 -10.e534 -123.e534 +0.e534 +10.e534 +123.e534 "
- ".0e534 .01e534 .1234567e534 -.0e534,-.02e534 -.10234e534 -.1234567e534 +.0e534,,+.02e534 +.1234567e534 "
- "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 "
- "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 "
- ".0e+534 .01e+534 .1234567e+534 -.0e+534 -.02e+534,,-.10234e+534 -.1234567e+534 +.0e+534,+.02e+534 +.1234567e+534 "
- "0.0e534f 10.0e534f 10.10e534f 123.1234567e534f -0.0e534f,-10.0e534f -10.10e534f -123.1234567e534f +0.0e534f +10.0e534f,+10.10e534f +123.1234567e534f "
- "0.e534f 10.e534f 34567.e534f -0.e534f -10.e534f -123.e534f +0.e534f +10.e534f +123.e534f "
- ".0e534f .01e534f .1234567e534f,-.0e534f -.02e534f,-.10234e534f -.1234567e534f +.0e534f +.02e534f +.1234567e534f "
- "0.0e53ULf 10.0e53ULf 10.10e53ULf 123.1234567e53ULf -0.0e53ULf -10.0e53ULf -10.10e53ULf -123.1234567e53ULf +0.0e53ULf +10.0e53ULf +10.10e53ULf +123.1234567e53ULf "
- "0.e53ULf 10.e53ULf,,34567.e53ULf -0.e53ULf -10.e53ULf,-123.e53ULf,+0.e53ULf,+10.e53ULf +123.e53ULf "
- ".0e53ULf .01e53ULf .1234567e53ULf -.0e53ULf -.02e53ULf -.10234e53ULf -.1234567e53ULf +.0e53ULf +.02e53ULf +.1234567e53ULf "
- "0.0e53UL 10.0e53UL 10.10e53UL 123.1234567e53UL -0.0e53UL,,-10.0e53UL -10.10e53UL,-123.1234567e53UL +0.0e53UL +10.0e53UL +10.10e53UL +123.1234567e53UL "
- "0.e53UL 10.e53UL 34567.e53UL -0.e53UL -10.e53UL -123.e53UL +0.e53UL +10.e53UL +123.e53UL "
- ".0e53UL .01e53UL,.1234567e53UL -.0e53UL -.02e53UL,-.10234e53UL,,-.1234567e53UL +.0e53UL +.02e53UL +.1234567e53UL, ";
- int decimal_count = 0;
- int octal_count = 0;
- int hex_count = 0;
- int float_count = 0;
- int total_count = 0;
- count_numbers(input, &decimal_count, &octal_count, &hex_count, &float_count, &total_count);
- printf("Total decimal numbers: %d\n", decimal_count);
- printf("Total octal numbers: %d\n", octal_count);
- printf("Total hexadecimal numbers: %d\n", hex_count);
- printf("Total floating-point numbers: %d\n", float_count);
- printf("Total numbers: %d\n", total_count);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement