119 lines
2.5 KiB
C
119 lines
2.5 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define LINE_MAX_LENGTH 256
|
|
#define NUM_0_CHARCODE 48
|
|
#define NUM_9_CHARCODE NUM_0_CHARCODE + 9
|
|
#define PART_2 1
|
|
#define FORWARD 1
|
|
#define BACKWARD -1
|
|
|
|
char* english_digit_strings[] = {
|
|
"zero",
|
|
"one",
|
|
"two",
|
|
"three",
|
|
"four",
|
|
"five",
|
|
"six",
|
|
"seven",
|
|
"eight",
|
|
"nine",
|
|
};
|
|
|
|
#define NUM_OF_DIGITS ((sizeof(english_digit_strings)) / (sizeof(english_digit_strings[0])))
|
|
|
|
char *english_digit_strings_reversed[NUM_OF_DIGITS];
|
|
|
|
int get_num(char *p, int dir) {
|
|
// Check whether it is a number
|
|
if (*p >= NUM_0_CHARCODE && *p <= NUM_9_CHARCODE) {
|
|
return *p - NUM_0_CHARCODE;
|
|
}
|
|
|
|
// Check spelled out numbers
|
|
if (PART_2) {
|
|
// Check each digit
|
|
for (int i = 0; i < NUM_OF_DIGITS; i++) {
|
|
char *reference = dir == FORWARD ? english_digit_strings[i] : english_digit_strings_reversed[i];
|
|
char *tmp = p;
|
|
int match = 1;
|
|
while (*reference != '\0') {
|
|
if (*tmp != *reference) {
|
|
match = 0;
|
|
break;
|
|
}
|
|
reference++;
|
|
tmp += dir;
|
|
}
|
|
|
|
if (match) {
|
|
return i;
|
|
}
|
|
}
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
char* reverse_string(char* str) {
|
|
char *p = str;
|
|
while (*p != '\0') p++;
|
|
|
|
int len = p - str;
|
|
|
|
char *newstr = (char *)malloc(len + 1);
|
|
memset(newstr, 0, len + 1);
|
|
p--;
|
|
|
|
|
|
char* newp = newstr;
|
|
while (p != str) {
|
|
*newp = *p;
|
|
newp++;
|
|
p--;
|
|
}
|
|
*newp = *p;
|
|
|
|
return newstr;
|
|
}
|
|
|
|
void create_reversed_digit_strings() {
|
|
for (int i = 0; i < NUM_OF_DIGITS; i++) {
|
|
english_digit_strings_reversed[i] = reverse_string(english_digit_strings[i]);
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
char *p, *buf, c;
|
|
int sum = 0, n = -1;
|
|
create_reversed_digit_strings();
|
|
|
|
buf = (char *)malloc(LINE_MAX_LENGTH);
|
|
memset(buf, 0, LINE_MAX_LENGTH);
|
|
p = buf;
|
|
|
|
while ((c = getchar()) != EOF) {
|
|
*p++ = c;
|
|
if (c == '\n') {
|
|
// Find last number (p points to the end of line)
|
|
while ((n = get_num(p, BACKWARD)) == -1) p--;
|
|
sum += n;
|
|
// Find first number
|
|
p = buf;
|
|
while ((n = get_num(p, FORWARD)) == -1) p++;
|
|
sum += n * 10;
|
|
|
|
memset(buf, 0, LINE_MAX_LENGTH);
|
|
p = buf;
|
|
}
|
|
}
|
|
|
|
free(buf);
|
|
for (int i = 0; i < NUM_OF_DIGITS; i++) {
|
|
free(english_digit_strings_reversed[i]);
|
|
}
|
|
printf("%i\n", sum);
|
|
}
|