64 - DARS. ABSTRAKT TIPLARNI TASVIRLASH.
Amaliy masalalarni echishda, shu soha uchun mos bo’lgan ma'lumotlar tiplarini aniqlab olish qo’laydir. Dasturda bu tiplar strukturali tiplar sifatida tasvirlanadi. Sungra shu tip bilan bog’lik hamma funktsiyalarni ta'riflab, biblioteka hosil qilinadi. Misol tariqasida kasrlar bilan bog’lik abstrakt tip kiritamiz. Kasrlar ustida quyidagi funktsiyalarni kiritamiz. Input() kasr soni kiritish;
Out() kasr soni ekranga chiqarish;
Add() kasrlarni qo’shish;
Sub() kasrlarni ayirish;
Mult() kasrlarni kupaytirish;
Divide() kasrlarni bo’lish;
//* fract.h
Typedef struct rational_fraction
{
int numerator;
int denominator;
} fraction;
void input (fraction * pd);
void out( fraction dr);
fraction add (fraction dr1, fraction dr2);
void sub (fraction dr1, fraction dr2, fraction * prd);
fraction * mult ( fraction dr1, fraction dr2);
fraction divide ( fraction * pd1, fraction * pd2);
//* fract.c
#include
#include
void input (fraction * pd)
{
int N;
Cout<<(“\n Chislitel':”);
Cin>>(“%d”, pd->numarator);
Cout<<(“Znamenatel':”);
Cin>>(“%d”, &N);
if (N==0)
{
Cout<<(“\n” Oshibka! Nulevoy znamenatel'”);
exit (0);
}
pd-> denominator=N;
void out ( fraction dr)
{
Cout<<(“ Ratsional'naya drob'”);
Cout<<( “%d/%d”, dr.numerator, dr.denominator);
}
fraction add ( fraction dr1, fraction dr2)
{
fraction dr;
dr.numerator=dr1.numarator * dr2.denominarator+ dr1.denominator * dr2.numarator;
dr.denominator=dr1.denominator * dr2.denominator;
return dr;
}
void sub ( fraction dr1, fraction dr2, fraction * pdr)
{
pdr-> numarator=dr1.numarator * dr2.numarator- dr2.numarator * dr1.denominator;
pdrw-> denominator= dr1.denominator* dr2.denominator;
}
fraction * mult ( fraction dr1, fraction dr2 )
{
fraction * mul;
mul= (fraction 8) malloc (sizeof ( fraction) );
mul-> nymerator=dr1.numerator * dr2.numerator;
mul-> denominator= dr1.denominator * dr2.denominator;
return 0;
}
fraction divide ( fraction * pd1, fraction * pd2)
{
fraction d;
d.numarator= pd1-> numarator * pd2 ->denominator;
d.denominator=pd1->denominator * pd2 ->numarator;
return d;
}
Qo’yidagi programma kiritilgan funktsiyalar yordamida kasrlar ishlashga misol bo’ladi
#Include “fract.h”
#include “fract.c”
void main()
{
fraction a,b,c;
fraction *p;
Cout<<(“\n Vvedite drobi”);
input(&a);
input(&b);
c=add(a,b);
out©;
p=mult(a,b);
out(*p);
free(p);
c=divide(&a,&b);
out(c);
}
Dostları ilə paylaş: |