-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
97 lines (85 loc) · 2.44 KB
/
common.h
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
#pragma once
#include <iostream>
//----------------------------------------------------------------
inline
bool operator>>=( std::istream& input, const char c )
{
if( input.peek() != c )
return false;
input.ignore();
return true;
}
bool operator>>=( std::istream& input, const char* s );
//----------------------------------------------------------------
std::istream& operator>>( std::istream& input, const char c );
std::istream& operator>>( std::istream& input, const char* s );
//================================================================
static class Any_line {} any_line;
inline std::istream& operator>>( std::istream& input, Any_line s )
{
//if( !input )
// return input;
input.ignore( INT_MAX, '\n' );
return input;
}
inline std::ostream& operator<<( std::ostream& output, Any_line s )
{
output << '\n';
return output;
}
//================================================================
static class Any_word {} any_word;
inline std::istream& operator>>( std::istream& input, Any_word s )
{
if( !input )
return input;
char c;
while( input.get( c))
{
if( !isgraph( c ) )
break;
}
input.unget();
return input;
}
inline std::ostream& operator<<( std::ostream& output, Any_word s )
{
output << 'X';
return output;
}
//================================================================
class Digit_char
{
public:
char value;
Digit_char( ) : value( '\0') {};
Digit_char( const char c ) : value( c-'0') {};
bool mayread( std::istream& input )
{
int c = input.peek() - '0';
if( c < 0 || c > 9 )
return false;
value = c;
input.ignore();
return true;
};
void read( std::istream& input )
{
//if( !input )
// return;
int c = input.peek() - '0';
if( 0 <= c && c <= 9 )
{
value = c;
input.ignore();
return;
}
std::cerr << "Wrong read! Char: \"" << char( c + '0' ) << "\" not is digit";
input.setstate( std::ios_base::failbit );
};
void print( std::ostream& output ) const { output << (value + '0'); };
};
//----------------------------------------------------------------
inline bool operator>>=( std::istream& input, Digit_char &value) { return value.mayread( input ); }
inline std::istream& operator>> ( std::istream& input, Digit_char &value) { value.read( input ); return input; }
inline std::ostream& operator<< ( std::ostream& output, const Digit_char value) { value.print( output ); return output; }