Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define MAXLOG10 10
- #define BUFSIZE (128 * 1024)
- FILE *fin, *fout;
- int p10[MAXLOG10 + 1] = { 0, 1, 10, 100, 1000, 10000, 100000, 1000000,
- 10000000, 100000000, 1000000000 }; // santinela pe prima pozitie
- int rpos; char rbuf[BUFSIZE];
- int wpos; char wbuf[BUFSIZE];
- // initializare citire
- static inline void initRead() {
- rpos = BUFSIZE - 1;
- }
- // citire caracter
- static inline char readChar() {
- if ( !(rpos = (rpos + 1) & (BUFSIZE - 1)) )
- fread( rbuf, 1, BUFSIZE, fin );
- return rbuf[rpos];
- }
- // citire intreg cu semn
- int readInt() { // citeste un intreg
- int ch, res = 0, semn = 1;
- while ( isspace( ch = readChar() ) );
- if ( ch == '-' ) {
- semn = -1;
- ch = readChar();
- }
- do
- res = 10 * res + ch - '0';
- while ( isdigit( ch = readChar() ) );
- return semn * res;
- }
- // initializare scriere
- static inline void initWrite() {
- wpos = 0;
- }
- // scriere caracter
- static inline void writeChar( char ch ) {
- wbuf[wpos] = ch;
- if ( !(wpos = (wpos + 1) & (BUFSIZE - 1)) )
- fwrite( wbuf, 1, BUFSIZE, fout );
- }
- // scriere intreg cu semn
- void writeInt( int x ) {
- int i, cf;
- if ( x < 0 ) {
- writeChar( '-' );
- x = -x;
- }
- i = MAXLOG10;
- while ( p10[i] > x )
- i--;
- if ( i == 0 )
- writeChar( '0' );
- else
- do {
- cf = '0';
- while ( p10[i] <= x ) {
- x -= p10[i];
- cf++;
- }
- writeChar( cf );
- } while ( --i > 0 );
- writeChar( ' ' ); // separatorul, poate sa difere ('\n'?)
- }
- // scrie caracterele ramase in buffer
- static inline void flushBuf() {
- fwrite( wbuf, 1, wpos, fout );
- }
- Utilizare:
- fin = fopen( "input.in", "r" );
- initRead();
- n = readInt();
- // ... alte citiri
- fclose( fin );
- fout = fopen( "input.out", "w" );
- initWrite();
- writeInt( n );
- // ... alte scrieri
- flushBuf();
- fclose( fout );
Advertisement
Add Comment
Please, Sign In to add comment