frPtr divideFraction(frPtr fr1, frPtr fr2) { frPtr frResult; frResult = (frPtr) malloc(sizeof(frac)); frResult->num = ((fr1->num) * (fr2->denom)); frResult->denom = ((fr1->denom) * (fr2->num)); return reduceFraction(frResult); }
int main() { int n, d; scanf("%d/%d", &n, &d); struct fraction f = {.numerator = n, .denominator = d}; reduceFraction(&f); printf("%d/%d", f.numerator, f.denominator); }
fraction_t multiplyFraction(fraction_t a,fraction_t b) { fraction_t c; c.numerator=a.numerator*b.numerator; c.denominator=a.denominator*b.denominator; c=reduceFraction(c); return c; }
fraction_t subtractFraction(fraction_t a,fraction_t b) { fraction_t c; c.denominator=a.denominator * b.denominator; c.numerator=a.numerator*b.denominator - b.numerator*a.denominator; c=reduceFraction(c); return c; }
fraction_t divideFraction(fraction_t a,fraction_t b) { fraction_t c; if(b.numerator==0) { c.numerator=10000000; c.denominator=1; return c; } c.numerator=a.numerator * b.denominator; c.denominator=a.denominator * b.numerator; c=reduceFraction(c); return c; }
int main() { char number[35]; int casenum=1; int mocniny[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000}; fgets(number,35,stdin); while(number[0]!='-') { int i; int x1,x2; int recurrentpart; int total; x1=x2=0; recurrentpart=number[0]-'0'; for(i=4;number[i]!='\n';i++) { x1=x1*10+number[i]-'0'; } total=i-4; x2=x1; if(recurrentpart!=0) { x2/=mocniny[recurrentpart]; x1-=x2; } x2=mocniny[total]; if(recurrentpart!=0) { x2-=mocniny[total-recurrentpart]; } reduceFraction(&x1,&x2); printf("Case %d: %d/%d\n",casenum,x1,x2); fgets(number,35,stdin); casenum++; } return 0; }
void menu() { int option; int frOption; frPtr lOp = 0; frPtr rOp = 0; do { printf("\nMENU : Fraction -- Tyler Hill"\ "\n1. Creating/Updating"\ "\n2. Adding"\ "\n3. Subtracting"\ "\n4. Multiplying"\ "\n5. Dividing"\ "\n6. Displaying"\ "\n7. Quit"); printf("\nEnter the option (1 or 2): "); scanf("%d", &option); switch (option) { case 1: do { printf("\nMENU : Creating/Updating"\ "\n1. Left Operand"\ "\n2. Right Operand"\ "\n3. Both Operands"\ "\n4. Displaying Current Operands"\ "\n5. Return"); printf("\nEnter the option: "); scanf("%d", &frOption); switch(frOption) { case 1: printf("\nLeft Operand: "); lOp = reduceFraction(createFraction()); break; case 2: printf("\nRight Operand: "); rOp = reduceFraction(createFraction()); break; case 3: printf("\nLeft Operand: "); lOp = reduceFraction(createFraction()); printf("\nRight Operand: "); rOp = reduceFraction(createFraction()); break; case 4: printf(""); printf("\nLeft Operand:"); displayFraction(lOp); printf(""); printf("\nRight Operand:"); displayFraction(rOp); break; case 5: break; } } while (frOption != 5); break; case 2: break; case 3: break; case 4: break; case 5: break; case 6: break; case 7: break; default: printf("\nYou should not be in this class!\n"); } } while (option != 7); }