本文整理汇总了C++中Rational::getNumerator方法的典型用法代码示例。如果您正苦于以下问题:C++ Rational::getNumerator方法的具体用法?C++ Rational::getNumerator怎么用?C++ Rational::getNumerator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Rational
的用法示例。
在下文中一共展示了Rational::getNumerator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: QString
Numeric* Integer::operator /(Numeric* c)const{
Integer* entier = dynamic_cast<Integer *>(c);
if(entier==NULL)
{
Real* reel=dynamic_cast<Real*>(c);
if(reel==NULL){
Rational *rationnel=dynamic_cast<Rational *>(c);
Rational *result=new Rational(Integer(_value*rationnel->getDenominator().getValue()),Integer(rationnel->getNumerator().getValue()));
result->simplification();
if(result->getDenominator().getValue()==1 || result->getNumerator().getValue()==0){
Integer* resE = new Integer(result->getNumerator().getValue());
delete result;
return resE;
}
return result;
}
if(reel->getValue() ==0)
throw QString("Can't divide by 0 !");
return new Real(((double)_value)/reel->getValue());
}
if(entier->getValue() ==0)
throw QString("Can't divide by 0 !");
Rational* res = new Rational(*this,*entier);
res->simplification();
if(res->getDenominator().getValue()==1 || res->getNumerator().getValue()==0){
Integer* resE = new Integer(res->getNumerator().getValue());
delete res;
return resE;
}
return res;
}
示例2:
bool operator==(Rational const & a, Rational const & b)
{
if (a.getNumerator() != b.getNumerator()) return false;
if (b.getDenominator() != b.getDenominator()) return false;
return true;
}
示例3: if
bool Rational::operator >=(const Rational& secondnum) const{
BigInt num1 = this->getNumerator();
BigInt num2 = secondnum.getNumerator();
BigInt deno1 = this->getDenomniator();
BigInt deno2 = secondnum.getDenomniator();
normalize(num1, deno1);
normalize(num2, deno2);
if(num1 < 0 && deno1 < 0){
num1 = -num1;
deno1 = -deno1;
}
else if(num1 > 0 && deno1 < 0){
num1 = -num1;
deno1 = -deno1;
}
if(num2 < 0 && deno2 < 0){
num2 = -num2;
deno2 = -deno2;
}
else if(num2 > 0 && deno2 < 0){
num2 = -num2;
deno2 = -deno2;
}
if(num1 * deno2 >= num2 * deno1){
return 1;
}
else{
return 0;
}
}
示例4: printRational
void TheoryArith::printRational(ExprStream& os, const Rational& r,
bool printAsReal)
{
// Print rational
if (r.isInteger()) {
if (r < 0) {
if (os.lang() == SPASS_LANG) {
os << "-" << (-r).toString();
if (printAsReal) os << ".0";
} else {
os << "(" << push;
if (os.lang() == SMTLIB_LANG) {
os << "~";
}
else {
os << "-";
}
os << space << (-r).toString();
if (printAsReal) os << ".0";
os << push << ")";
}
}
else {
os << r.toString();
if (printAsReal) os << ".0";
}
}
else {
os << "(" << push << "/ ";
Rational tmp = r.getNumerator();
if (tmp < 0) {
if (os.lang() == SPASS_LANG) {
os << "-" << (-tmp).toString();
if (printAsReal) os << ".0";
} else {
os << "(" << push;
if (os.lang() == SMTLIB_LANG) {
os << "~";
}
else {
os << "-";
}
os << space << (-tmp).toString();
if (printAsReal) os << ".0";
os << push << ")";
}
}
else {
os << tmp.toString();
if (printAsReal) os << ".0";
}
os << space;
tmp = r.getDenominator();
DebugAssert(tmp > 0 && tmp.isInteger(), "Unexpected rational denominator");
os << tmp.toString();
if (printAsReal) os << ".0";
os << push << ")";
}
}
示例5:
Timestamp::Timestamp(uint s, uint frames, const Rational &newFramerate) {
assert(newFramerate > 0);
uint fr;
if (newFramerate.getDenominator() == 1) {
fr = newFramerate.getNumerator();
} else {
Rational time = newFramerate.getInverse() * frames;
frames = time.getNumerator();
fr = time.getDenominator();
}
_secs = s + (frames / fr);
_framerateFactor = 1000 / gcd<uint>(1000, fr);
_framerate = fr * _framerateFactor;
_numFrames = (frames % fr) * _framerateFactor;
}
示例6: add
Rational Rational::add(Rational &secondRational)
{
cout<<numerator<<"vjnfiehb"<<endl;
long n=numerator*secondRational.getDenominator()+denominator*secondRational.getNumerator();
long d=denominator*secondRational.getDenominator();
//cout<<"n = "<<n<<' '<<"d = "<<d<<endl;
return Rational(n, d);
}
示例7: main
int main()
{
Rational oneEighth(1, 8);
Rational oneHalf(1,2);
Rational result = oneEighth * oneHalf;
cout << result.getNumerator() << " " << result.getDenominator() << endl;
result = result * oneEighth;
cout << result.getNumerator() << " " << result.getDenominator() << endl;
result = oneHalf * 2;//int转换成了Ratinal对象了
cout << result.getNumerator() << " " << result.getDenominator() << endl;
result = 2 * oneHalf;
cout << result.getNumerator() << " " << result.getDenominator() << endl;
return 0;
}
示例8: Rational
const Rational operator -(const Rational& num) {
BigInt pos_num = num.getNumerator();
BigInt deno = num.getDenomniator();
normalize(pos_num, deno);
BigInt neg_num = -pos_num;
return Rational(neg_num, deno);
}
示例9: gcd
const Rational operator +(const Rational& r1, const Rational& r2){
//If the parameters are const, then the get() function must be suffix const
BigInt n1(r1.getNumerator());
BigInt n2(r2.getNumerator());
BigInt d1(r1.getDenominator());
BigInt d2(r2.getDenominator());
BigInt gcd(GCD(d1, d2));
gcd.abs();
if(!n1.isNeg() && !n2.isNeg()){
BigInt n(n1 * (d2 / gcd) + n2 * (d1 / gcd));
BigInt d(d1 * d2 / gcd);
return Rational(n, d);
}
else if(n1.isNeg() && n2.isNeg()){
n1 = -n1;
d1 = -d1;
n2 = -n2;
d2 = -d2;
BigInt n(n1 * (d2 / gcd) + n2 * (d1 / gcd));
BigInt d(d1 * d2 / gcd);
return Rational(n, d);
}
else if(!n1.isNeg() && n2.isNeg()){
n2 = -n2;
d2 = -d2;
BigInt n(n1 * (d2 / gcd) + n2 * (d1 / gcd));
BigInt d(d1 * d2 / gcd);
return Rational(n, d);
}
else if(n1.isNeg() && !n2.isNeg()){
n1 = -n1;
d1 = -d1;
BigInt n(n2 * (d1 / gcd) + n1 * (d2 / gcd));
BigInt d(d1 * d2 / gcd);
return Rational(n, d);
}
}
示例10: simplify
void NatE::simplify(){
this->exponent->simplify();
this->coefficient->simplify();
if (this->coefficient->getType() == "Rational"){
Rational* newCo = dynamic_cast<Rational*>(this->coefficient);
if (newCo->getNumerator()->getType() == "NatE"){
NatE* newPi = dynamic_cast<NatE*>(newCo->getNumerator());
Add* add = new Add();
this->exponent = add->evaluate(this->exponent, newPi->getExponent());
newCo->setNumerator(newPi->getCoefficient());
newCo->simplify();
this->coefficient = newCo;
}
else if (newCo->getDenominator()->getType() == "NatE"){
NatE* newPi = dynamic_cast<NatE*>(newCo->getDenominator());
Subtract* sub = new Subtract();
this->exponent = sub->evaluate(this->exponent, newPi->getExponent());
newCo->setDenominator(newPi->getCoefficient());
newCo->simplify();
this->coefficient = newCo;
}
}
}
示例11: main
int main()
{
Rational p; // p uses the default constructor
Rational q(1, 2); // q uses the other constructor
Rational b(1); // uses constructor to initialize numerator and defaults the denominator to 1
cout << b << endl;
p.setNumerator(1); // set p to be 1/4
p.setDenominator(4);
p= p + q;
p= p * q;
//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);
//.........这里部分代码省略.........
示例12: ra
const Rational operator*(const Rational & lhs, const Rational & rhs)
{
Rational ra(lhs.getNumerator()*rhs.getNumerator(), lhs.getDenominator()*rhs.getDenominator());
return ra;
}
示例13: subtraction
Rational Rational::subtraction(const Rational& num)
{
int newDen = lcm(num.getDenominator(), _den);
Rational x(_neu * newDen / _den - num.getNumerator() * newDen / num.getDenominator(), newDen);
return x;
}
示例14: add
Rational Rational::add( const Rational & other ) const{
return Rational((getNumerator() * other.getDenominator() + other.getNumerator() * getDenominator()), (getDenominator() * other.getDenominator()));
}
示例15: parse
bool IDLAssertion::parse(TNode node, int c, bool negated) {
// Only unit coefficients allowed
if (c != 1 && c != -1) {
return false;
}
// Assume we're ok
bool ok = true;
// The kind of the node
switch(node.getKind()) {
case kind::NOT:
// We parse the negation
ok = parse(node[0], c, true);
// Setup the kind
if (ok) {
d_op = negateOp(d_op);
}
break;
case kind::EQUAL:
case kind::LT:
case kind::LEQ:
case kind::GT:
case kind::GEQ: {
// All relation operators are parsed on both sides
d_op = node.getKind();
ok = parse(node[0], c, negated);
if (ok) {
ok = parse(node[1],-c, negated);
}
break;
}
case kind::CONST_RATIONAL: {
// Constants
Rational m = node.getConst<Rational>();
if (m.isIntegral()) {
d_c += m.getNumerator() * (-c);
} else {
ok = false;
}
break;
}
case kind::MULT: {
// Only unit multiplication of variables
if (node.getNumChildren() == 2 && node[0].isConst()) {
Rational a = node[0].getConst<Rational>();
if (a == 1 || a == -1) {
ok = parse(node[1], c * a.sgn(), negated);
} else {
ok = false;
}
} else {
ok = false;
}
break;
}
case kind::PLUS: {
for(unsigned i = 0; i < node.getNumChildren(); ++i) {
ok = parse(node[i], c, negated);
if(!ok) {
break;
}
}
break;
}
case kind::MINUS: {
ok = parse(node[0], c, negated);
if (ok) {
ok = parse(node[1], -c, negated);
}
break;
}
case kind::UMINUS: {
ok = parse(node[0], -c, negated);
break;
}
default: {
if (c > 0) {
if (d_x.isNull()) {
d_x = node;
} else {
ok = false;
}
} else {
if (d_y.isNull()) {
d_y = node;
} else {
ok = false;
}
}
break;
}
//.........这里部分代码省略.........