Skip to content

Instantly share code, notes, and snippets.

@JanTvrdik
Created October 5, 2011 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JanTvrdik/1265485 to your computer and use it in GitHub Desktop.
Save JanTvrdik/1265485 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char c = ' '; // aktuálně čtený znak
int negative;
int result;
int sum = 0;
while (1) {
negative = 0;
result = 0;
// Přeskočení bílých znaků
if (c == EOF || c == '\n') break;
do {
c = getchar();
if (c == EOF || c == '\n') break;
} while (c == '\t' || c == ' ' || c == '\n');
// Kontrola volitelného znamínka
if (c == '-' || c == '+') {
negative = (c == '-');
c = getchar();
if (c == EOF) return 1;
}
// Čtení čísel
while(isdigit(c)) {
result = 10 * result + (c - '0');
c = getchar();
}
// Aplikace znamínka
if (negative == 1) {
result *= -1;
}
sum += result;
}
printf("\n\n%d\n\n", sum);
system("PAUSE");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment