#include <stdio.h>
#include "main.h"
#ifdef DOS
#include <conio.h>
#endif
#ifdef LINUX
#include "tcolor.h"
#endif

/* ---------------- DEFINIZIONI DI FUNZIONE ---------------------- */
int LeggiContr(int inf, int sup)       /* Lettura di inf<=N<=sup */
{
  int D;
  do{
#ifdef DOS
    textcolor(LIGHTBLUE);
    gotoxy(5,5);
    cprintf(" Inserire un numero compreso tra %d  e  %d \t--> ",inf,sup);
    textcolor(CYAN);
    cscanf(" %d",&D);
#endif
#ifdef LINUX
    iniz_screen(CLIGHTBLUE, BG);
    put_vt_cursor(5, 5);
    printf(" Inserire un numero compreso tra %d  e  %d --> ",inf,sup);
    put_vt_cursor(52, 5);
    scanf("%d",&D);
#endif
    while ( D<0 || D>50 ){
#ifdef DOS
    clreol();
    textcolor(13);
    gotoxy(20,10);
    cprintf("Non ci siamo capiti! ... riprova ... : ");
    cscanf("%d",&D);
#endif
#ifdef LINUX
    iniz_screen(CCYAN, BG);
    put_vt_cursor(20, 10);
    printf("Non ci siamo capiti! ... riprova ... : ");
    put_vt_cursor(61, 10);
    scanf("%d",&D);
#endif
   }
  }while (D>sup||D<inf);
   return D;
  }

/* ----------------- DEFINIZIONE DI FUNZIONI GESTIONE ARRAY ---------------- */
void LeggiElenco(int Dim,int A[]){      /* Lettura elenco di Dim valori */
int i;
#ifdef DOS
   clrscr();
   gotoxy(17,5);
   textcolor(LIGHTRED);
   cprintf("\n\n --- LETTURA ELENCO DI %d VALORI ---\n",Dim);
   textcolor(LIGHTRED);
   gotoxy(19,10);
#endif
#ifdef LINUX
   iniz_screen(FG, BG);
   ct_text_xy( &screen_buf, 17, 5, "\n\n --- LETTURA ELENCO ---\n", CLR(FG, BG) );
#endif
   for(i=0;i<Dim;i++){
#ifdef DOS
     gotoxy(25,10+i);
     cprintf(" %d. ",i+1);
     cscanf("%d",&A[i]);
#endif
#ifdef LINUX
     put_vt_cursor(25, 10+i);
     printf(" %d. ",i+1);
     put_vt_cursor(25, 10+i+7);
     scanf("%d",&A[i]);
#endof
   }
}

void ScriviElencoCol(int Dim,int A[],int col){     /* Visualizzaz. elenco di dim valori */
int i;
#ifdef LINUX
  iniz_screen(col, BG);
  put_vt_cursor(10, 5);
#endif
  printf("\n\n ------ ELENCO DI %d VALORI -------\n",Dim);
#ifdef DOS
  textcolor(col);
#endif
  for(i=0;i<Dim;i++){
#ifdef DOS
    cprintf("%d ",A[i]);
#endif
#ifdef LINUX
    put_vt_cursor(3, 10+i);
    printf("%d ",A[i]);
#endif
  }
}

void Ordseqsel(int A[], int N){         /* Ord. sequenziale selettivo di A */
  int i,k;
  int scambio,postmin;
  for(i=0;i <=N-2;i++){
    postmin=i;
    for(k=i+1;k<=N-1;k++){
      if(A[k] < A[postmin])
      postmin=k;
    }
    scambio=A[postmin];
    A[postmin]=A[i];
    A[i]=scambio;
  }
}
