75 printffloat2fn rationaltorealrat return 0

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: m, den) rat.num = num / gcd rat.den = den / gcd return (rat) } float rationalToReal(Rational rat) { return ((float) rat.num / rat.den) } int main(void) { Rational rat rat = rationalCreate(6, 8) /* 6/8 = 3/4 = 0.75 */ printf("float=%.2f\n", rationalToReal(rat)) return 0 } /* assuming the existence of hcf() */ /* simplifies the rational number */ float=0.75 CSC 1500 { Lecture 11 13 Complete Example #include <stdio.h> typedef struct { unsigned int unsigned int } Rational num den Rational rationalCreate(unsigned int num, unsigned int den) { Rational rat unsigned int gcd gcd = hcf(num, den) rat.num = num / gcd rat.den = den / gcd return (rat) } float rationalToReal(Rational rat) { return ((float) rat.num / rat.den) } Rational rationalAdd(Rational rat1, Rational rat2) { unsigned int num, den num = rat1.num * rat2.den + rat2.num * rat1.den den = rat1.den * rat2.den return (rationalCreate(num, den)) } int main(void) { Rational rat1, rat2 rat1 = rationalCreate(3, 4) rat2 = rati...
View Full Document

This note was uploaded on 05/23/2010 for the course COMPUTER S CSC1500 taught by Professor Fung during the Spring '10 term at CUHK.

Ask a homework question - tutors are online