SoDaFormat-1.0.0-main:f160581  1.0.0
Format_Test.cxx
Go to the documentation of this file.
1 #include "Format.hxx"
2 #include <iostream>
3 
4 int main() {
5  int i = 5;
6  double ef = 32157.5;
7  char c = 'T';
8  std::string fred("Fred");
9 
11  std::cout << SoDa::Format("print 5 like this: %0\n"
12  "let's print 5 again %0\n"
13  "%1 looks much better in engineering notation %2\n"
14  "Sometimes I just want to print a %% sign like this %0%%\n"
15  "I think that shirt he's wearing fits %3 to a %4\n")
16  .addI(i)
17  .addF(ef, 'f')
18  .addF(ef, 'e')
19  .addS(fred)
20  .addC(c);
21 
23 
25  std::cout << SoDa::Format("i = %0\n").addI(i);
26  std::cout << SoDa::Format("ef = \n\t(f format) %0 or \n\t(g format) %1 or \n\t(s format) %2 or \n\t(e format) %3\n")
27  .addF(ef, 'f').addF(ef, 'g').addF(ef, 's').addF(ef, 'e');
29 
30  std::cout << SoDa::Format("int [%0] double f [%1] double g [%2] "
31  "double e [%3] string [%4] "
32  "char [%5] char6[%6] char7[%7] "
33  "char8[%8] char9[%9] char10[%10] char11[%11]\n")
34  .addI(i).addF(ef, 'f').addF(ef, 'g').addF(ef, 'e').addS(fred).addC(c)
35  .addC('6')
36  .addC('7')
37  .addC('8')
38  .addC('9')
39  .addC('A')
40  .addC('B');
41 
43  SoDa::Format sft("Avogadro's number: %0\n");
45 
46 
48  // Print Avogadro's number the way we all remembered from high school.
49  double av = 6.02214076e23;
50  std::cout << sft.addF(av, 's');
51 
52  // But all that 10^23 jazz is the result of the evil CGS system.
53  // Let's do this the way a respectable MKS user would have wanted it.
54  // first, let's reuse our format:
56  sft.reset();
58  // and now print
59  std::cout << "Here's how right thinking people write "
60  << sft.addF(av, 'e');
62 
63  // we'll reuse this format in the test loop
64  SoDa::Format format("%0 p = %1 : [%2]\n");
65 
66  // let's test and demonstrate each of the four FP formats
67  std::list<char> fs = {'f', 'g', 's', 'e'};
68 
69  for(i = 0; i < 8; i++) {
70  std::cout << "---------\n" << ef << "\n--------------\n";
71  for(char fmt : fs) {
73  for(int p = 1; p < 4; p++) {
74  std::cout << format.reset().addC(fmt).addI(p).addF(ef, fmt, p+3, p);
75  }
77  }
78  ef = ef * 0.1;
79  std:: cout << "\n\n";
80  }
81 
82  SoDa::Format oops("%0\n");
83 
84  std::cout << oops.addI(3).addI(4);
85 
86 }
Format & reset()
reset the format string to its original value, with all the placeholders restored.
Definition: Format.cxx:207
Format & addI(int v, unsigned int width=0)
insert a signed integer into the format string
Definition: Format.cxx:50
Format & addF(double v, char fmt='f', unsigned int width=0, unsigned int frac_precision=3)
insert a float or double into the format string
Definition: Format.cxx:70
Format & addC(char v)
insert a character into the format string
Definition: Format.cxx:142
Format & addS(const std::string &v, unsigned int width=0)
insert a string into the format string
Definition: Format.cxx:132
int main()
Definition: Format_Test.cxx:4
A format object that may be "filled in" with integer, float, double, string, or character values.
Definition: Format.hxx:269