המשך שיעורי תכנות!

RichardSmith

New member
המשך שיעורי תכנות!

כן, כן, אני יודע שכולכם פשוט השתוקקתם לרגע. (קצת שכחתי לעלות שבוע שעבר את הקוד...
) אז הנה הקוד של שבוע שעבר:
import java.util.Scanner; public class zeller { public static void main(String[] args) { // create a Scanner object to read input from the screen Scanner input = new Scanner( System.in); int m, //month d1, //day y, //year c1, //century a,b,c,d,r; //help verbs System.out.println("Please master, give me a date!"); System.out.println("state it by month, day, year."); m = input.nextInt(); d1 = input.nextInt(); y = input.nextInt(); c1 = y/100; y=y%100; m = (m + 9)% 12 + 1; y = y - m / 11; a=(13*m-1)/5; b=y/4; c=c1/4; d = a + b + c + d1 + y -2*c1; r=d%7; r+=7; r=r/7+1; switch(r){ case 1: System.out.println("It is a Sunday, Sir."); case 2: System.out.println("It is a Monday, Sir."); case 3: System.out.println("It is a Tuesday, Sir."); case 4: System.out.println("It is a Wednesday, Sir."); case 5: System.out.println("It is a Thursday, Sir."); case 6: System.out.println("It is a Friday, Sir."); case 7: System.out.println("It is a Saturday, Sir."); } } }​
הקוד מקבל תאריך ומחשב את היום בשבוע שהתאריך "נפל" עליו. די מדליק, הא?
 

RichardSmith

New member
והנה הקוד של השבוע:

הקוד מקבל שלושה מספרים ומחשב את השורש הריבועי כאשר המספרים מייצגים את המקדמים במשווה ריבועית:
import java.util.Scanner; public class quadraticequation { public static void main(String[] args) { System.out.println("Please, Master, give me three number for which I can do my magic!"); System.out.println("I may compute the answers for the equation: ax^2+bx+c!"); System.out.println("All you need to do is to enter three numbers!: "); // create a Scanner object to read input from the screen Scanner input = new Scanner( System.in); double a,b,c,sol1,sol2,temp; a = input.nextInt(); b = input.nextInt(); c = input.nextInt(); if (c==0&&a!=0) { sol1=b/c; System.out.println("Sir! Here is your solution: "+sol1); } else if (c!=0&&a==0&&b==0) { System.out.println("Illegal equation, no solution, master!"); } else if (a==0&&b==0&&c==0) { System.out.println("Oh, Sir! It is an infinite number of solutions!"); } else if(a!=0&&(b*b-4*a*c)<0) { System.out.println("Sir! There isn't any real solution!"); } else if (a!=0&&(b*b-4*a*c)==0) { sol1=-b/2*a; System.out.println("Sir! Here is your solution: "+sol1); } else if (a!=0&&(b*b-4*a*c)>0) { temp=Math.sqrt(b*b-4*a*c); sol1=(-b+temp)/2*a; sol2=(-b-temp)/2*a; System.out.println("Sir! Here is your solution: "+sol1+" and the second:"+sol2); } } }​
מישהו יודע איך לסדר אוטומטית את הקוד באקליפס?...
 

מתן4ל

New member
טוב בא לי גם ../images/Emo13.gif

System.out.println("this is why never trust computers") double x=2; for(;x>Math.sqrt(x);x=Math.sqrt(x)) i++; System.out.println("your computer is liar it says 2^(2^(-"+i+"))=2^(2^(-"+(i+1)+"))")​
(זה תמיד עוצר...)
 

RichardSmith

New member
התכוונת ככה, כן?

public class math { public static void main(String[] args) { System.out.println("this is why never trust computers"); double x=2; for(int i=0; x>Math.sqrt(x);x=Math.sqrt(x)) { System.out.println("your computer is liar it says 2^(2^(-"+i+"))=2^(2^(-"+(i+1)+"))"); i++; } } }​
 
הממ

דוקא מה שהוא כתב אמור לעבוד. מה שאתה כתבת ידפיס את השורה your computer is a liar בכל איטרציה (גם כאשר החישוב של שורש של x נותן תוצאה שקטנה מ-x).
 

RichardSmith

New member
אבל...

"דוקא מה שהוא כתב אמור לעבוד."... שלו לא עובד, שלי עובד לא נכון, מה עדיף?
 

מתן4ל

New member
תיקון לקוד:

System.out.println("this is why never trust computers") double x=2,i=0; for(;x>Math.sqrt(x);x=Math.sqrt(x)) i++; System.out.println("your computer is liar it says 2^(2^(-"+i+"))=2^(2^(-"+(i+1)+"))")​
ההודעה כמובן תודפס פעם אחת וזה יקרה רק ברגע שהמחשב לא יחשב נכון שורש...
 

מתן4ל

New member
ברור שלא ../images/Emo13.gif

הנקודה היא שהמחשב מצליח לצאת מהלולאה - למרות שמתמתית זה לא אפשרי... (ואכן שכחתי לאתחל את i סורי...
 
משהו כאן לא ברור לי...

בשורות
r = d % 7 r += 7 r = r/7 + 1​
בשורה הראשונה r מקבל ערך בין 0 ל-6, בשורה השניה r גדל לערך בין 7 ל-13 ובשורה השלישית, r יהיה בהכרח 2, לא?
 

RichardSmith

New member
מוזר...

יש אצלי נוסח נכון:
import java.util.Scanner; public class zeller { public static void main(String[] args) { // create a Scanner object to read input from the screen Scanner input = new Scanner( System.in); int m, //month d, //day y, //year c, //century A,B,C,D,r; //help verbs System.out.println("Please master, give me a date!"); System.out.println("state it by month, day, year."); m = input.nextInt(); d = input.nextInt(); y = input.nextInt(); c = y/100; y=y%100; m = (m + 9)% 12 + 1; y = y - m / 11; A=(13*m-1)/5; B=y/4; C=c/4; D = A + B + C + d + y - 2*c; r=D%7; r+=7; r=r%7+1; switch(r){ case 1: System.out.println("It is a Sunday, Sir.");break; case 2: System.out.println("It is a Monday, Sir.");break; case 3: System.out.println("It is a Tuesday, Sir.");break; case 4: System.out.println("It is a Wednesday, Sir.");break; case 5: System.out.println("It is a Thursday, Sir.");break; case 6: System.out.println("It is a Friday, Sir.");break; case 7: System.out.println("It is a Saturday, Sir.");break; } } }​
לא יודע מהיכן יש לי את הנוסח השגוי ההוא...
 

RichardSmith

New member
בהחלט מוזר...

זה קורס תכנות שטותי למדי... הביאו לנו אלגוריתם שצריך להמיר לג'אווה... העיקר לכתוב את התוכנית, לא חשוב לחשוב בדרך.
 

sagima

New member
אהה...

דבר ראשון אני לא מכיר JAVA, אחרי שאמרתי את זה, לא היה יותר קל להכניס את התאריך לאוביקט כלשהו (שאני בטוח שיש בJAVA) שמייצג תאריך ואז לעשות משהו כמו:
date.GetDayOfWeek()​
?
 

RichardSmith

New member
כמובן...

אבל התוכנה באה ללמד פקודות בג'אווה, היום בשבוע לא ממש מעניין.
 

sagima

New member
JAVA זה לחלשים

בוא תעשה לנו הרצאת "מבוא לתיכנות מונחה עצמים"...
 

RichardSmith

New member
למה אני?...

אתה מוזמן לעשות זאת באותה מידה. ג'אוה היא כזו שפת תכנות, בקרוב יהיו תוכנות עם מחלקות שונות וכו'.
 
למעלה