צריך הכוונה בתרגיל ב-C# - עמוד 2 - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

צריך הכוונה בתרגיל ב-C#


gshhar

Recommended Posts

בהמשך לאותו התרגיל אני רוצה להוסיף תכונה של MOVE ל-SHAPE ז"א CLASS INTERFACE וש-SHAPE תירש אותה ואז כל הצורות שיורשות מ-SHAPE ירשו גם את ה-MOVE,שבו אני מכניס x ו-y והצורה (כל אחת מהם) תזוז בהתאם ל-x ול-y.

הגדרתי CLASS INTERFACE, הגדרתי את ה-MOVE ב-SHAPE אבל הבעיה שלי מתחילה כשאני רוצה לרשום את המתודת MOVE בכל אחת מהצורות ולא מצליח, כל מה שרשמתי לא הצליח אשמח לעזרה, ה-INTERFACE של ה-MOVE נמצא בתחילת הקוד ונקרא Imove

ומה שניסיתי למממש נמצא בצורה rectangle (מתודת ה-MOVE)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Shape;


namespace ConsoleApplication1
{
public interface Iexpandable
{
void Expand(float factor);
}


public interface Imove
{
void Move(float dx, float dy);
}


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 string ToString()
{
string line = " " + this.x + " " + this.y + " ";
return line;
}
}


public abstract class Shape : Imove
{
protected Point Position;
protected int Color;
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, Iexpandable
{
private float Width;
private float Heigth;


public Rectangle(Point Position, int Color, float BorderWidth, float Width, float 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()
{
float temp = 0;
temp = this.Heigth;
this.Heigth = this.Width;
this.Width = temp;
}


public override double Area()
{
return this.Width * this.Heigth;
}


public void Expand(float factor)
{
this.Width *= factor;
this.Heigth *= factor;
}


public void Move(float dx, float dy)
{

}
}


public class Circle : Shape, Iexpandable
{
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 override double Area()
{
return Math.PI * Math.Pow(Radius, 2);
}


public void Expand(float factor)
{
this.Radius *= factor;
}


public void Move(float dx, float dy)
{

}
}


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 Width, int Heigth)
: base(Position, Color, BorderWidth, Width, Heigth)
{

}


public override string ToString()
{
return "Square: \t" + base.ToString() + "\t\t\t\t\t";
}


public override double Area()
{
return SquareSide * SquareSide;
}
}


class Program
{
static void Main(string[] args)
{
int RectangleCount = 0;
int TriangleCount = 0;
int CircleCount = 0;
int SquareCount = 0;
int[] Numbers = new int[20];


//define shapes array
Shape[] ShapesArray = new Shape[20];


Console.WriteLine("1. Print All Shapes\n2. Move All Shapes");
int ShapesIndex = 0;
StreamReader InputTextFile = new StreamReader(@"D:/C#/ConsoleApplication1/Classes/Shape/Input.txt");


while (!InputTextFile.EndOfStream)
{
string line = InputTextFile.ReadLine();
string[] Parameters = line.Split(',');


for (int i = 0; i < Parameters.Length; i++)
{
Numbers[i] = int.Parse(Parameters[i]);
}


switch (Numbers[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]), int.Parse(Parameters[6]));
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;
}
}


int Input = int.Parse(Console.ReadLine());


switch (Input)
{
case 1:


//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");
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);


break;


case 2: //move all shapes















default:
break;
}


Console.WriteLine("move");


Point p1 = new Point(10,20);
Rectangle r1 = new Rectangle(p1, 1, 2, 3, 4);
Console.WriteLine(r1.ToString());
r1.Move(2, 2);
Console.WriteLine("After Move: " + r1.ToString());


Console.ReadLine();
}
}
}

קישור לתוכן
שתף באתרים אחרים

את הפונקציה MOVE אתה ממש במחלקה SHAPE, נכון?

נוסף על כך, אתה רוצה לכתוב MOVE שונה לצורות מסויימות שיורשות מ SHAPE, נכון?

אם יש פונקציה שאתה רוצה לשכתב אותה / לדרוס אותה , כדי שתוכל לבצע זאת היא חייבת להיות מוגדרת VIRTUAL.

אין שום קשר בין VIRTUAL לערך המוחזר מפונקציה...

חוץ מזה, למה אתה רוצה לממש את MOVE גם ב SHAPE וגם ב RECTANGLE אם הוא יורש את ה MOVE מ SHAPE???

קישור לתוכן
שתף באתרים אחרים

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

האם לעשות לו MOVE משלו ?

קישור לתוכן
שתף באתרים אחרים

אז תעמיס/תדרוס/תשכתב (קרא לזה איך בא לך) את הפונקציה SHAPE שבמחלקה של המשולש. בשביל זה אתה חייב להגדיר את הפונקציה VIRTUAL ב SHAPE (אם לא עובד תנסה להגדיר גם ב INTERFACE שתהיה VIRTUAL).

בהעמסה יש לך שתי אופציות. או לממש את כל ההזזה מחדש, או להזיז רק את הנקודה החסרה, ולאחר מכן לקרוא ל BASE.MOVE שיקרא ל MOVE של SHAPE שמזיז את שאר שתי הנקודות.

קישור לתוכן
שתף באתרים אחרים

תודה הסתדרתי בסופו של דבר, אגב אני מנסה להוסיף עוד כמה אפשרויות לתפריט בהתחלה, הדפסה של כל הצורות, הזזה וכו' ואחרי ההדפסה ה-SWITCH לא ממשיך אלא יוצא החוצה והתוכנה מסתיימת, הוספתי רק את הקוד של ה-MAIN:

 class Program
{
static void Main(string[] args)
{
int RectangleCount = 0;
int TriangleCount = 0;
int CircleCount = 0;
int SquareCount = 0;
int[] Numbers = new int[20];


//define shapes array
Shape[] ShapesArray = new Shape[20];


Console.WriteLine("1. Print All Shapes\n2. Move All Shapes\n0. Exit");
int ShapesIndex = 0;
StreamReader InputTextFile = new StreamReader(@"D:/C#/ConsoleApplication1/Classes/Shape/Input.txt");


while (!InputTextFile.EndOfStream)
{
string line = InputTextFile.ReadLine();
string[] Parameters = line.Split(',');


for (int i = 0; i < Parameters.Length; i++)
{
Numbers[i] = int.Parse(Parameters[i]);
}


switch (Numbers[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]), int.Parse(Parameters[6]));
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;
}
}


int Input = int.Parse(Console.ReadLine());


switch (Input)
{
case 1:


//print the shpes array
Console.WriteLine("\nThe Array Contain: ");
Console.WriteLine("------------------");
Console.WriteLine("\t\t" + "X1\t" + "Y1\t" + "X2\t" + "Y2\t" + "X3\t" + "Y3\t" + "Color\t" + "BorWid\t" + "Width\t" + "Heigth\t" + "Radius\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);

break;


case 2: //move all shapes


Console.Write("Enter X: " + int.Parse(Console.ReadLine()));
Console.Write("Enter X: " + int.Parse(Console.ReadLine()));
break;


case 0: //exit
return;


default:
break;
}


Input = int.Parse(Console.ReadLine());
Console.ReadLine();
}
}
}

קישור לתוכן
שתף באתרים אחרים

נראה לי אני אפרוש מהת'רד הזה, זה מרגיש כמו שיעור פרטי...

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

יש לך DEBUG בויזואל סטודיו אתה לא צריך אותי.

--עריכה--

למען הסדר הטוב (בתקווה שעדיין תפנים את מה שאמרתי למעלה)

יש לך בעיה - אתה טוען שזה יוצא מהלולאה - משמע - התנאי של הלולאה יוצא FALSE - משמע הגעת לסוף הקובץ.

בדקת שוב את קובץ הטקסט לוודא שיש שם לפחות 2 שורות?

קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

×
  • צור חדש...