פורסם 2011 באוגוסט 914 שנים יש לי תרגיל שיוצר צורות ז"א מוגדרים לי לכל צורה POSITION ועוד כמה פרמטרים (צבע, גובה וכו'), אני קורא מקובץ טקסט סטרינג של מספרים שהראשון אומר איזו צורה ואחריו הפרמטרים של POSITION, צבע וכו' (לכל פרמטר מספר), המספרים בקובץ הטקסט מופרדים בפסיקים ונכנסים למערך (רק המספרים) ואח"כ אני ממיר את אותו מערך למערך INT ונכנס לפקודת SWITCH שקוראת את המספר הראשון במערך (שמייצג איזו צורה) וככה בונה את הצורה, בתור התחלה לבדיקה יצירתי רק צורה אחת (מלבן) אבל אני לא מוצא מה הבעיה שלי ולמה אני לא מצליח להדפיס אותה, אשמח לעזרהאגב בקובץ הטקסט מופיעים המספרים 1,10,20,2,3,6,7 (1: סוג צורה, 2,3: POSITION - מייצג את הנק' השמאלית העליונה של הצורה, 4: צבע, 5: עובי, 6: רוחב, 7: גובה)using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace ConsoleApplication1{ public class Point { private float x; private float y; public Point(float a, float b) { this.x = a; this.y = b; } public Point() { this.x = 0; this.y = 0; } public Point(Point p) { this.x = p.x; this.y = p.y; } public void SetX(float x) { this.x = x; } public void SetY(float y) { this.y = y; } public void Setpoint(Point p) { this.x = p.x; this.y = p.y; } public float GetX() { return this.x; } public float GetY() { return this.y; } public void Move(float dx, float dy) { this.x += dx; this.y += dy; } } public abstract class Shape { protected Point Position; protected int Color; //1: black, 2: red, 3: blue, 4: yellow protected float BorderWidth; public Shape(Point Position, int Color, float BorderWidth) { this.Position = new Point(Position); this.Color = Color; this.BorderWidth = BorderWidth; } public Shape(Point Position) { this.Position = Position; this.Color = 1; this.BorderWidth = 1; } public virtual string ToString() { return "X: " + Position.GetX() + " " + "Y: " + Position.GetY() + " " + "Color: " + this.Color + " " + "BorderWidth: " + this.BorderWidth; } public void Move(float dx, float dy) { float TempX = this.Position.GetX() + dx; this.Position.SetX(TempX); float TempY = this.Position.GetX() + dy; this.Position.SetX(TempY); } public abstract double Area(); } public class Rectangle : Shape { private int Width; private int Heigth; public Rectangle(Point Position, int Color, float BorderWidth, int Width, int Heigth) : base(Position, Color, BorderWidth) { this.Width = Width; this.Heigth = Heigth; } public Rectangle(Point Position, int Width, int Heigth) : base(Position) { this.Width = Width; this.Heigth = Heigth; } public int RectangleCounter(int RectangleCount) { return RectangleCount; } public override string ToString() { return "Rectangle: \t" + base.ToString() + " " + "Width: " + this.Width + " " + "Heigth: " + this.Heigth; } public void Rotate() { int temp = 0; temp = this.Heigth; this.Heigth = this.Width; this.Width = temp; } public override double Area() { return this.Width * this.Heigth; } } public class Circle : Shape { private float Radius; public Circle(Point Position, int Color, float BorderWidth, int Radius) : base(Position, Color, BorderWidth) { this.Radius = Radius; } public Circle(Point Position, int Radius) : base(Position) { this.Radius = Radius; } public override string ToString() { return "Circle: \t" + base.ToString() + " " + "Radius: " + this.Radius; } public void Expand(float factor) { Radius *= factor; } public override double Area() { return Math.PI * Math.Pow(Radius, 2); } } public class Triangle : Shape { private Point p2; private Point p3; public Triangle(Point Position, Point p2, Point p3, int Color, float BorderWidth) : base(Position, Color, BorderWidth) { this.p2 = p2; this.p3 = p3; } public Triangle(Point Position, Point p2, Point p3) : base(Position) { this.p2 = p2; this.p3 = p3; } public override string ToString() { return "Triangle: \t" + base.ToString(); } public override double Area() { return 0; } } public class Square : Rectangle { private int SquareSide; public Square(Point Position, int Color, float BorderWidth, int SquareSide) : base(Position, Color, BorderWidth, SquareSide, SquareSide) { this.SquareSide = SquareSide; } public override string ToString() { return "Square: \t" + base.ToString(); } public override double Area() { return SquareSide * SquareSide; } } class Program { static void Main(string[] args) { //int count = 0; int RectangleCount = 0; int TriangleCount = 0; int CircleCount = 0; int SquareCount = 0; int OptionInput; //define shapes array Shape[] ShapesArray = new Shape[20]; int ShapesArrayIndex = 0; Console.WriteLine("5. Print Shapes\n"); StreamReader InputTextFile = new StreamReader(@"D:/C#/ConsoleApplication1/Classes/Shape/Input.txt"); string line = InputTextFile.ReadLine(); string[] Parameters = line.Split(','); int[] Numbers = new int[Parameters.Length]; int temp = 0; while (!InputTextFile.EndOfStream) { for (int i = 0; i < Parameters.Length; i++) { temp = int.Parse(Parameters[i]); Numbers[i] = temp; } switch (Numbers[0]) { case 1: //rectangle Point RectanglePoint = new Point(Numbers[1], Numbers[2]); Rectangle Rectangle1 = new Rectangle(RectanglePoint, Numbers[3], Numbers[4], Numbers[5], Numbers[6]); ShapesArray[ShapesArrayIndex] = Rectangle1; //put the shape into array RectangleCount++; break; continue; case 5: //exit continue; default: //choosing of a non-existing number. Console.WriteLine("The Input File Empty."); Console.WriteLine("----------------------\n"); break; } } Console.WriteLine(ShapesArray[0]); Console.ReadLine(); } }}
פורסם 2011 באוגוסט 914 שנים ניסית לדבג step by step את התכנית שלך?נ.ב. כשאתה יוצר StreamReader (ובכלל, כל אובייקט שהוא Disposable) תשתמש ב-using, כלומר:using (StreamReader InputTextFile = new StreamReader(...)) { // code that uses InputTextFile here}זה דואג לזה שהקובץ יסגר אוטומטית כשתסיים לעבוד איתו.
פורסם 2011 באוגוסט 914 שנים מחבר עשיתי BREAKPOINT אבל הוא לא ממש נכנס אליה, איפה הכי טוב לשים אותה ועם מה לעבוד F9,F10 ? מה ההבדלים ?
פורסם 2011 באוגוסט 914 שנים אם עשית breakpoint והתכנית לא עצרה, זה אומר שהוא פשוט לא הגיע ל-breakpoint הזה. שים את ה-breakpoint במקום יותר מוקדם בתכנית (אפילו בתחילת ה-main, כי התכנית שלך די קצרה) ותתקדם משם.לא זוכר מה זה מה (F9 ו-F10), אבל אחד נכנס לתוך פונקציות והאחר לא.
פורסם 2011 באוגוסט 1014 שנים מחבר אוקיי הצלחתי אם הדיבגר, הבעיה שלי היא עכשיו כזאת: יש לי בקובץ טקסט שורה אחד (שמייצגת צורה אחת) אז זה מדפיס לי אותה והכל בסדר, כשאני מכניס עוד שורה אחת של קטסט אני רואה בדיבגר שהתוכנית קוראת את השורה הראשונה, מכניסה את הפרמטרים של הצורה למערך וכו' ושהתוכנית מגיעה שוב ל-WHILE היא מיד יוצאת (אני רואה בדיבגר ב-LINE את השורה שנייה)using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace ConsoleApplication1{ public class Point { private float x; private float y; public Point(float a, float b) { this.x = a; this.y = b; } public Point() { this.x = 0; this.y = 0; } public Point(Point p) { this.x = p.x; this.y = p.y; } public void SetX(float x) { this.x = x; } public void SetY(float y) { this.y = y; } public void Setpoint(Point p) { this.x = p.x; this.y = p.y; } public float GetX() { return this.x; } public float GetY() { return this.y; } public void Move(float dx, float dy) { this.x += dx; this.y += dy; } } public abstract class Shape { protected Point Position; protected int Color; //1: black, 2: red, 3: blue, 4: yellow protected float BorderWidth; public Shape(Point Position, int Color, float BorderWidth) { this.Position = new Point(Position); this.Color = Color; this.BorderWidth = BorderWidth; } public Shape(Point Position) { this.Position = Position; this.Color = 1; this.BorderWidth = 1; } public virtual string ToString() { return "X: " + Position.GetX() + " " + "Y: " + Position.GetY() + " " + "Color: " + this.Color + " " + "BorderWidth: " + this.BorderWidth; } public void Move(float dx, float dy) { float TempX = this.Position.GetX() + dx; this.Position.SetX(TempX); float TempY = this.Position.GetX() + dy; this.Position.SetX(TempY); } public abstract double Area(); } public class Rectangle : Shape { private int Width; private int Heigth; public Rectangle(Point Position, int Color, float BorderWidth, int Width, int Heigth) : base(Position, Color, BorderWidth) { this.Width = Width; this.Heigth = Heigth; } public Rectangle(Point Position, int Width, int Heigth) : base(Position) { this.Width = Width; this.Heigth = Heigth; } public int RectangleCounter(int RectangleCount) { return RectangleCount; } public override string ToString() { return "Rectangle: \t" + base.ToString() + " " + "Width: " + this.Width + " " + "Heigth: " + this.Heigth; } public void Rotate() { int temp = 0; temp = this.Heigth; this.Heigth = this.Width; this.Width = temp; } public override double Area() { return this.Width * this.Heigth; } } public class Circle : Shape { private float Radius; public Circle(Point Position, int Color, float BorderWidth, int Radius) : base(Position, Color, BorderWidth) { this.Radius = Radius; } public Circle(Point Position, int Radius) : base(Position) { this.Radius = Radius; } public override string ToString() { return "Circle: \t" + base.ToString() + " " + "Radius: " + this.Radius; } public void Expand(float factor) { Radius *= factor; } public override double Area() { return Math.PI * Math.Pow(Radius, 2); } } public class Triangle : Shape { private Point p2; private Point p3; public Triangle(Point Position, Point p2, Point p3, int Color, float BorderWidth) : base(Position, Color, BorderWidth) { this.p2 = p2; this.p3 = p3; } public Triangle(Point Position, Point p2, Point p3) : base(Position) { this.p2 = p2; this.p3 = p3; } public override string ToString() { return "Triangle: \t" + base.ToString(); } public override double Area() { return 0; } } public class Square : Rectangle { private int SquareSide; public Square(Point Position, int Color, float BorderWidth, int SquareSide) : base(Position, Color, BorderWidth, SquareSide, SquareSide) { this.SquareSide = SquareSide; } public override string ToString() { return "Square: \t" + base.ToString(); } public override double Area() { return SquareSide * SquareSide; } } class Program { static void Main(string[] args) { int count = 0; int RectangleCount = 0; int TriangleCount = 0; int CircleCount = 0; int SquareCount = 0; //define shapes array Shape[] ShapesArray = new Shape[20]; Console.WriteLine("Please Choose Shape:\n" + "--------------------"); Console.WriteLine("1. Rectangle\n2. Triangle\n3. Circle\n4. Print Shapes\n5. Square\n6. Expand\n7. Rotate\n8. Area"); int ShapesIndex = 0; StreamReader InputTextFile = new StreamReader(@"D:/C#/ConsoleApplication1/Classes/Shape/Input.txt"); string line = InputTextFile.ReadLine(); string[] Parameters = line.Split(','); int temp = 0; while (!InputTextFile.EndOfStream) { line = InputTextFile.ReadLine(); Parameters = line.Split(','); switch (Parameters[0]) { case "1": Point RectanglePoint = new Point(float.Parse(Parameters[1]), float.Parse(Parameters[2])); Rectangle Rectangle1 = new Rectangle(RectanglePoint, int.Parse(Parameters[3]), float.Parse(Parameters[4]), int.Parse(Parameters[5]), int.Parse(Parameters[6])); //put the shape into array ShapesArray[ShapesIndex] = Rectangle1; ShapesIndex++; RectangleCount++; break; default: //choosing of a non-existing number. Console.WriteLine("No Such Shape, Try Again Or Press 4 For Exit."); Console.WriteLine("--------------------------------------------\n"); break; } } //print the shpes array Console.WriteLine("\nThe Array Contain: "); Console.WriteLine("------------------"); for (int i = 0; i < ShapesIndex; i++) { Console.WriteLine(ShapesArray[0].ToString()); } //Console.WriteLine(ShapesArray[0].ToString()); Console.ReadLine(); } }}
פורסם 2011 באוגוסט 1014 שנים יש שתי דרכים יותר פשוטות לקרוא שורות מתוך קובץ:א. להשתמש בפונקציה File.ReadAllLines.ב. להשתמש בלולאה שכאן (בדוגמה השנייה).לא צריך להשתמש ב-EndOfstream.ושוב, חשוב מאוד: כשעובדים עם Stream, ובאופן כללי כל אובייקט שהוא Disposable, חשוב לעשות לו Dispose כשמסיימים לעבוד איתו, או לעטוף את השימוש בו באמצעות using כמו בדוגמה שנתתי קודם (זה אפילו עדיף על Dispose).
פורסם 2011 באוגוסט 1014 שנים מחבר מה ז"א ?בקובץ טקסט יש לי שני שורות של מספרים שמופרדים בפסיקים, את השורה הראשונה התוכנה קוראת ואני מקבל בהדפסה את כל הפרטים הרלונטים.בפקודה line = InputTextFile.ReadLine(); בדיבגר אני שם את הסמן על ה-line (אבל זה שמופיע לפני ה-WHILE) ורואה שהיא מכילה את השורה השנייה בקובץ טקסט אבל אז התוכנה לא נכנסת בכלל ל-WHILE ויוצאת אז השורה השנייה לא נקראת לתוך המערך.
פורסם 2011 באוגוסט 1014 שנים ^אתה טועה. הרצתי את זה בעצמי ומה שאתה אומר ממש לא נכון.קודם כל, זה כן נכנס ל WHILE.איך אתה אומר שזה מדפיס לך את השורה הראשונה אם זה לא נכנס ל WHILE? הרי ב WHILE אתה מכניס את הצורה למערך, אז אם זה לא נכנס לשם איך זה בסוף מדפיס?כמו שאמרו לך, אתה מדלג על השורה הראשונה.מחוץ ל WILE אתה קורא אותה, ומיד אחרי זה בתוך ה WHILE אתה קורא ל LINE את השורה השניה, לפני ששמרת את הצורה ע"פ השורה הראשונה.שים לב שבלולאה שמדפיסה, אתה מדפיס רק את הצורה במקום ה- 0, ולא במקום ה i .בכל מקרה, תסתכל שוב על קובץ הטקסט שלך, נראה לי שאתה מתבלבל בין השורות.
פורסם 2011 באוגוסט 1014 שנים String line = InputTextFile.ReadLine(); string[] Parameters = line.Split(','); int temp = 0; while (!InputTextFile.EndOfStream) { line = InputTextFile.ReadLine();אתה קורא שורה מהקובץ (מחוץ ללולאה), ואז מיד קורא עוד שורה (בתוך הלולאה) בלי לעשות כלום עם הראשונה.
פורסם 2011 באוגוסט 1014 שנים מחבר אגב יש לי בעיה קטנה (ובטח ממש ממש פשוטה) שאני לא מצליח להדפיס את p2 ו-p3 של הצורה Triangle,ניסיתי וניסיתי אבל כנראה אני מפספס משהו ממש פשוט, מה אני אמור לרשום עלמנת להדפיס את 2 נק' האלו ? (בקוד הבא פשוט לא הכנסתי את p2 ו-p3 להדפסה) :using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace ConsoleApplication1{ public class Point { private float x; private float y; public Point(float a, float b) { this.x = a; this.y = b; } public Point() { this.x = 0; this.y = 0; } public Point(Point p) { this.x = p.x; this.y = p.y; } public void SetX(float x) { this.x = x; } public void SetY(float y) { this.y = y; } public void Setpoint(Point p) { this.x = p.x; this.y = p.y; } public float GetX() { return this.x; } public float GetY() { return this.y; } public void Move(float dx, float dy) { this.x += dx; this.y += dy; } } public abstract class Shape { protected Point Position; protected int Color; //1: black, 2: red, 3: blue, 4: yellow protected float BorderWidth; public Shape(Point Position, int Color, float BorderWidth) { this.Position = new Point(Position); this.Color = Color; this.BorderWidth = BorderWidth; } public Shape(Point Position) { this.Position = Position; this.Color = 1; this.BorderWidth = 1; } public virtual string ToString() { return Position.GetX() + "\t" + Position.GetY() + "\t" + this.Color + "\t" + this.BorderWidth + ""; } public void Move(float dx, float dy) { float TempX = this.Position.GetX() + dx; this.Position.SetX(TempX); float TempY = this.Position.GetX() + dy; this.Position.SetX(TempY); } public abstract double Area(); } public class Rectangle : Shape { private int Width; private int Heigth; public Rectangle(Point Position, int Color, float BorderWidth, int Width, int Heigth) : base(Position, Color, BorderWidth) { this.Width = Width; this.Heigth = Heigth; } public Rectangle(Point Position, int Width, int Heigth) : base(Position) { this.Width = Width; this.Heigth = Heigth; } public int RectangleCounter(int RectangleCount) { return RectangleCount; } public override string ToString() { return "Rectangle: \t" + base.ToString() + "\t\t" + this.Width + "\t" + this.Heigth + "\t"; } public void Rotate() { int temp = 0; temp = this.Heigth; this.Heigth = this.Width; this.Width = temp; } public override double Area() { return this.Width * this.Heigth; } } public class Circle : Shape { private float Radius; public Circle(Point Position, int Color, float BorderWidth, int Radius) : base(Position, Color, BorderWidth) { this.Radius = Radius; } public Circle(Point Position, int Radius) : base(Position) { this.Radius = Radius; } public override string ToString() { return "Circle: \t" + base.ToString() + " " + "\t\t\t\t" + this.Radius; } public void Expand(float factor) { Radius *= factor; } public override double Area() { return Math.PI * Math.Pow(Radius, 2); } } public class Triangle : Shape { private Point p2; private Point p3; public Triangle(Point Position, Point p2, Point p3, int Color, float BorderWidth) : base(Position, Color, BorderWidth) { this.p2 = p2; this.p3 = p3; } public Triangle(Point Position, Point p2, Point p3) : base(Position) { this.p2 = p2; this.p3 = p3; } public override string ToString() { return "Triangle: \t" + base.ToString(); } public override double Area() { return 0; } } public class Square : Shape { private int SquareSide; public Square(Point Position, int Color, float BorderWidth, int SquareSide) : base(Position, Color, BorderWidth) { this.SquareSide = SquareSide; } public override string ToString() { return "Square: \t" + base.ToString() + "\t\t\t\t\t" + SquareSide; } public override double Area() { return SquareSide * SquareSide; } } class Program { static void Main(string[] args) { int count = 0; int RectangleCount = 0; int TriangleCount = 0; int CircleCount = 0; int SquareCount = 0; //define shapes array Shape[] ShapesArray = new Shape[20]; Console.WriteLine("Please Choose Shape:\n" + "--------------------"); Console.WriteLine("1. Rectangle\n2. Triangle\n3. Circle\n4. Print Shapes\n5. Square\n6. Expand\n7. Rotate\n8. Area"); int ShapesIndex = 0; StreamReader InputTextFile = new StreamReader(@"D:/C#/ConsoleApplication1/Classes/Shape/Input.txt"); //string line = InputTextFile.ReadLine(); //string[] Parameters = line.Split(','); int temp = 0; while (!InputTextFile.EndOfStream) { string line = InputTextFile.ReadLine(); string[] Parameters = line.Split(','); switch (Parameters[0]) { case "1": //rectangle Point RectanglePoint = new Point(float.Parse(Parameters[1]), float.Parse(Parameters[2])); Rectangle Rectangle1 = new Rectangle(RectanglePoint, int.Parse(Parameters[3]), float.Parse(Parameters[4]), int.Parse(Parameters[5]), int.Parse(Parameters[6])); ShapesArray[ShapesIndex] = Rectangle1; //put the shape into array ShapesIndex++; RectangleCount++; break; case "2": //triangle Point TrianglePoint1 = new Point(float.Parse(Parameters[1]), float.Parse(Parameters[2])); Point TrianglePoint2 = new Point(float.Parse(Parameters[3]), float.Parse(Parameters[4])); Point TrianglePoint3 = new Point(float.Parse(Parameters[5]), float.Parse(Parameters[6])); Triangle Triangle1 = new Triangle (TrianglePoint1, TrianglePoint2, TrianglePoint3, int.Parse(Parameters[7]), float.Parse(Parameters[8])); ShapesArray[ShapesIndex] = Triangle1; //put the shape into array ShapesIndex++; TriangleCount++; break; case "3": //circle Point CirclePoint = new Point(float.Parse(Parameters[1]), float.Parse(Parameters[2])); Circle Circle1 = new Circle(CirclePoint, int.Parse(Parameters[3]), float.Parse(Parameters[4]), int.Parse(Parameters[5])); ShapesArray[ShapesIndex] = Circle1; //put the shape into array ShapesIndex++; CircleCount++; break; case "4": //square Point SquarePoint = new Point(float.Parse(Parameters[1]), float.Parse(Parameters[2])); Square Square1 = new Square(SquarePoint, int.Parse(Parameters[3]), float.Parse(Parameters[4]), int.Parse(Parameters[5])); ShapesArray[ShapesIndex] = Square1; //put the shape into array ShapesIndex++; SquareCount++; break; default: //choosing of a non-existing number. Console.WriteLine("No Such Shape, Try Again Or Press 4 For Exit."); Console.WriteLine("--------------------------------------------\n"); break; } } //print the shpes array Console.WriteLine("\nThe Array Contain: "); Console.WriteLine("------------------"); Console.WriteLine("\t\t" + "X\t" + "Y\t" + "Color\t" + "BorderWidth\t" + "Width\t" + "Heigth\t" + "Radius\t" + "SquareSide\t"); Console.WriteLine("-------------------------------------------------------------------------------------------"); for (int i = 0; i < ShapesIndex; i++) { Console.WriteLine(ShapesArray[i].ToString()); Console.WriteLine("-------------------------------------------------------------------------------------------"); } Console.WriteLine("\nThe Array Contain:\n" + "------------------\n" + "Rectangles: " + RectangleCount + "\n" + "Triangles: " + TriangleCount + "\n" + "Circles: " + CircleCount + "\n" + "Squares: " + SquareCount); Console.ReadLine(); } }}
פורסם 2011 באוגוסט 1014 שנים לא מבין.מה זאת אומרת לא מצליח?? תביא קוד לדוגמא של השורה ש"לא עובדת" לך.למה לא הוספת קוד להדפיס אותם?תדרוס את TOSTRING של POINT, וב TOSTRING של המשלוש, לפני שאתה קורא ל BASE.TOSTRING תשרשר את P2 ו- P3
פורסם 2011 באוגוסט 1014 שנים מחבר בשורה הזו, איפה שיש את ה-P2 ו-p3 אני מקבל ConsoleApplication1.Point10 במקום את המספר public override string ToString() { return "Triangle: \t" + p2.ToString() + p3.ToString() + base.ToString(); }
ארכיון
דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.