Исходник класса рациональных дробей, работа с дробями в очереди с двусторонним доступом


Добавил:DMT
Дата создания:25 апреля 2008, 14:18
Дата обновления:25 апреля 2008, 14:18
Просмотров:4361 последний сегодня, 0:45
Комментариев: 0

Исходник класса рациональных дробей, работа с дробями в очереди с двусторонним доступом

Код на C++
  1. #include <process.h>
  2. #include <iostream.h>
  3. #include <conio.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. class Racio
  7. {
  8. private:
  9. float chisl;
  10. float znamen;
  11.  
  12. public:
  13. Racio() {};
  14. Racio(float c, float z=1) {chisl=c; znamen=z;}
  15. ~Racio() {}
  16. int operator==(Racio &obj);
  17. int operator<(Racio &obj);
  18. int operator>(Racio &obj);
  19. Racio operator*(Racio &);
  20. friend ostream &operator<<(ostream &, Racio &);
  21. friend istream &operator>>(istream &, Racio &);
  22. };
  23. int Racio::operator==(Racio &obj)
  24. {
  25. if(chisl==obj.chisl&&znamen==obj.znamen)return 1;
  26. else
  27. return 0;
  28. }
  29. int Racio::operator<(Racio &obj)
  30. {
  31. float d1=chisl/znamen;
  32. float d2=obj.chisl/obj.znamen;
  33. if(d1<d2)return 1;
  34. return 0;
  35. }
  36. //Перегрузка >
  37. int Racio::operator>(Racio &obj)
  38. {
  39. float d1=chisl/znamen;
  40. float d2=obj.chisl/obj.znamen;
  41. if(d1>d2)return 1;
  42. return 0;
  43. }
  44. //Перегрузка *
  45. Racio Racio::operator*(Racio &fp1)
  46. {
  47. float i,j;
  48. i=chisl*fp1.chisl;
  49. j=znamen*fp1.znamen;
  50. fp1.chisl=i;
  51. fp1.znamen=j;
  52. return fp1;
  53. }
  54. ostream &operator<< (ostream &fo, Racio &fp)
  55. {
  56. fo << fp.chisl << "/" <<fp.znamen<<"\n";
  57. return fo;
  58. }
  59.  
  60. //Перегрузка оператора >>
  61. istream &operator>>(istream &fi, Racio &fp)
  62. {
  63. cout<< "Введите числитель:";
  64. fi >>fp.chisl;
  65. cout<< "Введите знаменатель:";
  66. fi >> fp.znamen;
  67. if(fp.znamen==0) {cout << "The Error"; getch(); exit(1);}//ноль недопустим
  68. float a=fp.chisl;
  69. float b=fp.znamen;
  70. if (a<0 && b<0) {a=abs(-a);b=abs(-b);}
  71. else if(b<0){b=-b;a=-a;};
  72. int a1=abs(a);
  73. fp.chisl=a;
  74. fp.znamen=b;
  75. while (a1!=b)
  76. {
  77. if(a1>b) a1=a1-b;
  78. if(b>a1) b=b-a1;
  79. }
  80. fp.chisl=fp.chisl/a1;
  81. fp.znamen=fp.znamen/a1;
  82. return fi;
  83. }
  84. template <class T>
  85. struct Deque
  86. {
  87. T info;
  88. struct Deque <T> *next;
  89. };
  90.  
  91. template <class T>
  92. struct LIST
  93. {
  94. Deque <T> *root;//єърчрЄхы№ эр ¤ыхьхэЄ
  95. //ъюэёЄЁєъЄюЁ
  96. LIST() { root = 0;}
  97. };
  98.  
  99. template <class T>
  100. class DEQUE:public LIST <T>
  101. {
  102. public:
  103. void insert(T x)
  104. {
  105. root=::insert(root, x);
  106. }
  107.  
  108. void append(T x)
  109. {
  110. root=::append(root, x);
  111. }
  112.  
  113. void take_first(T x)
  114. {
  115. root=::take_first(root, x);
  116. }
  117.  
  118. void take_last(T x)
  119. {
  120. root=::take_last(root, x);
  121. }
  122.  
  123. void find(T x)
  124. {
  125. root=::find(root, x);
  126. }
  127.  
  128. void show()
  129. {
  130. ::show(root);
  131. }
  132. void isempty()
  133. {
  134. ::isempty(root);
  135. }
  136.  
  137. };
  138.  
  139. template <class T>
  140. Deque <T> *insert(Deque <T> *root, T x)
  141. {
  142. Deque <T> *new_x = (Deque <T> *)malloc (sizeof(Deque <T>));
  143. new_x->info = x;
  144. new_x->next = root;
  145. root = new_x;
  146. return root;
  147. }
  148.  
  149. template <class T>
  150. Deque <T> *append(Deque <T> *root, T x)
  151. {
  152. Deque <T> *current = root, *prev=0;
  153. Deque <T> *new_x = (Deque <T> *)malloc (sizeof(Deque <T>));
  154. new_x->info = x;
  155. new_x->next = 0;
  156. while (current)
  157. {
  158. prev = current;
  159. current = current->next;
  160. }
  161. if(prev) prev->next = new_x;
  162. else root = new_x;
  163. return root;
  164. }
  165.  
  166. template <class T>
  167. Deque <T> *take_first(Deque <T> *root, T x)
  168. {
  169. Deque <T> *old_x = root;
  170. T old_info=0;
  171. if(root)
  172. {
  173. old_info = old_x->info;
  174. root=(root)->next;
  175. free(old_x);
  176. }
  177. return root;
  178. }
  179.  
  180. template <class T>
  181. Deque <T> *take_last(Deque <T> *root, T x)
  182. {
  183. Deque <T> *current = root, *prev = root;
  184. T old_info =0;
  185. if(current->next == 0)
  186. {
  187. root = 0;
  188. old_info = current->info;
  189. free(current);
  190. }
  191. else
  192. while (current->next!=0)
  193. {
  194. prev = current;
  195. current = current->next;
  196. }
  197. old_info = current->info;
  198. free (current);
  199. prev->next = 0;
  200. return root;
  201. }
  202.  
  203. template <class T>
  204. Deque <T> *find(Deque <T> *root, T x)
  205. {
  206. if(q)
  207. return(q)->info;
  208. else return 0;
  209. }
  210.  
  211. template <class T>
  212. void isempty(Deque <T> *q)
  213. {
  214. if(*q) return 0;
  215. else return 1;
  216.  
  217. }
  218.  
  219. template <class T>
  220. void show(Deque <T> *q)
  221. {
  222. cout<<"\n";
  223. if(q)
  224. {
  225. cout<<" "<<q->info;
  226. show(q->next);
  227. }
  228. }
  229.  
  230. void main()
  231. {
  232. clrscr();
  233. Racio r1(0,0);
  234. DEQUE<Racio>deq;
  235. cin >> r1;deq.insert(r1);
  236. cin >> r1; deq.append(r1);
  237. deq.show();
  238. cout <<"Press Any Key To Remove First and Last Elements" << endl;
  239. getch();
  240. deq.take_first(r1);
  241. deq.show();
  242. cout << "The Elements Of The Deque Have Been Removed" << endl;
  243. cout <<"Press Any Key To Exit" << endl;
  244. getch();
  245.  
  246. }
При использовании обязательна ссылка на http://DMTSoft.ru
up