当前位置: 首页>>代码示例>>C++>>正文


C++ Rational::reduce方法代码示例

本文整理汇总了C++中Rational::reduce方法的典型用法代码示例。如果您正苦于以下问题:C++ Rational::reduce方法的具体用法?C++ Rational::reduce怎么用?C++ Rational::reduce使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Rational的用法示例。


在下文中一共展示了Rational::reduce方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1:

	Rational Rational::operator*(const Rational & n) 	//overwriting operator
    {

		Rational temp {(n.top * top), (n.bottom * bottom)};
		temp.reduce();
		return temp;;
    }
开发者ID:dcods22,项目名称:Rational,代码行数:7,代码来源:Rational.cpp

示例2: Rational

//分数减法
Number *Rational::sub(Number *number2)
{
    Rational *tmp = SCAST_RATIONAL(number2);
    Rational *result = new Rational();
    result->numerator_ = numerator_*tmp->denominator_ - denominator_*tmp->numerator_;
    result->denominator_ = denominator_ * tmp->denominator_;
    result->reduce();//最后要进行约分
    return result;
}
开发者ID:yuchenlin,项目名称:SchemeCalc,代码行数:10,代码来源:rational.cpp

示例3: Rational

Number *Rational::sub(Number *number2)
{
	Rational *tmp = SCAST_RATIONAL(number2);
	Rational *result = new Rational();
	result->num_ = num_*tmp->den_ - den_*tmp->num_;
	result->den_ = den_ * tmp->den_;
	result->reduce();
	return result;
}
开发者ID:elicassion,项目名称:MyScheme,代码行数:9,代码来源:rational.cpp

示例4: main


//.........这里部分代码省略.........

	//print out p and q
	p.streamInsert(cout);

	cout << "p is " << p.getNumerator() << "/" << p.getDenominator() << endl; 
	cout << "q is " << q.getNumerator() << "/" << q.getDenominator() << endl; 
	
	Rational r; 
	Rational s; 
	cout << "Enter a rational number (a/b): "; 
	cin >> r; 
	cout << "Enter a rational number (a/b): "; 
	cin >> s; 

	cout << "You entered the rational numbers " << r << " and " << s << endl;
	
	//Confirm +,-,*, and / works with rationals.
	Rational sum = r + s;
	Rational product = r * s;
	Rational difference = r - s;
	Rational divide = r/s;

	// Test greater than, greater than or equal to, and isEqual
	cout << "Changing value of r and s..." << endl;
	r.setNumerator(3);
	r.setDenominator(4);
	
	s.setNumerator(1);
	s. setDenominator(4);

	if (r >= s)
	{
		cout << r << " is greater than or equal to " << s << endl;
		if (r > s)
		{
			cout << "okay..." << r << " is actually greater than " << s << endl;
		}
		else if (r== s)
		{
			cout << "okay..." << r << " is actually equal to " <<  s << endl;
		}
	}
	else
	{
		cout << "Oops, I enter an incorrect rational to test greater than or equal to!" << endl;
	}

	// Test less than, less than or equal to, and isEqual(again)
	cout << "Changing value of r and s again..." << endl;
	r.setNumerator(1);
	r.setDenominator(4);

	s.setNumerator(3);
	s. setDenominator(4);

	if (r <= s)
	{
		cout << r << " is less than or equal to " << s << endl;
		if (r < s)
		{
			cout << "okay..." << r << " is actually less than " << s << endl;
		}
		else if (r== s)
		{
			cout << "okay..." << r  << " is actually equal to " << s << endl;
		}
	}
	else
	{
		cout << "Oops, I enter an incorrect rational to test less than or equal to!" << endl;
	}
	
	// Test the ability to cout new ADT.
	cout << r << " + " << s << " = " << sum << endl;
	cout << r << " * " << s << " = " << product << endl;
	cout << r << " - " << s << " = " << difference << endl;
	cout << r << " / " << s << " = " << divide << endl;
	
	// Test cloning or coping Rational ADT
	Rational t(s); 
	double tFloat = t.convertToFloatingPoint();
	cout << "s copied the rational " << s << " to t " << t << "." << endl;
	cout << "t in decimal equals: " << tFloat << endl; 

	// Test reducing rational **extra credit***
	r.setNumerator(12);
	r.setDenominator(4);

	s.setNumerator(6);
	s. setDenominator(8);
	
	r.reduce();
	s.reduce();

	cout << "r is now reduced to " << r << endl;
	cout << "s is now reduced to " << s << endl;
	cin.ignore();
	cin.get();
	return 0; 
}
开发者ID:morriscasey,项目名称:cplusplus,代码行数:101,代码来源:rationaltester.cpp


注:本文中的Rational::reduce方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。