struct rational substract(struct rational r1, struct rational r2) {
    changeToSameDenominator(&r1,&r2);
    struct rational r;
    r.numerator = r1.numerator - r2.numerator;
    r.denominator = r.numerator==0 ? 0 : r1.denominator;
    return simplifyRational(r);
}
struct rational add(struct rational r1, struct rational r2) {
    changeToSameDenominator(&r1,&r2);
    struct rational r;
    r.numerator = r1.numerator + r2.numerator;
    r.denominator = r1.denominator;
    return simplifyRational(r);
}
示例#3
0
KNumber::KNumber(TQString const & num)
{
  if (TQRegExp("^(inf|-inf|nan)$").exactMatch(num))
    _num = new _knumerror(num);
  else if (TQRegExp("^[+-]?\\d+$").exactMatch(num))
    _num = new _knuminteger(num);
  else if (TQRegExp("^[+-]?\\d+/\\d+$").exactMatch(num)) {
    _num = new _knumfraction(num);
    simplifyRational();
  }
  else if (TQRegExp("^[+-]?\\d+(\\.\\d*)?(e[+-]?\\d+)?$").exactMatch(num))
    if (_fraction_input == true) {
      _num = new _knumfraction(num);
      simplifyRational();
    } else
      _num = new _knumfloat(num);
  else
    _num = new _knumerror("nan");
}
struct rational product(struct rational r1, struct rational r2) {
    struct rational r;
    r.numerator = r1.numerator * r2.numerator;
    r.denominator = r1.denominator * r2.denominator;
    
    if(r.denominator<0) {
        r.denominator *= -1;
        r.numerator   *= -1;
    }
    
    return simplifyRational(r);
}
struct rational promptUserForRational() {
    struct rational r;
    
    printf("Please type in your rational values:\n");
    scanf("%d",&r.numerator);
    printf("/\n");
    scanf("%d",&r.denominator);
    
    while(r.denominator == 0) {
        printf("Wrong denominator, please try again:\n%d / ",r.numerator);
        scanf("%d",&r.denominator);
    }
    
    // adopt signed value
    if(r.denominator<0) {
        r.denominator *= -1;
        r.numerator   *= -1;
    }
    
    return simplifyRational(r);
}
void pratica25exercise6(int argc, const char * argv[]) {
    printf("\n\nExercise 6:\n");
    
    struct rational r = promptUserForRational();
    printf("Simplified it's %d/%d\n\n",r.numerator,r.denominator);
    
    struct rational r1;
    struct rational r2;
    r1.numerator = 8;
    r1.denominator = 4;
    r2.numerator = 3;
    r2.denominator = 6;
    
    r1 = simplifyRational(r1);
    test(r1.numerator==2,"Test simplify numerator");
    test(r1.denominator==1,"Test simplify denominator");
    
    r2 = simplifyRational(r2);
    test(r2.numerator==1,"Test simplify numerator");
    test(r2.denominator==2,"Test simplify denominator");
    
    struct rational r3 = add(r1,r2);
    test(r3.numerator==5, "Check add");
    test(r3.denominator==2, "Check add");
    
    r2.numerator = -2;
    r3 = add(r1,r2);
    test(r3.numerator==1, "Check add");
    test(r3.denominator==1, "Check add");
    
    r2.numerator = -2;
    r3 = substract(r1,r2);
    test(r3.numerator==3, "Check substraction");
    test(r3.denominator==1, "Check substraction");
    
    r2.numerator = 2;
    r3 = substract(r1,r2);
    test(r3.numerator==1, "Check substraction");
    test(r3.denominator==1, "Check substraction");
    
    // 2/1 * -2/2 = -4/2 = -2/1
    r2.numerator = -2;
    r3 = product(r1,r2);
    test(r3.numerator==-2, "Check substraction");
    test(r3.denominator==1, "Check substraction");
    
    // 2/1 * -2/2 = 2/1 * 2/-2 = 4/-2 = -2/1
    r3 = divide(r1,r2);
    test(r3.numerator==-2, "Check substraction");
    test(r3.denominator==1, "Check substraction");
    
    // 2/2 * -2/6 = -4/12= -1/3
    r2.numerator = -2;
    r1.denominator = 2;
    r2.denominator = 6;
    r3 = product(r1,r2);
    test(r3.numerator==-1, "Check substraction");
    test(r3.denominator==3, "Check substraction");
    
    // 2/2 * -2/6 = 2/2 * 6/-2 = 12/-4= -3/1
    r2.numerator = -2;
    r1.denominator = 2;
    r2.denominator = 6;
    r3 = divide(r1,r2);
    test(r3.numerator==-3, "Check substraction");
    test(r3.denominator==1, "Check substraction");
}