Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 24th, 2012  |  syntax: None  |  size: 1.19 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // text.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <conio.h>
  9.  
  10. #define INPUT_FILENAME "input.txt"
  11. #define MAX_STRING_SIZE 64
  12.  
  13. int main()
  14. {
  15.     char nums_string[MAX_STRING_SIZE];
  16.     FILE* input;
  17.     char* token;
  18.     int nums_positive = 0, sum_positive = 0;
  19.     int number;
  20.  
  21.     if ((input = fopen(INPUT_FILENAME, "r")) == NULL)
  22.     {
  23.         fprintf(stderr, "Can't open file \"%s\"", INPUT_FILENAME);
  24.         _getch();
  25.         _exit(EXIT_FAILURE);
  26.     }
  27.  
  28.     while (fgets(nums_string, MAX_STRING_SIZE, input))
  29.     {
  30.         token = strtok(nums_string, " \n");
  31.         while (token != NULL)
  32.         {
  33.             number = atoi(token);
  34.             if (number > 0)
  35.             {
  36.                 ++nums_positive;
  37.                 sum_positive += number;
  38.             }
  39.  
  40.             token = strtok(NULL, " \n");
  41.         }
  42.     }
  43.  
  44.     printf("Average is: %.2f\n", (float) sum_positive / nums_positive);
  45.     printf("Number of positive: %d", nums_positive);
  46.  
  47.     return 0;
  48. }
  49. int _tmain(int argc, _TCHAR* argv[])
  50. {
  51.         return 0;
  52. }