Advertisement
huutho_96

Untitled

Mar 12th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.12 KB | None | 0 0
  1. //
  2.  
  3.  
  4. #include <iostream>
  5. #include <list>
  6. #include <ctype.h>
  7. #include "iostream"
  8. #include "math.h"
  9. #include <windows.h>
  10. #include <conio.h>
  11.  
  12. using namespace std;
  13.  
  14. list<int> data;
  15. list<int>::iterator iter;
  16. void gotoxy(int x, int y)
  17. {
  18. COORD pos = { x, y };
  19. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  20. }
  21. struct NODE
  22. {
  23. int x;
  24. NODE *pNEXT;
  25. };
  26. struct LIST
  27. {
  28. NODE *pHead, *pTail;
  29. };
  30. void CreatEmptyList(LIST &list)
  31. {
  32. list.pHead = list.pTail = NULL;
  33. }
  34. int ListLen(LIST list)
  35. {
  36. int dem = 0;
  37. NODE *p = list.pHead;
  38. p = p->pNEXT;
  39. while (p != NULL)
  40. {
  41. dem = dem + 1;
  42. p = p->pNEXT;
  43. }
  44. return dem;
  45. }
  46. bool IsEmptyList(LIST list)
  47. {
  48. return(list.pHead == NULL && list.pTail == NULL);
  49. }
  50. void AddHead(LIST &list, NODE *pNew)
  51. {
  52. pNew->pNEXT = list.pHead;
  53. list.pHead = pNew;
  54. }
  55. NODE *CreatNode(int x)
  56. {
  57. NODE *p;
  58. p = new NODE;
  59. if (p == NULL)
  60. exit(0);
  61. p->x = x;
  62. p->pNEXT = NULL;
  63. return p;
  64. }
  65. void AddNode(LIST &list, NODE *pNew, int k)
  66. {
  67. NODE *p = list.pHead;
  68. for (int i = 1; i < k; i++)
  69. p = p->pNEXT;
  70. pNew->pNEXT = p->pNEXT;
  71. p->pNEXT = pNew;
  72. }
  73. void Input(LIST &list)
  74. {
  75. NODE *pNew;
  76. int x;
  77. char c[2];
  78. do
  79. {
  80. cout << "nhap gia tri nguyen (# de ket thuc): ";
  81. cin >> c;
  82. fflush(stdin);
  83. if (c[0] == '#') break;
  84. x = atoi(c);
  85. pNew = CreatNode(x);
  86. AddHead(list, pNew);
  87. } while (1);
  88. }
  89. void Output(LIST list)
  90. {
  91. int x;
  92. NODE *pNew;
  93. pNew = list.pHead;
  94. while (pNew != NULL)
  95. {
  96. x = pNew->x;
  97. cout << x << endl;
  98. pNew = pNew->pNEXT;
  99. }
  100. }
  101. float Timchanduong(LIST list, int &dem)
  102. {
  103. int x, tong = 0;
  104. float tb;
  105.  
  106. NODE *pNEW;
  107. pNEW = list.pHead;
  108.  
  109. while (pNEW != NULL)
  110. {
  111. x = pNEW->x;
  112. if (x % 2 == 0 && x>0)
  113. {
  114. tong += x;
  115. dem++;
  116. }
  117. pNEW = pNEW->pNEXT;
  118. }
  119. return float(tong) / dem;
  120. }
  121. int GetInput()
  122. {
  123. int choice;
  124. cin >> choice;
  125. return choice;
  126. }
  127. int cau5(LIST list)
  128. {
  129. int dem = 0;
  130. float tbc;
  131. tbc = Timchanduong(list, dem);
  132. cout << "co " << dem << " so chan duong\ntrungbinh: " << tbc << endl;
  133. return 0;
  134. }
  135. int timk(LIST list, int k)
  136. {
  137. NODE *p = list.pHead;
  138. p = p->pNEXT;
  139. int dem = 0;
  140. while (p != NULL)
  141. {
  142. if (p->x == k) dem = dem + 1;
  143. p = p->pNEXT;
  144. }
  145. return dem;
  146. }
  147. int timchanlientiep(LIST list, int &vitri)
  148. {
  149. NODE *p = list.pHead;
  150. p = p->pNEXT;
  151. int dem = 0;
  152. vitri = 0;
  153. while (p != NULL && dem < 3)
  154. {
  155. vitri++;
  156. if (p->x % 2 == 0) dem++;
  157. else dem = 0;
  158. p = p->pNEXT;
  159. }
  160. if (dem == 3)
  161. {
  162.  
  163. return 1;
  164. }
  165. else return 0;
  166. }
  167. void ChinhPhuong(LIST list)
  168. {
  169. NODE *p = list.pHead;
  170. p = p->pNEXT;
  171. int a;
  172. while (p != NULL)
  173. {
  174. a = sqrt(float(p->x));
  175. if (p->x == a * a) cout << p->x << endl;
  176. p = p->pNEXT;
  177. }
  178. }
  179. int snt(int n)
  180. {
  181. if (n < 2) return 0;
  182. if (n == 2) return 1;
  183. for (int i = 2; i <= sqrt(float(n)); i++)
  184. if (n % i == 0) return 0;
  185. return 1;
  186. }
  187. void delnode(LIST &list, NODE *pDel)
  188. {
  189. NODE *p = list.pHead;
  190. if (list.pHead->pNEXT == pDel) list.pHead->pNEXT = pDel->pNEXT;
  191. else
  192. {
  193. while (p->pNEXT != pDel) p = p->pNEXT;
  194. p->pNEXT = pDel->pNEXT;
  195. }
  196. }
  197. void xoant(LIST &list)
  198. {
  199. NODE *p = list.pHead;
  200. p = p->pNEXT;
  201. while (p != NULL)
  202. {
  203. if (snt(p->x) == 1) delnode(list, p);
  204. p = p->pNEXT;
  205. }
  206. }
  207. void xoatrung(LIST &list)
  208. {
  209. NODE *p = list.pHead;
  210. p = p->pNEXT;
  211. NODE *q = new NODE;
  212. while (p != NULL)
  213. {
  214. q = p;
  215. while (q->pNEXT != NULL)
  216. {
  217. if (p->x == q->pNEXT->x)
  218. {
  219. NODE *t = new NODE;
  220. t->pNEXT = q->pNEXT->pNEXT;
  221. delete q->pNEXT;
  222. q->pNEXT = t;
  223. }
  224. q = q->pNEXT;
  225. }
  226. p = p->pNEXT;
  227. }
  228. }
  229. void KhungChuNhat(int a, int b, int c, int d)
  230. {
  231. int i;
  232. for (i = a; i <= c; i++)
  233. {
  234. gotoxy(i, b); printf("\xcd");
  235. gotoxy(i, d); printf("\xcd");
  236. }
  237. for (i = b; i <= d; i++)
  238. {
  239. gotoxy(a, i); printf("\xba");
  240. gotoxy(c, i); printf("\xba");
  241. }
  242. gotoxy(a, b); printf("\xc9");
  243. gotoxy(a, d); printf("\xc8");
  244. gotoxy(c, b); printf("\xbb");
  245. gotoxy(c, d); printf("\xbc");
  246. }
  247. void DisplayMainMenu()
  248. {
  249. system("cls");
  250. int i = 13;
  251. KhungChuNhat(12, 5, 70, 10);
  252. gotoxy(42, 7); cout << "BAI LAM NHOM";
  253. KhungChuNhat(12, 12, 70, 30);
  254. gotoxy(15, i++); cout << "Lua chon cong viec\n";
  255. gotoxy(15, i++); cout << "1 - Tao danh sach so\n";
  256. gotoxy(15, i++); cout << "2 - Them mot phan tu vao danh sach\n";
  257. gotoxy(15, i++); cout << "3 - Dem cac so k trong day va kiem";
  258. gotoxy(20, i); cout << "tra xem co chia het 3 khong\n";
  259. i++;
  260. gotoxy(15, i++); cout << "4 - Kiem tra va xuat 3 so chan duong canh nhau (neu co)\n";
  261. gotoxy(15, i++); cout << "5 - Dem so cac phan tu la so chan ";
  262. gotoxy(20, i); cout << "duong va xuat trung binh cong cac so\n";
  263. i++;
  264. gotoxy(15, i++); cout << "6 - Sap xep va hien danh sach theo thu tu tang dan\n";
  265. gotoxy(15, i++); cout << "7 - Liet ke cac so chinh phuong\n";
  266. gotoxy(15, i++); cout << "8 - Xoa tat ca cac so nguyen to trong danh sach\n";
  267. gotoxy(15, i++); cout << "9 - Xoa cac gia tri trung nhau trong danh sach\n";
  268. gotoxy(15, i++); cout << "Lua chon: ";
  269. }
  270. void InterchangeSort_ListLinked(LIST &list)
  271. {
  272. NODE *p = new NODE;
  273. NODE *q = new NODE;
  274. NODE *u = new NODE;
  275. NODE *v = new NODE;
  276. NODE *t = new NODE;
  277. for (u = list.pHead, p = list.pHead->pNEXT; p != NULL; u=u->pNEXT, p = p->pNEXT)
  278. for (v = u->pNEXT, q = p->pNEXT; q != NULL; v = v->pNEXT, q = q->pNEXT)
  279. if (p->x > q->x)
  280. if (p->pNEXT == q)
  281. {
  282. p->pNEXT = q->pNEXT;
  283. q->pNEXT = p;
  284.  
  285. u->pNEXT = p;
  286. }
  287. else
  288. {
  289. t->pNEXT = q->pNEXT;
  290. q->pNEXT = p->pNEXT;
  291. p->pNEXT = t->pNEXT;
  292. u->pNEXT = q;
  293. v->pNEXT = p;
  294. }
  295. //delete t;
  296. }
  297. int main()
  298. {
  299. LIST list;
  300. CreatEmptyList(list);
  301. do
  302. {
  303. DisplayMainMenu();
  304. switch (GetInput())
  305. {
  306. case 1:
  307. system("cls");
  308. Input(list);
  309. break;
  310. case 2:
  311. int giatri;
  312. int vitri;
  313. NODE *p;
  314. system("cls");
  315. do
  316. {
  317. cout << "Vi tri muon them: ";
  318. cin >> vitri;
  319. } while (vitri < 0 || vitri > ListLen(list));
  320. cout << "Gia tri muon them: ";
  321. cin >> giatri;
  322. p = CreatNode(giatri);
  323. AddNode(list, p, vitri);
  324. break;
  325. case 3:
  326. system("cls");
  327. int k, dem;
  328. do
  329. {
  330. cout << "Gia tri k: ";
  331. cin >> k;
  332. } while (k == 0);
  333. dem = timk(list, k);
  334. if (dem == 0) cout << "Khong co gia tri k";
  335. else
  336. {
  337. cout << "co gia tri " << k << "\n";
  338. if (dem % 3 == 0) cout << "chia het 3\n";
  339. else cout << "khong chia het 3";
  340. }
  341. _getch();
  342. break;
  343. case 4:
  344. int vitri1;
  345. system("cls");
  346. if (timchanlientiep(list, vitri1) == 1) cout << "co 3 so chan lien tiep, vi tri: " << vitri1;
  347. _getch();
  348. break;
  349. case 5:
  350. system("cls");
  351. cau5(list);
  352. _getch();
  353. break;
  354. case 6:
  355. system("cls");
  356. InterchangeSort_ListLinked(list);
  357. Output(list);
  358. _getch();
  359. break;
  360. case 7:
  361. ChinhPhuong(list);
  362. _getch();
  363. break;
  364. case 8:
  365. system("cls");
  366. xoant(list);
  367. Output(list);
  368. _getch();
  369. break;
  370. case 9:
  371. system("cls");
  372. xoatrung(list);
  373. Output(list);
  374. _getch();
  375. break;
  376. default:
  377. system("cls");
  378. Output(list);
  379. _getch();
  380. break;
  381. }
  382.  
  383. } while (1);
  384.  
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement