2009年12月6日 星期日

simple iostream

 
改自:http://okmij.org/ftp/packages/iostream.h
#include <stdio.h>

static class Iostream {} cin, cout;
static class Endl{} endl;

Iostream& operator << (Iostream& o, Endl& data) {
    puts (""); return o;
}

Iostream& operator << (Iostream& o, void* data) {
    printf ("%x", data);
    return o;
}

Iostream& operator << (Iostream& o, bool data) {
    printf ("%s", data? "true": "false");
    return o;
}

Iostream& operator >> (Iostream& i, char* data) {
    scanf ("%s", data);
    return i;
}

#define DEF_OS(T,fmt) \
    Iostream& operator << (Iostream& o, T data) {\
        printf ("%"#fmt,data);\
        return o;\
    }\
    Iostream& operator >> (Iostream& i, T& data) {\
        scanf ("%"#fmt,&data);\
        return i;\
    }

DEF_OS (const char*  ,  s)
DEF_OS (char*        ,  s)
DEF_OS (char         ,  c)
DEF_OS (int          ,  d)
DEF_OS (float        ,  g)
DEF_OS (double       , lg)
DEF_OS (long         , ld)
DEF_OS (long long    , ll)
DEF_OS (unsigned     ,  u)
DEF_OS (unsigned long, lu)


int main ()
{
    char   s[64];
    double d;

    cin >> s >> d;
    cout << "input: " <<s <<' ' << d <<endl <<&d;
}

沒有留言:

張貼留言