#include <perms.h>
void Newline(void) { Printsymbol(Nl); }
void Space(void) { Printsymbol(' '); }
void Newlines(int Count) {
  while (Count > 0) {
    Printsymbol(Nl);
    Count--;
  }
}
void Spaces(int Count) {
  while (Count > 0) {
    Printsymbol(' ');
    Count--;
  }
}
void Printstring(_imp_string S) {
  int L;
  int I;
  L = *Length(S);
  for (I = 1; I <= L; I++) Printsymbol(*Charno(S, I));
}
void Skipsymbol(void) {
  int Trash;
  Readsymbol(Trash);
}
_imp_string Substring(_imp_string S, int From, int To) {
  int Get;
  int Put;
  _imp_string Temp;
  if (0 > From || From > *Length(S))
    _imp_signal(6, 2, From, _imp_str_literal(""));
  if (0 > To || To > *Length(S)) _imp_signal(6, 2, To, _imp_str_literal(""));
  if (From > To) _imp_signal(5, 3, 0, _imp_str_literal(""));
  *Length(Temp) = (To - From) + 1;
  Put = 1;
  Get = From;
  while (Get <= To) {
    *Charno(Temp, Put) = *Charno(S, Get);
    Put++;
    Get++;
  }
  return (Temp);
}
void Tolower(_imp_string *S) {
  int I;
  unsigned char *P;
  for (I = 1; I <= *Length(*S); I++) {
    P = Charno(*S, I);
    if ('A' <= *P && *P <= 'Z') *P = *P + ('a' - 'A');
  }
}
void Toupper(_imp_string *S) {
  int I;
  unsigned char *P;
  for (I = 1; I <= *Length(*S); I++) {
    P = Charno(*S, I);
    if ('a' <= *P && *P <= 'z') *P = *P - ('a' - 'A');
  }
}
_imp_string Trim(_imp_string S, int Max) {
  if (Max < 0) _imp_signal(6, 2, Max, _imp_str_literal(""));
  if (*Length(S) > Max) *Length(S) = Max;
  return (S);
}
_imp_string Int2ascii(int Nn, int Base, int Places) {
  static _imp_string Basechar =
      _imp_str_literal("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  _imp_string Reversed;
  _imp_string Human;
  int N;
  int Np;
  int R;
  int I;
  int Negative;
  if (Base < 2) Base = 10;
  if (Base > 36) Base = 10;
  *Length(Reversed) = 0;
  N = Nn;
  if (N == 0) {
    R = 0;
    *Length(Reversed) = *Length(Reversed) + 1;
    *Charno(Reversed, *Length(Reversed)) = *Charno(Basechar, R + 1);
  } else {
    if (N > 0) {
      Negative = 0;
      N = -N;
    } else
      Negative = 1;
    while (N < 0) {
      Np = N / Base;
      R = (Np * Base) - N;
      *Length(Reversed) = *Length(Reversed) + 1;
      *Charno(Reversed, *Length(Reversed)) = *Charno(Basechar, R + 1);
      N = Np;
    }
    if (Negative > 0) {
      *Length(Reversed) = *Length(Reversed) + 1;
      *Charno(Reversed, *Length(Reversed)) = '-';
    }
  }
  if (Places <= 0)
    Places = -Places;
  else
    Places++;
  if (Places > *Length(Reversed))
    while (*Length(Reversed) < Places) {
      *Length(Reversed) = *Length(Reversed) + 1;
      *Charno(Reversed, *Length(Reversed)) = ' ';
    }
  *Length(Human) = 0;
  for (I = *Length(Reversed); I >= 1; I--) {
    *Length(Human) = *Length(Human) + 1;
    *Charno(Human, *Length(Human)) = *Charno(Reversed, I);
  }
  return (Human);
}
_imp_string Formatnumber(int N, int Base, int Places) {
  _imp_string S;
  _imp_string X;
  int I;
  if (Base == 10)
    X = Int2ascii(N, Base, Places);
  else {
    S = Int2ascii(N, Base, 0);
    X = Int2ascii(Base, 10, Places - *Length(S) - 1);
    *Length(X) = *Length(X) + 1;
    *Charno(X, *Length(X)) = '_';
    for (I = 1; I <= *Length(S); I++) {
      *Length(X) = *Length(X) + 1;
      *Charno(X, *Length(X)) = *Charno(S, I);
    }
  }
  return (X);
}
_imp_string Itos(int N, int Places) {
  _imp_string S;
  S = Int2ascii(N, 10, Places);
  return (S);
}
void Write(int N, int Places) {
  _imp_string Ch;
  int New;
  int Digit;
  int Sign;
  int I;
  *Length(Ch) = 0;
  if (!N) {
    *Length(Ch) = *Length(Ch) + 1;
    *Charno(Ch, *Length(Ch)) = '0';
    Sign = 0;
  } else {
    Sign = 1;
    if (N > 0) {
      Sign = 0;
      N = -N;
    }
    while (N) {
      New = N / 10;
      Digit = (New * 10) - N;
      *Length(Ch) = *Length(Ch) + 1;
      *Charno(Ch, *Length(Ch)) = Digit + '0';
      N = New;
    }
  }
  if (Sign) {
    *Length(Ch) = *Length(Ch) + 1;
    *Charno(Ch, *Length(Ch)) = '-';
  } else if (Places > 0) {
    *Length(Ch) = *Length(Ch) + 1;
    *Charno(Ch, *Length(Ch)) = ' ';
  }
  if (Places <= 0)
    Places = -Places;
  else
    Places++;
  while (Places > *Length(Ch)) {
    *Length(Ch) = *Length(Ch) + 1;
    *Charno(Ch, *Length(Ch)) = ' ';
    Places--;
  }
  for (I = *Length(Ch); I >= 1; I--) Printsymbol(*Charno(Ch, I));
}
void Print(double X, int Places) {
  int Exponent;
  int Digit;
  int Point;
  int Printexpo;
  if (!X) {
    Printsymbol('0');
    Printsymbol('.');
    Printsymbol('0');
    while (Places > 3) {
      Printsymbol('0');
      Places--;
    }
    return;
  }
  if (X < 0) {
    Printsymbol('-');
    X = -X;
    Places--;
  }
  if (Places < 3) Places = 3;
  Exponent = 0;
  Printexpo = 0;
  while (X < 1) {
    X = X * 10;
    Exponent--;
  }
  while (X >= 10) {
    X = X / 10;
    Exponent++;
  }
  Point = Places - 2;
  if (Exponent >= Places || Exponent < -Point) {
    Printexpo = Exponent;
    Exponent = 0;
    Places -= 2;
  }
  if (Exponent < 0) {
    Printsymbol('0');
    Printsymbol('.');
    Places -= 2;
    while (Exponent < -1) {
      Printsymbol('0');
      Exponent++;
      Places--;
    }
    Point = -1;
  } else
    Point = Exponent;
  while (Places > 0) {
    Digit = Intpt(X);
    if (Digit > 9) Digit = 9;
    Printsymbol(Digit + '0');
    X = (X - Digit) * 10;
    if (!Point) {
      Printsymbol('.');
      Places--;
    }
    Point--;
    Places--;
  }
  if (Printexpo) {
    Printsymbol('@');
    Write(Printexpo, 1);
  }
}
int Intpt(double X) {
  int I;
  I = Int(X);
  if (I) {
    X -= I;
    if (I > 0) {
      if (X < 0) I--;
    } else if (X > 0)
      I++;
  }
  return (I);
}
