马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 andalousie 于 2014-3-5 12:43 编辑
ios.h#ifndef IOS_H
#define IOS_H
#include <stdio.h>
namespace std{
class ostream{
public:
void flush() { if(stdout) fflush(stdout); }
ostream& operator << (ostream& (__cdecl *m)(ostream&));
ostream& __cdecl operator << (int);
ostream& __cdecl operator << (char);
ostream& __cdecl operator << (double);
ostream& __cdecl operator << (long);
ostream& __cdecl operator << (unsigned);
ostream& __cdecl operator << (unsigned long);
ostream& __cdecl operator << (char *);
ostream& __cdecl operator << (const char *);
ostream& __cdecl operator << (const void *);
};
ostream& ostream::operator <<(ostream& (__cdecl *m)(ostream&))
{
return ((*m)(*this));
}
ostream& __cdecl ostream::operator << (int val)
{
printf("%d", val);
return *this;
}
ostream& __cdecl ostream::operator << (char ch)
{
printf("%c", ch);
return *this;
}
ostream& __cdecl ostream::operator << (double val)
{
printf("%g", val);
return *this;
}
ostream& __cdecl ostream::operator << (long val)
{
printf("%I64d", val);
return *this;
}
ostream& __cdecl ostream::operator << (unsigned val)
{
printf("%u", val);
return *this;
}
ostream& __cdecl ostream::operator << (unsigned long val)
{
printf("%I64u", val);
return *this;
}
ostream& __cdecl ostream::operator << (char * str)
{
printf("%s", str);
return *this;
}
ostream& __cdecl ostream::operator << (const char * str)
{
printf("%s", str);
return *this;
}
ostream& __cdecl ostream::operator << (const void * p)
{
printf("0x%x", p);
return *this;
}
ostream cout;
ostream cerr;
ostream& __cdecl flush(ostream& os) { os.flush(); return os; }
ostream& __cdecl endl(ostream& os) { os << '\n'; os.flush(); return os; }
class istream{
ostream *os;
public:
istream& __cdecl operator >> (int&);
istream& __cdecl operator >> (char&);
istream& __cdecl operator >> (float&);
istream& __cdecl operator >> (double&);
istream& __cdecl operator >> (char *);
int __cdecl get();
istream& __cdecl get (char& c);
istream& __cdecl getline (char* s, int n);
};
istream& __cdecl istream::operator >> (int& val)
{
os->flush();
scanf("%d", &val);
return *this;
}
istream& __cdecl istream::operator >> (char& ch)
{
os->flush();
scanf("%c", &ch);
return *this;
}
istream& __cdecl istream::operator >> (float& val)
{
os->flush();
scanf("%f", &val);
return *this;
}
istream& __cdecl istream::operator >> (double& val)
{
os->flush();
scanf("%lf", &val);
return *this;
}
istream& __cdecl istream::operator >> (char * str)
{
os->flush();
gets(str);
return *this;
}
int __cdecl istream::get()
{
return getchar();
}
istream& __cdecl istream::get (char& c)
{
os->flush();
c = getchar();
return *this;
}
istream& __cdecl istream::getline (char* s, int n)
{
int c, i;
for(i = 0; i < n-1 && (c=getchar())!=EOF && c !='\n'; ++i)
s[i] = c;
if(c == '\n')
s[i++]=c;
s[i] = '\0';
return *this;
}
istream cin;
}
#endif
做了改动,增加了ostream引用指针的inserter,于是可以大胆用std::cout << std::flush了。Last modified at 12:40, 03-05
|