Разработайте и реализуйте фасет почтового индекса. Сделайте это как минимум для двух стран с непохожими соглашениями относительно написания адресов. Например, NJ 07932 и CB21QA.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
#include <iostream> using std::cout; using std::endl; using std::cin; using std::ostream; using std::istream; using std::ios_base; #include <locale> using std::locale; using std::has_facet; using std::use_facet; #include <string> using std::string; #include <algorithm> using std::find; struct Post_index //тип номера { string str; Post_index(const string& s="00"):str(s){} }; //базовый класс для фасетов номеров class Post_index_io:public locale::facet { public: Post_index_io(int i=0):locale::facet(i){} ~Post_index_io(){}//обеспечивает возможность уничтожения объектов Season_io virtual const string& to_str(Post_index& x)const=0;//строковое представление для х virtual bool from_str(const string& s, Post_index& x)const=0; static locale::id id; }; locale::id Post_index_io::id;//определяем индентифицирующий объект ostream& operator<<(ostream& s,Post_index& x) { const locale loc=s.getloc(); //Если существует фасет Season_io, то вернуть поток с использованным фасетом и его функцией //виртуальной перегруженой to_str() if(has_facet<Post_index_io>(loc)) return s <<use_facet<Post_index_io>(loc).to_str(x); return s <<x.str; } istream& operator>>(istream& s,Post_index& x) { const locale loc=s.getloc();//получить текущую локализацию //если существует фасет Phone_io if(has_facet<Post_index_io>(loc)) { const Post_index_io& f=use_facet<Post_index_io>(loc);//создаем используемый фасет string buf;//задаем буфер s.ignore(333,'\n');//очищаем поток if(!(getline(s,buf)!=0&&f.from_str(buf,x))) { s.setstate(ios_base::failbit); } return s; } //читаем числовое представление string i; s >>i; x=Post_index(i); return s; } class US_post_index_io:public Post_index_io { public: US_post_index_io(){} const string& to_str(Post_index&)const; bool from_str(const string&, Post_index&)const; //отсутствует US_season_id }; const string& US_post_index_io::to_str(Post_index& x)const { if(x.str.size()!=6) { static const string ss="no-such-post-index"; return ss; } return x.str; } bool US_post_index_io::from_str(const string& s,Post_index& x)const { //cout <<"s= "<<s<<endl; if(s.size()!=6) { return false; } x.str=s; return true; } class RU_post_index_io:public Post_index_io { public: RU_post_index_io(){} const string& to_str(Post_index&)const; bool from_str(const string&, Post_index&)const; //отсутствует US_season_id }; const string& RU_post_index_io::to_str(Post_index& x)const { if(x.str.size()!=8||x.str[2]!=' ') { static const string ss="no-such-phone"; return ss; } return x.str; } bool RU_post_index_io::from_str(const string& s,Post_index& x)const { //cout <<"s= "<<s<<endl; if(s.size()!=8||s[2]!=' ') { return false; } x.str=s; return true; } int main() { setlocale(LC_CTYPE,"Russian_Russia.1251"); Post_index x; cin >>x; cout <<x<<endl; /* locale loc(locale(),new US_phone_io()); cout.imbue(loc); cin.imbue(loc); cin >>x; cout <<x<<endl;*/ locale loc1(locale(),new RU_post_index_io()); cout.imbue(loc1); cin.imbue(loc1); cin >>x; cout <<x<<endl; return 0; } |
[youtube]https://www.youtube.com/watch?v=ypAE7ksdwBE[/youtube]