iostream.cpp

来自「eC++编译器源码」· C++ 代码 · 共 207 行

CPP
207
字号
#pragma iostream
#pragma qualified

#include <InOut.h>
#include <RealIO.h>
#include <RealInOut.h>
#include <stdio.h>
#include <iomanip.h>
#include <SYSTEM.h>

typedef char Fields[4];

const unsigned int WIDTH=0;
const unsigned int PRECISION=1;
const unsigned int FLAGS=2;
const unsigned int FILL=3;
const unsigned int EOF=127;

iostreams & iostreams::operator<<(iomanip.ManipBase x)
{Fields *pf; unsigned int i;
 pf = SYSTEM.ADR(p);
 i = ORD((*pf)[FLAGS])&~ORD((iomanip.hex|iomanip.oct|iomanip.binary|iomanip.dec));
 (*pf)[FLAGS] = CHR(ORD(x)|i);
 return *this;
};

iostreams & iostreams::operator<<(iomanip.iosret x)
{ Fields *pf;   int i;
  pf = SYSTEM.ADR(p);
  switch (TRUNC(long(x)>>16L)) {
  case 1:  //width
        i = TRUNC(long(x)&0xFFFFL);
        (*pf)[WIDTH] = CHR(i);
        break;
  case 2:  //precision
        i = TRUNC(long(x)&0xFFFFL);
        (*pf)[PRECISION] = CHR(i);
        break;
  case 3:  //flags
        i = TRUNC(long(x)&0xFFFFL);
        (*pf)[FLAGS] = CHR(i);
        break;
  case 4:  //reset
        (*pf)[WIDTH]='\0';  (*pf)[PRECISION]='\3';
        (*pf)[FLAGS]='\0'
        break;
  case 5:  //fill
        i = TRUNC(long(x)&0xFFFFL);
        (*pf)[FILL] = CHR(i);
        break;
  
  };
  return *this;
};

iostreams & iostreams::operator<<(int x)
{ typedef unsigned int C;
  Fields *pf;
  pf = SYSTEM.ADR(p);
  if ((ORD((*pf)[FLAGS])&ORD(iomanip.hex))!=0) {
    InOut.WriteString("0x");
    InOut.WriteHex(C(x), ORD((*pf)[WIDTH]));
  } else if ((ORD((*pf)[FLAGS])&ORD(iomanip.oct))!=0) {
    InOut.Write('0');
    InOut.WriteOct(C(x), ORD((*pf)[WIDTH]));   
  } else InOut.WriteInt(x, ORD((*pf)[WIDTH]));
  return *this;
};

iostreams & iostreams::operator<<(double x)
{
  Fields *pf;
  pf = SYSTEM.ADR(p);
  if ((ORD((*pf)[FLAGS])&ORD(iomanip.hex))!=0) {
    InOut.WriteString("0x");
    InOut.WriteLongHex(long(x), ORD((*pf)[WIDTH]));
  } else if ((ORD((*pf)[FLAGS])&ORD(iomanip.oct))!=0) {
    InOut.Write('0');
    InOut.WriteLongOct(long(x), ORD((*pf)[WIDTH]));   
  } else {
    if ((ORD((*pf)[FLAGS])&ORD(iomanip.ios::scientific))!=0) RealInOut.WriteReal(x,ORD((*pf)[WIDTH])); 
    else {
      if ((*pf)[WIDTH]==CHR(0)) RealIO.Write(x, 7,ORD((*pf)[PRECISION]));
      else RealIO.Write(x, ORD((*pf)[WIDTH]),ORD((*pf)[PRECISION]));
    };
  };
  return *this;
};

iostreams & iostreams::operator<<(unsigned int x)
{
  Fields *pf;
  pf = SYSTEM.ADR(p);
  if ((ORD((*pf)[FLAGS])&ORD(iomanip.hex))!=0) {
    InOut.WriteString("0x");
    InOut.WriteHex(x, ORD((*pf)[WIDTH]));
  } else if ((ORD((*pf)[FLAGS])&ORD(iomanip.oct))!=0) {
    InOut.Write('0');
    InOut.WriteOct(x, ORD((*pf)[WIDTH]));   
  } else InOut.WriteCard(x, ORD((*pf)[WIDTH]));
  return *this;
};

iostreams & iostreams::operator<<(long x)
{
  Fields *pf;
  pf = SYSTEM.ADR(p);
  if ((ORD((*pf)[FLAGS])&ORD(iomanip.hex))!=0) {
    InOut.WriteString("0x");
    InOut.WriteLongHex(x, ORD((*pf)[WIDTH]));
  } else if ((ORD((*pf)[FLAGS])&ORD(iomanip.oct))!=0) {
    InOut.Write('0');
    InOut.WriteLongOct(x, ORD((*pf)[WIDTH]));   
  } else InOut.WriteLongInt(x, ORD((*pf)[WIDTH]));
  return *this;
};

iostreams & iostreams::operator<<(char x)
{
  InOut.Write(x);
  return *this;
};

iostreams & iostreams::operator<<(char x[])
{
  InOut.WriteString(x);
  return *this;
};

iostreams & iostreams::operator<<(boolean x)
{
  if (x) InOut.Write('T'); else InOut.Write('F');
  return *this;
};

boolean iostreams::eof()
{
  Fields *pf;
  pf = SYSTEM.ADR(p);
  return (ORD((*pf)[FLAGS])&EOF)!=0;
};

iostreams & iostreams::operator>>(int &x)
{
  Fields *pf;
  pf = SYSTEM.ADR(p);
  if (stdio.scanf("%i",x)!=1) (*pf)[FLAGS] = CHR ( ORD((*pf)[FLAGS])|EOF );
  return *this;
};

iostreams & iostreams::operator>>(unsigned int &x)
{
  Fields *pf;
  pf = SYSTEM.ADR(p);
  if (stdio.scanf("%u",x)!=1) (*pf)[FLAGS] = CHR ( ORD((*pf)[FLAGS])|EOF );
  return *this;
};

iostreams & iostreams::operator>>(long &x)
{
  Fields *pf;
  pf = SYSTEM.ADR(p);
  if (stdio.scanf("%ld",x)!=1) (*pf)[FLAGS] = CHR ( ORD((*pf)[FLAGS])|EOF );
  return *this;
};

iostreams & iostreams::operator>>(double &x)
{
  Fields *pf;
  pf = SYSTEM.ADR(p);
  if (stdio.scanf("%f",x)!=1) (*pf)[FLAGS] = CHR ( ORD((*pf)[FLAGS])|EOF );
  return *this;
};

iostreams & iostreams::operator>>(char &x)
{
  Fields *pf;
  pf = SYSTEM.ADR(p);
  if (stdio.scanf("%c",x)!=1) (*pf)[FLAGS] = CHR ( ORD((*pf)[FLAGS])|EOF );
  return *this;
};

iostreams & iostreams::operator>>(char &x[])
{
  Fields *pf;
  pf = SYSTEM.ADR(p);
  if (stdio.scanf("%s",x)!=1) (*pf)[FLAGS] = CHR ( ORD((*pf)[FLAGS])|EOF );
  return *this;
};

iostreams & iostreams::operator>>(boolean &x)
{ char b;
  Fields *pf;
  pf = SYSTEM.ADR(p);
  if (stdio.scanf("%c",b)==1) {
    x = CAP(b)=='T';
  } else (*pf)[FLAGS] = CHR ( ORD((*pf)[FLAGS])|EOF );
  return *this;
};

void iostream()
{
        cin.p = SYSTEM.ADDRESS(0L);
        cout.p = SYSTEM.ADDRESS(0L);
        cout<<(iomanip.setprecision(3));
};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?