#include <process.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
class Racio
{
private:
float chisl;
float znamen;
public:
Racio() {};
Racio(float c, float z=1) {chisl=c; znamen=z;}
~Racio() {}
int operator==(Racio &obj);
int operator<(Racio &obj);
int operator>(Racio &obj);
Racio operator*(Racio &);
friend ostream &operator<<(ostream &, Racio &);
friend istream &operator>>(istream &, Racio &);
};
int Racio::operator==(Racio &obj)
{
if(chisl==obj.chisl&&znamen==obj.znamen)return 1;
else
return 0;
}
int Racio::operator<(Racio &obj)
{
float d1=chisl/znamen;
float d2=obj.chisl/obj.znamen;
if(d1<d2)return 1;
return 0;
}
//Перегрузка >
int Racio::operator>(Racio &obj)
{
float d1=chisl/znamen;
float d2=obj.chisl/obj.znamen;
if(d1>d2)return 1;
return 0;
}
//Перегрузка *
Racio Racio::operator*(Racio &fp1)
{
float i,j;
i=chisl*fp1.chisl;
j=znamen*fp1.znamen;
fp1.chisl=i;
fp1.znamen=j;
return fp1;
}
ostream &operator<< (ostream &fo, Racio &fp)
{
fo << fp.chisl << "/" <<fp.znamen<<"\n";
return fo;
}
//Перегрузка оператора >>
istream &operator>>(istream &fi, Racio &fp)
{
cout<< "Введите числитель:";
fi >>fp.chisl;
cout<< "Введите знаменатель:";
fi >> fp.znamen;
if(fp.znamen==0) {cout << "The Error"; getch(); exit(1);}//ноль недопустим
float a=fp.chisl;
float b=fp.znamen;
if (a<0 && b<0) {a=abs(-a);b=abs(-b);}
else if(b<0){b=-b;a=-a;};
int a1=abs(a);
fp.chisl=a;
fp.znamen=b;
while (a1!=b)
{
if(a1>b) a1=a1-b;
if(b>a1) b=b-a1;
}
fp.chisl=fp.chisl/a1;
fp.znamen=fp.znamen/a1;
return fi;
}
template <class T>
struct Deque
{
T info;
struct Deque <T> *next;
};
template <class T>
struct LIST
{
Deque <T> *root;//єърчрЄхы№ эр ¤ыхьхэЄ
//ъюэёЄЁєъЄюЁ
LIST() { root = 0;}
};
template <class T>
class DEQUE:public LIST <T>
{
public:
void insert(T x)
{
root=::insert(root, x);
}
void append(T x)
{
root=::append(root, x);
}
void take_first(T x)
{
root=::take_first(root, x);
}
void take_last(T x)
{
root=::take_last(root, x);
}
void find(T x)
{
root=::find(root, x);
}
void show()
{
::show(root);
}
void isempty()
{
::isempty(root);
}
};
template <class T>
Deque <T> *insert(Deque <T> *root, T x)
{
Deque <T> *new_x = (Deque <T> *)malloc (sizeof(Deque <T>));
new_x->info = x;
new_x->next = root;
root = new_x;
return root;
}
template <class T>
Deque <T> *append(Deque <T> *root, T x)
{
Deque <T> *current = root, *prev=0;
Deque <T> *new_x = (Deque <T> *)malloc (sizeof(Deque <T>));
new_x->info = x;
new_x->next = 0;
while (current)
{
prev = current;
current = current->next;
}
if(prev) prev->next = new_x;
else root = new_x;
return root;
}
template <class T>
Deque <T> *take_first(Deque <T> *root, T x)
{
Deque <T> *old_x = root;
T old_info=0;
if(root)
{
old_info = old_x->info;
root=(root)->next;
free(old_x);
}
return root;
}
template <class T>
Deque <T> *take_last(Deque <T> *root, T x)
{
Deque <T> *current = root, *prev = root;
T old_info =0;
if(current->next == 0)
{
root = 0;
old_info = current->info;
free(current);
}
else
while (current->next!=0)
{
prev = current;
current = current->next;
}
old_info = current->info;
free (current);
prev->next = 0;
return root;
}
template <class T>
Deque <T> *find(Deque <T> *root, T x)
{
if(q)
return(q)->info;
else return 0;
}
template <class T>
void isempty(Deque <T> *q)
{
if(*q) return 0;
else return 1;
}
template <class T>
void show(Deque <T> *q)
{
cout<<"\n";
if(q)
{
cout<<" "<<q->info;
show(q->next);
}
}
void main()
{
clrscr();
Racio r1(0,0);
DEQUE<Racio>deq;
cin >> r1;deq.insert(r1);
cin >> r1; deq.append(r1);
deq.show();
cout <<"Press Any Key To Remove First and Last Elements" << endl;
getch();
deq.take_first(r1);
deq.show();
cout << "The Elements Of The Deque Have Been Removed" << endl;
cout <<"Press Any Key To Exit" << endl;
getch();
}