#include "func.h"

void setmass(int *massSize){
    printf("\t\tSetMassiveSize\n");
    printf("Set Massive Size - input integer\n");
    printf("Exit from this menu - input 0\n\n\n");
    printf("Input: ");
    int n;
    while(scanf(" %d",&n)!=1||n>MAXMASSIVESIZE){
        if(flush())return;
        flush();
        if(n>10000)printf("Input cannot be >%d",MAXMASSIVESIZE);
        printf("Input Err\nInput: ");
    }

    flush();

    if(n==0)
        return;
    else
        *massSize = n;
    return;
}

int fillmass(int size, int *massive){
    printf("\t\tFill Massive\n");
    printf("You can devide elements with space or enter.\n");
    printf("You also can decide to from which element start with e.\n");
    printf("To exit input g.\n\n\n");
    printf("Input: ");


    int x, i, e1,e = 0, p=-1, minus = 0;

    for(i = 0;i < size;i++){
        if(i<0)i=0;
        e1 = e = massive[i] = 0;
        while(((x = getchar()) != '\n' && x !=' ' 
            && x != EOF && x != 4) 
            && ((x>='0' && x<='9') 
            || x == 'e' || (x == '-' && e==0 && massive[i]==0)))
            {
            if (x == 'e')e1=1;
            else if (x == '-')minus=1;
            
            else if (e1) e = (e*10)+(x-'0');
            else massive[i] = (massive[i]*10) + (x-'0');
            }

        if(x == EOF || x == 3){return 0;}
        if(e1&&minus)minus=0;
        if(minus && (x == '\n' || x == ' ')){minus = 0; massive[i]*=-1;}
        if(e>size) {printf("e is out of range.\n");i=p;}
        else if(e1) {i = e - 1;putchar('\n');}
        if(x == 'g'){return 1;}
        if((x<='0' && x>='9')||x==' '||x=='\n'||x<=32);
           else {
            while((x=getchar())!='\n'&&x!=' '&&x!=EOF&&x!=4);
            i--;
            printf("ErrInput\n");}
        if(p!=i&&e1!=1)
            printf("Massive - [%d]\t%d\n",i,massive[i]);
        p=i;
    }
    return 1;
}


void viewmass(int size, int *massive){
    printf("\t\tView Massive\n");
    for(int i = 0; i < size; i++){
        printf(" | [%6d]\t%p\t%d\n",i,&massive[i],massive[i]);
    }
}


void selfunc(int size, int *massive){
    printf("\t\tFunctions\t(my - 7|16|24)\n");
    
    printf("\tRange Operations\n");
    printf("2. Find Max Range\n");
    printf("3. Find Min Range\n");
    printf("10. Sum Range\n");
    printf("11. Average Range\n");
    printf("18. Diff Min Max Range\n\n");

    printf("\tValue Search\n");
    printf("6. Find Last Positive\n");
    printf("7. Find Last Negative\n\n");

    printf("\tIndex Search\n");
    printf("8. Find Last Positive Index\n");
    printf("9. Find Last Negative Index\n");
    printf("13. Find Indices Between\n");
    printf("17. Find Indices Value Range\n\n");

    printf("\tValue Filtering\n");
    printf("4. Find Indices Above\n");
    printf("5. Find Indices Below\n");
    printf("19. Count Below Value\n");
    printf("20. Count Above Value\n");
    printf("21. Sum Below Value\n");
    printf("22. Sum Above Value\n");
    printf("23. Find Indices Below Value\n");
    printf("24. Find Indices Above Value\n\n");

    printf("\tAverage Comparisons\n");
    printf("25. Count Above Average\n");
    printf("26. Count Below Average\n");
    printf("27. Find Indices Below Average\n");
    printf("28. Find Indices Above Average\n");
    printf("29. Sum Below Average\n");
    printf("30. Sum Above Average\n\n");

    printf("\tSpecial Conditions\n");
    printf("1. Find Min Max\n");
    printf("12. Average Range\n");
    printf("14. Count Multiples\n");
    printf("15. Count Between Equals\n");
    printf("16. Sum Value Range\n\n");

    printf("\tExit - 0\n");

    int n;
    while(scanf(" %d",&n)!=0){
        if(flush())return;
        if (n == 0) return;
        if (n == 1 || n == 2 || n == 3 || n == 4 || n == 5 || 
            n == 10 || n == 11 || n == 12 || n == 13 || 
            n == 16 || n == 17 || n == 18) {
            range(n, size, massive);
        }else if ((n >= 6 && n <= 9) || n == 14 || n == 15 || 
                (n >= 19 && n <= 30)) {
            find(n, size, massive);
        } else {
            printf("Function number %d invalid.\n", n);
        }
        printf("Input: ");
    }
}

int flush(void){
    int c;
while ((c = getchar()) != '\n' && c != EOF) 
    if (c == 4 || c == 3) return 1;
if (c == EOF) return 1;
return 0;
}
