[̲] [] []

 

9. ˲

 

9.

 

. ij, . , . , . ֳ .

ᒺ , , , . , . , : () . , , . .

, , , . , , .

 

 

.

 

M

M1, M2, ..., Mn;

a1=c1; ...; ak=ck;

t1=def_t1; ...; tl=def_tl;

x1: t1; ...; xm: tm;

def_N1;

...

def_Nd;

R

P

.

 

M1, ..., Mn ; a1, ..., ak ; c1, ..., ck ; t1, ..., tl ; def_t1, ..., def_tl ; x1, ..., xm ; t1, ..., tm ; def_N1, ..., def_Nd :

 

Ni( y1: r1; ...; yf: rf;

z1: s1; ..., zg: sg;

v1: u1; ..., vh: uh)

Fi(y1: r1; ...; yf: rf): r;

 

R , , . , , .

P. . , P .

, , :

 

M1, M2, ..., Mn;

 

M1, ..., Mn .

 

M , , , , , M, , , x1. , . , 糺 . , , , M.x1.

 

.

9.1. (, ).

. , Rational. 䳿 :

1.    : ;

2.    : ;

3.    : .

 

Rational

 

Rat =

Nom: ; {}

Den: {}

;

RatError: ; { }

 

ReadRat( r: Rat); { }

WriteRat(r: Rat); { }

AddRat(a, b: Rat): Rat; { }

EqRat(a, b: Rat): ; { }

 

 

{ }

GCD(m, n: ):

m<>n

m>n m <- m-n

n <- n-m ;

GCD <- m

;

 

{ }

Reduce( r: Rat)

k: ;

r.Nom=0 r.Den <- 1

k <- GCD(abs(r.Nom),r.Den);

r.Nom <- r.Nom div k;

r.Den <- r.Den div k

;

 

{ }

ReadRat( r: Rat)

RatError<>0 ;

(r.Nom, r.Den);

r.Den=0 RatError <- 1

;

 

{ }

WriteRat(r: Rat)

(r.Nom);

r.Den<>1

('/',r.Den)

;

 

{ }

AddRat(a, b: Rat): Rat

c: Rat;

RatError<>0 ;

c.Nom <- a.Nom*b.Den+b.Nom*a.Den;

c.Den <- a.Den*b.Den;

Reduce(c);

AddRat <- c

;

 

{ }

EqRat(a, b: Rat):

EqRat <- a.Nom*b.Den=b.Nom*a.Den

;

 

RatError <- 0

.

 

: , , < >. AddRat EqRat.

Rational , : GCD Reduce. ֳ .

RatError . 0. ³ 0. . , . , . ReadRat AddRat .

 

:

 

RatTst

Rational;

a,b,c: Rat;

ReadRat(a);

Rational.RatError=0 ;

(' 0');

Rational.RatError <- 0

;

ReadRat(b);

Rational.RatError=0 ;

(' 0');

Rational.RatError <- 0

;

c <- AddRat(a,b);

WriteRat(c);

(' ');

ØEqRat(a,b) (' ') ;

('')

.

 

Rational RatError. Rational . , 糿 .

 

, -. :

 

unit M;

interface

uses M1, M2, ..., Mn;

const a1=c1; ...; ak=ck;

type t1=def_t1; ...; tl=def_tl;

var x1: t1; ...; xm: tm;

def_N1;

...

def_Nd;

implementation

uses M1, M2, ..., Me;

Pas(R)

begin

Pas(P)

end.

 

M1, ..., Mn ; a1, ..., ak ; c1, ..., ck ; t1, ..., tl ; def_t1, ..., def_tl ; x1, ..., xm ; t1, ..., tm ; def_N1, ..., def_Nd :

 

procedure Ni(y1: r1; ...; yf: rf;

var z1: s1; ..., var zg: sg);

function Fi(y1: r1; ...; yf: rf): r;

 

M1, M2, ..., Me , .

, implementetion, , , . , , . , , , ,

procedure Ni;

function Fi;

 

, begin, Pas(P). , begin Pas(P) .

(Program), uses, :

 

uses M1, M2, ..., Mn;

 

, , .

, , , . - , :

 

System / , ;

Crt ;

Dos ;

Graph .

 

, , System uses.

 

9P. (, ) ( 9.1).

 

unit Rational;

 

interface

 

type Rat = record

Nom: integer;

Den: word

end;

var RatError: word;

 

procedure ReadRat(var r: Rat);

procedure WriteRat(r: Rat);

procedure AddRat(a, b: Rat; var c: Rat);

function EqRat(a, b: Rat): boolean;

 

implementation

 

function GCD(m, n: word): word;

begin

while m<>n do

if m>n then m:=m-n

else n:=n-m;

GCD:=m

end;

 

procedure Reduce(var r: Rat);

var k: word;

begin

if r.Nom=0 then r.Den:=1

else begin

k:=GCD(abs(r.Nom),r.Den);

r.Nom:=r.Nom div k;

r.Den:=r.Den div k

end

end;

 

procedure ReadRat(var r: Rat);

begin

if RatError<>0 then exit;

readln(r.Nom, r.Den);

if r.Den=0 then RatError:=1

end;

 

procedure WriteRat(r: Rat);

begin

write(r.Nom);

if r.Den<>1 then begin

write('/');

writeln(r.Den)

end

else writeln

end;

 

procedure AddRat(a, b: Rat; var c: Rat);

begin

if RatError<>0 then exit;

c.Nom:=a.Nom*b.Den+b.Nom*a.Den;

c.Den:=a.Den*b.Den;

Reduce(c)

end;

 

function EqRat(a, b: Rat): boolean;

begin

EqRat:=a.Nom*b.Den=b.Nom*a.Den

end;

 

 

begin

RatError:=0

end.

 

Rational , .

 

Program RatTst;

 

uses Rational;

 

var a,b,c: Rat;

 

begin

while true do begin

writeln(' ');

ReadRat(a);

if RatError=0 then break;

writeln(' 0');

RatError:=0

end;

while true do begin

writeln(' ');

ReadRat(b);

if RatError=0 then break;

writeln(' 0');

RatError:=0

end;

AddRat(a,b,c);

write('='); WriteRat(c);

write(' ');

if not EqRat(a,b) then write(' ');

writeln('')

end.

 

ѳ . . ѳ , . , main(). . .H , , , . , , . , ;:

 

extern void n(t1 x1, ..., tn xn);

extern t f(t1 x1, ..., tn xn);

 

extern , .

#include. ,

 

#include myfile.h

 

. , < > , ,

 

#include <stdio.h>

 

#include #include .

ѳ .H .C. .H , .C . #include.

ѳ, , ѳ . , .PRJ , .

: ѳ 糿 , .

 

9C. (, ) ( 9.1).

. 3 : rat.h rat.c rattst.c.

 

/* rat.h*/

 

typedef struct {

int nom;

unsigned den;

} rat;

 

extern int rat_error;

 

extern void add_rat(rat a, rat b, rat* pc);

 

extern int eq_rat(rat a, rat b);

 

extern void read_rat(rat* pr);

 

extern void write_rat(rat r);

 

 

/* rat.c*/

 

#include <stdio.h>

#include <stdlib.h>

#include "rat.h"

 

int rat_error=0;

 

static unsigned gcd(unsigned n, unsigned m)

{

while (n != m)

if (n > m) n -= m;

else m -= n;

return m;

}

 

static void reduce(rat* pr)

{

unsigned k;

 

if ((*pr).nom == 0) (*pr).den = 1;

else

{

k = gcd(abs((*pr).nom),(*pr).den);

(*pr).nom /= k; (*pr).den /= k;

}

}

 

void add_rat(rat a, rat b, rat* pc)

{

if (rat_error != 0) exit(1);

(*pc).nom = a.nom*b.den+b.nom*a.den;

(*pc).den = a.den*b.den;

reduce(pc);

}

 

int eq_rat(rat a, rat b)

{

return a.nom*b.den == b.nom*a.den;

}

 

void read_rat(rat* pr)

{

if (rat_error != 0) exit(1);

scanf("%d/%u",&(*pr).nom,&(*pr).den);

if ((*pr).den == 0) rat_error = 1;

}

 

void write_rat(rat r)

{

if (r.den == 1) printf("%d\n",r.nom);

else printf("%d/%u\n",r.nom, r.den);

}

 

static gcd reduce , rat.c .

 

/* rattst.c */

 

#include <stdio.h>

#include "rat.h"

 

 

main()

{

rat a, b, c;

while(1)

{

printf(" 1 \n");

read_rat(&a);

if (rat_error == 0) break;

printf(" 0\n");

rat_error = 0;

}

while(1)

{

printf(" 2 \n");

read_rat(&b);

if (rat_error == 0) break;

printf(" 0\n");

rat_error = 0;

}

printf("=");

add_rat(a,b,&c); write_rat(c);

if (eq_rat(a,b)) printf(" \n");

else printf(" \n");

}

 

 

9.1. , <.

 

9.2. , , , , , , , , . :

 

¥ i z2i+1

S (-1) -------

i=0 (2i+1)!

 

z .

 

[̲] [] []