Loading lang_c_ex_0701...
void test_Interval(void) { printf("\n~~~~ %s() ~~~~\n", __func__); // ... }
void test_Fraction(void) { printf("\n~~~~ %s() ~~~~\n", __func__); // ... }
void test_Rectangle(void) { printf("\n~~~~ %s() ~~~~\n", __func__); // ... }
//----------------------------------------------------------------------------
#ifndef NEWTYPES_H #define NEWTYPES_H
typedef struct { double low, high; } Interval;
Interval Interval_add(Interval lhs, Interval rhs);
Interval Interval_mul(Interval lhs, double rhs);
typedef struct { int numer, denom; } Fraction;
Fraction Fraction_add(Fraction lhs, Fraction rhs);
Fraction Fraction_mul(Fraction lhs, int rhs);
typedef struct { double width, height; } Rectangle;
Rectangle Rectangle_add(Rectangle lhs, Rectangle rhs);
Rectangle Rectangle_mul(Rectangle lhs, double rhs);
#endif // NEWTYPES_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "newTypes.h"
Interval Interval_add(Interval lhs, Interval rhs) { Interval result; result.low=lhs.low+rhs.low; result.high=lhs.high+rhs.high; return result; }
Interval Interval_mul(Interval lhs, double rhs) { Interval result={lhs.low*rhs, lhs.high*rhs}; return result; }
Fraction Fraction_add(Fraction lhs, Fraction rhs) { Fraction result; result.numer=lhs.numer*rhs.denom+rhs.numer*lhs.denom; result.denom=lhs.denom*rhs.denom; return result; }
Fraction Fraction_mul(Fraction lhs, int rhs) { Fraction result={lhs.numer*rhs, lhs.denom}; return result; }
Rectangle Rectangle_add(Rectangle lhs, Rectangle rhs) { Rectangle result; result.width=lhs.width+rhs.width; result.height=lhs.height+rhs.height; return result; }
Rectangle Rectangle_mul(Rectangle lhs, double rhs) { Rectangle result={lhs.width*rhs, lhs.height*rhs}; return result; }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "newTypes.h"
#include <stdio.h>
void test_Interval(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const Interval i1={2.5, 6.3}; const Interval i2={0.4, 2.1}; const Interval i3=Interval_add(i1, i2); const double factor=10.0; const Interval i4=Interval_mul(i3, factor); printf("[%g;%g] + [%g;%g] = [%g;%g]\n", i1.low, i1.high, i2.low, i2.high, i3.low, i3.high); printf("[%g;%g] * %g = [%g;%g]\n", i3.low, i3.high, factor, i4.low, i4.high); }
void test_Fraction(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const Fraction f1={2, 3}; const Fraction f2={4, 5}; const Fraction f3=Fraction_add(f1, f2); const int factor=5; const Fraction f4=Fraction_mul(f3, factor); printf("%d/%d + %d/%d = %d/%d\n", f1.numer, f1.denom, f2.numer, f2.denom, f3.numer, f3.denom); printf("%d/%d * %d = %d/%d\n", f3.numer, f3.denom, factor, f4.numer, f4.denom); }
void test_Rectangle(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const Rectangle r1={10.1, 20.2}; const Rectangle r2={30.3, 40.4}; const Rectangle r3=Rectangle_add(r1, r2); const double factor=0.5; const Rectangle r4=Rectangle_mul(r3, factor); printf("(%g,%g) + (%g,%g) = (%g,%g)\n", r1.width, r1.height, r2.width, r2.height, r3.width, r3.height); printf("(%g,%g) * %g = (%g,%g)\n", r3.width, r3.height, factor, r4.width, r4.height); }
int main(void) { test_Interval(); test_Fraction(); test_Rectangle(); return 0; }
//----------------------------------------------------------------------------