Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int num(const char *name, int *res);
- int num(const char *name, int *res)
- {
- FILE*fp; double x2, x1; int count = 0, countg = 0, counta = 0, counte = 0, opr = 0;
- double arifm, geom;
- if (!(fp = fopen(name, "r")))
- return -1;
- fscanf(fp, "%lf", &x1);
- if (feof(fp))
- {
- fclose(fp);
- *res = 4;
- return 0;
- }
- fscanf(fp, "%lf", &x2);
- if (feof(fp))
- {
- fclose(fp);
- if (x2>x1 || x2<x1) *res = 4;
- else *res = 3;
- return 0;
- }
- arifm = x2 - x1;
- if ((x2<0 || x2>0) && (x1<0 || x1>0)) geom = x2 / x1;
- x1 = x2;
- while (fscanf(fp, "%lf", &x2) == 1)
- {
- count++;
- if ((x2<0 || x2>0) && (x1<0 || x1>0))
- {
- if ((x2 > x1 || x2 < x1))
- {
- if (x2 / x1 <= geom && x2 / x1 >= geom) countg++;
- if (x2 - x1 <= arifm && x2 - x1 >= arifm) counta++;
- if (countg > 0 && counta > 0 && (x2 - x1<x2 / x1 || x2 - x1>x2 / x1))
- {
- if (countg == 1) countg = 0;
- else counta = 0;
- fclose(fp);
- *res = 0;
- return 0;
- }
- }
- else counte++;
- }
- else {if (x2>=x1 && x2<x1)counte++; }
- x1 = x2;
- }
- if (count == counta) opr = 1;
- if (count == countg) opr = 2;
- if (count == counte) opr = 3;
- if (!feof(fp))
- {
- fclose(fp);
- return -2;
- }
- fclose(fp);
- *res = opr;
- return 0;
- }
- int main(void)
- {
- const char *fname = "a.txt";
- int answer; int ret;
- ret = num(fname, &answer);
- if (ret < 0)
- {
- switch (ret)
- {
- case -1:
- printf("Cannot open %s\n", fname);
- break;
- case -2:
- printf("Cannot read %s\n", fname);
- break;
- default:
- printf("Error %d in file %s\n", ret, fname);
- }
- return 1;
- }
- printf("posl = %d\n", answer);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement