output format......

Hi all,
I dont know if I am in the right section or not....any way, let's try my lock ..

I wanna write a program that compute a formula which depent at the value I will enter.....  this is too easy but I want the output be as vector on the x-axis & y-axis ... how I can do this format .


Thanks alot
Reta


Answer this question

output format......

  • DRooD

    Hi,Reta.

    Will you put your output to console or windows (with graphics) I'm not sure what you want.


  • urnabc

    +123456789012345678901234567890123456789+1234567890123456789012345678901234567+
    1                                       |                                     |
    2                                       |                                     |
    3       *****                           |      ******                         |
    4     **    ***                         |     **    **                        |
    5    **       **                        |   **       **                       |
    6   **         **                       |   *          *                      |
    7  **           *                       |  *           **                     |
    8  *             *                      | **            **                    |
    9 *              **                     |**              *                    |
    0**               **                    |*                *                   |
    **                 *                    *                 **                  |
    *-------------------*------------------**------------------*------------------+
    3                   **                 *|                   *                 *
    4                    *                * |                   **               **
    5                     *              ** |                    **              *|
    6                     **            **  |                     *             * |
    7                      **           *   |                      *           ** |
    8                       *          *    |                      **         **  |
    9                        **       **    |                       **       **   |
    0                         **    **      |                        ***    **    |
    1                          ******       |                          *****      |
    2                                       |                                     |
    +---------------------------------------+-------------------------------------+



    IdeaHow about this

    Its formated by my class CCharacterDraw. Code is below.

    //CharacterDraw.h

    #pragma once

    class CCharacterDraw
    {
    public:
     CCharacterDraw(void);
     ~CCharacterDraw(void);
    protected:
     char** m_DrawArea;
     int m_Width;
     int m_Height;
     double m_dWidth;
     double m_dHeight;
     double m_dOrgX;
     double m_dOrgY;
     double m_dRatioX;
     double m_dRatioY;

     bool m_bDrawBorder;
     double m_dCurrentX;
     double m_dCurrentY;

    protected:
     void Border(void);

    public:
     int Draw(void);
     int TextOut(double x, double y, const char *szText, int nCount = -1);
     char PutPixel(double x, double y);
     int MoveTo(double x, double y);
     int LineTo(double x, double y);
     void VLine(double x);
     void HLine(double y);

     bool LPToDP(double dX, double dY, int *pX, int *pY = NULL);
     bool DPToLP(int dX, int dY, double *pX, double *pY = NULL);
     inline bool SetViewportExt(double cx, double cy)
     {
      m_dWidth = cx;
      m_dHeight = cy;
      m_dRatioX = (double)m_Width / cx;
      m_dRatioY = (double)m_Height / cy;
      return true;
     };
     inline bool SetViewportOrg(double x, double y)
     {
      m_dOrgX = x;
      m_dOrgY = y;
      return true;
     };
     inline bool SetDrawBorder(bool bDrawBorder = true)
     {
      bool bOld = m_bDrawBorder;
      m_bDrawBorder = bDrawBorder;
      return bOld;
     };
    };


    //CharacterDraw.cpp

    #include <iostream>
    #include ".\characterdraw.h"
    using namespace std;

    CCharacterDraw::CCharacterDraw(void)
    : m_DrawArea(NULL)
    , m_Width(79)  //Default screen width is 80 character, the last one if for carriage-return char.
    , m_Height(24)  //Default screen height is 25 line, the last one is for "Press any key to continue" message.
    , m_dWidth(100)  //Default logical width.
    , m_dHeight(100) //Default logical height.
    , m_dOrgX(0)  //Default origin x.
    , m_dOrgY(0)  //Default origin y.
    , m_bDrawBorder(true) //Will show the border
    , m_dCurrentX(0) //Current position x.
    , m_dCurrentY(0) //Current position y.
    {
     m_dRatioX = (double)m_Width / m_dWidth;  //For converting between logical unit and device unit.
     m_dRatioY = (double)m_Height / m_dHeight;

     m_DrawArea = new char*[m_Height]; //The screen is filled by this string array.
     if (m_DrawArea)
     {
      for (int i = 0; i < m_Height; i++)
      {
       m_DrawAreaIdea = new char[m_Width + 1]; //Width plus one is for the null char.
       if (!m_DrawAreaIdea)
       {
        while (i)
        {
         delete []m_DrawArea[--i];
         m_DrawAreaIdea = NULL;
        }
        break;
       }
       m_DrawAreaIdea[m_Width] = 0; //The string is null-terminated.
       memset(m_DrawAreaIdea, ' ', m_Width * sizeof(char)); //Empty the screen buffer.
      }
     }
    }

    CCharacterDraw::~CCharacterDraw(void)
    {
     if (m_DrawArea)
     {
      for (int i = 0; i < m_Height; i++)
      {
       delete []m_DrawAreaIdea;
       m_DrawAreaIdea = NULL;
      }
      delete m_DrawArea;
      m_DrawArea = NULL;
     }
    }

    //Output the screen buffer
    int CCharacterDraw::Draw(void)
    {
     if (!m_DrawArea)
      return 0;

     if (m_bDrawBorder) //Will show the border
      Border();

     for (int i = 0; i < m_Height; i++)
     {
      cout << m_DrawAreaIdea << endl;
     }

     return 1;
    }

    //TextOut function, the coordinate is double type in logical unit.
    //Return the outputed character count.
    int CCharacterDraw::TextOut([in] double x, [in] double y, [in] const char *szText, [in] int nCount)
    {
     int nX, nY;
     LPToDP(x, y, &nX, &nY);

     if ((nX >= m_Width) || (nY >= m_Height) || (nY < 0))
      return 0;

     char *pString = (char*)szText;

     if (nCount == -1)
      nCount = (int)strlen(szText);

     if (nX < 0)
     {
      if ((nX + nCount) < 0)
       return 0;
      else
      {
       pString -= nX;
       nCount += nX;
      }
     }

     if ((nX + nCount) > m_Width)
     {
      nCount = m_Width - nX;
     }

     int i;
     for (i = nX; i < nX + nCount; i++)
     {
      m_DrawArea[nY]Idea = pString[i - nX];
     }

     return i;
    }

    //PutPixel function, the coordinate is double type in logical unit.
    //Return the old character.
    char CCharacterDraw::PutPixel([in] double x, [in] double y)
    {
     int nX, nY;
     LPToDP(x, y, &nX, &nY);

     if (!m_DrawArea || nX < 0 || nX >= m_Width || nY < 0 || nY >= m_Height)
      return 0;

     char ch = m_DrawArea[nY][nX];
     m_DrawArea[nY][nX] = '*';

     return ch;
    }

    //VLine function, the coordinate is double type in logical unit.
    //Draw a vertical line at x position.
    void CCharacterDraw::VLine([in] double x)
    {
     int nX;
     LPToDP(x, 0, &nX, NULL);

     if (!m_DrawArea || nX < 0 || nX >= m_Width)
      return;
     for (int i = 0; i < m_Height; i++)
     {
      if (m_DrawAreaIdea[nX] == '-')
       m_DrawAreaIdea[nX] = '+';
      else
       m_DrawAreaIdea[nX] = '|';
     }
    }

    //HLine function, the coordinate is double type in logical unit.
    //Draw a horizontal line at y position.
    void CCharacterDraw::HLine([in] double y)
    {
     int nY;
     LPToDP(0, y, NULL, &nY);

     if (!m_DrawArea || nY < 0 || nY >= m_Height)
      return;
     for (int i = 0; i < m_Width; i++)
     {
      if (m_DrawArea[nY]Idea == '|')
       m_DrawArea[nY]Idea = '+';
      else
       m_DrawArea[nY]Idea = '-';
     }
    }

    //LPToDP function converts logical coordinates into device coordinates.
    //The out coordinates is stored in pX and pY with int type.
    bool CCharacterDraw::LPToDP([in] double dX, [in] double dY, [out] int *pX, [out] int *pY)
    {
     if (pX)
      *pX = int((dX + m_dOrgX) * m_dRatioX + 0.5);
     if (pY)
      *pY = int((dY + m_dOrgY) * m_dRatioY + 0.5);

     return true;
    }

    //DPToLP function converts device coordinates into logical coordinates.
    //The out coordinates is stored in pX and pY with double type.
    bool CCharacterDraw::DPToLP(/*[in] */int nX, /*[in] */int nY, /*[out] */double *pX, /*[out] */double *pY)
    {
     if (pX)
      *pX = (double)nX / m_dRatioX - m_dOrgX;
     if (pY)
      *pY = (double)nY / m_dRatioY - m_dOrgY;

     return true;
    }

    //Border fuction fill the screen border by characters.
    void CCharacterDraw::Border(void)
    {
     if (!m_DrawArea)
      return;

     int i;
     for (i = 1; i < m_Width - 1; i++)
     {
      if (m_DrawArea[0]Idea == ' ')
       m_DrawArea[0]Idea = '0' + i % 10;
      else if (m_DrawArea[0]Idea == '|')
       m_DrawArea[0]Idea = '+';
      if (m_DrawArea[m_Height - 1]Idea == ' ')
       m_DrawArea[m_Height - 1]Idea = '-';
      else if (m_DrawArea[m_Height - 1]Idea == '|')
       m_DrawArea[m_Height - 1]Idea = '+';
     }
     for (i = 1; i < m_Height - 1; i++)
     {
      if (m_DrawAreaIdea[0] == ' ')
       m_DrawAreaIdea[0] = '0' + i % 10;
      else if (m_DrawAreaIdea[0] == '-')
       m_DrawAreaIdea[0] = '+';
      if (m_DrawAreaIdea[m_Width - 1] == ' ')
       m_DrawAreaIdea[m_Width - 1] = '|';
      else if (m_DrawAreaIdea[m_Width - 1] == '-')
       m_DrawAreaIdea[m_Width - 1] = '+';
     }
     if (m_DrawArea[0][0] == ' ')
      m_DrawArea[0][0] = '+';
     if (m_DrawArea[0][m_Width - 1] == ' ')
      m_DrawArea[0][m_Width - 1] = '+';
     if (m_DrawArea[m_Height - 1][0] == ' ')
      m_DrawArea[m_Height - 1][0] = '+';
     if (m_DrawArea[m_Height - 1][m_Width - 1] == ' ')
      m_DrawArea[m_Height - 1][m_Width - 1] = '+';
    }

    //MoveTo function moves the current position to the point specified by x and y.
    int CCharacterDraw::MoveTo([in] double x, [in] double y)
    {
     m_dCurrentX = x;
     m_dCurrentY = y;

     return 1;
    }

    //LineTo function draws a line from the current position up to the specified point.
    //If LineTo succeeds, the current position is set to the specified ending point.
    int CCharacterDraw::LineTo(/*[in] */double x, /*[in] */double y)
    {
     if (!m_DrawArea)
      return 0;

     int nX, nY, nCurX, nCurY;
     LPToDP(m_dCurrentX, m_dCurrentY, &nCurX, &nCurY);
     LPToDP(x, y, &nX, &nY);

     if (nX == nCurX && nY == nCurY)
     {
      PutPixel(x, y);
      MoveTo(x, y);
      return 1;
     }

     if (nX < nCurX)
     {
      int nTemp = nX;
      nX = nCurX;
      nCurX = nTemp;
     }
     if (nCurX < 0)
      nCurX = 0;
     if (nX >= m_Width)
      nX = m_Width - 1;

     if (nCurX >=0 && nCurX < m_Width && nCurY >= 0 && nCurY < m_Height)
      m_DrawArea[nCurY][nCurX] = '*';

     double dSlope = (y - m_dCurrentY) / (x - m_dCurrentX);

     double dCurX, dRightX;
     DPToLP(nCurX, 0, &dCurX, NULL);
     DPToLP(nX, 0, &dRightX, NULL);
     double dY;
     double dOffsetX = 1 / m_dRatioY * (x - m_dCurrentX) / (y - m_dCurrentY);
     if (dOffsetX < 0)
      dOffsetX = -dOffsetX;
     if (dOffsetX > 1)
      dOffsetX = 1;
     for (double dX = dCurX; dX < dRightX; dX += dOffsetX)
     {
      dY = (dX - m_dCurrentX) * dSlope + m_dCurrentY;
      LPToDP(dX, dY, &nX, &nY);
      if (nY >= 0 && nY < m_Height)
       m_DrawArea[nY][nX] = '*';
     }
     MoveTo(x, y);

     return 1;
    }


    You can use this class as this sample code:

    //SinMain.cpp

    #include <iostream>
    #include <valarray>

    #include ".\SinMain.h"
    #include ".\characterdraw.h"

    using namespace std;

    const double PI = 3.14159265;

    main()
    {
     CCharacterDraw cDraw;

     cDraw.SetViewportExt(4 * PI, -2.6);
     cDraw.SetViewportOrg(2 * PI, -1.3);
     cDraw.HLine(0);
     cDraw.VLine(0);

     double x, y;
     cDraw.MoveTo(-2 * PI, 0);
     for (x = -2 * PI; x < 2 * PI; x += PI / 100)
     {
      y = sin(x);
      cDraw.LineTo(x, y);
      //cDraw.PutPixel(x, y); //Two method to get the output
     }

     cDraw.SetDrawBorder(true);
     cDraw.Draw();
    }

    I designed this class just like the MFC class CDC's behavior. So, it's easy for you to move your drawing code to GDI, except the logical unit is double type here.




  • alemayehu

    OH !!!!

    I do not know what I say!!

    All these lines in order to sovle it!!! I tried and my code is extremly different from yours .... but the last one is the best..

    Good job Macks ... I can not describe my feeling .

    Thanks a lot Fire

    My Regards
    Big Smile

  • Timothy J. Suhr

    Thanks Ayman about your responce ...
    all what you wrote is too easy but let me be more specific .
    I have a sine wave formula and I must show the output as a signal
    so I must print the x-axis & y-axis and show the signal as time domain plotting (the time determine the value in the x-axis and the value which I calculated from the formula-time function- determine the y-axis and this display as a dot or what ever you want )
    s(t)=A sin (2π f t + )  >>>>>> formula
    t: time        A: amplitude        f: frequency       : phase which is equal to zero
    A and f are constants

    one thing more   >>>>>>> it is time function so I must feel the signal moving and you have a choise to display the x-axis only in the middle of your output screen , +ve value will be in the right and the -ve value will be at the left...
    the scale is very important in your output..

    Many Thanks for youe patient
    Reta

    < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />                                                                                                                                       

     

     

     

     

     

                                                                                                 

     

    < xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />< xml:namespace prefix = w ns = "urn:schemas-microsoft-com:office:word" /> 

                                                                      


  • AndyjW1

    I just hope thai I can help you.

  • Ridge

     OK......I will try to drew what I mean    

    < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 

     

     

     

                         

                      *  *

                   *        *

                *             *

              *                 *

    ------*-------------*----------------------

                                   *               *

                                    *             *

                                     *           *

                                       *       *

                                           *



                                                                    |

                                                                    |       *

                                                                    |               *

                                                                    |                    *  

                                                                    |                      *

                                                                    |                   *

                                                                    |            *

                                                                    |     *

                                                                   *

                                                            *      |

                                                    *              |

                                              *                    |

                                           *                       |

                                              *                    | 

                                                     *             |

                                                               *   |

                                                                    | 

    I want to show one of these outpot &
    .  This is only one cycle and there is no need to make y-axis visible
    The only problem that I faced is the formatting , how could I scale these point in the output
    hope I clearify all things. 


  • juunas

    Oh, my god, I make a mistake.

    the code is displayed error by the emoticons icon. The code like these:

    m_DrawArea[nY]Idea = pString[i - nX];

    should be

    m_DrawArea[nY][ i ] = pString[i - nX];



  • hauptman

    Please be more specific.

    Give an example of what you want to see as an output Do you just want to see the value of x is blahblah and the value of Y is blahblah or do you want to see  something like (value of x,value of y).

    If you want the second output, here is a small sample that displays the out as (x,y).

    #include <iostream>
    using namespace std;
    int main()
    {
     //read your values
     //calculate the values of x & y
     //assume the below value of x & y;
     int x=8;
     int y=10;
     cout<<"The solution is ("<<x<<","<<y<<")";
    }


    The output will be something like:
    The solution is (8,10)

    If  that is the solution you wanted or if this is part of a school assignment, I would really suggest you go reading more about the C++ language.

    Thanks,
      Ayman Shoukry
      VC++ Team



  • output format......