PhoneNumber类

(用重载的流读取运算符读取电话号码)图18.5 重载了流读取和流插入运算符,用于输人和输出PhoneNumber 类的对象。重新编写流读取运算符, 使它能够执行下列输入检查。 注意,需要重新实现operator>>函数。
无检测

#include<iostream>
using namespace std;
class PhoneNumber
{
	friend ostream& operator<<(ostream& output, PhoneNumber& num);
	friend istream& operator>>(istream& input, PhoneNumber& num);
public:
	PhoneNumber();
private:
	char areaCode[4];//3位区号
	char exchange[4];//3位局号
	char line[5];	//4位线路号

};
PhoneNumber::PhoneNumber()//构造初始化
{
	memset(areaCode, 0, sizeof(areaCode));
	memset(exchange, 0, sizeof(exchange));
	memset(line, 0, sizeof(line));
}
ostream& operator<<(ostream& cout,PhoneNumber& num)
{
	cout << "(" << num.areaCode << ")" << num.exchange << "-" << num.line;
	return cout;
}//

istream& operator>>(istream& cin, PhoneNumber& num)
{
	cin.ignore();
	if (cin.fail())
	{
		cin.clear();
	}
	cin.read(num.areaCode, sizeof(num.areaCode) - 1);
	cin.ignore();
	if (cin.fail())
	{
		cin.clear();
	}
	cin.read(num.exchange, sizeof(num.exchange) - 1);
	cin.ignore();
	if (cin.fail())
	{
		cin.clear();
	}
	cin.read(num.line, sizeof(num.line) - 1);
	return cin;
}

int main()
{
	PhoneNumber phone;
	cout << "Please input your phonenumber:" << endl;
	cin >> phone;
	cout << "The phonenumber:" << phone << endl;
}

有检测

#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
class PhoneNumber {
	friend ostream& operator<<(ostream& ouput, PhoneNumber& num);
	friend istream& operator>>(istream& input, PhoneNumber& num);
private:
	string areaCode; //3位区号
	string exchange; //3位局号
	string line;     //4位线路号
};


ostream& operator<<(ostream& ouput, PhoneNumber& num) {       //输出流重载
	ouput << '(' << num.areaCode << ')' << ' ' << num.exchange << '-' << num.line << endl;
	return ouput;
}
istream& operator>>(istream& input, PhoneNumber& num) {       //输入流重载
	while (1) {
		if (input.peek() != '(')
		{												  //检测起始符(
			input.clear(ios::failbit);
			input.clear();
			cout << "格式错误,请加上(后,重新输入:" << endl;
			cin.ignore(20, '\n');                          //重置输入流缓存区
			continue;                                       //错误时结束执行while
		}

		input.ignore();                                     //忽略(

		if (input.peek() == '0' || input.peek() == '1')
		{													//检测区号首位不是0或1
			input.clear(ios::failbit);
			input.clear();
			cout << "区号首位不能是0和1,请重新输入:" << endl;
			cin.ignore(20, '\n');                           //重置输入流缓存区
			continue;                                       //错误时结束执行while
		}
		else
			input >> setw(3) >> num.areaCode;

		if (num.areaCode[1] != '0' && num.areaCode[1] != '1')
		{													//检测区号第二位是0或1
			input.clear(ios::failbit);
			input.clear();
			cout << "区号第二位应含有0或1,请重新输入:" << endl;
			cin.ignore(20, '\n');					        //重置输入流缓存区
			continue;                                       //错误时结束执行while
		}


		if (input.peek() != ')')
		{												       //检测含有)
			input.clear(ios::failbit);
			input.clear();
			cout << "格式错误请加上)后,重新输入:" << endl;
			cin.ignore(20, '\n');                          //重置输入流缓存区
			continue;                                       //错误时结束执行while
		}
		input.ignore();                                     //忽略字符 )

		if (input.peek() != ' ')
		{									                   //检测含有'空格'
			input.clear(ios::failbit);
			input.clear();
			cout << "格式错误,请加上'空格'后请重新输入:" << endl;
			cin.ignore(20, '\n');                          //重置输入流缓存区
			continue;                                       //错误时结束执行while
		}

		input.ignore();                                     //忽略'空格'

		if (input.peek() == '0' || input.peek() == '1')
		{													//判断局号首位是否满足条件
			input.clear(ios::failbit);
			input.clear();
			cout << "局号首位不能是0和1,请重新输入:" << endl;
			cin.ignore(20, '\n');                          //重置输入流缓存区
			continue;                                       //错误时结束执行while
		}
		else
			input >> setw(3) >> num.exchange;

		if (input.peek() != '-')
		{												    //判断输入格式是否正确
			input.clear(ios::failbit);
			input.clear();
			cout << "格式错误,请加上-后,重新输入" << endl;
			cin.ignore(20, '\n');                          //重置输入流缓存区
			continue;                                       //错误时结束执行while
		}
		input.ignore();                                     //忽略字符'-'

		input >> setw(4) >> num.line;							//线路号输入完成	
		break;                                              //满足所有条件,完成输入
	}
	return input;
}

int main()
{
	PhoneNumber phone;
	cout << "Please input your phonenumber:" << endl << "etc:(210) 520-1314" << endl;
	cin >> phone;
	cout << "The phonenumber:" << phone << endl;
}

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试
应支付0元
点击重新获取
扫码支付

支付成功即可阅读