Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "dfa.h"
  4.  
  5.  
  6.  
  7.  
  8.  
  9. int main()
  10. { int currentState;
  11.  
  12.  
  13.  
  14. struct DFA Q1;
  15. struct DFA* Q1p = malloc(sizeof(struct DFA));
  16. Q1p=&Q1;
  17. Q1p->States=7;
  18. Q1p->finStates=6;
  19. new_DFA(Q1, 8);
  20. int TQ1[7][128];
  21.  
  22.  
  23. Q1.finStates=6;
  24. for(int i=0;i<Q1p->States;i++){
  25.  
  26. for(int j=0;j<128;j++){
  27. TQ1[i][j]=-1;
  28. }
  29. }
  30. Q1p->ArrayP= &TQ1[0][0];
  31.  
  32.  
  33. DFA_set_transition(Q1p, 0, 'c', 1);
  34. DFA_set_transition(Q1p, 1, 's', 2);
  35. DFA_set_transition(Q1p, 2, 'c', 3);
  36. DFA_set_transition(Q1p, 3, '1', 4);
  37. DFA_set_transition(Q1p, 4, '7', 5);
  38. DFA_set_transition(Q1p, 5, '3', 6);
  39.  
  40.  
  41. bool a=true;
  42.  
  43. printf("Testing DFAs that accept exactly the string csc173 \n");
  44. while(a){
  45.  
  46. printf("Please enter your input (enter 'quit' to quit): ");
  47. char input[20];
  48. scanf("%s", input);
  49. if(strcmp(input,"quit")==0 ){break;}
  50. if(DFA_execute(Q1,input)==0){
  51.  
  52. printf("The input '%s' is NOT accepted\n\n", input);
  53.  
  54. }else printf("The input '%s' is accepted\n\n", input); // DFA execution
  55.  
  56.  
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. struct DFA Q2;
  65. struct DFA* Q2p = malloc(sizeof(struct DFA));
  66. Q2p=&Q2;
  67. Q2p->States=4;
  68. Q2p->finStates=3;
  69. new_DFA(Q2, 4);
  70. int TQ2[4][128];
  71.  
  72.  
  73.  
  74. Q2.finStates=3;
  75. for(int i=0;i<States;i++){
  76.  
  77. for(int j=0;j<128;j++){
  78. TQ2[i][j]=-1;
  79. }
  80. }
  81. Q2p->ArrayP= &TQ2[0][0];
  82.  
  83. DFA_set_transition(Q2p, 0, 'c', 1);
  84. DFA_set_transition(Q2p, 1, 'a', 2);
  85. DFA_set_transition(Q2p, 2, 't', 3);
  86. DFA_set_transition_all(Q2,3,3);
  87. for(int i=3;i<4;i++){
  88.  
  89. for(int j=0;j<128;j++){
  90. printf("value is %i ", TQ2[i][j]);
  91. }
  92. }
  93. printf("valueeeeeeee %i", TQ2[3]['z']);
  94.  
  95. a=true;
  96.  
  97. printf("Testing DFAs that accept the strings that start with 'cat' \n");
  98. while(a){
  99.  
  100. printf("Please enter your input (enter 'quit' to quit): ");
  101. char input[20];
  102. scanf("%s", input);
  103. if(strcmp(input,"quit")==0 ){break;}
  104. if(DFA_execute(Q2,input)==0){
  105.  
  106. printf("The input '%s' is NOT accepted\n\n", input);
  107.  
  108. }else printf("The input '%s' is accepted\n\n", input); // DFA execution
  109.  
  110.  
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. struct DFA Q3;
  120. struct DFA* Q3p = malloc(sizeof(struct DFA));
  121. Q3p=&Q3;
  122. Q3p->States=3;
  123. Q3p->finStates=2;
  124. new_DFA(Q3, 3);
  125. int TQ3[3][128];
  126.  
  127. Q3.Rows=3;
  128. Q3.Columns=128;
  129. Q3.finStates=2;
  130. for(int i=0;i<Q3.Rows;i++){
  131.  
  132. for(int j=0;j<128;j++){
  133. TQ3[i][j]=-1;
  134. }
  135. }
  136.  
  137. Q3p->ArrayP= &TQ3[0][0];
  138.  
  139. DFA_set_transition(Q3p, 0, '0', 1);
  140. DFA_set_transition(Q3p, 0, '1', 0);
  141. DFA_set_transition(Q3p, 1, '0', 2);
  142. DFA_set_transition(Q3p, 1, '1', 1);
  143. DFA_set_transition(Q3p, 2, '0', 1);
  144. DFA_set_transition(Q3p, 2, '1', 2);
  145.  
  146.  
  147. printf("Testing DFAs that accept the strings that have even number of 0s \n");
  148. while(a){
  149.  
  150. printf("Please enter your input (enter 'quit' to quit): ");
  151. char input[20];
  152. scanf("%s", input);
  153. printf("this isssssssssssss %s",input);
  154. if(strcmp(input,"quit")==0 ){break;}
  155. if(DFA_execute(Q3,input)==0){
  156.  
  157. printf("The input '%s' is NOT accepted\n\n", input);
  158.  
  159. }else printf("The input '%s' is accepted\n\n", input); // DFA execution
  160.  
  161.  
  162. }
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. //Question 4
  171.  
  172. struct DFA Q4;
  173. struct DFA* Q4p = malloc(sizeof(struct DFA));
  174. Q4p=&Q4;
  175. Q4p->States=4;
  176. Q4p->finStates=0;
  177. new_DFA(Q4, 4);
  178. int TQ4[4][128];
  179.  
  180. Q4.Rows=4;
  181. Q4.Columns=128;
  182. Q4.finStates=0;
  183. for(int i=0;i<Q4.Rows;i++){
  184.  
  185. for(int j=0;j<128;j++){
  186. TQ4[i][j]=-1;
  187. }
  188. }
  189.  
  190.  
  191. Q4p->ArrayP= &TQ4[0][0];
  192.  
  193. DFA_set_transition(Q4p, 0, '0', 1);
  194. DFA_set_transition(Q4p, 0, '1', 3);
  195. DFA_set_transition(Q4p, 1, '0', 0);
  196. DFA_set_transition(Q4p, 1, '1', 2);
  197. DFA_set_transition(Q4p, 2, '0', 3);
  198. DFA_set_transition(Q4p, 2, '1', 1);
  199. DFA_set_transition(Q4p, 3, '0', 2);
  200. DFA_set_transition(Q4p, 3, '1', 0);
  201.  
  202.  
  203. printf("Testing DFAs that accept the strings that have even number of 0s and 1s \n");
  204. while(a){
  205.  
  206. printf("Please enter your input (enter 'quit' to quit): ");
  207. char input[20];
  208. scanf("%s", input);
  209. printf("this isssssssssssss %s",input);
  210. if(strcmp(input,"quit")==0 ){break;}
  211. if(DFA_execute(Q4,input)==0){
  212.  
  213. printf("The input '%s' is NOT accepted\n\n", input);
  214.  
  215. }else printf("The input '%s' is accepted\n\n", input); // DFA execution
  216.  
  217.  
  218. }
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229. //question 5
  230. // the pattern i created recognizes the binary string that starts with 1 and ends with 0
  231. struct DFA Q5;
  232. struct DFA* Q5p = malloc(sizeof(struct DFA));
  233. Q5p=&Q5;
  234. Q5p->States=3;
  235. Q5p->finStates=2;
  236. new_DFA(Q5, 3);
  237. int TQ5[3][128];
  238.  
  239. Q5.Rows=3;
  240. Q5.Columns=128;
  241. Q5.finStates=2;
  242. for(int i=0;i<Q5.Rows;i++){
  243.  
  244. for(int j=0;j<128;j++){
  245. TQ5[i][j]=-1;
  246. }
  247. }
  248.  
  249.  
  250. Q5p->ArrayP= &TQ5[0][0];
  251.  
  252. DFA_set_transition(Q5p, 0, '1', 1);
  253. DFA_set_transition(Q5p, 1, '0', 2);
  254. DFA_set_transition(Q5p, 1, '1', 1);
  255. DFA_set_transition(Q5p, 2, '0', 2);
  256. DFA_set_transition(Q5p, 2, '1', 1);
  257.  
  258.  
  259. printf("Testing DFAs that accept the binary strings that start with 1 and end with 0\n");
  260. while(a){
  261.  
  262. printf("Please enter your input (enter 'quit' to quit): ");
  263. char input[20];
  264. scanf("%s", input);
  265. printf("this isssssssssssss %s",input);
  266. if(strcmp(input,"quit")==0 ){break;}
  267. if(DFA_execute(Q5,input)==0){
  268.  
  269. printf("The input '%s' is NOT accepted\n\n", input);
  270.  
  271. }else printf("The input '%s' is accepted\n\n", input); // DFA execution
  272.  
  273.  
  274. }
  275.  
  276. // printf(" %i" , TQ1[2]['c']);
  277. // printf("Size of DFA is %i\n",DFA_get_size(Q1p));
  278. // printCounter(Q1p);
  279. // printf("Hello world!\n");
  280. return 0;
  281. }
  282.  
  283.  
  284.  
  285. /*
  286. * File: LinkedList.c
  287. * Creator: George Ferguson
  288. * Created: Thu Jun 30 14:47:12 2016
  289. * Time-stamp: <Fri Aug 4 10:29:48 EDT 2017 ferguson>
  290. */
  291.  
  292. #include <stdio.h>
  293. #include <stdlib.h>
  294. #include "LinkedList.h"
  295.  
  296. /**
  297. * Structure for each element of a doubly-linked LinkedList.
  298. */
  299. typedef struct Node* Node;
  300. struct Node {
  301. void *data;
  302. Node next;
  303. Node prev;
  304. };
  305.  
  306. /**
  307. * Linked list with first and last (head and tail) pointers.
  308. */
  309. struct LinkedList {
  310. Node first;
  311. Node last;
  312. };
  313.  
  314. /**
  315. * Allocate, initialize and return a new (empty) LinkedList.
  316. */
  317. LinkedList new_LinkedList() {
  318. LinkedList this = (LinkedList)malloc(sizeof(struct LinkedList));
  319. this->first = this->last = NULL;
  320. return this;
  321. }
  322.  
  323. static Node new_Node(void *data) {
  324. Node this = (Node)malloc(sizeof(struct Node));
  325. this->data = data;
  326. this->next = this->prev = NULL;
  327. return this;
  328. }
  329.  
  330. /**
  331. * Free the memory used for the given LinkedList.
  332. * If boolean free_data_also is true, also free the data associated with
  333. * each element.
  334. */
  335. void LinkedList_free(LinkedList this, bool free_data_also) {
  336. if (this == NULL) {
  337. return;
  338. }
  339. // Free the elements
  340. Node node = this->first;
  341. while (node != NULL) {
  342. Node next = node->next;
  343. if (free_data_also && node->data != NULL) {
  344. free(node->data);
  345. }
  346. free(node);
  347. node = next;
  348. }
  349. // Free the list itself
  350. free(this);
  351. }
  352.  
  353. /**
  354. * Return true if the given LinkedList is empty.
  355. */
  356. bool LinkedList_isEmpty(const LinkedList this) {
  357. return this->first == NULL;
  358. }
  359.  
  360. /**
  361. * Add the given void* value at the front of the given LinkedList.
  362. */
  363. void LinkedList_add_at_front(LinkedList this, void *data) {
  364. Node node = new_Node(data);
  365. node->next = this->first;
  366. if (this->first != NULL) {
  367. this->first->prev = node;
  368. }
  369. this->first = node;
  370. if (this->last == NULL) {
  371. this->last = node;
  372. }
  373. }
  374.  
  375. /**
  376. * Add the given void* value at the end of the given LinkedList.
  377. */
  378. void LinkedList_add_at_end(LinkedList this, void *data) {
  379. Node node = new_Node(data);
  380. node->prev = this->last;
  381. if (this->last != NULL) {
  382. this->last->next = node;
  383. }
  384. this->last = node;
  385. if (this->first == NULL) {
  386. this->first = node;
  387. }
  388. }
  389.  
  390. /**
  391. * Return true if then given LinkedList contains given void* value.
  392. * Note this doesn't any kind of equals function, just plain ``==''.
  393. */
  394. bool LinkedList_contains(const LinkedList this, void *data) {
  395. for (Node node=this->first; node != NULL; node=node->next) {
  396. if (node->data == data) {
  397. return true;
  398. }
  399. }
  400. return false;
  401. }
  402.  
  403. /**
  404. * Remove the given void* value from the given LinkedList if it is there.
  405. * This function uses ``=='' to test for the element.
  406. * Note that this does not free the data associated with the element.
  407. */
  408. void LinkedList_remove(LinkedList this, void *data) {
  409. for (Node node=this->first; node != NULL; node=node->next) {
  410. if (node->data == data) {
  411. if (node == this->first) {
  412. this->first = node->next;
  413. }
  414. if (node == this->last) {
  415. this->last = node->prev;
  416. }
  417. if (node->prev != NULL) {
  418. node->prev->next = node->next;
  419. }
  420. if (node->next != NULL) {
  421. node->next->prev = node->prev;
  422. }
  423. free(node);
  424. return;
  425. }
  426. }
  427. }
  428.  
  429. /**
  430. * Return the void* value at the given index in the given LinkedList, or
  431. * NULL if there is no such.
  432. * Note that this means you can't store NULL in a LinkedList. C'est la vie.
  433. */
  434. void* LinkedList_elementAt(LinkedList this, int index) {
  435. int i = 0;
  436. for (Node node=this->first; node != NULL; node=node->next) {
  437. if (i == index) {
  438. return node->data;
  439. }
  440. i += 1;
  441. }
  442. return NULL;
  443. }
  444.  
  445. /**
  446. * Remove and return the first element from the given LinkedList.
  447. * Returns NULL if the list is empty.
  448. */
  449. void* LinkedList_pop(LinkedList this) {
  450. void *data = LinkedList_elementAt(this, 0);
  451. if (data != NULL) {
  452. LinkedList_remove(this, data); // Removes first occurrence
  453. }
  454. return data;
  455. }
  456.  
  457. /**
  458. * Call the given function on each element of given LinkedList, passing the
  459. * void* value to the function.
  460. */
  461. void LinkedList_iterate(const LinkedList this, void (*func)(void *)) {
  462. for (Node node=this->first; node != NULL; node=node->next) {
  463. func(node->data);
  464. }
  465. }
  466.  
  467. /**
  468. * A LinkedListIterator is simply a pointer to a node. It is initialized
  469. * to this->first and increments following next pointers until NULL.
  470. */
  471. struct LinkedListIterator {
  472. Node next;
  473. };
  474.  
  475. /**
  476. * Return an LinkedListIterator for the given LinkedList.
  477. * Don't forget to free() this when you're done iterating.
  478. */
  479. LinkedListIterator LinkedList_iterator(const LinkedList this) {
  480. LinkedListIterator iterator = (LinkedListIterator)malloc(sizeof(struct LinkedListIterator));
  481. iterator->next = this->first;
  482. return iterator;
  483. }
  484.  
  485. /**
  486. * Return true if the given LinkedListIterator will return another element
  487. * if LinkedListIterator_next() is called.
  488. */
  489. bool LinkedListIterator_hasNext(const LinkedListIterator this) {
  490. return this->next != NULL;
  491. }
  492.  
  493. /**
  494. * Return the next value from the given LinkedListIterator and increment it
  495. * to point to the next element.
  496. * Will return NULL if there is no such element.
  497. * This means that you can't store NULL in a LinkedList. C'est la vie.
  498. * It would be easy to allow it and signal `no such element' some other way...
  499. */
  500. void* LinkedListIterator_next(LinkedListIterator this) {
  501. if (this->next == NULL) {
  502. return NULL;
  503. } else {
  504. void *data = this->next->data;
  505. this->next = this->next->next;
  506. return data;
  507. }
  508. }
  509.  
  510. #ifdef MAIN
  511.  
  512. /**
  513. * Print the given LinkedList to stdout, assuming that the values are
  514. * all null-terminated strings.
  515. * Also prints a newline (because why not).
  516. */
  517. void LinkedList_print_string_list(LinkedList this) {
  518. printf("[");
  519. for (Node node=this->first; node != NULL; node=node->next) {
  520. printf("%s", (char*)(node->data));
  521. if (node->next != NULL) {
  522. printf(" ");
  523. }
  524. }
  525. printf("]\n");
  526. }
  527.  
  528. int main(int argc, char **argv) {
  529. LinkedList list = new_LinkedList();
  530. printf("new list =");
  531. LinkedList_print_string_list(list);
  532.  
  533. printf("adding three elements: ");
  534. LinkedList_add_at_end(list, "foo");
  535. LinkedList_add_at_end(list, "bar");
  536. LinkedList_add_at_end(list, "baz");
  537. LinkedList_print_string_list(list);
  538. printf("adding Ted at front: ");
  539. LinkedList_add_at_front(list, "Ted");
  540. LinkedList_print_string_list(list);
  541.  
  542. printf("iterating over list:\n");
  543. LinkedListIterator iterator = LinkedList_iterator(list);
  544. while (LinkedListIterator_hasNext(iterator)) {
  545. void *data = LinkedListIterator_next(iterator);
  546. char *str = (char*)data;
  547. printf("%s\n", str);
  548. }
  549. free(iterator);
  550.  
  551. // Test remove in middle
  552. printf("removing bar from middle: ");
  553. LinkedList_remove(list, "bar");
  554. LinkedList_print_string_list(list);
  555. // Test remove at head
  556. printf("removing Ted from start: ");
  557. LinkedList_remove(list, "Ted");
  558. LinkedList_print_string_list(list);
  559. // Test remove in end
  560. printf("removing baz from end: ");
  561. LinkedList_remove(list, "baz");
  562. LinkedList_print_string_list(list);
  563.  
  564. // Test remove only element
  565. void *elt = LinkedList_elementAt(list, 0);
  566. printf("element 0 is \"%s\"\n", (char*)elt);
  567. printf("removing only remaining element: ");
  568. LinkedList_remove(list, elt);
  569. LinkedList_print_string_list(list);
  570.  
  571. printf("list isEmpty: %d\n", LinkedList_isEmpty(list));
  572.  
  573. printf("freeing list\n");
  574. LinkedList_free(list, false);
  575. }
  576.  
  577. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement