סדנא תרגיל 1 שאלה 1

benny++C, class, סדנא ב++C, תרגילים בסדנא ב++CLeave a Comment

תרגיל_1_-_תכנות_מונחה_עצמים

מחלקה לשברים רציונליים

 

 

 


#include <iostream>

using namespace std;

#include "rational.h"
int main()
{

	cout<<"enter two rational numbers"<<endl; int a,b,c,d; // a is the first mone, b is the first mechane, c is the second mone, d is the second mechane. char g; // for the separator line ('/') bool i; Rational A; Rational B; // create 2 rational objects cin>>a>>g>>b>>c>>g>>d;   
	
	if (b==0||d==0||a>b||c>d)       // input chack
	{
		cout<<"ERROR"<<endl;
		return 0;
	}

	i = A.equal(a,b,c,d);            // go to the equal function in the class.
	if (i)                           // if the numbers are equal...
	{
		cout<<"equal"<<endl;

	//	system("pause");
		return 0;
	}
	else                           // if the numbers are not equal...
	{
		
		A.set_d(a);                //enter the num valus in the class fields
		A.set_n(b);
		A.help(a,b);               // and go to the help function to chack if the number can be minimize
		
		B.set_d(c);                //enter the second num valus in the class fields
		B.set_n(d);          
		B.help(c,d);                // and go to the help function to chack if the number can be minimize
	
		cout<<"different "<<A.get_d()<<"/"<<A.get_n()<<" "<<B.get_d()<<"/"<<B.get_n()<<endl;            

	}

	//system("pause");
	return 0;
}



מימוש הפונקציות של הקלאס




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




Rational::Rational(int x, int y)
{

	d=x;
	n=y;



}// the c-tor with 3 option:  1) default values (0 parameters), 2) 1 default 1 parameter, 3) 2 parameters. (the default change if the user enter a value into the parameters)


  
  
  void Rational::set_d(int mone)
  {
  	d = mone;
  }
  
  int Rational::get_d()  { return d; }
  
  void Rational::set_n(int mechane)
  {
  	if (mechane != 0)       // to Avoid math error.
  	{
  		n = mechane;
  	}
  
  }
  
  int Rational::get_n() { return n; }
  
  void Rational::print()          
  {
  
  	cout<<d<<"/"<<n<<endl;
  
  }
  bool Rational::equal(float a,float b,float c,float d)
  {
  	if (a/c==b/d)             // if its the same number (mone/mone = mechane/mechane).
  	{
  		return true;
  	}
  	else
  	{
  		return false;
  	}
  }
  
  void Rational::help (int x,int y)
  {
  
  	int i=2;        // 
  	while (i<=x)
  	{
  		if (x%i==0)  // if the mone divided in this num (i)......
  		{
  			if (y%i==0)    // so chack..  if the mechane allso divided in this num (i)   so we can minimize the nember.
  			{
  				x/=i; y/=i;     // so  minimize the nember by dividing in this (i).
  			}
  		}
  		i++;
  	}
  
  	d=x; n=y;    // now update the valuse of tje numbers in the class.  
  }
  
  
  Rational::~Rational()
{

}



תכנית ראשית



/************OUTPUT******************


lecturer: david Cohen
discription: difine class and use the class to chack numbers if thay are equal or not

*************************************/

#include <iostream>

using namespace std;

#include "rational.h"
int main()
{

	cout<<"enter two rational numbers"<<endl; int a,b,c,d; // a is the first mone, b is the first mechane, c is the second mone, d is the second mechane. char g; // for the separator line ('/') bool i; Rational A; Rational B; // create 2 rational objects cin>>a>>g>>b>>c>>g>>d;   
	
	if (b==0||d==0||a>b||c>d)       // input chack
	{
		cout<<"ERROR"<<endl;
		return 0;
	}

	i = A.equal(a,b,c,d);            // go to the equal function in the class.
	if (i)                           // if the numbers are equal...
	{
		cout<<"equal"<<endl;

	//	system("pause");
		return 0;
	}
	else                           // if the numbers are not equal...
	{
		
		A.set_d(a);                //enter the num valus in the class fields
		A.set_n(b);
		A.help(a,b);               // and go to the help function to chack if the number can be minimize
		
		B.set_d(c);                //enter the second num valus in the class fields
		B.set_n(d);          
		B.help(c,d);                // and go to the help function to chack if the number can be minimize
	
		cout<<"different "<<A.get_d()<<"/"<<A.get_n()<<" "<<B.get_d()<<"/"<<B.get_n()<<endl;            

	}

	//system("pause");
	return 0;
}

/************OUTPUT******************

enter two rational numbers
1/2 2/6
different 1/2 1/3

*************************************

*************OUTPUT 2****************
enter two rational numbers
1/3 3/9
equal

*************************************/

תרגיל 1 שאלה 1 סדנא ב C++

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *