This is a simple program which generates C++ dynamic programming code from given state-translation equation.
Unit tests are included.
for the input:
dp[full] -> dp[full - 1] + dp[full - 2] (full >= 2)
-> 1 (full == 1 or full == 2)
-> 0 (else)It generates:
/// Generated by Gen4DP at: Tue Jan 3 17:03:42 CST 2017
/// Language: C++
/// This open source generator is under GNU General Public License v3.0.
/// Source: https://github.com/ice1000/Gen4DP
#include <iostream>
#include <algorithm>
using namespace std;
const int SIZE=101;
typedef int number;
number dp[SIZE];
int main(const int argc, const char *argv[]) {
int full,full_len;
cin>>full_len;
for (full=0; full<=full_len; ++full) {
if (full>=2) {
dp[full] = dp[full-1]+dp[full-2];
}
else if (full==1||full==2) {
dp[full] = 1;
}
else {
dp[full] = 0;
}
}
cout<<dp[full_len]<<endl;
return 0;
}If the equation has two dimensions:
a[i, j]-> a[i - 1, j] + a[i, j - 1] (i >= 1 and j >= 1)
-> 1 (else)It generates:
/// Generated by Gen4DP at: Tue Jan 3 17:22:25 CST 2017
/// Language: C++
/// This open source generator is under GNU General Public License v3.0.
/// Source: https://github.com/ice1000/Gen4DP
#include <iostream>
#include <algorithm>
using namespace std;
const int SIZE=101;
typedef int number;
number a[SIZE][SIZE];
int main(const int argc, const char *argv[]) {
int i,j,i_len,j_len;
cin>>i_len>>j_len;
for (i=0; i<=i_len; ++i) {
for (j=0; j<=j_len; ++j) {
if (i>=1&&j>=1) {
a[i][j] = a[i-1][j]+a[i][j-1];
}
else {
a[i][j] = 1;
}
}
}
cout<<a[i_len][j_len]<<endl;
return 0;
}It will also give you error messages, like 'braces don't match', 'invalid naming', etc.