c++输入输出的实现

#include"CIn.h" 的内容------是输入的实现

#include<stdio.h>

#define CHAR char
#define TCHAR char*

class CIn
{
public:
	CIn();
	~CIn();
	CIn &operator>>(int &i);
	CIn &operator>>(short &s);
	CIn &operator>>(double &d);
	CIn &operator>>(float &f);
	CIn &operator>>(CHAR &c);
	CIn &operator>>(TCHAR t);
};

CIn::CIn()
{

}

CIn::~CIn()
{

}

CIn &CIn::operator>>(int &i)
{
	scanf_s("%d",&i);
	return *this;
}

CIn &CIn::operator>>(short &s)
{ 
	scanf_s("%d", &s);
	return *this;
}

CIn &CIn::operator>>(double &d)
{
	scanf_s("%lf", &d);
	return *this;
}

CIn &CIn::operator>>(float &f)
{
	scanf_s("%lf", &f);
	return *this;
}

CIn &CIn::operator>>(CHAR &c)
{
	/*清理缓冲区*/
	fflush(stdin);
	c = getchar();
	/*scanf_s("%c", c,sizeof(c));*/
	return *this;
}

CIn &CIn::operator>>(TCHAR t)
{
	scanf_s("%s", t,sizeof(t));
	return *this;
}

#include"COut.h" 的内容----是输出的实现

#include<stdio.h>

#define TCHAR char*
#define CHAR  char
#define endl  "\n"

class COut
{
public:
	COut();
	~COut();
	COut &operator<<(const int   &i);
	COut &operator<<(const short &s);
	COut &operator<<(const TCHAR t);
	COut &operator<<(const CHAR  &c);
	COut &operator<<(const double &d);
	COut &operator<<(const float &f);
private:

};

COut::COut()
{
}

COut::~COut()
{
}

COut &COut::operator<<(const int &i)
{
	printf("%d",i);
	return *this;
}

COut &COut::operator<<(const short &s)
{
	printf("%d",s);
	return *this;
}

COut &COut::operator<<(const TCHAR t)
{
	printf(t);
	return *this;
}

COut &COut::operator<<(const CHAR &c)
{
	printf("%c",c);
	return *this;
}

COut &COut::operator<<(const double &d)
{
	printf("%lf", d);
	return *this;
}

COut &COut::operator<<(const float &f)
{
	printf("%lf", f);
	return *this;
}

主函数

#include"COut.h"
#include<stdlib.h>
#include"CIn.h"

COut out;
CIn in;

int main()
{
	/************************************/
	int nID = 1001;
	CHAR text[20];
	out << 25 <<endl;
	out <<"请输入整数:" << endl;
	in >> nID;
	out << nID << endl;
	out << "请输入字符串:";
	in >> text;
	out << text << endl;
	system("pause");
	/************************************/

	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38611124/article/details/81071337
今日推荐