fork download
  1. //Coded entirely by Terence Cox
  2. #include <iostream>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <math.h>
  6. #include<iomanip>
  7.  
  8. using namespace std;
  9.  
  10. string substitute(double, double, string);
  11. string simplify(bool,char, string);
  12. double** fill2Ddyrow6(int, int, double, double, double, double, double, double**);
  13.  
  14. double evaluator(string p_equ, bool debug)
  15. {
  16. start1:
  17. int fp = -1, lp = -1;
  18. //Search for Paraenthesis
  19. for (int c = 0; c < p_equ.size(); c++)
  20. {
  21. if (p_equ.at(c) == '(')
  22. fp = c;
  23. if (p_equ.at(c) == ')')
  24. {
  25. lp = c;
  26. break;
  27. }
  28. }
  29. //if parenthesis exist extract the expression within them
  30. if (fp < 0 && lp < 0)
  31. {
  32. if (debug == true)
  33. cout << "no more parentheses" << endl;
  34. }
  35. else
  36. {
  37. //extract from parenthesis and start the evauluation with the rest, then replaces solution in primary equ
  38. string s_equ(p_equ, fp + 1, (lp - 1) - fp);
  39. //solves what's in the parenthesis and eliminates the ()'s
  40. p_equ.replace(fp, (lp + 1) - fp, to_string(evaluator(s_equ,debug)));
  41. goto start1;
  42. }
  43. //perform PEMDAS
  44. p_equ = simplify(debug,'-', simplify(debug,'+', simplify(debug,'*', simplify(debug,'/', simplify(debug,'^', p_equ)))));
  45.  
  46. //string fsresult(p_equ, fp + 1, lp - fp);
  47. double fresult = stod(p_equ);
  48. return fresult;
  49. }
  50.  
  51.  
  52. string simplify(bool debug, char oper, string expr)
  53. {
  54. start2:
  55. int p, fnfp = -1, snlp = -1, reader = 1, runs = 0;
  56. double num1, num2, result;
  57. bool single_neg=false;
  58.  
  59. p = -1;
  60. for (int c = 0; c < expr.size(); c++)
  61. {
  62. if (expr.at(c) == oper)
  63. {
  64. if (c > 0) //prevents error when a negative number is first in the expression
  65. {
  66. //assigns the position of the operator to p
  67. p = c;
  68. break;
  69. }
  70.  
  71. }
  72. }
  73. if (p < 0)
  74. {
  75. if (debug == true)
  76. cout << "no " << oper << endl;
  77. }
  78. else
  79. {
  80. runs++;
  81. while (expr.at(p - reader) == '-' || expr.at(p - reader) == '.' || isdigit(expr.at(p - reader)))
  82. {
  83. fnfp = p - reader;
  84. reader++;
  85. if (p - reader < 0) //handles exception where this reads outside of memory
  86. break;
  87. if (expr.at(fnfp) == '-')//hnadles a runtime error which causes negative numbers to be omitted
  88. {
  89. break;
  90. }
  91. }
  92. reader = 1;
  93. while (expr.at(p + reader) == '-' ||expr.at(p + reader) == '.' || isdigit(expr.at(p + reader)))
  94. {
  95. snlp = p + reader;
  96. //This code checks to make sure only one negative symbol is allowed, will break if another is found.
  97. if (expr.at(snlp) == '-')
  98. {
  99. if (single_neg == true)
  100. break;
  101. else
  102. single_neg = true;
  103. }
  104. reader++;
  105. if (p + reader > expr.size() - 1) //handles exception where this reads outside of memory
  106. break;
  107. }
  108. string strnum1(expr, fnfp, p - fnfp);
  109. string strnum2(expr, p + 1, snlp - p);
  110. double num1 = stod(strnum1);
  111. double num2 = stod(strnum2);
  112.  
  113.  
  114. switch (oper)
  115. {
  116. case '^':
  117. result = pow(num1, num2);
  118. break;
  119. case '/':
  120. result = num1 / num2;
  121. break;
  122. case '*':
  123. result = num1 * num2;
  124. break;
  125. case '+':
  126. result = num1 + num2;
  127. break;
  128. case '-':
  129. result = num1 - num2;
  130. break;
  131. }
  132. //result = (int)(result * 10000.0) / 10000.0;
  133. if (debug == true)
  134. cout << "after performing " << oper << " the result is " << result << endl;
  135. //replace the solve part of the expression into the entire expression
  136. expr.replace(fnfp, (snlp + 1) - fnfp, to_string(result));
  137. goto start2;
  138. }
  139. if (debug == true)
  140. cout << "solved " << runs << " " << oper << " operations" << endl;
  141. return expr;
  142. }
  143.  
  144. string substitute(double X, double Y, string equ)
  145. {
  146. //checks each character for X or Y to replace them with actual values
  147. for (int c = 0; c < equ.size(); c++)
  148. {
  149. if (equ.at(c) == 'X' || equ.at(c) == 'x')
  150. equ.replace(c, 1, to_string(X));
  151. if (equ.at(c) == 'Y' || equ.at(c) == 'y')
  152. equ.replace(c, 1, to_string(Y));
  153. }
  154. return equ;
  155. }
  156.  
  157.  
  158.  
  159. int main()
  160. {
  161. Begin:
  162. cout.precision(5);
  163. string equation;
  164. int steps;
  165. float X1, X2, Y1, K1, Z1, K2, h, newY;
  166. double change;
  167. bool debug= false,use_table=true,userres;
  168. cout << "What is the differential equation you want to use Euler's method on?\ny'= ";
  169. cin >> equation;
  170. cout << "Fill this in\n (y(x) = ___)\n | \n v \nx= ";
  171. cin >> X1;
  172. cout << "and this\n (y(" << X1 << ") = __)\n |\n v\n y= ";
  173. cin >> Y1;
  174. cout << "How many steps of accuracy do you want to use: ";
  175. cin >> steps;
  176. cout << "What is the value of X you want to use to solve Y" << endl;
  177. cin >> X2;
  178. h = (X2 - X1) / steps;
  179. cout << "The size of h is " << h << "." << endl;
  180. cout << "Last Question, do you want to see the table of values(Takes longer to process) 1 or 0" << endl;
  181. cin >> use_table;
  182. //create a new 2D Dynamic Array
  183. double** table = new double*[steps + 1];
  184. for (int i = 0; i < steps + 1; ++i)
  185. table[i] = new double[6];
  186.  
  187. //fill in table with obtained information
  188. table = fill2Ddyrow6(0, 0, X1, Y1, 0,0,0, table);
  189.  
  190. //perform the Euler's process for the inputted amount of steps
  191. for (int t = 0; t < steps; t++)
  192. {
  193. K1 = evaluator(substitute(table[t][1], table[t][2], equation),debug);
  194. table[t][3] = K1;
  195. Z1 = table[t][2] + abs(h)*K1;
  196. table[t][4] = Z1;
  197. K2 = evaluator(substitute(X1 + (h*(t + 1)), Z1, equation),debug);
  198. table[t][5] = K2;
  199. newY = table[t][2]+((K1+K2)/2)*abs(h);
  200.  
  201. table = fill2Ddyrow6(t + 1, t + 1, X1 + (h*(t + 1)), newY, 0, 0, 0, table);
  202. }
  203.  
  204. //outputs the values in the table into the console
  205. if (use_table == true|| use_table == 't'|| use_table == 'T' )
  206. {
  207. cout << left << setw(16) << "step i" << setw(16) << "X1" << setw(16) << "Y1" << setw(16) << "K1" << setw(16) << "Z1" << setw(16) << "K2" << endl;
  208. for (int row = 0; row < steps + 1; row++)
  209. {
  210. cout << endl;
  211. for (int column = 0; column < 6; column++)
  212. {
  213. cout << setw(16) << table[row][column];
  214. }
  215. }
  216. }
  217. cout << endl;
  218. cout << "The Final Answer is " << table[steps][2] << endl;
  219. cout << "Done" << endl;
  220. cout << "Do you want to enter another Diffy-Q?, 1(True) or 0(False)" << endl;
  221. cin >> userres;
  222. if (userres == 1)
  223. goto Begin;
  224. system("pause");
  225. }
  226.  
  227. double** fill2Ddyrow6(int row, int step, double X, double Y, double K1,double Z1,double K2, double** tablef)
  228. {
  229. tablef[row][0] = step;
  230. tablef[row][1] = X;
  231. tablef[row][2] = Y;
  232. tablef[row][3] = K1;
  233. tablef[row][4] = Z1;
  234. tablef[row][5] = K2;
  235. return tablef;
  236. }
  237.  
  238.  
  239.  
Success #stdin #stdout #stderr 0s 15248KB
stdin
(x^2)/(y-1)
-1
-.5
50
2
1
stdout
What is the differential equation you want to use Euler's method on?
y'= Fill this in
 (y(x) = ___)
    |     
    v     
x= and this
 (y(-1) = __)
           |
           v
        y= How many steps of accuracy do you want to use: What is the value of X you want to use to solve Y
The size of h is 0.06.
Last Question, do you want to see the table of values(Takes longer to process) 1 or 0
step i          X1              Y1              K1              Z1              K2              

0               -1              -0.5            -0.66667        -0.54           -0.57377        
1               -0.94           -0.53721        -0.57481        -0.5717         -0.49272        
2               -0.88           -0.56924        -0.49349        -0.59885        -0.42055        
3               -0.82           -0.59666        -0.42113        -0.62193        -0.35612        
4               -0.76           -0.61998        -0.35655        -0.64137        -0.29853        
5               -0.7            -0.63963        -0.29885        -0.65756        -0.24711        
6               -0.64           -0.65601        -0.24734        -0.67085        -0.20133        
7               -0.58           -0.66947        -0.2015         -0.68156        -0.1608         
8               -0.52           -0.68034        -0.16092        -0.68999        -0.12521        
9               -0.46           -0.68892        -0.12529        -0.69644        -0.094315       
10              -0.4            -0.69551        -0.094367       -0.70117        -0.067953       
11              -0.34           -0.70038        -0.067985       -0.70446        -0.045997       
12              -0.28           -0.7038         -0.046015       -0.70656        -0.028361       
13              -0.22           -0.70603        -0.02837        -0.70773        -0.014991       
14              -0.16           -0.70733        -0.014994       -0.70823        -0.005854       
15              -0.1            -0.70796        -0.005855       -0.70831        -0.000937       
16              -0.04           -0.70816        -0.000937       -0.70822        -0.000234       
17              0.02            -0.7082         -0.000234       -0.70821        -0.003747       
18              0.08            -0.70831        -0.003746       -0.70854        -0.011472       
19              0.14            -0.70877        -0.01147        -0.70946        -0.023399       
20              0.2             -0.70982        -0.023394       -0.71122        -0.039504       
21              0.26            -0.7117         -0.039493       -0.71407        -0.059741       
22              0.32            -0.71468        -0.05972        -0.71826        -0.084038       
23              0.38            -0.71899        -0.084003       -0.72403        -0.1123         
24              0.44            -0.72488        -0.11224        -0.73162        -0.14437        
25              0.5             -0.73258        -0.14429        -0.74124        -0.1801         
26              0.56            -0.74231        -0.17999        -0.75311        -0.21927        
27              0.62            -0.75429        -0.21912        -0.76744        -0.26162        
28              0.68            -0.76871        -0.26143        -0.7844         -0.30688        
29              0.74            -0.78576        -0.30665        -0.80416        -0.35474        
30              0.8             -0.8056         -0.35445        -0.82687        -0.40484        
31              0.86            -0.82838        -0.40451        -0.85265        -0.45686        
32              0.92            -0.85422        -0.45647        -0.88161        -0.51041        
33              0.98            -0.88323        -0.50998        -0.91383        -0.56515        
34              1.04            -0.91548        -0.56466        -0.94936        -0.62072        
35              1.1             -0.95105        -0.62018        -0.98826        -0.67677        
36              1.16            -0.98995        -0.6762         -1.0305         -0.73301        
37              1.22            -1.0322         -0.7324         -1.0762         -0.78914        
38              1.28            -1.0779         -0.7885         -1.1252         -0.84491        
39              1.34            -1.1269         -0.84424        -1.1775         -0.9001         
40              1.4             -1.1792         -0.89941        -1.2332         -0.95452        
41              1.46            -1.2348         -0.95381        -1.2921         -1.008          
42              1.52            -1.2937         -1.0073         -1.3541         -1.0604         
43              1.58            -1.3557         -1.0597         -1.4193         -1.1117         
44              1.64            -1.4209         -1.111          -1.4875         -1.1618         
45              1.7             -1.489          -1.1611         -1.5587         -1.2106         
46              1.76            -1.5602         -1.2099         -1.6328         -1.2581         
47              1.82            -1.6342         -1.2574         -1.7097         -1.3044         
48              1.88            -1.7111         -1.3037         -1.7893         -1.3493         
49              1.94            -1.7907         -1.3486         -1.8716         -1.393          
50              2               -1.8729         0               0               0               
The Final Answer is -1.8729
Done
Do you want to enter another Diffy-Q?, 1(True) or 0(False)
stderr
sh: 1: pause: not found