Advertisement
Grugaloragran

Untitled

Oct 22nd, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.15 KB | None | 0 0
  1. #include <stdio.h>
  2. int num(const char *name, int *res);
  3. int num(const char *name, int *res)
  4. {
  5.     FILE*fp; double x2, x1; int count = 0, countg = 0, counta = 0, counte = 0, opr = 0;
  6.     double arifm, geom;
  7.     if (!(fp = fopen(name, "r")))
  8.         return -1;
  9.     fscanf(fp, "%lf", &x1);
  10.     if (feof(fp))
  11.     {
  12.         fclose(fp);
  13.         *res = 4;
  14.         return 0;
  15.     }
  16.     fscanf(fp, "%lf", &x2);
  17.     if (feof(fp))
  18.     {
  19.         fclose(fp);
  20.         if (x2>x1 || x2<x1) *res = 4;
  21.         else *res = 3;
  22.         return 0;
  23.     }
  24.     arifm = x2 - x1;
  25.     if ((x2<0 || x2>0) && (x1<0 || x1>0)) geom = x2 / x1;
  26.     x1 = x2;
  27.  
  28.         while (fscanf(fp, "%lf", &x2) == 1)
  29.         {
  30.  
  31.             count++;
  32.            if ((x2<0 || x2>0) && (x1<0 || x1>0))
  33.            {
  34.             if ((x2 > x1 || x2 < x1))
  35.             {
  36.  
  37.                   if (x2 / x1 <= geom && x2 / x1 >= geom) countg++;
  38.                 if (x2 - x1 <= arifm && x2 - x1 >= arifm) counta++;
  39.                 if (countg > 0 && counta > 0 && (x2 - x1<x2 / x1 || x2 - x1>x2 / x1))
  40.                 {
  41.                     if (countg == 1) countg = 0;
  42.                     else counta = 0;
  43.                     fclose(fp);
  44.                     *res = 0;
  45.                     return 0;
  46.                 }
  47.  
  48.             }
  49.             else counte++;
  50.            }
  51.            else {if (x2>=x1  && x2<x1)counte++; }
  52.  
  53.             x1 = x2;
  54.         }
  55.         if (count == counta) opr = 1;
  56.         if (count == countg) opr = 2;
  57.         if (count == counte) opr = 3;
  58.  
  59.     if (!feof(fp))
  60.     {
  61.         fclose(fp);
  62.         return -2;
  63.     }
  64.     fclose(fp);
  65.     *res = opr;
  66.     return 0;
  67.  
  68. }
  69. int main(void)
  70. {
  71.     const char *fname = "a.txt";
  72.     int answer; int ret;
  73.     ret = num(fname, &answer);
  74.     if (ret < 0)
  75.     {
  76.         switch (ret)
  77.         {
  78.         case -1:
  79.             printf("Cannot open %s\n", fname);
  80.             break;
  81.         case -2:
  82.             printf("Cannot read %s\n", fname);
  83.             break;
  84.         default:
  85.             printf("Error %d in file %s\n", ret, fname);
  86.  
  87.         }
  88.         return 1;
  89.     }
  90.     printf("posl = %d\n", answer);
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement