שיעור בתכנות.

RichardSmith

New member
שיעור בתכנות.

קצת איבדתי את מספר השיעורים. צריך להביא גם את התוכנות הקודמות. בכל מקרה, זאת התוכנה של השבוע. שרשור לכאן שאלות בנוגע להסברים לגבי חלקים מהקוד:
import javax.swing.*; //Our nice poly will represent the polynom of order 2: p(x) = ax^2 + bx + c public class Poly2 { private double A; private double B; private double C; public Poly2() { A=0; B=0; C=0; } public Poly2(int a, int b, int c) { A = a; B = b; C = c; } /* *returns the derivative of the polynomial. *To test the method write *p3 = p1.deriv(); *where p3 is a Poly2 object created by the first constructor p3 = new Poly2(); */ public Poly2 deriv(){ Poly2 p3 = new Poly2(); p3.A=0; p3.B=2*A; p3.C=B; return p3; } /* *prints the polynomial. *The method will print the polynomial in the form ax^2+bx+c . */ public void print(){ JOptionPane.showMessageDialog(null,A+"x^2+"+B+"x+"+A); } /* *prints the roots of the quadratic equation p(x) = 0 * ( use the algorithm of exercise number 3 ). */ public void print_roots(){ if (C==0.0&&A!=0.0) { JOptionPane.showMessageDialog(null,B/C); }else if (C!=0.0 && A==0.0 && B==0.0) { JOptionPane.showMessageDialog(null,"There isn't a solution, Master."); } else if (C==0.0 && A==0.0 && B==0.0) { JOptionPane.showMessageDialog(null,"There is an infinite number of solutions, Master."); } else if (A!=0&&(B*B-4*A*C)< 0) { JOptionPane.showMessageDialog(null,"Sir, there isn't any real solution."); } else if (A!=0&&(B*B-4*A*C)==0) { JOptionPane.showMessageDialog(null,"Sir, here is your solution: " + -B/(2*A)); } else if (A!=0&&(B*B-4*A*C)>0) { JOptionPane.showMessageDialog(null,"Sir, here are the two solutions, number 1: " + + (-B+Math.sqrt(B*B-4*A*C))/(2*A) + ".\n And the second: " + + (-B-Math.sqrt(B*B-4*A*C))/(2*A)); } } /*returns the value of the * polynomial in point x. */ public double value( double x ){ return A*x*x+B*x+C; } /* *p.plus(p1, p2) means p = p1 + p2. *To test the method write p4.plus( p1, p2); *where p4 is a Poly2 object created by *the first constructor - p4 = new Poly2(); */ public void plus( Poly2 p1, Poly2 p2 ){ Poly2 p4 = new Poly2(); p4.A=p1.A+p2.A; p4.B=p1.B+p2.B; p4.C=p1.C+p2.C; } /* * p.minus(p1, p2) means p = p1 - p2. * To test the method write * p5.minus( p1, p2); * where p5 is a Poly2 object created by * the first constructor - p5 = new Poly2(); */ public void minus( Poly2 p1, Poly2 p2 ){ Poly2 p5 = new Poly2(); p5.A=p1.A-p2.A; p5.B=p1.B-p2.B; p5.C=p1.C-p2.C; } }​
התוכנה יוצרת אובייקט של משוואה ריבועית.
 

mantrix

New member
נצל"ש: קוד קטן ומעניין- FastInvSqrt

הרבה יותר קצר מהנ"ל, אך עם הרבה יותר תחכום פנימי... http://www.beyond3d.com/articles/fastinvsqrt/ מי שיכול להסביר את זה בצורה פשוטה בעברית מוזמן
 
למעלה