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

مشكل في رسم مستطيل وفي معرفة القيمة المرجعة من الدالة داخل الclass

بدأه soufianosse في 27 ديسمبر 2009 · 5 رد · 1,037 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

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

عندي استفسار حول تمرين مطلوب منا عمله وتسليمه للاستاد يوم غد

التمرين بسيط ستفهمونه من الكود

#include <iostream.h> 
#include <windows.h>
#include <conio.h>
#include<iomanip.h>

using namespace std;

class calcule

{      
    private:
            int s;
            int c;

    public:

           void Dessin()
           {               
           SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), FOREGROUND_GREEN);         

           for (int i=0;i<30;i++)

           cout <<"-";
           cout << endl;

               for(int j=0;j<10;j++)  

                if (j==0) 
                {

                cout << "-       Nom de Programme     -" << endl;
                }

               cout<<"-"<<setw(29)<<"-"<<endl; 

                       for(int k=0;k<30;k++)

                       cout<<"-";
                       cout<<endl;
                 }   

           void get_s()
           {
           cout << endl; 
           cout << "Entrer le salaire :" ;
           cin >> s; 
           }

           int compare ()
           {

            if (cin.fail())
            {
                           cout << " erreur" << endl;
                          return 0;
                           }
            else if(s>2000 && s<4000)
            {
                     c = 20;
                     c = (s*c)/100;
                     c = c+s;
                     return c;
                      }
           else if (s>4000 && s<6000)
               {
                     c = 40;
                     c = (s*c)/100;
                     c = c+s;
                     return c;
                   }
                 else if (s>6000 && s<8000)

                 {
                    c = 60;
                    c = (s*c)/100;
                    c = c+s;
                     return c;
                    }
   }

              };

      int  main() 

      { 

       calcule object; 
        object.Dessin();
       object.get_s ();     
       object.compare ();


       cout << "le salaire est :" << object.compare()<< endl<< endl;

       system("PAUSE");

            }

المشكل الدي واجهني هو كيف اقوم برسم المستطيل في وسط الشاشة فوق واكتب وسطه

طبعا انا رسمت المستطيل وكتبت وسطه لكن كما ترون في الصورة

fff2o.jpg

اريده هدا المستطيل ان يكون في الوسط وليس الجانب الايسر

ومشكل الكتابة وسطه كما تلاحضون استعملت في الكود ،

 for(int j=0;j<10;j++)  

                if (j==0) 
                {

                cout << "-       Nom de Programme     -" << endl;
                }

               cout<<"-"<<setw(29)<<"-"<<endl;

عرض المستطيل هو 10 خانات ويطيني فقط 4 او 5

كيف احل هدا المشكل ؟

امر اخر وهو عندما يدخل المستخدم حرفا بدل رقم الدالة Compare ترجع القيمة 0

كيف اعرف اعرف ان هده الدالة ارجعت القيمة صفر من داخل الدالة Main ؟

او يطبع جملة erreur ولا ينفد شيئا بعدها ؟

اقصد هنا

 if (cin.fail())
            {
                           cout << " erreur" << endl;
                          return 0;
                           }

ارجو المساعدة علي تقديم التمرين غدا وكما ترون فمحاولتي قد وضعتها

شكرا

تم تعديل هذه المشاركة بواسطة soufianosse في 27 ديسمبر 2009 في 16:03

#2

هل من جواب ؟

#3

السلام عليكم هذا تعديل اتمنى يكون المطلوب

void Dessin() 	
{ 	
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), FOREGROUND_GREEN);
cout <<setw(20)<<setfill(' ')
<<"-"<<setw(30)
<<setfill('-')<<"-\n"
<<setw(45)<<setfill(' ') 
<<"Nom de Programme\n" 	
<<setw(20)<<setfill(' ')<<"-"
<<setw(30)<<setfill('-')<<"-\n"; 
}

تم تعديل هذه المشاركة بواسطة فهدالشلوي في 27 ديسمبر 2009 في 19:13

tvquran_6.gif

#4

شكرا لك اخي

لكن ليس هدا المطلوب :lol:

الاستاد طلب مستطيل كامل ووسطه كتابة باستعمال الحلقة for :wacko:

تم تعديل هذه المشاركة بواسطة soufianosse في 27 ديسمبر 2009 في 20:08

#5

ربما ردي متأخر

جرب هذا الكود

في الكود ستجد class من مشاركة قديمة للأستاذ هاني أتاسي

/index.php?showtopic=6077

#include <iostream.h> 
#include <windows.h>
#include <conio.h>
#include<iomanip.h>
#include <stdio.h>
//using namespace std;
//****************************************************************************
//
// This program demonstrates how to implement 
//      the next functions in VC++ :
//
//	  void textbackground( COLORS newcolor );	// Set the text background color
//	  void textcolor( COLORS newcolor );		// Set the text foreground color
//	  int  wherex( void );				// Return the current x location of the cursor
//	  int  wherey( void );				// Return the current y location of the cursor
//	  void gotoxy( int x, int y );			// Set the coordinate of the cursor
//
// Programmed by : Hani Atassi (atassi@arabteam2000.com)
//


#include < io.h >
#include < fcntl.h >


/* Standard error macro for reporting API errors */ 
#define PERR(bSuccess, api){if(!(bSuccess))	printf("%s:Error %d from %s on line %dn", __FILE__, GetLastError(), api, __LINE__);}

class MyConsole {
public:
	MyConsole();
	~MyConsole();

	enum COLORS {
		/* dark colors */
		BLACK			= 0,          
		BLUE			= FOREGROUND_BLUE,
		GREEN			= FOREGROUND_GREEN,
		CYAN			= FOREGROUND_BLUE|FOREGROUND_GREEN,
		RED				= FOREGROUND_RED,
		MAGENTA			= FOREGROUND_RED|FOREGROUND_BLUE,
		BROWN			= FOREGROUND_RED|FOREGROUND_GREEN,
		LIGHTGRAY		= FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_BLUE,
		/* light colors */
		DARKGRAY		= FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_BLUE,       
		LIGHTBLUE		= FOREGROUND_INTENSITY|FOREGROUND_BLUE,
		LIGHTGREEN		= FOREGROUND_INTENSITY|FOREGROUND_GREEN,
		LIGHTCYAN		= FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_GREEN,
		LIGHTRED		= FOREGROUND_INTENSITY|FOREGROUND_RED,
		LIGHTMAGENTA	= FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE,
		YELLOW			= FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN,
		WHITE			= FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED
	};

	void clrscr( void );					// Clear the screen

	void textbackground( COLORS newcolor );	// Set the text background color
	void textcolor( COLORS newcolor );		// Set the text foreground color
	int  wherex( void );					// Return the current x location of the cursor
	int  wherey( void );					// Return the current y location of the cursor
	void gotoxy( int x, int y );			// Set the coordinate of the cursor

private:
	WORD	m_CurrentColor;
	HANDLE	m_hConsole; 

	void RedirectStdOut();
};

MyConsole::MyConsole()
{
	m_CurrentColor = 0;

	RedirectStdOut();

	m_hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
}

MyConsole::~MyConsole()
{
	// Do nothing
}

void MyConsole::RedirectStdOut()
{
	int hCrt;
	FILE *hf;

	hCrt = _open_osfhandle(
			 (long) GetStdHandle(STD_OUTPUT_HANDLE),
			 _O_TEXT
		  );
	hf = _fdopen( hCrt, "w" );
	*stdout = *hf;
	setvbuf(stdout, NULL, _IONBF, 0 );
}

void MyConsole::clrscr( void )
{
    COORD coordScreen = { 0, 0 };    /* here's where we'll home the
                                        cursor */ 
    BOOL bSuccess;
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */ 
    DWORD dwConSize;                 /* number of character cells in
                                        the current buffer */ 

    /* get the number of character cells in the current buffer */ 

    bSuccess = GetConsoleScreenBufferInfo( m_hConsole, &csbi );
    PERR( bSuccess, "GetConsoleScreenBufferInfo" );
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

    /* fill the entire screen with blanks */ 

    bSuccess = FillConsoleOutputCharacter( m_hConsole, (TCHAR) ' ',
       dwConSize, coordScreen, &cCharsWritten );
    PERR( bSuccess, "FillConsoleOutputCharacter" );

    /* get the current text attribute */ 

    bSuccess = GetConsoleScreenBufferInfo( m_hConsole, &csbi );
    PERR( bSuccess, "ConsoleScreenBufferInfo" );

    /* now set the buffer's attributes accordingly */ 

    bSuccess = FillConsoleOutputAttribute( m_hConsole, csbi.wAttributes,
       dwConSize, coordScreen, &cCharsWritten );
    PERR( bSuccess, "FillConsoleOutputAttribute" );

    /* put the cursor at (0, 0) */ 

    bSuccess = SetConsoleCursorPosition( m_hConsole, coordScreen );
    PERR( bSuccess, "SetConsoleCursorPosition" );

    return;
}

void MyConsole::textbackground( COLORS __newcolor )
{
	BOOL bSuccess;
	m_CurrentColor &= ~0xf0;
	m_CurrentColor |= __newcolor << 4;

	bSuccess = SetConsoleTextAttribute(m_hConsole, m_CurrentColor);		
    PERR( bSuccess, "SetConsoleTextAttribute" );
}

void MyConsole::textcolor( COLORS __newcolor )
{
	BOOL bSuccess;
	m_CurrentColor &= ~0x0f;
	m_CurrentColor |= __newcolor;

	bSuccess = SetConsoleTextAttribute(m_hConsole, m_CurrentColor);		
    PERR( bSuccess, "SetConsoleTextAttribute" );
}

int  MyConsole::wherex( void )
{
	BOOL bSuccess;
	CONSOLE_SCREEN_BUFFER_INFO cinfo;

	bSuccess = GetConsoleScreenBufferInfo(m_hConsole, &cinfo);
    PERR( bSuccess, "GetConsoleScreenBufferInfo" );

	return cinfo.dwCursorPosition.X;
}

int  MyConsole::wherey( void )
{
	BOOL bSuccess;
	CONSOLE_SCREEN_BUFFER_INFO cinfo;

	bSuccess = GetConsoleScreenBufferInfo(m_hConsole, &cinfo);
    PERR( bSuccess, "GetConsoleScreenBufferInfo" );

	return cinfo.dwCursorPosition.Y;
}

void MyConsole::gotoxy( int x, int y )
{
	BOOL bSuccess;
	COORD pos = {x, y};

	bSuccess = SetConsoleCursorPosition(m_hConsole, pos);
    PERR( bSuccess, "SetConsoleCursorPosition" );
}

//****************************************************************************
class calcule
{          
private:            
	int s;            
	int c; 
	MyConsole cons;
public:                     
	void Dessin()           
		{                          
			cons.clrscr();
			//SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ), FOREGROUND_GREEN); 
			cons.textcolor(MyConsole::GREEN);
			cons.gotoxy(25,cons.wherey());
			for (int i=0;i<30;i++)                     
				cout <<"-";           
			cout << endl; 
			cons.gotoxy(25,cons.wherey());
			for(int j=0;j<10;j++) 
			{ //added
			if (j==0)                
				{                                
					cout << "-       Nom de Programme     -" << endl;
					cons.gotoxy(25,cons.wherey());
                }                               
			cout<<"-"<<setw(29)<<"-"<<endl; 
			cons.gotoxy(25,cons.wherey());
			}//added

			for(int k=0;k<30;k++)                                             
				cout<<"-";                      
			cout<<endl; 
			cons.gotoxy(25,cons.wherey());
		}                         
	void get_s()           
		{           
			cout << endl;            
			cout << "Entrer le salaire :" ;           
			cin >> s;            
		}                    
	int compare ()          
		{                           
			if (cin.fail())            
			{                          
				cout << " erreur" << endl;                         
				return 0;                           
			}           
			else if(s>2000 && s<4000)           
			{                     
				c = 20;                     
				c = (s*c)/100;                    
				c = c+s;                     
				return c;                      
			}           
			else if (s>4000 && s<6000)               
			{                     
				c = 40;                     
				c = (s*c)/100;                    
				c = c+s;                     
				return c;                  
			}                 
			else if (s>6000 && s<8000)                                  
			{                    
				c = 60;                    
				c = (s*c)/100;                   
				c = c+s;                     
				return c;                    
			}   
		}                 
};         
void  main()         
{                            
	calcule object;         
	object.Dessin();       
	object.get_s ();            
	//object.compare ();        //why you call it twice  ?        
   cout << "le salaire est :" << object.compare()<< endl<< endl;             
	system("PAUSE");  
}

في استدعاء الدالة gotoxy اخترت 25 كقيمة لـ x لان شاشة الـ console عادة ما تكون 80x25 . طبعاً هذا يعتمد على الـ TEXTMODE المستخدم لكن في الأغلب هي 80x25 . حيث أن المستطيل الذي تريده بطول 30 فلتوسيطه في الشاشة ستكون نقطه بدايته هي 25 ...

في الدالة compare لم تناقش القيم اقل من او تساوي 2000 ....

باستخدام MyCosole Class يمكنك التغيير في الكود حسب ما تريد ..

تم تعديل هذه المشاركة بواسطة YaSeenTA في 29 ديسمبر 2009 في 09:17

#6

شكرا لك اخي

رغم انه جاء متأخرا فالمهم هو الاستفادة وليس تقديم التمرين للاستاد :blush:

بارك الله فيك

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

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…