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

برنامج يعمل اتحاد وتقاطع

مغلق
بدأه مبرمجة ++C في 4 نوفمبر 2004 · 1 رد · 526 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

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

لا اعلم ماهو رغم صحة البرنامج

// Chapter 7 of C++ How to Program
// integerset.cpp
#include <iostream> 

using std::cout; 
using std::cin; 

#include <iomanip> 

#include <new>

using std::setw; 

#include "integerset.h"

/* Write include directive for integerSet.h here */

// constructor
IntegerSet::IntegerSet( int s )
{
   size = s;
   set = new int[ size ];
   emptySet();
   /* write call to emptySet */

} // end class IntegerSet constructor

// copy constructor
IntegerSet::IntegerSet( const IntegerSet &init )
{
   size = init.size;
   set = new int[ size ];
   /* write statement to allocate sufficient memory */
   emptySet();

   for ( int i = 0; i < size; i++ )
   set [ i ] = init.set[ i ] ;
      /* write statement to copy elements of init */

} // end copy constructor

void IntegerSet::emptySet()
{
	for ( int i = 0; i < size; i++)
  set[ i ] = 0;
}
/* write a definition for emptySet */

// input set
void IntegerSet::inputSet()
{
   int number;

   // input set information
   do {
      cout << "Enter an element (-1 to end): ";
      cin >> number;

      // check number first
      if ( validEntry( number ) )
         set[ number ] = 1;

      else if ( number != -1 )
         cout << "Invalid Element\n";

   } while ( number != -1 );

   cout << "Entry complete\n";

} // end function inputSet

// print the set 
void IntegerSet::setPrint() const
{
   int x = 1;
   bool empty = true;  // assume set is empty
   
   cout << '{';

   for ( int u = 0; u < size; ++u )

      if ( set[ u ] ) {
         cout << setw( 4 ) << u << ( x % 10 == 0 ? "\n" : "" );
         empty = false; // set is not empty
         ++x;

      } // end if

   if ( empty )
      cout << setw( 4 ) << "---";  // display an empty set
  
   cout << setw( 4 ) << "}" << '\n';

} // end function setPrint

// finds union of Integer sets
IntegerSet IntegerSet::unionOfIntegerSets( const IntegerSet &r )
{   
   IntegerSet temp( size > r.size ? size : r.size );

   temp.emptySet();

   int iterations = ( size < r.size ? size : r.size );

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

      if ( set[ i ] == 1 || r.set[ i ] == 1 )

         temp.set[ i ] = 1;

         if ( size > r.size )

    for ( int j = r.size; j < size; j++)

     if ( set[ j ] )

      temp.set[ j ] = 1;
     else
      for ( int k = size; k < r.size; k++)

       if ( r.set[ k ] )

        temp.set[ k ] = 1;
    
   return temp;

} // end function unionOfIntegerSets

/* write definition for intersectionOfIntegerSets */ 
IntegerSet IntegerSet::intersectionOfIntegerSets( const IntegerSet &r )
{    
   IntegerSet temp( size < r.size ? size : r.size );

   temp.emptySet();

   int iterations = ( size > r.size ? size : r.size );

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

      if ( set[ i ] == 1 && r.set[ i ] == 1 )

         temp.set[ i ] = 1;

  return temp;
}

// insert element into set
void IntegerSet::insertElement( int k )
{
   if ( validEntry( k ) )
      set[ k ] = 1;

   else
      cout << "Invalid insert attempted!\n";

} // end function insertElement

// delete element from set
void IntegerSet::deleteElement( int k )
{
   if ( validEntry( k ) )
      set[ k ] = 0;

   else
      cout << "Invalid value!\n";

} // end function deleteElement

/* write definition for isEqualTo */

bool IntegerSet::isEqualTo ( const IntegerSet &r) const
{
	bool X = true;
	int i = 0;
	int iterations = ( size < r.size ? size : r.size );


   	 while ( X != false && i < iterations )
  {
      if ( set[ i ] != r.set[ i ])
     	 X = false;
      else
     	 i++;
  }


return X;
}

هذا ملف المين

// Chapter 7 of C++ How to Program
// driver for integerSet class
#include <iostream> 

using std::cout; 
using std::endl; 

#include "integerset.h"

int main()
{
   IntegerSet a( 101 );  
   IntegerSet b( 101 );
   IntegerSet c( 101 );
   IntegerSet d( 101 );

   cout << "Enter set A:\n";
   a.inputSet();
   cout << "\nEnter set B:\n";
   b.inputSet();

   /* Write call to unionOfIntegerSets for object a 
      passing it b; assign the result to c */
   c = a.unionOfIntegerSets( b );

   /* Write call to intersectionOfIntegerSets for 
      object a passing it b; assign the result to d */
   d = a.intersectionOfIntegerSets( b );

   cout << "\nUnion of A and B is:\n";
   c.setPrint();
   cout << "Intersection of A and B is:\n";
   d.setPrint();

   if ( a.isEqualTo( b ) )
      cout << "Set A is equal to set B\n";

   else
      cout << "Set A is not equal to set B\n";

   cout << "\nInserting 77 into set A...\n";
   a.insertElement( 77 );
   cout << "Set A is now:\n";
   a.setPrint();

   cout << "\nDeleting 77 from set A...\n";
   a.deleteElement( 77 );
   cout << "Set A is now:\n";
   a.setPrint();

   cout << endl;
   return 0;

} // end main

ملف الهيدر

// Chapter 7 of C++ How to Program
// integerset.h
#ifndef INTSET_H
#define INTSET_H

// class IntegerSet definition
class IntegerSet {

public:
   IntegerSet( int );
   IntegerSet( const IntegerSet& );

   IntegerSet unionOfIntegerSets( const IntegerSet& );
   IntegerSet intersectionOfIntegerSets( const IntegerSet& );
   void emptySet();
   void inputSet();
   void insertElement( int );
   void deleteElement( int );
   void setPrint( ) const;
   /* write prototype for setPrint */
   bool isEqualTo( const IntegerSet& ) const;

private:
   int *set;  // dynamically allocated set
   int size;

   // function validEntry definition
   bool validEntry( int x ) const 
   {
      return x >= 0 && x < size;   
   /*write here because just public member use this function*/

   } // end function validEntry

}; // end class IntegerSet

#endif // INTSET_H
#2

بصراحة خيبتوا ظني

الحمد لله عرفته بس توقعت انكم رح تساعدوني

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

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