الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

ارجوا المساعده

مغلق
بدأه انثى خجوله في 14 فبراير 2012 · 10 رد · 636 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

عندي كتابة كود

السؤال

The coordinates of any point in a rectangular grid are two integers. The X-coordinate increases going East along the X-axis, and the Y-coordinate increases going North along the Y-axis.

You are to implement a class Point subject to the following guidelines:

a) Define data members ( X, and Y) that represent a coordinate for any point

b) Define the following member functions:

void moveNorth(); void moveSouth(); void moveEast(); void moveWest(); The four functions move the current Point object one unit length in the specified direction. For example if the current Point is at the coordinates X=3, Y=2, then a call to moveEast() will update the coordinates to X=4, Y=2.

c) The Point class should implement also the following operation:

int distance (Point p); The function computes the distance between the current points and P. The distance is computed by walking along horizontal and vertical lines in the grid. For example, if the current Point is at the coordinates X=3, Y=2, and the Point p is at the coordinates X=-2, Y=7, then the distance will be 10: we need to walk Math.abs(3-(-2)) = 5 units along the horizontal axis, and Math.abs(2-7) = 5 units along the vertical axis.

d) In the main, implement the below menu:

If the user chooses choice 6, the program should ask for the X and Y coordinates of the new point in order to compute the distance

الحل تبعي يطلع فيه ايرور

الحل

#include <iostream>

#include <string>

using namespace std;

class Point implements PointI {

private:

int xcoord, ycoord;

public:

Point (int x, int y) { xcoord=x; ycoord=y; }

int getX () { return xcoord; }

int getY () { return ycoord; }

void moveNorth () { ycoord++; }

void moveSouth () { ycoord--; }

void moveEast () { xcoord++; }

void moveWest () { xcoord--; }

int distance (Point p) {

int px = p.getX();

int py = p.getY();

int distance = 0;

distance = distance + Math.abs(xcoord - px);

distance = distance + Math.abs(ycoord - py);

return distance;

}

String toString () {

return "X= " + xcoord + ", Y= " + ycoord;

}

}

#2

تفضلي أختي الكود السابق فيه بعض أخطاء الـ syntax

#include <iostream>
#include <string>
#include <math.h>
using namespace std;
class Point
{
private:
    int xcoord, ycoord;

public:
    Point (int x, int y)
    {
        xcoord=x;
        ycoord=y;
    }

    int getX ()
    {
        return xcoord;
    }
    int getY ()
    {
        return ycoord;
    }

    void moveNorth ()
    {
        ycoord++;
    }
    void moveSouth ()
    {
        ycoord--;
    }
    void moveEast ()
    {
        xcoord++;
    }
    void moveWest ()
    {
        xcoord--;
    }

    int distance (Point p)
    {
        int px = p.getX();
        int py = p.getY();
        int distance = 0;
        distance = distance + abs(xcoord - px);
        distance = distance + abs(ycoord - py);
        return distance;
    }    
};

تم تعديل هذه المشاركة بواسطة Mohammad Walid في 14 فبراير 2012 في 22:32

#3
اقتباس
تفضلي أختي الكود السابق فيه بعض أخطاء الـ syntax

أين الدالة tostring العضو فى الفئة point

post-15367-027053900%201340345097.gifpost-15367-087183200%201340344597.gif
post-247365-0-10080400-1379403554_thumb.

#4

السلام عليكم

مرحبا إخوتي :)

القاعدة الأولى من قواعد طرح المشاركات تقول :

اللغة العربيه هى اساس المشاركه لذا كتابتك لموضوع بأى لغة غير العربيه سيتم غلقه مهما كانت درجة اهميته.

إذا فالموضوع مُخالف !!

أظن أن المشاركة في هذا النوع من المواضيع يُساعد المخالفين على التمادي في الأخطاء المتكررة التي يقعون فيها في كل مرة ....

بالتوفيق.

#5
محمد ابوزايد كتب:

أين الدالة tostring العضو فى الفئة point

سقطت على الطريق من جهازي إلى المنتدى :)

khaled_prg كتب:

السلام عليكم

مرحبا إخوتي :)

القاعدة الأولى من قواعد طرح المشاركات تقول :

اللغة العربيه هى اساس المشاركه لذا كتابتك لموضوع بأى لغة غير العربيه سيتم غلقه مهما كانت درجة اهميته.

إذا فالموضوع مُخالف !!

أظن أن المشاركة في هذا النوع من المواضيع يُساعد المخالفين على التمادي في الأخطاء المتكررة التي يقعون فيها في كل مرة ....

بالتوفيق.

من طرح الموضوع هو المخالف و ليس نحن ؟؟ أليس كذلك أخي ؟

#6
اقتباس
من طرح الموضوع هو المخالف و ليس نحن ؟؟ أليس كذلك أخي ؟

نعم هو كذلك :)

و لكن يجب التعامل مع مخالفي القانون بشيئ من الصرامة حتى ينتبهوا و يحسوا بأنهم تجاوزوا الحدود.

على الأقل لا نشارك في المواضيع المخالفة و نتركها للمشرف لكي يغلقها. بهذا نستطيع الحفاظ على سمعة القانون و تنظيم المواضيع.

أليس كذلك أخي ؟

تم تعديل هذه المشاركة بواسطة khaled_prg في 15 فبراير 2012 في 00:08

#7
khaled_prg كتب:

نعم هو كذلك :)

و لكن يجب التعامل مع مخالفي القانون بشيئ من الصرامة حتى ينتبهوا و يحسوا بأنهم تجاوزوا الحدود.

على الأقل لا نشارك في المواضيع المخالفة و نتركها للمشرف لكي يغلقها. بهذا نستطيع الحفاظ على سمعة القانون و تنظيم المواضيع.

أليس كذلك أخي ؟

في المرة القادمة بإذن الله

#9
khaled_prg كتب:

بارك الله فيك أخي :), دمت ذخرا للمنتدى

و فيك أخي العزيز

#10

الكود بعد تعديل بسيط وعذرا القليل من التنظيم لمساعدة اخوانى المبرمجين للأضافة:

/* 
 * File:   main.cpp
 * Author: Mohammed Abu Zayed.
 * Created on February 15, 2012, 8:15 AM
 */

/*
The coordinates of any point in a rectangular grid are two integers.
The X-coordinate increases going East along the X-axis,
and the Y-coordinate increases going North along the Y-axis.
You are to implement a class Point subject to the following guidelines:
        a) Define data members ( X, and Y) that represent a coordinate for any point
        b) Define the following member functions:
                void moveNorth();
                void moveSouth();
                void moveEast();
                void moveWest();
                    The four functions move the current Point object one unit length in
                    the specified direction.
                    For example if the current Point is at the coordinates X=3, Y=2,
                    then a call to moveEast() will update the coordinates to X=4, Y=2.

        c) The Point class should implement also the following operation:
                int distance (Point p);
                        The function computes the distance between the current points and P.
                        The distance is computed by walking along horizontal and vertical lines
                        in the grid.
                 For example, if the current Point is at the coordinates X=3, Y=2,
                 and the Point p is at the coordinates X=-2, Y=7,
                 then the distance will be 10: we need to walk Math.abs(3-(-2)) = 5
                 units along the horizontal axis,
                 and Math.abs(2-7) = 5 units along the vertical axis.

        d) In the main, implement the below menu:
                If the user chooses choice 6,
                the program should ask for the X and Y coordinates of
                the new point in order to compute the distance

 */

#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>

using namespace std;

class Point // _implements_PointI
{
private:
    int xcoord, ycoord;
public:

    Point()
    {
    } // default constractor

    Point(int x = 0, int y = 0)
    {
        xcoord = x;
        ycoord = y;
    }

    int getX()
    {
        return xcoord;
    }

    int getY()
    {
        return ycoord;
    }

    void moveNorth()
    {
        ycoord++;
    }

    void moveSouth()
    {
        ycoord--;
    }

    void moveEast()
    {
        xcoord++;
    }

    void moveWest()
    {
        xcoord--;
    }

    int distance(Point p)
    {
        int px = p.getX();
        int py = p.getY();
        int distance = 0;
        distance = distance + abs(xcoord - px);
        distance = distance + abs(ycoord - py);
        return distance;
    }

    string ToString()
    {
        std::stringstream out;
        out << "X= ";
        out << xcoord;
        out << ", Y= ";
        out << ycoord;
        return out.str();
    }
};

int main()
{

    Point p1(4, 5);
    cout << p1.ToString();
    return 0;
}

المخرجات:

X= 4, Y= 5
RUN SUCCESSFUL (total time: 129ms)

تحياتى العطرة والله ولى التوفيق ...

post-15367-027053900%201340345097.gifpost-15367-087183200%201340344597.gif
post-247365-0-10080400-1379403554_thumb.

#11

بسم الله الرحمن الرحيم

السلام عليكم ورحمة الله وبركاته..

اخوتي المبرمجين بارك الله في جهدكم ..

ماذا لو أضفنا هذه القائمة على نفس البرنامج في الـ main وطلبنا من المستخدم الاختيار.

:In the main, implement the below menu

Enter new point with X and Y coordinates(1.

2) Move point north.

3) Move point south.

4) Move point east.

5) Move point west.

6) Compute distance.

7) Exit

.If the user chooses choice 6, the program should ask for the X and Y coordinates of the new point in order to compute the distance

ملاحظة:

.Don’t forget to define the Constructor that initializes all data members to zero, Sets, and gets functions for each data member

تقبلوا تحياتي...

هذا الموضوع مغلق.

مواضيع مشابهة