#include <graph.h>
#define SIZE 200
//!!!extern Graph Func[All_Graph];
extern int far Reg_Dim[];
int X_Graph[SIZE];

int Kilometr[SIZE],Piket[SIZE],Styk[SIZE];
 //Км,Пк,Ст последнего расчета регулировки зазоров

//int main_nnnn1(void);
void VCH (int &,int &);
int DELTA,EPS,DR[SIZE];
int S[SIZE],HLA[SIZE],SLA[SIZE],SH[SIZE],DS[SIZE],
    DN[SIZE],DEL[SIZE];
int I; //Реальная размерность массивов
////////////////////////////////////
//int main_nnnn2(void);
//void DEL2 (int &,int &), VCH_2 (int &,int &,int &);
void VCH2_2 (int &), abs (int *);
int min(int,int);
int DU[SIZE],HSd[SIZE],Sd[SIZE],DSd[SIZE],ULA[SIZE];
int EPS2,DELTA2;
int I2; //Реальная размерность массивов

////////////////////////////////////
int main_nnn21(void);
void //VVOD3(void),PECH3(void),
 PROM(int,int,int,int,int),VCH2_3(int &);
int BLA[SIZE],DHLA[SIZE],BHLA[SIZE],DELH[SIZE],DBLA[SIZE];
int EPS3;
int I3,K1; //I3 - Реальная размерность массивов
////////////////////////////////////
int main_nn2(void);
void DEL2_4 (int &,int &),VCH_4 (int &,int &,int &),
 PROM1(int,int),VDSB(int &);
float EPS4;int DELTA4,ZLA[200],DD;
int I4,K4; // I4 - Реальная размерность массивов
////////////////////////////////////
/*
int main_nnnn1(void) {
 int J,J1,j=0;
 while(j<I){
  DEL[j]=DR[j]-DN[j];
  j++;
 }
 S[0]=0;
 SH[0]=0;
 VCH(1,0);
 S[0]=S[1];
 SH[0]=SH[1];
 DS[0]=DS[1];
 J=1;
 while(J<I) {
  J1=J-1;
  VCH(J,J1);
  J++;
 }
 /////////////////////////////////////////////////////////
 Reg_Dim[0]=I; //!!!
 return 0;
}
*/
/***********************************************************/
void VCH (int &J1,int &J2) {
 S[J1]=S[J2]+SLA[J1];
 SH[J1]=SH[J2]+HLA[J1];
 DS[J1]=S[J1]-SH[J1]; return;
}
/**************************************************************************/
/*
int main_nnnn2(void) {
 int J,J1,xd,j;
 j=0;
 while(j<I2){
  DEL[j]=DR[j]-DN[j];
  j++;
 }
 DELTA2=DEL[0];
 S[0]=0;  SH[0]=0;  Sd[0]=0; DEL2(1,0);
 S[0]=S[1];  SH[0]=SH[1];  DS[0]=DS[1];  Sd[0]=Sd[1];
 DSd[0]=DSd[1];  DEL[0]=DEL[1]; VCH2_2(0); J=1;
 while(J<I2) {
  J1=J-1;
  DELTA2=DEL[J];  DEL2(J,J1);  VCH2_2(J);
  J++;
 }
 J=0;
 while(J<I2) {
  xd=DEL[J];
  abs(&xd);
  DU[J]=DR[J]-xd;
  if (DEL[J]<0) {DU[J]=-DU[J];}
  J++;
 }
 /////////////////////////////////////////////////////////
 for (xd=0; xd<I2; xd++) X_Graph[xd]=xd;
// !!! Func[0].Data_Array.X = Func[1].Data_Array.X = &X_Graph[0];
// Func[0].Data_Array.Y = &DS[0];
// Func[1].Data_Array.Y = &Sd[0];
// Func[0].Len_Data = Func[1].Len_Data = I2;
// Func[0].Cond = Func[1].Cond = 0x04;
 /////////////////////////////////////////////////////////
 return 0;
}
*/
/***********************************************************/
/* void VCH_2 (int &J1,int &J2,int &DE) {
 S[J1]=S[J2]+SLA[J1];
 SH[J1]=SH[J2]+HLA[J1];
 DS[J1]=S[J1]-SH[J1];
 Sd[J1]=Sd[J2]+DE;
 DSd[J1]=DS[J1]-Sd[J1]; return;
}  */
/************************************************************/
void VCH2_2(int &J) {
 ULA[J]=HLA[J]+DEL[J];
 HSd[J]=SH[J]+DEL[J]; return;
}
/**************************************************************/
/* void DEL2(int &K1,int &K2) {
 int A,B,X1,X2,SP,SHP,DSP,SdP,DSdP,P,ADSdP,ADSd,xa;
 A=-1*DELTA2; B=DELTA2; P=B-A; xa=0; abs(&P);
 while(P>EPS2) {
  X1=A+xa;  X2=B-xa;
  if((X1>0)&(X2<0)){
   VCH_2(K1,K2,X1);
   SP=S[K1]; SHP=SH[K1]; DSP=DS[K1]; SdP=Sd[K1]; DSdP=DSd[K1];
   VCH_2(K1,K2,X2);
   ADSdP=DSdP;  abs(&ADSdP);   ADSd=DSd[K1];
   abs(&ADSd);
   if(ADSdP>ADSd){A=X1;
    SP=S[K1];  SHP=SH[K1];  DSP=DS[K1];  SdP=Sd[K1];  DSdP=DSd[K1];
    DEL[K1]=X2;
   }
   else { DEL[K1]=X1;  B=X2; }
   P=B-A; abs (&P);
   if(B<=A) P=0;
  }
  xa+=1;
 }
 S[K1]=SP;
 SH[K1]=SHP;
 DS[K1]=DSP;
 Sd[K1]=SdP;
 DSd[K1]=DSdP; return;
}
*/
/**********************************************/
void abs (int *r) { if(*r<0) { *r=-1*(*r); } return; }
//void fabs (float *r) { if(*r<0) { *r=-1.*(*r); } return; }
int min(int a,int b) { return (a<b ? a : b); }
/**************************************************************************/

int main_nnn21(void) {
 int xx1,xx2,xx3;
 int i1,j1,j,jj,jj1,kon;
 j=0;
 while(j<I3){
  DELH[j]=DR[j]-DN[j];j++;
 }
 i1=I3-1;
 while(i1>2){
  BLA[i1]=0;   DHLA[i1]=0;
  BHLA[i1]=0;  j1=0;    kon=K1-1;
  while(j1<=kon) { 
   j=i1-j1; xx1=DHLA[i1]; xx2=BHLA[i1]; xx3=BLA[i1];
   PROM(xx1,xx2,xx3,i1,j); 
   j1++;
  }
  DBLA[i1]=BLA[i1]-BHLA[i1];
  i1--;
 }
 DHLA[2]=DELH[0]+DELH[2]+DELH[1];
 DHLA[1]=DELH[0]+DELH[1];
 DHLA[0]=DELH[0];
 BHLA[2]=HLA[2]+HLA[1]+HLA[0];
 BHLA[1]=HLA[1]+HLA[0];
 BHLA[0]=HLA[0];
 BLA[2]=SLA[2]+SLA[1]+SLA[0];
 BLA[1]=SLA[1]+SLA[0];
 BLA[0]=SLA[0];
 DBLA[2]=BLA[2]-BHLA[2];
 DBLA[1]=BLA[1]-BHLA[1];
 DBLA[0]=BLA[0]-BHLA[0];
 S[0]=0; SH[0]=0;
 VCH(1,0);  S[0]=S[1];  SH[0]=SH[1];  DS[0]=DS[1];  jj=1;
 while(jj<I3) {
  jj1=jj-1;  VCH(jj,jj1);  jj++; }
 jj=0;
 while(jj<I3) {  VCH2_3(jj);  jj++; }
 /////////////////////////////////////////////////////////
 Reg_Dim[0]=I3; //!!!
 return 0;
}
/*************************************/
void VCH2_3(int &J) {
 ULA[J]=HLA[J]+DELH[J]; return;
}
/*************************************/
void PROM (int DS1,int DS2,int DS3,int ii,int jj){
 DHLA[ii]=DS1+DELH[jj];
 BHLA[ii]=DS2+HLA[jj];
 BLA[ii]=DS3+SLA[jj]; return;
}
/**************************************************************************/

int main_nn2(void) {
 int XD,xx1,xx2,xx3;
 int J,J1,i1,j1,j,j2,jj,jj1,kon;
 j=0;
 while(j<I4) {  DELH[j]=DR[j]-DN[j]; j++; }
 i1=I4-1;
 while(i1>2){
  BLA[i1]=0;    DHLA[i1]=0;  BHLA[i1]=0;   j1=0;    kon=K4-1;
  while(j1<=kon) {
   j=i1-j1; xx1=DHLA[i1]; xx2=BHLA[i1]; xx3=BLA[i1];
   PROM(xx1,xx2,xx3,i1,j);  j1++;
  }
  DBLA[i1]=BLA[i1]-BHLA[i1];  i1--;
 }
 DHLA[2]=DELH[0]+DELH[2]+DELH[1];
 DHLA[1]=DELH[0]+DELH[1];
 DHLA[0]=DELH[0];
 BHLA[2]=HLA[2]+HLA[1]+HLA[0];
 BHLA[1]=HLA[1]+HLA[0];
 BHLA[0]=HLA[0];
 BLA[2]=SLA[2]+SLA[1]+SLA[0];
 BLA[1]=SLA[1]+SLA[0];
 BLA[0]=SLA[0];
 DBLA[2]=BLA[2]-BHLA[2];
 DBLA[1]=BLA[1]-BHLA[1];
 DBLA[0]=BLA[0]-BHLA[0];
 S[0]=0;     SH[0]=0;     Sd[0]=0;
 DELTA4=DELH[0]; abs(&DELTA4); jj=0;
 while(jj<I4){  DEL[jj]=DELH[jj];  jj++; }
 VCH(1,0);    S[0]=S[1];      SH[0]=SH[1];      DS[0]=DS[1];
 jj=1;
 while(jj<I4){    jj1=jj-1;   VCH(jj,jj1);  jj++;  }
 jj=I4-1;
 Sd[jj]=DS[jj];
 DSd[jj]=DS[jj];
 VCH2_2(jj);
 J=I4-1; kon=1;
 while(J>=0) {
  J1=J-1;
  DELTA4=DEL[J];  abs(&DELTA4);
  DD=DS[J]-DS[J1];
  if(DBLA[J]>=0){
   if(DS[J]>=0){
    if(DD>=0)  DEL[J]=DELTA4;
    else  DEL[J]=-1*DELTA4;
   }
   else{
    if(DD>=0.)  DEL[J]=DELTA4;
    else DEL[J]=-1*DELTA4;
   }
  }
  else {
   if(DS[J]>=0.){
    if(DD>=0.) DEL[J]=DELTA4;
    else DEL[J]=-1*DELTA4;
   }
   else {
    if(DD>=0.) DEL[J]=DELTA4;
    else DEL[J]=-1*DELTA4;
   }
  }
  XD=DEL[J];  VCH_4(J,J1,XD);
  VCH2_2(J);  PROM1(J,J);
  if(DBLA[J]>ZLA[J]) {
   kon=0;      DEL2_4(J,J1);   VCH2_2 (J);  }
  else {
   if (kon==0) {
    if ((DSd[J]<0)&(DSd[J1]>0)) { VDSB(J);kon=1; }
    else {
     if ((DSd[J]>0)&(DSd[J1]<0)) { VDSB(J);kon=1; }
    }
   }
  }
  J--;
 }
 J=0;
 while(J<I4) {
  XD=DEL[J];  abs(&XD);
  DU[J]=DR[J]-XD;
  J++;
 }
 /////////////////////////////////////////////////////////
 for (XD=0; XD<I4; XD++) X_Graph[XD]=XD;
/* !!! Func[0].Data_Array.X = Func[1].Data_Array.X = &X_Graph[0];
 Func[0].Data_Array.Y = &DS[0];
 Func[1].Data_Array.Y = &Sd[0];
 Func[0].Len_Data = Func[1].Len_Data = I4;
 Func[0].Cond = Func[1].Cond = 0x04;
*/
 Reg_Dim[1]=I4; //!!!
 /////////////////////////////////////////////////////////
 return 0;
}

/*************************************/
void PROM1 (int DS4,int ii){
 ZLA[ii]=K4*DEL[ii]; return;
}
/***********************************************************/
void VCH_4 (int &J1,int &J2,int &DE) {
 Sd[J2]=Sd[J1]-DE;
 DSd[J2]=DS[J2]-Sd[J2]; return;
}
/***********************************************************/
void DEL2_4(int &K1,int &K2) {
 int A,B,X1,X2,SP,SHP,DSP,SdP,DSdP,P,ADSdP,ADSd,xa;
 A=-1*DELTA4;  B=DELTA4;   P=B-A;  xa=0;  abs(&P);
 while(P>EPS4) {
  X1=A+xa;  X2=B-xa;
  if((X1>0)&(X2<0)) {
   VCH_4(K1,K2,X1);
   SP=S[K2];   SHP=SH[K2];
   DSP=DS[K2]; SdP=Sd[K2];
   DSdP=DSd[K2];
   VCH_4(K1,K2,X2);
   ADSdP=DSdP;  abs(&ADSdP);
   ADSd=DSd[K2];abs(&ADSd);
   if(ADSdP>ADSd) {
    A=X1;
    SP=S[K2];   SHP=SH[K2];    DSP=DS[K2];    SdP=Sd[K2];
    DSdP=DSd[K2];  DEL[K2]=X2;
   }
   else {
    DELH[K2]=X1;  B=X2;
   }
   P=B-A; abs (&P);
   if(B<=A) P=0;
  }
  xa+=1;
 }
 S[K2]=SP;
 SH[K2]=SHP;
 DS[K2]=DSP;
 Sd[K2]=SdP;
 DSd[K2]=DSdP; return;
}
/**********************************************/
void VDSB(int &jk){
 int jk1,jk2,xxp,xxp2,xxp1,sp,sp1;
 jk1=jk-1;
 if(DSd[jk]>DSd[jk1]) sp=DSd[jk];
 else { jk=jk1; jk2=jk1+1; sp=DSd[jk]; }
 xxp=DEL[jk];
 DEL[jk]=xxp-sp;
 while(jk<=jk2) {
  jk1=jk-1;
  Sd[jk]=Sd[jk1]-DEL[jk];
  DSd[jk]=DS[jk]-Sd[jk];
  ULA[jk]=HLA[jk]+DEL[jk];
  ZLA[ jk]=K4*DEL[jk];
  jk++;
 } return;
}

/**************************************************************************/
/**************************************************************************/

#include <stdlib.h>
#include <string.h>

extern unsigned km[6];
void Write0(char *,int,int,long),
 Write1(char *,int,int,long),
 Write2(char *,int,int,long),
 Write3(char *,int,int,long),
 Read0(char *,int,int,long);
void Init_Dopusk(int,int);
unsigned char StrBuf[16];
/*
void Write0(char *Str,int Len,int Fil,long Num) {
 int i;
 for (i=0;i<Len;i++) *(Str+i)=' ';
 if (Num<I) switch (Fil) {
  case 0: return; //i=km[0];break;
  case 1: i=Num+1;break;
  case 2: i=SLA[Num];break;
  case 3: i=S[Num];break;
  case 4: i=SH[Num];break;
  case 5: i=DS[Num];break;
  default: return;
 }
 itoa (i,StrBuf,10);
 i=min(Len,strlen(StrBuf));
 strncpy (Str,StrBuf,i);
 return;
};
*/
extern char FirstZap;
extern float Tmp_All;
void Read0(char *Str,int Len,int Fil,long Num) {
 int i=atoi (Str);
 switch (Fil) {
  case 6: HLA[Num]=i; break;
  case 5: SLA[Num]=i; break;
  case 4://1 раз читает приведенную температуру
   if (FirstZap) {
    Tmp_All=i; FirstZap=0x00;
   }
  break;
  case 0: Kilometr[Num]=i;  break;
  case 1: Piket[Num]=i;     break;
  case 2: Styk[Num]=i;      break;
  //Для правой нити - номера 8,7 соотв.
/*
  case 2:
   if (RegType==0x01) DR[Num]=i;
   else  DR[Num]=i;
   Dop14[0]+=i;
  break;
  case 3: DN[Num]=i; break;
  default: Mistake (3); break;
*/
 }  return;
}

extern float Dop14[],A14[];
void Init_Dopusk (int All, int I) { //I=0,1
 int i,Num;
 for (Num=0; Num<All; Num++) {  DN[Num]=A14[I]; DR[Num]=Dop14[I];  }
 return;
}
/*
void Write1 (char *Str,int Len,int Fil,long Num) {
 int i=0;
 for (i=0;i<Len;i++) *(Str+i)=' ';
 if (Num<I2) switch (Fil) {
  case 0: return; //i=km[0];break;
  case 1: i=Num+1;break;
  case 2: i=SLA[Num];break;
  case 3: i=S[Num];break;
  case 4: i=DS[Num];break;
  case 5: i=DEL[Num];break;
  case 6: i=DSd[Num];break;
  case 7: i=ULA[Num];break;
  case 8: i=DU[Num];break;
  default: return;
 }
 itoa (i,StrBuf,10);
 i=min(Len,strlen(StrBuf));
 strncpy (Str,StrBuf,i);
 return;
}
*/
void Write2 (char *Str,int Len,int Fil,long Num) {
 int i;
 for (i=0;i<Len;i++) *(Str+i)=' ';
 if (Num<I3) switch (Fil) {
  case 0: i=Kilometr[Num];break;
  case 1: i=Piket[Num];break;
  case 2: i=Styk[Num];break;
  case 3: i=SLA[Num];break;
  case 4: i=BLA[Num];break;
  case 5: i=BHLA[Num];break;
  case 6: i=DBLA[Num];break;
  case 7: i=DS[Num];break;
  case 8: i=ULA[Num];break;
  default: return;
 }
 itoa (i,StrBuf,10);
 i=min(Len,strlen(StrBuf));
 strncpy (Str,StrBuf,i);
 return;
}

void Write3 (char *Str,int Len,int Fil,long Num) {
 int i;
 for (i=0;i<Len;i++) *(Str+i)=' ';
 if (Num<I4) switch (Fil) {
  case 0: i=Kilometr[Num];break;
  case 1: i=Piket[Num];break;
  case 2: i=Styk[Num];break;
  case 3: i=SLA[Num];break;
  case 4: i=BLA[Num];break;
  case 5: i=BHLA[Num];break;
  case 6: i=DBLA[Num];break;
  case 7: i=DSd[Num];break;
  case 8: i=DEL[Num];break;
  case 9: i=ULA[Num];break;
  default: return;
 }
 itoa (i,StrBuf,10);
 i=min(Len,strlen(StrBuf));
 strncpy (Str,StrBuf,i);
 return;
}
