Advertisement
RSzacki

Program calculates sum of given string of integers

Aug 10th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. /* Mój pierwszy program! */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5.  
  6. #define STRING 512
  7. #define MAX    10
  8.  
  9. /* Funkcja nr 5: skipwhite()
  10.  * Pomija biaîe znaki.
  11.  * Zwraca pierwszy niebiaîy znak.
  12.  * Wskaúnik na ciâg wskazuje na kolejny znak.
  13.  */
  14.  
  15. int skipwhite( char **p )
  16. {
  17.     char *s = *p;
  18.     char c;
  19.  
  20.     while( ( c = *s++ ) != '\0' && ( c == ' ' || c == '\t' || c == '\n' ) )
  21.         ;
  22.     /* c: Zawiera pierwszy niebiaîy znak lub \0 */
  23.     /* s: Wskazuje na nastëpny znak */
  24.  
  25.     *p = s;
  26. }
  27.  
  28. /* Funkcja nr 4: getint()
  29.  * Pobiera nastëpnâ liczbë z ciâgu tekstowego.
  30.  * s: adres wskaúnika na ciâg
  31.  * n: adres liczby caîkowitej
  32.  * wynik: 1 = dobrze, 0 = koniec ciâgu
  33.  */
  34.  
  35. int getint( char **p, int *n )
  36. {
  37.     char *s;
  38.     char c;
  39.     int m;
  40.     int sign = 1;
  41.  
  42.     if( ( c = skipwhite( p ) ) == '\0' )
  43.         return( 0 );
  44.  
  45.     s = *p;
  46.  
  47.     if( c == '-' ) {
  48.         sign = -1;
  49.         c = *s++;
  50.     }
  51.  
  52.     m = 0;
  53.     while( isdigit( c ) ) {
  54.         m = ( 10 * m ) + ( c - '0' );
  55.         c = *s++;
  56.     }
  57.  
  58.     m *= sign;
  59.  
  60.     *n = m;
  61.     *p = s;
  62.  
  63.     return( 1 );
  64. }
  65.  
  66. /* Funkcja nr 1: getints()
  67.  * Przeksztaîca ciâg tekstowy w ciâg liczb calkowitych.
  68.  * a: ciâg liczb
  69.  * n: rozmiar tablicy
  70.  * s: ciâg tekstowy
  71.  * wynik: ile liczb przeksztaîcono.
  72.  */
  73.  
  74. int getints( int *a, int n, char *s )
  75. {
  76.     int i;
  77.  
  78.     i = 0;
  79.     while( i < n && getint( &s, &a[ i ] ) == 1 ) {
  80.         /* Przechodzimy do nast. elementu */
  81.         i++;
  82.     }
  83.  
  84.     return( i );
  85. }
  86.  
  87. /* Funkcja nr 2: calcsum()
  88.  * Liczy sumë ciâgu liczb caîkowitych
  89.  * a: ciâg liczb
  90.  * n: rozmiar tablicy
  91.  * wynik: suma
  92.  */
  93.  
  94. int calcsum( int *a, int n )
  95. {
  96.     int i, sum;
  97.  
  98.     i   = 0;
  99.     sum = 0;
  100.     while( i < n )
  101.         /* Dodajemy element ciâgu */
  102.         sum += a[ i++ ];
  103.  
  104.     return( sum );
  105. }
  106.  
  107. /* Funkcja nr 3: main()
  108.  * Liczy i wyôwietla sumë ciâgu wprowadzonego przez uűytkownika
  109.  */
  110.  
  111. int main( void )
  112. {
  113.     char s[ STRING ];
  114.     int a[ MAX ];
  115.     int n, sum;
  116.  
  117.     if( fgets( s, STRING, stdin )
  118.     &&( n = getints( a, MAX, s ) ) > 0 ) {
  119.         sum = calcsum( a, n );
  120.         printf( "Suma = %d\n", sum );
  121.     } else
  122.         printf( "Blad odczytu.\n" );
  123.     return( 0 );
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement