PaweU

java 5

Dec 10th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Locale;
  4.  
  5. public class Sum extends Node {
  6.  
  7. List<Node> args = new ArrayList<>();
  8.  
  9. Sum(){}
  10.  
  11. Sum(Node n1, Node n2){
  12. args.add(n1);
  13. args.add(n2);
  14. }
  15.  
  16.  
  17. Sum add(Node n){
  18. args.add(n);
  19. return this;
  20. }
  21.  
  22. Sum add(double c){
  23. args.add(new Constant(c));
  24. return this;
  25. }
  26.  
  27. Sum add(double c, Node n) {
  28. Node mul = new Prod(c,n);
  29. args.add(mul);
  30. return this;
  31. }
  32.  
  33. @Override
  34. double evaluate() {
  35. double result =0;
  36. //oblicz sumę wartości zwróconych przez wywołanie evaluate skłądników sumy
  37. for (int i = 0; i < args.size(); i++) {
  38. result += args.get(i).evaluate();
  39. }
  40. return sign*result;
  41. }
  42.  
  43. int getArgumentsCount(){return args.size();}
  44.  
  45. public String toString(){
  46. StringBuilder b = new StringBuilder();
  47. if(sign<0)b.append("-(");
  48.  
  49. for (int i = 0; i < args.size(); i++) {
  50. if (i > 0) b.append(" + ");
  51. b.append(args.get(i).toString());
  52. }
  53.  
  54. if(sign<0)b.append(")");
  55. return b.toString();
  56. }
  57.  
  58.  
  59. static void buildAndPrint(){
  60. Variable x = new Variable("x");
  61. Node exp = new Sum()
  62. .add(2.1,new Power(x,3))
  63. .add(new Power(x,2))
  64. .add(-2,x)
  65. .add(7);
  66. System.out.println(exp.toString());
  67. }
  68.  
  69. static void buildAndEvaluate() {
  70. Variable x = new Variable("x");
  71. Node exp = new Sum()
  72. .add(new Power(x, 3))
  73. .add(-2, new Power(x, 2))
  74. .add(-1, x)
  75. .add(2);
  76. for (double v = -5; v < 5; v += 0.1) {
  77. x.setValue(v);
  78. double result = exp.evaluate();
  79. System.out.printf(Locale.US, "f(%f)=%f", v, result);
  80. if (result > -0.00001 && result < 0.00001) System.out.print(" <-- Znaleziono miejsce zerowe!");
  81. System.out.print("\n");
  82. }
  83. }
  84.  
  85. static void defineCircle(){
  86. Variable x = new Variable("x");
  87. Variable y = new Variable("y");
  88. Node circle = new Sum()
  89. .add(new Power(x,2))
  90. .add(new Power(y,2))
  91. .add(8,x)
  92. .add(4,y)
  93. .add(16);
  94. System.out.println(circle.toString());
  95.  
  96. double xv = 100*(Math.random()-.5);
  97. double yv = 100*(Math.random()-.5);
  98. x.setValue(xv);
  99. y.setValue(yv);
  100. double fv = circle.evaluate();
  101. System.out.print(String.format("Punkt (%f,%f) leży %s koła %s",xv,yv,(fv<0?"wewnątrz":"na zewnątrz"),circle.toString()));
  102. }
  103.  
  104. static void pointsInCircle() {
  105. Variable x = new Variable("x");
  106. Variable y = new Variable("y");
  107. Node circle = new Sum()
  108. .add(new Power(x, 2))
  109. .add(new Power(y, 2))
  110. .add(8, x)
  111. .add(4, y)
  112. .add(16);
  113. System.out.println(circle.toString());
  114.  
  115. int found = 0;
  116. while (found < 100) {
  117. double xv = 100 * (Math.random() - .5);
  118. double yv = 100 * (Math.random() - .5);
  119. x.setValue(xv);
  120. y.setValue(yv);
  121. double fv = circle.evaluate();
  122. if (fv < 0) {
  123. found++;
  124. System.out.print(String.format("%d. Punkt (%f,%f)\n", found, xv, yv));
  125. }
  126.  
  127. }
  128. }
  129.  
  130.  
  131. public static void main(String[] args) {
  132. buildAndPrint();
  133. buildAndEvaluate();
  134. defineCircle();
  135. pointsInCircle();
  136. }
  137.  
  138. }
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160. abstract public class Node {
  161. int sign=1;
  162. Node minus(){
  163. sign = -1;
  164. return this;
  165. }
  166. Node plus(){
  167. sign = 1;
  168. return this;
  169. }
  170. int getSign(){return sign;}
  171.  
  172. /**
  173. * Oblicza wartość wyrażenia dla danych wartości zmiennych
  174. * występujących w wyrażeniu
  175. */
  176. abstract double evaluate();
  177.  
  178. /**
  179. *
  180. * zwraca tekstową reprezentację wyrażenia
  181. */
  182. public String toString(){return "";}
  183.  
  184. /**
  185. *
  186. * Zwraca liczbę argumentów węzła
  187. */
  188. int getArgumentsCount(){return 0;}
  189. }
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. import java.text.DecimalFormat;
  203. import java.text.DecimalFormatSymbols;
  204. import java.util.Locale;
  205.  
  206. public class Constant extends Node {
  207. double value;
  208. Constant(double value){
  209. this.sign = value<0?-1:1;
  210. this.value = value<0?-value:value;
  211. }
  212.  
  213.  
  214. @Override
  215. double evaluate() {
  216. return sign*value;
  217. }
  218.  
  219. @Override
  220. public String toString() {
  221. String sgn=sign<0?"-":"";
  222. // return sgn+Double.toString(value);
  223. DecimalFormat format = new DecimalFormat("0.#####",new DecimalFormatSymbols(Locale.US));
  224. return sgn+format.format(value);
  225. }
  226. }
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242. public class Variable extends Node {
  243. String name;
  244. Double value;
  245. Variable(String name){
  246. this.name = name;
  247. }
  248. void setValue(double d){
  249. value = d;
  250. }
  251.  
  252.  
  253. @Override
  254. double evaluate() {
  255. return sign*value;
  256. }
  257.  
  258.  
  259. @Override
  260. public String toString() {
  261. String sgn=sign<0?"-":"";
  262. return sgn+name;
  263. }
  264.  
  265. }
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286. public class Power extends Node {
  287. double p;
  288. Node arg;
  289. Power(Node n,double p){
  290. arg = n;
  291. this.p = p;
  292. }
  293.  
  294. @Override
  295. double evaluate() {
  296. double argVal = arg.evaluate();
  297. return Math.pow(argVal,p);
  298. }
  299.  
  300.  
  301. int getArgumentsCount(){return 1;}
  302.  
  303.  
  304. @Override
  305. public String toString() {
  306. StringBuilder b = new StringBuilder();
  307. if(sign<0)b.append("-");
  308. int argSign = arg.getSign();
  309. int cnt = arg.getArgumentsCount();
  310. boolean useBracket = false;
  311. if(argSign<0 ||cnt>1)useBracket = true;
  312. String argString = arg.toString();
  313. if(useBracket)b.append("(");
  314. b.append(argString);
  315. if(useBracket)b.append(")");
  316. b.append("^");
  317. b.append(p);
  318. return b.toString();
  319. }
  320.  
  321. }
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337. import java.util.ArrayList;
  338. import java.util.List;
  339.  
  340. public class Prod extends Node {
  341. List<Node> args = new ArrayList<>();
  342.  
  343. Prod(){}
  344.  
  345. Prod(Node n1){
  346. args.add(n1);
  347. }
  348.  
  349. Prod(double c){
  350. //wywołaj konstruktor jednoargumentowy przekazując new Constant(c)
  351. this(new Constant(c));
  352. }
  353.  
  354. Prod(Node n1, Node n2){
  355. args.add(n1);
  356. args.add(n2);
  357.  
  358. }
  359. Prod(double c, Node n){
  360. this(new Constant(c), n);
  361. }
  362.  
  363. Prod mul(Node n){
  364. args.add(n);
  365. return this;
  366. }
  367.  
  368. Prod mul(double c){
  369. args.add(new Constant(c));
  370. return this;
  371. }
  372.  
  373. @Override
  374. double evaluate() {
  375. double result =1;
  376. for (int i = 0; i < args.size(); i++) {
  377. result *= args.get(i).evaluate();
  378. }
  379. return sign*result;
  380. }
  381.  
  382. int getArgumentsCount(){return args.size();}
  383.  
  384.  
  385. public String toString(){
  386. StringBuilder b = new StringBuilder();
  387. if(sign<0)b.append("-");
  388.  
  389. for (int i = 0; i < args.size(); i++) {
  390. if (i == 0){
  391. int argSign = args.get(0).getSign();
  392. if(argSign < 0) b.append("(");
  393. b.append(args.get(0).toString());
  394. if(argSign < 0) b.append(")");
  395.  
  396. } else {
  397. b.append("*");
  398. b.append(args.get(i).toString());
  399. }
  400. }
  401.  
  402. return b.toString();
  403. }
  404.  
  405.  
  406. }
Add Comment
Please, Sign In to add comment