| 1 | // txt2c.cpp : This simple app builds a table |
| 2 | // of discrete probability distribution functions |
| 3 | // and generates a .cpp/.h file that describe them. |
| 4 | // |
| 5 | |
| 6 | #include <string.h> |
| 7 | #include <iostream> |
| 8 | #include <fstream> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | |
| 12 | #include "dpdf.h" |
| 13 | |
| 14 | using namespace std; |
| 15 | |
| 16 | int |
| 17 | main(int argc, char* argv[]) |
| 18 | { |
| 19 | if (argc < 4) |
| 20 | { |
| 21 | cerr << "Usage is " << argv[0] << " maxsides maxrolls scale" << endl; |
| 22 | cerr << "This will build dpdf_table.cpp and dpdf_table.h" << endl; |
| 23 | return -2; |
| 24 | } |
| 25 | |
| 26 | ifstream is(argv[1], ios::in); |
| 27 | |
| 28 | int maxsides, maxrolls, scale, roll, side; |
| 29 | |
| 30 | maxsides = atoi(argv[1]); |
| 31 | maxrolls = atoi(argv[2]); |
| 32 | scale = atoi(argv[3]); |
| 33 | |
| 34 | ofstream os("dpdf_table.cpp"); |
| 35 | if (!os) |
| 36 | { |
| 37 | cerr << "Couldn't open for write " << "dpdf_table.cpp" << endl; |
| 38 | } |
| 39 | ofstream hos("dpdf_table.h"); |
| 40 | if (!hos) |
| 41 | { |
| 42 | cerr << "Couldn't open for write " << "dpdf_table.h" << endl; |
| 43 | } |
| 44 | |
| 45 | // Build the header file. |
| 46 | hos << "// dpdf_table.h" << endl |
| 47 | << "// Automagically generated by builddpdf" << endl |
| 48 | << "// DO NOT HAND EDIT. Yes, this means you." << endl |
| 49 | << endl; |
| 50 | |
| 51 | hos << "// Dimensions of table, these are inclusive." << endl; |
| 52 | hos << "#define DPDF_MAXSIDES " << maxsides << endl; |
| 53 | hos << "#define DPDF_MAXROLLS " << maxrolls << endl; |
| 54 | hos << "#define DPDF_SCALE " << scale << endl; |
| 55 | hos << endl; |
| 56 | |
| 57 | hos << "// Since 0 and 1 are obvious for sides, hence the -1" << endl; |
| 58 | hos << "extern const int glb_dpdftable[DPDF_MAXROLLS+1][DPDF_MAXSIDES-1];" << endl; |
| 59 | |
| 60 | |
| 61 | os << "// dpdf_table.cpp" << endl |
| 62 | << "// Automagically generated by builddpdf" << endl |
| 63 | << "// DO NOT HAND EDIT. Yes, this means you." << endl |
| 64 | << endl; |
| 65 | |
| 66 | os << "#include \"dpdf_table.h\"" << endl |
| 67 | << endl; |
| 68 | |
| 69 | os << "const int glb_dpdftable[DPDF_MAXROLLS+1][DPDF_MAXSIDES-1] =" << endl |
| 70 | << "{" << endl; |
| 71 | |
| 72 | for (roll = 0; roll <= maxrolls; roll++) |
| 73 | { |
| 74 | os << " { "; |
| 75 | for (side = 2; side <= maxsides; side++) |
| 76 | { |
| 77 | DPDF r(1, side), u(1, side); |
| 78 | int mean; |
| 79 | int i; |
| 80 | |
| 81 | for (i = 0; i < roll; i++) |
| 82 | { |
| 83 | r.max(r, u); |
| 84 | } |
| 85 | |
| 86 | mean = (int) (r.expectedValue() * scale + 0.5); |
| 87 | |
| 88 | os << mean << ", "; |
| 89 | } |
| 90 | os << "}," << endl; |
| 91 | } |
| 92 | |
| 93 | os << "};" << endl; |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 |