fork download
  1. <?php
  2. error_reporting(E_ERROR | E_WARNING | E_PARSE);
  3. class Computable
  4. {
  5. function compute() { }
  6. }
  7.  
  8. class Value extends Computable
  9. {
  10. public $val;
  11. function __construct($nval)
  12. {
  13. $this->val = $nval;
  14. }
  15. function compute()
  16. {
  17. return $this->val;
  18. }
  19. function printo($ntabs = 0)
  20. {
  21. return $this->compute() . ".0f";
  22. }
  23. function printnv()
  24. {
  25. return $this->compute() . ".0f";
  26. }
  27. }
  28.  
  29. class Operator
  30. {
  31. public $name;
  32.  
  33. function __construct()
  34. {
  35. $a = array('+', '-', '/', '*');
  36. $nname = $a[rand(0, 3)];
  37. $this->name = $nname;
  38. }
  39. function compute($a, $b)
  40. {
  41. if ($this->name == '+') return $a + $b;
  42. if ($this->name == '-') return $a - $b;
  43. if ($this->name == '/')
  44. {
  45. if ($b == 0)
  46. return 1;
  47. else return $a / $b;
  48. }
  49. if ($this->name == '*') return $a * $b;
  50. }
  51. function tos()
  52. {
  53. return $this->name;
  54. }
  55. };
  56.  
  57. class Expression extends Computable
  58. {
  59. public $lhs;
  60. public $rhs;
  61. public $op;
  62. function __construct($nlhs, $nrhs)
  63. {
  64. $this->lhs = $nlhs;
  65. $this->rhs = $nrhs;
  66. $this->op = new Operator();
  67. }
  68. function compute()
  69. {
  70. return $this->op->compute($this->lhs->compute(), $this->rhs->compute());
  71. }
  72. function printo($ntabs = 0)
  73. {
  74. $tabs = "";
  75. $tabs2 = "";
  76. for ($i = 0; $i < $ntabs; $i++) $tabs .= " ";
  77. for ($i = 0; $i < $ntabs-1; $i++) $tabs2 .= " ";
  78. $ntabs++;
  79. return "\n" . $tabs . "(" . $this->lhs->printo($ntabs) . " " . $this->op->tos() . " " . $this->rhs->printo($ntabs) . ")" . "\n" . $tabs2;
  80. }
  81. function printnv()
  82. {
  83. return "(" . $this->lhs->printnv() . " " . $this->op->tos() . " " . $this->rhs->printnv() . ")";
  84. }
  85. }
  86.  
  87. function generateRandomString($length = 10) {
  88. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';
  89. $randomString = '';
  90. $us = false;
  91. for ($i = 0; $i < $length; $i++) {
  92. $c = $characters[rand(0, strlen($characters) - 1)];
  93. if ($i == 0 && is_numeric($c))
  94. {
  95. while ($i == 0 && is_numeric($c)) $c = $characters[rand(0, strlen($characters) - 1)];
  96. $randomString .= $c;
  97. } else if ($i >= 1 && !$us && $c == '_')
  98. {
  99. $us = true;
  100. $randomString .= $c;
  101. } else if ($i >= 1 && $us && $c != '_')
  102. {
  103. $us = false;
  104. $randomString .= $c;
  105. } else if ($i >= 1 && $us && $c == '_')
  106. {
  107. while ($i >= 1 && $us && $c == '_') $c = $characters[rand(0, strlen($characters) - 1)];
  108. $randomString .= $c;
  109. } else $randomString .= $c;
  110. }
  111. return $randomString;
  112. }
  113.  
  114. echo "#include <iostream>\n";
  115. echo "#include <vector>\n";
  116. echo "using namespace std;\n\n";
  117.  
  118. for ($i = 0; $i < 10; $i++)
  119. {
  120. $test = new Expression(new Value(rand(1, 2)), new Value(rand(1, 2)));
  121. $lhs = $test;
  122. $ident = generateRandomString();
  123. for ($j = 0; $j < rand(0, 1); $j++)
  124. {
  125.  
  126. $c = new Expression(new Value(rand(1, 100)), new Value(rand(1, 100)));
  127. $lhs = new Expression($lhs, $c);
  128. }
  129. $rhs = $test;
  130. for ($j = 0; $j < rand(0, 1); $j++)
  131. {
  132.  
  133. $c = new Expression(new Value(rand(1, 100)), new Value(rand(1, 100)));
  134. $rhs = new Expression($rhs, $c);
  135. }
  136. $test = new Expression($lhs, $rhs);
  137. echo "constexpr float ".$ident." = " . $test->printnv() . ";\n";
  138. echo "// should equal " . $test->compute() . "\n";
  139. $vars[$ident] = $test->compute();
  140. }
  141. echo "int main() {\n";
  142. echo "\tvector<float> vars;\n";
  143.  
  144. foreach ($vars as $key => $value)
  145. echo "\tvars.push_back(".$key.");\n";
  146.  
  147. echo "\tfor (auto it = vars.begin(); it != vars.end(); ++it) {\n";
  148. echo "\t\tcout << *it << endl;\n";
  149. echo "\t}\n";
  150. echo "\treturn 0;\n}";
  151. ?>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
#include <iostream>
#include <vector>
using namespace std;

constexpr float tupRbBbHlz = (((1.0f - 1.0f) * (81.0f - 64.0f)) + (1.0f - 1.0f));
// should equal 0
constexpr float MeNdIds9HW = (((1.0f / 2.0f) / (55.0f + 12.0f)) - (1.0f / 2.0f));
// should equal -0.49253731343284
constexpr float g7qutm2wwP = (((2.0f * 1.0f) / (30.0f - 3.0f)) - ((2.0f * 1.0f) / (21.0f + 40.0f)));
// should equal 0.041287188828172
constexpr float M55H8lPzQi = (((1.0f * 1.0f) / (88.0f - 82.0f)) - ((1.0f * 1.0f) + (71.0f + 2.0f)));
// should equal -73.833333333333
constexpr float FoVru_7DlW = ((2.0f + 2.0f) + (2.0f + 2.0f));
// should equal 8
constexpr float xJBgrCuCfW = ((1.0f + 1.0f) + (1.0f + 1.0f));
// should equal 4
constexpr float F2d92lMnhY = (((1.0f * 1.0f) / (51.0f / 6.0f)) + (1.0f * 1.0f));
// should equal 1.1176470588235
constexpr float km_gy1oPab = ((2.0f * 2.0f) - (2.0f * 2.0f));
// should equal 0
constexpr float oC0mawpL3_ = ((2.0f / 1.0f) * (2.0f / 1.0f));
// should equal 4
constexpr float bXpJZOy8Z1 = ((2.0f + 2.0f) / (2.0f + 2.0f));
// should equal 1
int main() {
	vector<float> vars;
	vars.push_back(tupRbBbHlz);
	vars.push_back(MeNdIds9HW);
	vars.push_back(g7qutm2wwP);
	vars.push_back(M55H8lPzQi);
	vars.push_back(FoVru_7DlW);
	vars.push_back(xJBgrCuCfW);
	vars.push_back(F2d92lMnhY);
	vars.push_back(km_gy1oPab);
	vars.push_back(oC0mawpL3_);
	vars.push_back(bXpJZOy8Z1);
	for (auto it = vars.begin(); it != vars.end(); ++it) {
		cout << *it << endl;
	}
	return 0;
}