\onecolumn

%\noindent\textbf{\large printf() Guide}
\section{printf() Guide}


\lstset{language=C++}
\begin{lstlisting}
#include <iomanip>
#include <cstdio>

/*            printf/sprintf is your friend!
    Most of the time, problems say "two decimal places", and you want "%.2f"
printf codes:
  %d      signed integer            %s    C-style string
  %u    unsigned integer            %c    character
  %e/E  scientific notation         %%    percent sign
  %f    floating-point              %lf   long float (double)
  %g/G  shorter of %e or %f         %#f   force decimal place to be printed
  %x/X  unsigned hex                %#x   print with 0x prefix

"%12d"    means field of width 12, padded with spaces
"%012d"   means field of width 12, padded with zeroes

"%12.6f"  means at least 12 spaces wide, w/ 6 decimal places
"%-12.6f" means same as above, but left-aligned instead of right-aligned

"%5.10s"   means string printed with min 5 & max 10 chars
*/
   printf("|%s|\n", "hello");       // |hello|
   printf("|%.4s|\n", "hello");     // |hell|
   printf("|%10.4s|\n", "hello");   // |      hell|

   printf("|%lf|\n", big);        |654321.123456|
   printf("|%-20lf|\n", big);     |654321.123456       |
   printf("|%20lf|\n", big);      |       654321.123456|
   printf("|%020lf|\n", big);     |0000000654321.123456|

   printf("|%.10lf|\n", big);     |654321.1234560000|
   printf("|%.4lf|\n", big);      |654321.1235|

// printf will perform rounding! That's usually good, but it MAY not be what you want!
   printf("%.2f\n", 0.145);  // 0.14
   printf("%.2f\n", 0.1451); // 0.15

   trunc(2, .146); // 0.14

// Also, you don't have to embed the precision in the printf string:
   printf("%.*lf\n", 2, val); /* == */ printf("%.2lf\n", val);


// truncates strings to the specified number of places after the decimal
string trunc(int places, double val) {
  char buf[256];
  sprintf(buf, "%lf", val);
  string rv(buf);
  if(rv.find('.') != string::npos)
      rv.erase(rv.find('.') + 1 + places);
  return rv;
}

// Things you can do with cout that are not as easy to do with printf:
//        BOOL FORMATTING
  bool t = true;
  cout << t << endl; // 1
  cout << boolalpha;
  cout << t << endl; // true

//         NON-FIXED PRINTING
  cout << 0.1 << ", " << 1.0 << endl; // gives 0.1, 1
  printf("%f, %f\n", 0.1f, 1.0f); // gives 0.100000, 1.000000

//         INTERNAL ALIGNMENT
  cout << setw(10) << setfill('*') << internal << -1.0 << "." << endl;
          // gives -********1

    const float tenth = 0.1;
    const float one   = 1.0;
    const float big   = 1234567890.0;
    const float neg1  = -1.0;

    printf("%f\n", tenth);
    printf("%f\n", one);

    cout << "A. "                    << tenth << ", " << one << ", " << big << endl;
    cout << "B. " << fixed           << tenth << ", " << one << ", " << big << endl;
    cout << "C. " << scientific      << tenth << ", " << one << ", " << big << endl;
    cout << "D. " << fixed << setprecision(3) << tenth << ", " << one << ", " << big << endl;
    cout << "E. " << setprecision(20) << tenth << endl;
    cout << "F. " << setw(8) << setfill('*') << 34 << 45 << endl;
    cout << "G. " << setw(8) << 34 << setw(8) << 45 << endl;
/*
A. 0.1, 1, 1.23457e+09
B. 0.100000, 1.000000, 1234567936.000000
C. 1.000000e-01, 1.000000e+00, 1.234568e+09
D. 0.100, 1.000, 1234567936.000
E. 0.10000000149011611938
F. ******3445
G. ******34******45
*/
   cout.unsetf(ios::floatfield);
\end{lstlisting}

\section{Java Regex Quickref}
\begin{verbatim}
Char classes:
        [abc] [^abc] [a-zA-Z] [a-z&&[^bc]]==[ad-z]
Predefined char classes:
        \d      digit           \D      not digit
        \s      whitespace      \S      blackspace
        \w      word char: [a-zA-Z_0-9]
POSIX classes:
        \p{Lower}, \p{Upper}, \p{Alnum} etc
Boundaries:
        ^       start of line
        $       end of line
        \b      word boundary   \B non-word boundary
        \A      start of input
        \G      end of prev. match
        \Z      end of input (ignoring trailing newline)

(?i:__whatever__) -- matches __whatever__ case-insensitive
Flag for dotall mode is s
\end{verbatim}
\normalsize
\twocolumn
%%% Local Variables:
%%% mode: latex
%%% TeX-master: "ref-doc"
%%% End:
