1
Name:
Anonymous
2009-08-16 23:08
Let us discuss the various implementations of theF I B O N A C C I B U T T S O R T .
67
Name:
Anonymous
2009-08-25 3:48
Guize I have written an implementation in C with UNICODE support.
[m] [b] [i] # i n c l u d e < s t d i o . h >
# i n c l u d e < w c h a r . h >
# i n c l u d e < s t r i n g . h >
# i n c l u d e < l o c a l e . h >
# i n c l u d e < e r r n o . h >
F I L E * f f f f f f f i b o n a c c i _ b u t t _ s o r t ( F I L E * s , s i z e _ t n )
{
s i z e _ t i = 0 ;
w i n t _ t t a g s [ ] = { L ' u ' , L ' o ' } ;
w i n t _ t c = W E O F ;
f o r ( i = 0 ; i < n ; + + i )
{
i f ( ( c = f g e t w c ( s ) ) = = L ' [u][/u]n ' )
{
f p u t w c ( L ' [u][/u]n ' , s t d o u t ) ;
r e t u r n s ;
}
e l s e i f ( i s w s p a c e ( c ) )
{
f p u t w c ( c , s t d o u t ) ;
}
e l s e i f ( c ! = W E O F )
{
w p r i n t f ( L " [ % l c ] % l c [ / % l c ] " , t a g s [ ( i % 2 ) ] , c , t a g s [ ( i % 2 ) ] ) ;
}
e l s e
{
r e t u r n N U L L ;
}
}
r e t u r n s ;
}
i n t m a i n ( )
{
i f ( ! s e t l o c a l e ( L C _ C T Y P E , N U L L ) )
{
f w p r i n t f ( s t d e r r , L " W a r n i n g : L o c a l e n o t s p e c i f i e d . [o] [/o]n " ) ;
}
c o n s t s i z e _ t B U F F A _ S I Z E = 4 0 9 6 ;
w i n t _ t c = W E O F ;
f p u t w s ( L " [ b ] [ i ] " , s t d o u t ) ;
w h i l e ( ( c = f g e t w c ( s t d i n ) ) ! = W E O F )
{
i f ( u n g e t w c ( c , s t d i n ) = = W E O F )
{
f p u t w s ( L " F a i l e d t o u n g e t w c <span class="o">. [u][/u]n " , s t d e r r ) ;
b r e a k ;
}
i f ( ! f f f f f f f i b o n a c c i _ b u t t _ s o r t ( s t d i n , B U F F A _ S I Z E ) )
{
b r e a k ;
}
}
f p u t w s ( L " [ / i ] [ / b <span class="o">] [u][/u]n " , s t d o u t ) ;
i f ( f e r r o r ( s t d i n ) )
{
p e r r o r ( s t r e r r o r ( e r r n o ) ) ;
f p u t w s ( L " I n p u t e r r o r o c c u r e d . O u t p u t p r o b a b l y c o r r u p t e d . "
" S h i t f u c k i n g s u c k s . [u] [/u]n " , s t d e r r ) ;
}
r e t u r n 0 ;
}
[/i] [/b] [/m]
[code]#include <stdio.h>
#include <wchar.h>
#include <string.h>
#include <locale.h>
#include <errno.h>
FILE* fffffffibonacci_butt_sort(FILE *s, size_t n)
{
size_t i = 0;
wint_t tags[] = {L'u', L'o'};
wint_t c = WEOF;
for(i = 0; i < n; ++i)
{
if((c = fgetwc(s)) == L'\n')
{
fputwc(L'\n', stdout);
return s;
}
else if(iswspace(c))
{
fputwc(c, stdout);
}
else if(c != WEOF)
{
wprintf(L"[%lc]%lc[/%lc]", tags[(i%2)], c, tags[(i%2)]);
}
else
{
return NULL;
}
}
return s;
}
int main()
{
if(!setlocale(LC_CTYPE, NULL))
{
fwprintf(stderr, L"Warning: Locale not specified.\n");
}
const size_t BUFFA_SIZE = 4096;
wint_t c = WEOF;
fputws(L"[b][i]", stdout);
while((c = fgetwc(stdin)) != WEOF)
{
if(ungetwc(c, stdin) == WEOF)
{
fputws(L"Failed to ungetwc.\n", stderr);
break;
}
if(!fffffffibonacci_butt_sort(stdin, BUFFA_SIZE))
{
break;
}
}
fputws(L"[/i][/b]\n", stdout);
if(ferror(stdin))
{
perror(strerror(errno));
fputws(L"Input error occured. Output probably corrupted."
"Shit fucking sucks.\n", stderr);
}
return 0;
}[/code]
Here's an example for UTF-8 input:
[spoiler]Invalid or incomplete multibyte or wide character: Invalid or incomplete multibyte or wide character[/spoiler]