Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- enum
- {
- EMPTY_STRING = 0x0bad1dea,
- WRONG_FORMAT = 0xbedabeda,
- NEG = -1,
- BASE = 10
- };
- char*
- getline2(FILE *inpt);
- int
- main(void)
- {
- char *strt_string;
- int cnt_string = 0;
- while ((strt_string = getline2(stdin)) != NULL) {
- ++cnt_string;
- char *it = strt_string;
- while(isspace(*it)) {
- ++it;
- }
- if (*it == 0) {
- printf("%d\n", EMPTY_STRING + cnt_string);
- }
- else {
- _Bool wrong_string = 0;
- _Bool was_sign = 0;
- int cnt_number = 1;
- int sgn = 1;
- int sum = 0;
- while (*it != 0) {
- if (isspace(*it)) {
- if (was_sign) {
- printf("%d\n", WRONG_FORMAT + cnt_string);
- wrong_string = 1;
- break;
- }
- ++it;
- was_sign = 0;
- continue;
- }
- if ((*it < '0' || '9' < *it) && (*it != '+') && (*it != '-')) {
- printf("%d\n", WRONG_FORMAT + cnt_string);
- wrong_string = 1;
- break;
- }
- if ((*it == '+' || *it == '-') && was_sign) {
- printf("%d\n", WRONG_FORMAT + cnt_string);
- wrong_string = 1;
- break;
- }
- if (*it == '+') {
- was_sign = 1;
- sgn = 1;
- }
- if (*it == '-'){
- was_sign = 1;
- sgn = NEG;
- }
- if ('0' <= *it && *it <= '9') {
- char *end;
- int curr = strtol(it, &end, BASE);
- if (curr != strtoll(it, &end, BASE)) {
- sum += sgn * cnt_number;
- sgn = 1;
- ++cnt_number;
- was_sign = 0;
- } else {
- sum += curr;
- }
- it = end - 1;
- }
- ++it;
- }
- if (!wrong_string) {
- printf("%d\n", sum);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment