Advertisement
Guest User

Untitled

a guest
May 24th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <string.h>
  5.  
  6. //NUMLIST
  7.  
  8. typedef struct numlist_element numlist_element;
  9. typedef struct numlist_handle numlist_handle;
  10.  
  11. numlist_handle * numlist_newHandle();
  12. numlist_element * numlist_newElement(double element);
  13. double numlist_get(int index, numlist_handle * head);
  14. void numlist_add(double element, numlist_handle * head);
  15. void numlist_remove(int index, numlist_handle * head);
  16. void numlist_insert(int index, numlist_handle * head, double element);
  17. void numlist_clear(numlist_handle * head);
  18. void numlist_reverse(numlist_handle * head);
  19. void numlist_swap(numlist_handle * head, int first, int second);
  20. void numlist_set(int index, double value, numlist_handle * head);
  21. void numlist_sort(numlist_handle * head);
  22. numlist_handle numlist_copy(numlist_handle * source);
  23.  
  24. //BOOLLIST
  25. typedef struct boollist_element boollist_element;
  26. typedef struct boollist_handle boollist_handle;
  27.  
  28. boollist_handle * boollist_newHandle();
  29. boollist_element * boollist_newElement(int element);
  30. int boollist_get(int index, boollist_handle * head);
  31. void boollist_add(int element, boollist_handle * head);
  32. void boollist_remove(int index, boollist_handle * head);
  33. void boollist_insert(int index, boollist_handle * head, int element);
  34. void boollist_clear(boollist_handle * head);
  35. void boollist_reverse(boollist_handle * head);
  36. void boollist_swap(boollist_handle * head, int first, int second);
  37. void boollist_set(int index, int value, boollist_handle * head);
  38. boollist_handle boollist_copy(boollist_handle * source);
  39.  
  40. //STRING
  41. typedef struct string_element string_element;
  42. typedef struct string_handle string_handle;
  43.  
  44. string_handle * string_newHandle();
  45. string_element * string_newElement(char element);
  46. char string_get(int index, string_handle * head);
  47. void string_add(char element, string_handle * head);
  48. void string_remove(int index, string_handle * head);
  49. void string_insert(int index, string_handle * head, char element);
  50. void string_clear(string_handle * head);
  51. void string_reverse(string_handle * head);
  52. void string_swap(string_handle * head, int first, int second);
  53. void string_set(int index, char value, string_handle * head);
  54. void string_sort(string_handle * head);
  55. string_handle string_copy(string_handle * source);
  56. int string_equals(string_handle * first, string_handle * second);
  57. //LIST OF STRINGS
  58.  
  59. typedef struct stringlist_handle stringlist_handle;
  60. typedef struct stringlist_element stringlist_element;
  61. stringlist_handle stringlist_new();
  62. stringlist_element *stringlist_newElement(string_handle *inputElement);
  63. string_handle stringlist_get(int index, stringlist_handle * head);
  64. void stringlist_add(string_handle *inputElement, stringlist_handle * head);
  65. void stringlist_remove(int index, stringlist_handle * head);
  66. void stringlist_insert(int index, stringlist_handle * head, string_handle *inputElement);
  67. void stringlist_clear(stringlist_handle * head);
  68. void stringlist_reverse(stringlist_handle * head);
  69. void stringlist_swap(stringlist_handle * head, int first, int second);
  70. void stringlist_set(int index, string_handle *value, stringlist_handle * head);
  71. stringlist_handle stringlist_copy(stringlist_handle * source);
  72.  
  73. //STANDARD FUNCTIONS
  74. int program_convertNumToString(double input, string_handle * output);
  75. int program_convertBoolToString(int input, string_handle * output);
  76. int program_convertStringToNum(string_handle * input, double *output);
  77. int program_convertStringToBool(string_handle * input, int *output);
  78. string_handle standard_read();
  79. void standard_printNum(double input);
  80. void standard_printBool(int input);
  81. void standard_printString(string_handle * input);
  82. void standard_printChars(char input[]);
  83. string_handle standard_createString(char input[]);
  84. void standard_appendChars(string_handle * head, char input[]);
  85. void standard_appendString(string_handle * head, string_handle * input);
  86. void standard_appendNum(string_handle * head, double input);
  87. void standard_appendBool(string_handle * head, int input);
  88. //NUMLIST
  89. struct numlist_handle {
  90. numlist_element *first;
  91. numlist_element *last;
  92. int size;
  93. };
  94.  
  95. struct numlist_element{
  96. double element;
  97. numlist_element *next;
  98. };
  99.  
  100. numlist_handle numlist_new(){
  101. numlist_handle head;
  102. head.first = NULL;
  103. head.last = NULL;
  104. head.size = 0;
  105.  
  106. return head;
  107. }
  108.  
  109. numlist_element *numlist_newElement(double inputElement){
  110. numlist_element * element = malloc(sizeof(numlist_element));
  111. element->element = inputElement;
  112. element->next = NULL;
  113.  
  114. return element;
  115. }
  116.  
  117. double numlist_get(int index, numlist_handle * head){
  118. numlist_element * current = head->first;
  119. int i;
  120. for (i=0; i<index && current != NULL; i++) {
  121. current = current->next;
  122. }
  123. if (current == NULL){
  124. raise(SIGSEGV); //Segmentation fault
  125. }
  126. return current->element;
  127. }
  128.  
  129. void numlist_add(double inputElement, numlist_handle * head){
  130. numlist_element * element = numlist_newElement(inputElement);
  131. if (head->first == NULL)
  132. {
  133. head->first = element;
  134. }
  135. else{
  136. numlist_element * current = head->first;
  137. while(current->next != NULL){
  138. current = current->next;
  139. }
  140. current->next = element;
  141. }
  142. head->last = element;
  143. head->size ++;
  144. }
  145.  
  146. void numlist_remove(int index, numlist_handle * head){
  147. if (index >= head->size){
  148. raise(SIGSEGV); //Segmentation fault
  149. }
  150. numlist_element * current = head->first;
  151. numlist_element * previous = NULL;
  152.  
  153. if(index == 0 && head->first != NULL){
  154. head->first = head->first->next;
  155. }
  156. else{
  157. for (int i=0; i<index; i++) {
  158. previous = current;
  159. current = current->next;
  160. }
  161. previous->next = current->next;
  162. }
  163.  
  164.  
  165.  
  166. if(current->next == NULL){
  167. head->last = previous;
  168. }
  169. free(current); //releases memory
  170. head->size--;
  171. }
  172.  
  173. void numlist_insert(int index, numlist_handle * head, double inputElement){
  174. if (index > head->size){
  175. raise(SIGSEGV); //Segmentation fault
  176. }
  177. numlist_element * element = numlist_newElement(inputElement);
  178. numlist_element * current = head->first;
  179. numlist_element * previous = NULL;
  180.  
  181. if(index == 0){
  182. element->next = current;
  183. head->first = element;
  184. }
  185. else{
  186. for (int i=0; i<index; i++) {
  187. previous = current;
  188. current = current->next;
  189. }
  190. element->next = current;
  191. previous->next = element;
  192. }
  193.  
  194.  
  195.  
  196. if(element->next == NULL){
  197. head->last = element;
  198. }
  199. head->size++;
  200. }
  201.  
  202. void numlist_clear(numlist_handle * head){
  203. while(head->size != 0){
  204. numlist_remove(0, head);
  205. }
  206. }
  207.  
  208. void numlist_reverse(numlist_handle * head){
  209. //PROBLEMER MED LISTE AF LISTER
  210. numlist_handle temporayHandle = numlist_new();
  211. while(head->size != 0){
  212. double element = numlist_get(0, head);
  213. numlist_remove(0, head);
  214. numlist_insert(0, &temporayHandle, element);
  215. }
  216. //ET ALTERNATIV TIL HEAD = TEMPORAYHANDLE?
  217. while(temporayHandle.size != 0){
  218. double element = numlist_get(0, &temporayHandle);
  219. numlist_remove(0, &temporayHandle);
  220. numlist_add(element, head);
  221. }
  222. }
  223.  
  224. void numlist_swap(numlist_handle * head, int first, int second){
  225. if (first >= head->size || second >= head->size){
  226. raise(SIGSEGV); //Segmentation fault
  227. }
  228. double temp = numlist_get(first, head);
  229. numlist_set(first, numlist_get(second, head), head);
  230. numlist_set(second, temp, head);
  231. }
  232.  
  233. void numlist_set(int index, double value, numlist_handle * head){
  234. if (index >= head->size){
  235. raise(SIGSEGV); //Segmentation fault
  236. }
  237. numlist_element * current = head->first;
  238.  
  239. for (int i=0; i<index; i++) {
  240. current = current->next;
  241. }
  242. current->element = value;
  243. }
  244.  
  245. int max (numlist_handle * head, int n, int i, int j, int k) {
  246. int m = i;
  247. if (j < n && numlist_get(j, head) > numlist_get(m, head)) {
  248. m = j;
  249. }
  250. if (k < n && numlist_get(k, head) > numlist_get(m, head)) {
  251. m = k;
  252. }
  253. return m;
  254. }
  255.  
  256. void downheap (numlist_handle * head, int n, int i) {
  257. while (1) {
  258. int j = max(head, n, i, 2 * i + 1, 2 * i + 2);
  259. if (j == i) {
  260. break;
  261. }
  262. numlist_swap(head, j, i);
  263. i = j;
  264. }
  265. }
  266.  
  267. void numlist_sort(numlist_handle * head){
  268. //https://rosettacode.org/wiki/Sorting_algorithms/Heapsort#C
  269. int n = head->size;
  270. for (int i = (n - 2) / 2; i >= 0; i--) {
  271. downheap(head, n, i);
  272. }
  273. for (int i = 0; i < n; i++) {
  274. numlist_swap(head, n - i - 1, 0);
  275. downheap(head, n - i - 1, 0);
  276. }
  277. }
  278.  
  279. numlist_handle numlist_copy(numlist_handle * source){
  280. numlist_handle destination = numlist_new();
  281. numlist_element * current = source->first;
  282. for (int i = 0; i < source->size; ++i)
  283. {
  284. numlist_element * element = numlist_newElement(current->element);
  285. current = current->next;
  286. numlist_add(element->element, &destination);
  287. }
  288. return destination;
  289. }
  290.  
  291. //BOOLLIST
  292. struct boollist_handle {
  293. boollist_element *first;
  294. boollist_element *last;
  295. int size;
  296. };
  297.  
  298. struct boollist_element{
  299. int element;
  300. boollist_element *next;
  301. };
  302.  
  303. boollist_handle boollist_new(){
  304. boollist_handle head;
  305. head.first = NULL;
  306. head.last = NULL;
  307. head.size = 0;
  308.  
  309. return head;
  310. }
  311.  
  312. boollist_element *boollist_newElement(int inputElement){ // ALLWAYS HAVE ADDCHARACTORTOLIST() CALL THIS!
  313. boollist_element * element = malloc(sizeof(boollist_element));
  314. element->element = inputElement;
  315. element->next = NULL;
  316.  
  317. return element;
  318. }
  319.  
  320. int boollist_get(int index, boollist_handle * head){
  321. boollist_element * current = head->first;
  322. int i;
  323. for (i=0; i<index && current != NULL; i++) {
  324. current = current->next;
  325. }
  326. if (current == NULL){
  327. raise(SIGSEGV); //Segmentation fault
  328. }
  329. return current->element;
  330. }
  331.  
  332. void boollist_add(int inputElement, boollist_handle * head){
  333. boollist_element * element = boollist_newElement(inputElement);
  334. if (head->first == NULL)
  335. {
  336. head->first = element;
  337. }
  338. else{
  339. boollist_element * current = head->first;
  340. while(current->next != NULL){
  341. current = current->next;
  342. }
  343. current->next = element;
  344. }
  345. head->last = element;
  346. head->size ++;
  347. }
  348.  
  349. void boollist_remove(int index, boollist_handle * head){
  350. if (index >= head->size){
  351. raise(SIGSEGV); //Segmentation fault
  352. }
  353. boollist_element * current = head->first;
  354. boollist_element * previous = NULL;
  355.  
  356. if(index == 0 && head->first != NULL){
  357. head->first = head->first->next;
  358. }
  359. else{
  360. for (int i=0; i<index; i++) {
  361. previous = current;
  362. current = current->next;
  363. }
  364. previous->next = current->next;
  365. }
  366.  
  367.  
  368.  
  369. if(current->next == NULL){
  370. head->last = previous;
  371. }
  372. free(current); //releases memory
  373. head->size--;
  374. }
  375.  
  376. void boollist_insert(int index, boollist_handle * head, int inputElement){
  377. if (index > head->size){
  378. raise(SIGSEGV); //Segmentation fault
  379. }
  380. boollist_element * element = boollist_newElement(inputElement);
  381. boollist_element * current = head->first;
  382. boollist_element * previous = NULL;
  383.  
  384. if(index == 0){
  385. element->next = current;
  386. head->first = element;
  387. }
  388. else{
  389. for (int i=0; i<index; i++) {
  390. previous = current;
  391. current = current->next;
  392. }
  393. element->next = current;
  394. previous->next = element;
  395. }
  396.  
  397.  
  398.  
  399. if(element->next == NULL){
  400. head->last = element;
  401. }
  402. head->size++;
  403. }
  404.  
  405. void boollist_clear(boollist_handle * head){
  406. while(head->size != 0){
  407. boollist_remove(0, head);
  408. }
  409. }
  410.  
  411. void boollist_reverse(boollist_handle * head){
  412. //PROBLEMER MED LISTE AF LISTER
  413. boollist_handle temporayHandle = boollist_new();
  414. while(head->size != 0){
  415. int element = boollist_get(0, head);
  416. boollist_remove(0, head);
  417. boollist_insert(0, &temporayHandle, element);
  418. }
  419. //ET ALTERNATIV TIL HEAD = TEMPORAYHANDLE?
  420. while(temporayHandle.size != 0){
  421. int element = boollist_get(0, &temporayHandle);
  422. boollist_remove(0, &temporayHandle);
  423. boollist_add(element, head);
  424. }
  425. }
  426.  
  427. void boollist_swap(boollist_handle * head, int first, int second){
  428. if (first >= head->size || second >= head->size){
  429. raise(SIGSEGV); //Segmentation fault
  430. }
  431. int temp = boollist_get(first, head);
  432. boollist_set(first, boollist_get(second, head), head);
  433. boollist_set(second, temp, head);
  434. }
  435.  
  436. void boollist_set(int index, int value, boollist_handle * head){
  437. if (index >= head->size){
  438. raise(SIGSEGV); //Segmentation fault
  439. }
  440. boollist_element * current = head->first;
  441.  
  442. for (int i=0; i<index; i++) {
  443. current = current->next;
  444. }
  445. current->element = value;
  446. }
  447.  
  448. boollist_handle boollist_copy(boollist_handle * source){
  449. boollist_handle destination = boollist_new();
  450. boollist_element * current = source->first;
  451. for (int i = 0; i < source->size; ++i)
  452. {
  453. boollist_element * element = boollist_newElement(current->element);
  454. current = current->next;
  455. boollist_add(element->element, &destination);
  456. }
  457. return destination;
  458. }
  459.  
  460. //STRING
  461. struct string_handle {
  462. string_element *first;
  463. string_element *last;
  464. int size;
  465. };
  466.  
  467. struct string_element{
  468. char element;
  469. string_element *next;
  470. };
  471.  
  472. string_handle string_new(){
  473. string_handle head;
  474. head.first = NULL;
  475. head.last = NULL;
  476. head.size = 0;
  477.  
  478. return head;
  479. }
  480.  
  481. string_element *string_newElement(char inputElement){ // ALLWAYS HAVE ADDCHARACTORTOLIST() CALL THIS!
  482. string_element * element = malloc(sizeof(string_element));
  483. element->element = inputElement;
  484. element->next = NULL;
  485.  
  486. return element;
  487. }
  488.  
  489. char string_get(int index, string_handle * head){
  490. string_element * current = head->first;
  491. int i;
  492. for (i=0; i<index && current != NULL; i++) {
  493. current = current->next;
  494. }
  495. if (current == NULL){
  496. raise(SIGSEGV); //Segmentation fault
  497. }
  498. return current->element;
  499. }
  500.  
  501. void string_add(char inputElement, string_handle * head){
  502. string_element * element = string_newElement(inputElement);
  503. if (head->first == NULL)
  504. {
  505. head->first = element;
  506. }
  507. else{
  508. string_element * current = head->first;
  509. while(current->next != NULL){
  510. current = current->next;
  511. }
  512. current->next = element;
  513. }
  514. head->last = element;
  515. head->size ++;
  516. }
  517.  
  518. void string_remove(int index, string_handle * head){
  519. if (index >= head->size){
  520. raise(SIGSEGV); //Segmentation fault
  521. }
  522. string_element * current = head->first;
  523. string_element * previous = NULL;
  524.  
  525. if(index == 0 && head->first != NULL){
  526. head->first = head->first->next;
  527. }
  528. else{
  529. for (int i=0; i<index; i++) {
  530. previous = current;
  531. current = current->next;
  532. }
  533. previous->next = current->next;
  534. }
  535.  
  536.  
  537.  
  538. if(current->next == NULL){
  539. head->last = previous;
  540. }
  541. free(current); //releases memory
  542. head->size--;
  543. }
  544.  
  545. void string_insert(int index, string_handle * head, char inputElement){
  546. if (index > head->size){
  547. raise(SIGSEGV); //Segmentation fault
  548. }
  549. string_element * element = string_newElement(inputElement);
  550. string_element * current = head->first;
  551. string_element * previous = NULL;
  552.  
  553. if(index == 0){
  554. element->next = current;
  555. head->first = element;
  556. }
  557. else{
  558. for (int i=0; i<index; i++) {
  559. previous = current;
  560. current = current->next;
  561. }
  562. element->next = current;
  563. previous->next = element;
  564. }
  565.  
  566.  
  567.  
  568. if(element->next == NULL){
  569. head->last = element;
  570. }
  571. head->size++;
  572. }
  573.  
  574. void string_clear(string_handle * head){
  575. while(head->size != 0){
  576. string_remove(0, head);
  577. }
  578. }
  579.  
  580. void string_reverse(string_handle * head){
  581. //PROBLEMER MED LISTE AF LISTER
  582. string_handle temporayHandle = string_new();
  583. while(head->size != 0){
  584. char element = string_get(0, head);
  585. string_remove(0, head);
  586. string_insert(0, &temporayHandle, element);
  587. }
  588. //ET ALTERNATIV TIL HEAD = TEMPORAYHANDLE?
  589. while(temporayHandle.size != 0){
  590. char element = string_get(0, &temporayHandle);
  591. string_remove(0, &temporayHandle);
  592. string_add(element, head);
  593. }
  594. }
  595.  
  596. void string_swap(string_handle * head, int first, int second){
  597. if (first >= head->size || second >= head->size){
  598. raise(SIGSEGV); //Segmentation fault
  599. }
  600. char temp = string_get(first, head);
  601. string_set(first, string_get(second, head), head);
  602. string_set(second, temp, head);
  603. }
  604.  
  605. void string_set(int index, char value, string_handle * head){
  606. if (index >= head->size){
  607. raise(SIGSEGV); //Segmentation fault
  608. }
  609. string_element * current = head->first;
  610.  
  611. for (int i=0; i<index; i++) {
  612. current = current->next;
  613. }
  614. current->element = value;
  615. }
  616.  
  617. string_handle string_copy(string_handle * source){
  618. string_handle destination = string_new();
  619. string_element * current = source->first;
  620. for (int i = 0; i < source->size; ++i)
  621. {
  622. string_element * element = string_newElement(current->element);
  623. current = current->next;
  624. string_add(element->element, &destination);
  625. }
  626. return destination;
  627. }
  628.  
  629. int string_equals(string_handle * first, string_handle * second){
  630. if(first->size == second->size){
  631. for (int i = 0; i < first->size; ++i)
  632. {
  633. if(string_get(i, first) != string_get(i, second))
  634. return 0;
  635. }
  636. return 1;
  637. }
  638. else
  639. return 0;
  640. }
  641.  
  642.  
  643. //LIST OF STRINGS
  644.  
  645. struct stringlist_handle {
  646. stringlist_element *first;
  647. stringlist_element *last;
  648. int size;
  649. };
  650.  
  651. struct stringlist_element{
  652. string_handle element;
  653. stringlist_element *next;
  654. };
  655.  
  656. stringlist_handle stringlist_new(){
  657. stringlist_handle head;
  658. head.first = NULL;
  659. head.last = NULL;
  660. head.size = 0;
  661.  
  662. return head;
  663. }
  664.  
  665. stringlist_element *stringlist_newElement(string_handle *inputElement){
  666. stringlist_element * element = malloc(sizeof(stringlist_element));
  667. element->element = *inputElement;
  668. element->next = NULL;
  669.  
  670. return element;
  671. }
  672.  
  673. string_handle stringlist_get(int index, stringlist_handle * head){
  674. stringlist_element * current = head->first;
  675. int i;
  676. for (i=0; i<index && current != NULL; i++) {
  677. current = current->next;
  678. }
  679. if (current == NULL){
  680. raise(SIGSEGV); //Segmentation fault
  681. }
  682. return current->element;
  683. }
  684.  
  685. void stringlist_add(string_handle *inputElement, stringlist_handle * head){
  686. stringlist_element * element = stringlist_newElement(inputElement);
  687. if (head->first == NULL)
  688. {
  689. head->first = element;
  690. }
  691. else{
  692. stringlist_element * current = head->first;
  693. while(current->next != NULL){
  694. current = current->next;
  695. }
  696. current->next = element;
  697. }
  698. head->last = element;
  699. head->size ++;
  700. }
  701.  
  702. void stringlist_remove(int index, stringlist_handle * head){
  703. if (index >= head->size){
  704. raise(SIGSEGV); //Segmentation fault
  705. }
  706. stringlist_element * current = head->first;
  707. stringlist_element * previous = NULL;
  708.  
  709. if(index == 0 && head->first != NULL){
  710. head->first = head->first->next;
  711. }
  712. else{
  713. for (int i=0; i<index; i++) {
  714. previous = current;
  715. current = current->next;
  716. }
  717. previous->next = current->next;
  718. }
  719.  
  720.  
  721.  
  722. if(current->next == NULL){
  723. head->last = previous;
  724. }
  725. free(current); //releases memory
  726. head->size--;
  727. }
  728.  
  729. void stringlist_insert(int index, stringlist_handle * head, string_handle *inputElement){
  730. if (index > head->size){
  731. raise(SIGSEGV); //Segmentation fault
  732. }
  733. stringlist_element * element = stringlist_newElement(inputElement);
  734. stringlist_element * current = head->first;
  735. stringlist_element * previous = NULL;
  736.  
  737. if(index == 0){
  738. element->next = current;
  739. head->first = element;
  740. }
  741. else{
  742. for (int i=0; i<index; i++) {
  743. previous = current;
  744. current = current->next;
  745. }
  746. element->next = current;
  747. previous->next = element;
  748. }
  749.  
  750. if(element->next == NULL){
  751. head->last = element;
  752. }
  753. head->size++;
  754. }
  755.  
  756. void stringlist_clear(stringlist_handle * head){
  757. while(head->size != 0){
  758. stringlist_remove(0, head);
  759. }
  760. }
  761.  
  762. void stringlist_reverse(stringlist_handle * head){
  763. stringlist_handle temporayHandle = stringlist_new();
  764. for (int i = 0; i < head->size; ++i)
  765. {
  766. string_handle temp = stringlist_get(i, head);
  767. stringlist_insert(0, &temporayHandle, &temp);
  768. }
  769.  
  770. for (int i = 0; i < temporayHandle.size; ++i)
  771. {
  772. string_handle temp = stringlist_get(i, &temporayHandle);
  773. stringlist_set(i, &temp, head);
  774. }
  775. }
  776.  
  777.  
  778. void stringlist_swap(stringlist_handle * head, int first, int second){
  779. if (first >= head->size || second >= head->size){
  780. raise(SIGSEGV); //Segmentation fault
  781. }
  782. string_handle tempFirst = stringlist_get(first, head);
  783. string_handle tempSecond = stringlist_get(second, head);
  784. stringlist_set(first, &tempSecond, head);
  785. stringlist_set(second, &tempFirst, head);
  786. }
  787.  
  788. void stringlist_set(int index, string_handle *value, stringlist_handle * head){
  789. if (index >= head->size){
  790. raise(SIGSEGV); //Segmentation fault
  791. }
  792. stringlist_element * current = head->first;
  793.  
  794. for (int i=0; i<index; i++) {
  795. current = current->next;
  796. }
  797. current->element = *value;
  798. }
  799.  
  800. stringlist_handle stringlist_copy(stringlist_handle * source){
  801. stringlist_handle destination = stringlist_new();
  802. stringlist_element * current = source->first;
  803. for (int i = 0; i < source->size; ++i)
  804. {
  805. stringlist_element * element = stringlist_newElement(&current->element);
  806. current = current->next;
  807. stringlist_add(&element->element, &destination);
  808. }
  809. return destination;
  810. }
  811.  
  812. //STANDARD FUNCTIONS
  813. int program_convertNumToString(double input, string_handle * output){
  814. char str[50];
  815. sprintf(str,"%lf",input);
  816. string_clear(output);
  817. for (int i = 0; str[i] != '\0' && i<50; ++i)
  818. {
  819. string_add(str[i], output);
  820. }
  821. return 1;
  822. }
  823. int program_convertBoolToString(int input, string_handle * output){
  824. char str[6], t[] = "true", f[] = "false";
  825. string_clear(output);
  826.  
  827. if(input){
  828. strcpy(str, t);
  829. }
  830. else
  831. strcpy(str, f);
  832. for (int i = 0; str[i] != '\0' && i<6; ++i)
  833. string_add(str[i], output);
  834.  
  835. return 1;
  836. }
  837. int program_convertStringToNum(string_handle * input, double *output){
  838. char *str = malloc(sizeof(char)*input->size);
  839. for (int i = 0; i < input->size; ++i)
  840. {
  841. str[i] = string_get(i, input);
  842. }
  843. int result = sscanf(str, "%lf", output);
  844. free(str);
  845. return result;
  846. }
  847. int program_convertStringToBool(string_handle * input, int *output){
  848. char *str = malloc(sizeof(char)*input->size);
  849. for (int i = 0; i < input->size; ++i)
  850. {
  851. str[i] = string_get(i, input);
  852. }
  853.  
  854. int result = sscanf(str, "%d", output);
  855. free(str);
  856. return result;
  857. }
  858. string_handle standard_read(){
  859. char str[100];
  860. scanf ("%[^\n]%*c", str);
  861.  
  862. string_handle head = string_new();
  863. for (int i = 0; str[i] != '\0' && i<100; ++i)
  864. {
  865. string_add(str[i], &head);
  866. }
  867. fflush(stdin);
  868. return head;
  869. }
  870.  
  871. void standard_printNum(double input){
  872. printf("%lf", input);
  873. }
  874.  
  875. void standard_printBool(int input){
  876. string_handle myString = string_new();
  877. program_convertBoolToString(input, &myString);
  878. for (int i = 0; i < myString.size; ++i)
  879. {
  880. printf("%c", string_get(i, &myString));
  881. }
  882. }
  883.  
  884. void standard_printString(string_handle * input){
  885. for (int i = 0; i < input->size; ++i)
  886. {
  887. printf("%c", string_get(i, input));
  888. }
  889. }
  890.  
  891. void standard_printChars(char input[]){
  892. printf("%s", input);
  893. }
  894.  
  895. string_handle standard_createString(char input[]){
  896. string_handle myString = string_new();
  897. for (int i = 0; input[i] != '\0'; ++i)
  898. {
  899. string_add(input[i], &myString);
  900. }
  901.  
  902. return myString;
  903. }
  904.  
  905. void standard_appendChars(string_handle * head, char input[]){
  906. string_handle myInputString = standard_createString(input);
  907. standard_appendString(head, &myInputString);
  908. string_clear(&myInputString);
  909. }
  910.  
  911. void standard_appendString(string_handle * head, string_handle * input){
  912. for (int i = 0; i < input->size; ++i)
  913. {
  914. string_add(string_get(i, input), head);
  915. }
  916. /* if(head->size == 0){
  917. head->last = input->last;
  918. head->first = input->first;
  919. }
  920. else{
  921. head->last->next = input->first;
  922. head->last = input->last;
  923. }
  924.  
  925. head->size += input->size;*/
  926. }
  927.  
  928. void standard_appendNum(string_handle * head, double input){
  929. string_handle myString = string_new();
  930. program_convertNumToString(input, &myString);
  931. standard_appendString(head, &myString);
  932. }
  933.  
  934. void standard_appendBool(string_handle * head, int input){
  935. string_handle myString = string_new();
  936. program_convertBoolToString(input, &myString);
  937. standard_appendString(head, &myString);
  938. }
  939.  
  940. typedef struct person person;
  941. string_handle person_ToString(person *this);
  942. typedef struct personlist_handle personlist_handle;
  943. typedef struct personlist_element personlist_element;
  944. personlist_handle personlist_new();
  945. personlist_element *personlist_newElement(person *inputElement);
  946. person personlist_get(int index, personlist_handle * head);
  947. void personlist_add(person *inputElement, personlist_handle * head);
  948. void personlist_remove(int index, personlist_handle * head);
  949. void personlist_insert(int index, personlist_handle * head, person *inputElement);
  950. void personlist_clear(personlist_handle * head);
  951. void personlist_reverse(personlist_handle * head);
  952. void personlist_swap(personlist_handle * head, int first, int second);
  953. void personlist_set(int index, person *value, personlist_handle * head);
  954. personlist_handle personlist_copy(personlist_handle * source);struct person {
  955. double age;string_handle name;
  956. };
  957. struct personlist_handle {
  958. personlist_element *first;
  959. personlist_element *last;
  960. int size;
  961. };
  962.  
  963. struct personlist_element{
  964. person element;
  965. personlist_element *next;
  966. };
  967.  
  968. personlist_handle personlist_new(){
  969. personlist_handle head;
  970. head.first = NULL;
  971. head.last = NULL;
  972. head.size = 0;
  973.  
  974. return head;
  975. }
  976.  
  977. personlist_element *personlist_newElement(person *inputElement){
  978. personlist_element * element = malloc(sizeof(personlist_element));
  979. element->element = *inputElement;
  980. element->next = NULL;
  981.  
  982. return element;
  983. }
  984.  
  985. person personlist_get(int index, personlist_handle * head){
  986. personlist_element * current = head->first;
  987. int i;
  988. for (i=0; i<index && current != NULL; i++) {
  989. current = current->next;
  990. }
  991. if (current == NULL){
  992. raise(SIGSEGV); //Segmentation fault
  993. }
  994. return current->element;
  995. }
  996.  
  997. void personlist_add(person *inputElement, personlist_handle * head){
  998. personlist_element * element = personlist_newElement(inputElement);
  999. if (head->first == NULL)
  1000. {
  1001. head->first = element;
  1002. }
  1003. else{
  1004. personlist_element * current = head->first;
  1005. while(current->next != NULL){
  1006. current = current->next;
  1007. }
  1008. current->next = element;
  1009. }
  1010. head->last = element;
  1011. head->size ++;
  1012. }
  1013.  
  1014. void personlist_remove(int index, personlist_handle * head){
  1015. if (index >= head->size){
  1016. raise(SIGSEGV); //Segmentation fault
  1017. }
  1018. personlist_element * current = head->first;
  1019.  
  1020. personlist_element * previous = NULL;
  1021.  
  1022. if(index == 0 && head->first != NULL){
  1023. head->first = head->first->next;
  1024. }
  1025.  
  1026. else{
  1027. for (int i=0; i<index; i++) {
  1028. previous = current;
  1029. current = current->next;
  1030. }
  1031. previous->next = current->next;
  1032. }
  1033.  
  1034. if(current->next == NULL){
  1035. head->last = previous;
  1036. }
  1037.  
  1038. string_clear(&current->element.name);
  1039. free(current); //releases memory
  1040. head->size--;
  1041. }
  1042.  
  1043. void personlist_insert(int index, personlist_handle * head, person *inputElement){
  1044. if (index > head->size){
  1045. raise(SIGSEGV); //Segmentation fault
  1046. }
  1047. personlist_element * element = personlist_newElement(inputElement);
  1048. personlist_element * current = head->first;
  1049. personlist_element * previous = NULL;
  1050.  
  1051. if(index == 0){
  1052. element->next = current;
  1053. head->first = element;
  1054. }
  1055. else{
  1056. for (int i=0; i<index; i++) {
  1057. previous = current;
  1058. current = current->next;
  1059. }
  1060. element->next = current;
  1061. previous->next = element;
  1062. }
  1063.  
  1064. if(element->next == NULL){
  1065. head->last = element;
  1066. }
  1067. head->size++;
  1068. }
  1069.  
  1070. void personlist_clear(personlist_handle * head){
  1071. while(head->size != 0){
  1072. personlist_remove(0, head);
  1073. }
  1074. }
  1075.  
  1076. void personlist_reverse(personlist_handle * head){
  1077. personlist_handle temporayHandle = personlist_new();
  1078. for (int i = 0; i < head->size; ++i)
  1079. {
  1080. person temp = personlist_get(i, head);
  1081. personlist_insert(0, &temporayHandle, &temp);
  1082. }
  1083.  
  1084. for (int i = 0; i < temporayHandle.size; ++i)
  1085. {
  1086. person temp = personlist_get(i, &temporayHandle);
  1087. personlist_set(i, &temp, head);
  1088. }
  1089. }
  1090.  
  1091.  
  1092. void personlist_swap(personlist_handle * head, int first, int second){
  1093. if (first >= head->size || second >= head->size){
  1094. raise(SIGSEGV); //Segmentation fault
  1095. }
  1096. person tempFirst = personlist_get(first, head);
  1097. person tempSecond = personlist_get(second, head);
  1098. personlist_set(first, &tempSecond, head);
  1099. personlist_set(second, &tempFirst, head);
  1100. }
  1101.  
  1102. void personlist_set(int index, person *value, personlist_handle * head){
  1103. if (index >= head->size){
  1104. raise(SIGSEGV); //Segmentation fault
  1105. }
  1106. personlist_element * current = head->first;
  1107.  
  1108. for (int i=0; i<index; i++) {
  1109. current = current->next;
  1110. }
  1111. current->element = *value;
  1112. }
  1113.  
  1114. personlist_handle personlist_copy(personlist_handle * source){
  1115. personlist_handle destination = personlist_new();
  1116. personlist_element * current = source->first;
  1117. for (int i = 0; i < source->size; ++i)
  1118. {
  1119. personlist_element * element = personlist_newElement(&current->element);
  1120. current = current->next;
  1121. personlist_add(&element->element, &destination);
  1122. }
  1123. return destination;
  1124. }
  1125. person person_copy(person* input){
  1126. person * returnValue = malloc(sizeof(person));
  1127. returnValue->age = input->age;
  1128. returnValue->name = string_copy(&input->name);
  1129. return *returnValue;
  1130. }
  1131. string_handle person_ToString(person *this){string_handle tempName = this->name;tempName = string_copy(&tempName);double tempAge = this->age;string_handle _0 = string_new();
  1132. standard_appendChars(&_0,"");standard_appendNum(&_0,tempAge);standard_appendChars(&_0,"\n");return _0;string_clear(&_0);}
  1133. void main(){
  1134. personlist_handle personList = personlist_new();
  1135. string_handle _1 = string_new();
  1136. standard_appendChars(&_1,"please enter the information asked for if you wish to stop enter \"Cancel\"");standard_printString(&_1);string_clear(&_1);
  1137. string_handle _2 = string_new();
  1138. standard_appendChars(&_2,"");string_handle name = _2;name = string_copy(&name);string_clear(&_2);
  1139. string_handle _3 = string_new();
  1140. standard_appendChars(&_3,"Cancel");while ((!string_equals(&name, &_3))){
  1141. string_handle _3 = string_new();
  1142. standard_appendChars(&_3,"Enter your name:");standard_printString(&_3);string_clear(&_3);
  1143. name = standard_read();
  1144. string_handle _4 = string_new();
  1145. standard_appendChars(&_4,"Cancel");if((!string_equals(&name, &_4))){
  1146. string_handle _4 = string_new();
  1147. standard_appendChars(&_4,"Enter your age:");standard_printString(&_4);string_clear(&_4);
  1148. string_handle ageString = standard_read();
  1149. double ageNum = 0 ;
  1150. if(!program_convertStringToNum(&ageString, &ageNum)){
  1151. string_handle _5 = string_new();
  1152. standard_appendChars(&_5,"Value entered was not a numeric value age was set to 0");standard_printString(&_5);string_clear(&_5);
  1153. }
  1154. else{
  1155. }
  1156.  
  1157. person tempPerson;
  1158. tempPerson.name = name;
  1159. tempPerson.age = ageNum;
  1160.  
  1161. personlist_add(&tempPerson, & personList );
  1162. }
  1163. else{
  1164. }
  1165. string_clear(&_4);
  1166. }
  1167. string_clear(&_3);
  1168. for (double j = 0 ;j< 10000 ;j++){
  1169. for (double i = 0 ;i< 10001 ;i++){
  1170. person tempPerson;
  1171. tempPerson.age = 5 ;
  1172. string_handle _4 = string_new();
  1173. standard_appendChars(&_4,"hej");tempPerson.name = string_copy(&_4);
  1174. string_clear(&_4);
  1175. personlist_add(&tempPerson, & personList );
  1176. }
  1177.  
  1178. personlist_clear( & personList );
  1179.  
  1180. string_handle _4 = string_new();
  1181. standard_appendChars(&_4,"woohoo");standard_printString(&_4);string_clear(&_4);
  1182.  
  1183. printf("%d",personList.size);
  1184.  
  1185. }
  1186.  
  1187. double cancer = personList.size;
  1188. string_handle _4 = string_new();
  1189. standard_appendChars(&_4,"");standard_appendNum(&_4,cancer);standard_appendChars(&_4,"");standard_printString(&_4);string_clear(&_4);
  1190. string_handle _6 = standard_read();string_clear(&_6);
  1191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement