Advertisement
huutho_96

Untitled

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