Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "dfa.h"
- int main()
- { int currentState;
- struct DFA Q1;
- struct DFA* Q1p = malloc(sizeof(struct DFA));
- Q1p=&Q1;
- Q1p->States=7;
- Q1p->finStates=6;
- new_DFA(Q1, 8);
- int TQ1[7][128];
- Q1.finStates=6;
- for(int i=0;i<Q1p->States;i++){
- for(int j=0;j<128;j++){
- TQ1[i][j]=-1;
- }
- }
- Q1p->ArrayP= &TQ1[0][0];
- DFA_set_transition(Q1p, 0, 'c', 1);
- DFA_set_transition(Q1p, 1, 's', 2);
- DFA_set_transition(Q1p, 2, 'c', 3);
- DFA_set_transition(Q1p, 3, '1', 4);
- DFA_set_transition(Q1p, 4, '7', 5);
- DFA_set_transition(Q1p, 5, '3', 6);
- bool a=true;
- printf("Testing DFAs that accept exactly the string csc173 \n");
- while(a){
- printf("Please enter your input (enter 'quit' to quit): ");
- char input[20];
- scanf("%s", input);
- if(strcmp(input,"quit")==0 ){break;}
- if(DFA_execute(Q1,input)==0){
- printf("The input '%s' is NOT accepted\n\n", input);
- }else printf("The input '%s' is accepted\n\n", input); // DFA execution
- }
- struct DFA Q2;
- struct DFA* Q2p = malloc(sizeof(struct DFA));
- Q2p=&Q2;
- Q2p->States=4;
- Q2p->finStates=3;
- new_DFA(Q2, 4);
- int TQ2[4][128];
- Q2.finStates=3;
- for(int i=0;i<States;i++){
- for(int j=0;j<128;j++){
- TQ2[i][j]=-1;
- }
- }
- Q2p->ArrayP= &TQ2[0][0];
- DFA_set_transition(Q2p, 0, 'c', 1);
- DFA_set_transition(Q2p, 1, 'a', 2);
- DFA_set_transition(Q2p, 2, 't', 3);
- DFA_set_transition_all(Q2,3,3);
- for(int i=3;i<4;i++){
- for(int j=0;j<128;j++){
- printf("value is %i ", TQ2[i][j]);
- }
- }
- printf("valueeeeeeee %i", TQ2[3]['z']);
- a=true;
- printf("Testing DFAs that accept the strings that start with 'cat' \n");
- while(a){
- printf("Please enter your input (enter 'quit' to quit): ");
- char input[20];
- scanf("%s", input);
- if(strcmp(input,"quit")==0 ){break;}
- if(DFA_execute(Q2,input)==0){
- printf("The input '%s' is NOT accepted\n\n", input);
- }else printf("The input '%s' is accepted\n\n", input); // DFA execution
- }
- struct DFA Q3;
- struct DFA* Q3p = malloc(sizeof(struct DFA));
- Q3p=&Q3;
- Q3p->States=3;
- Q3p->finStates=2;
- new_DFA(Q3, 3);
- int TQ3[3][128];
- Q3.Rows=3;
- Q3.Columns=128;
- Q3.finStates=2;
- for(int i=0;i<Q3.Rows;i++){
- for(int j=0;j<128;j++){
- TQ3[i][j]=-1;
- }
- }
- Q3p->ArrayP= &TQ3[0][0];
- DFA_set_transition(Q3p, 0, '0', 1);
- DFA_set_transition(Q3p, 0, '1', 0);
- DFA_set_transition(Q3p, 1, '0', 2);
- DFA_set_transition(Q3p, 1, '1', 1);
- DFA_set_transition(Q3p, 2, '0', 1);
- DFA_set_transition(Q3p, 2, '1', 2);
- printf("Testing DFAs that accept the strings that have even number of 0s \n");
- while(a){
- printf("Please enter your input (enter 'quit' to quit): ");
- char input[20];
- scanf("%s", input);
- printf("this isssssssssssss %s",input);
- if(strcmp(input,"quit")==0 ){break;}
- if(DFA_execute(Q3,input)==0){
- printf("The input '%s' is NOT accepted\n\n", input);
- }else printf("The input '%s' is accepted\n\n", input); // DFA execution
- }
- //Question 4
- struct DFA Q4;
- struct DFA* Q4p = malloc(sizeof(struct DFA));
- Q4p=&Q4;
- Q4p->States=4;
- Q4p->finStates=0;
- new_DFA(Q4, 4);
- int TQ4[4][128];
- Q4.Rows=4;
- Q4.Columns=128;
- Q4.finStates=0;
- for(int i=0;i<Q4.Rows;i++){
- for(int j=0;j<128;j++){
- TQ4[i][j]=-1;
- }
- }
- Q4p->ArrayP= &TQ4[0][0];
- DFA_set_transition(Q4p, 0, '0', 1);
- DFA_set_transition(Q4p, 0, '1', 3);
- DFA_set_transition(Q4p, 1, '0', 0);
- DFA_set_transition(Q4p, 1, '1', 2);
- DFA_set_transition(Q4p, 2, '0', 3);
- DFA_set_transition(Q4p, 2, '1', 1);
- DFA_set_transition(Q4p, 3, '0', 2);
- DFA_set_transition(Q4p, 3, '1', 0);
- printf("Testing DFAs that accept the strings that have even number of 0s and 1s \n");
- while(a){
- printf("Please enter your input (enter 'quit' to quit): ");
- char input[20];
- scanf("%s", input);
- printf("this isssssssssssss %s",input);
- if(strcmp(input,"quit")==0 ){break;}
- if(DFA_execute(Q4,input)==0){
- printf("The input '%s' is NOT accepted\n\n", input);
- }else printf("The input '%s' is accepted\n\n", input); // DFA execution
- }
- //question 5
- // the pattern i created recognizes the binary string that starts with 1 and ends with 0
- struct DFA Q5;
- struct DFA* Q5p = malloc(sizeof(struct DFA));
- Q5p=&Q5;
- Q5p->States=3;
- Q5p->finStates=2;
- new_DFA(Q5, 3);
- int TQ5[3][128];
- Q5.Rows=3;
- Q5.Columns=128;
- Q5.finStates=2;
- for(int i=0;i<Q5.Rows;i++){
- for(int j=0;j<128;j++){
- TQ5[i][j]=-1;
- }
- }
- Q5p->ArrayP= &TQ5[0][0];
- DFA_set_transition(Q5p, 0, '1', 1);
- DFA_set_transition(Q5p, 1, '0', 2);
- DFA_set_transition(Q5p, 1, '1', 1);
- DFA_set_transition(Q5p, 2, '0', 2);
- DFA_set_transition(Q5p, 2, '1', 1);
- printf("Testing DFAs that accept the binary strings that start with 1 and end with 0\n");
- while(a){
- printf("Please enter your input (enter 'quit' to quit): ");
- char input[20];
- scanf("%s", input);
- printf("this isssssssssssss %s",input);
- if(strcmp(input,"quit")==0 ){break;}
- if(DFA_execute(Q5,input)==0){
- printf("The input '%s' is NOT accepted\n\n", input);
- }else printf("The input '%s' is accepted\n\n", input); // DFA execution
- }
- // printf(" %i" , TQ1[2]['c']);
- // printf("Size of DFA is %i\n",DFA_get_size(Q1p));
- // printCounter(Q1p);
- // printf("Hello world!\n");
- return 0;
- }
- /*
- * File: LinkedList.c
- * Creator: George Ferguson
- * Created: Thu Jun 30 14:47:12 2016
- * Time-stamp: <Fri Aug 4 10:29:48 EDT 2017 ferguson>
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include "LinkedList.h"
- /**
- * Structure for each element of a doubly-linked LinkedList.
- */
- typedef struct Node* Node;
- struct Node {
- void *data;
- Node next;
- Node prev;
- };
- /**
- * Linked list with first and last (head and tail) pointers.
- */
- struct LinkedList {
- Node first;
- Node last;
- };
- /**
- * Allocate, initialize and return a new (empty) LinkedList.
- */
- LinkedList new_LinkedList() {
- LinkedList this = (LinkedList)malloc(sizeof(struct LinkedList));
- this->first = this->last = NULL;
- return this;
- }
- static Node new_Node(void *data) {
- Node this = (Node)malloc(sizeof(struct Node));
- this->data = data;
- this->next = this->prev = NULL;
- return this;
- }
- /**
- * Free the memory used for the given LinkedList.
- * If boolean free_data_also is true, also free the data associated with
- * each element.
- */
- void LinkedList_free(LinkedList this, bool free_data_also) {
- if (this == NULL) {
- return;
- }
- // Free the elements
- Node node = this->first;
- while (node != NULL) {
- Node next = node->next;
- if (free_data_also && node->data != NULL) {
- free(node->data);
- }
- free(node);
- node = next;
- }
- // Free the list itself
- free(this);
- }
- /**
- * Return true if the given LinkedList is empty.
- */
- bool LinkedList_isEmpty(const LinkedList this) {
- return this->first == NULL;
- }
- /**
- * Add the given void* value at the front of the given LinkedList.
- */
- void LinkedList_add_at_front(LinkedList this, void *data) {
- Node node = new_Node(data);
- node->next = this->first;
- if (this->first != NULL) {
- this->first->prev = node;
- }
- this->first = node;
- if (this->last == NULL) {
- this->last = node;
- }
- }
- /**
- * Add the given void* value at the end of the given LinkedList.
- */
- void LinkedList_add_at_end(LinkedList this, void *data) {
- Node node = new_Node(data);
- node->prev = this->last;
- if (this->last != NULL) {
- this->last->next = node;
- }
- this->last = node;
- if (this->first == NULL) {
- this->first = node;
- }
- }
- /**
- * Return true if then given LinkedList contains given void* value.
- * Note this doesn't any kind of equals function, just plain ``==''.
- */
- bool LinkedList_contains(const LinkedList this, void *data) {
- for (Node node=this->first; node != NULL; node=node->next) {
- if (node->data == data) {
- return true;
- }
- }
- return false;
- }
- /**
- * Remove the given void* value from the given LinkedList if it is there.
- * This function uses ``=='' to test for the element.
- * Note that this does not free the data associated with the element.
- */
- void LinkedList_remove(LinkedList this, void *data) {
- for (Node node=this->first; node != NULL; node=node->next) {
- if (node->data == data) {
- if (node == this->first) {
- this->first = node->next;
- }
- if (node == this->last) {
- this->last = node->prev;
- }
- if (node->prev != NULL) {
- node->prev->next = node->next;
- }
- if (node->next != NULL) {
- node->next->prev = node->prev;
- }
- free(node);
- return;
- }
- }
- }
- /**
- * Return the void* value at the given index in the given LinkedList, or
- * NULL if there is no such.
- * Note that this means you can't store NULL in a LinkedList. C'est la vie.
- */
- void* LinkedList_elementAt(LinkedList this, int index) {
- int i = 0;
- for (Node node=this->first; node != NULL; node=node->next) {
- if (i == index) {
- return node->data;
- }
- i += 1;
- }
- return NULL;
- }
- /**
- * Remove and return the first element from the given LinkedList.
- * Returns NULL if the list is empty.
- */
- void* LinkedList_pop(LinkedList this) {
- void *data = LinkedList_elementAt(this, 0);
- if (data != NULL) {
- LinkedList_remove(this, data); // Removes first occurrence
- }
- return data;
- }
- /**
- * Call the given function on each element of given LinkedList, passing the
- * void* value to the function.
- */
- void LinkedList_iterate(const LinkedList this, void (*func)(void *)) {
- for (Node node=this->first; node != NULL; node=node->next) {
- func(node->data);
- }
- }
- /**
- * A LinkedListIterator is simply a pointer to a node. It is initialized
- * to this->first and increments following next pointers until NULL.
- */
- struct LinkedListIterator {
- Node next;
- };
- /**
- * Return an LinkedListIterator for the given LinkedList.
- * Don't forget to free() this when you're done iterating.
- */
- LinkedListIterator LinkedList_iterator(const LinkedList this) {
- LinkedListIterator iterator = (LinkedListIterator)malloc(sizeof(struct LinkedListIterator));
- iterator->next = this->first;
- return iterator;
- }
- /**
- * Return true if the given LinkedListIterator will return another element
- * if LinkedListIterator_next() is called.
- */
- bool LinkedListIterator_hasNext(const LinkedListIterator this) {
- return this->next != NULL;
- }
- /**
- * Return the next value from the given LinkedListIterator and increment it
- * to point to the next element.
- * Will return NULL if there is no such element.
- * This means that you can't store NULL in a LinkedList. C'est la vie.
- * It would be easy to allow it and signal `no such element' some other way...
- */
- void* LinkedListIterator_next(LinkedListIterator this) {
- if (this->next == NULL) {
- return NULL;
- } else {
- void *data = this->next->data;
- this->next = this->next->next;
- return data;
- }
- }
- #ifdef MAIN
- /**
- * Print the given LinkedList to stdout, assuming that the values are
- * all null-terminated strings.
- * Also prints a newline (because why not).
- */
- void LinkedList_print_string_list(LinkedList this) {
- printf("[");
- for (Node node=this->first; node != NULL; node=node->next) {
- printf("%s", (char*)(node->data));
- if (node->next != NULL) {
- printf(" ");
- }
- }
- printf("]\n");
- }
- int main(int argc, char **argv) {
- LinkedList list = new_LinkedList();
- printf("new list =");
- LinkedList_print_string_list(list);
- printf("adding three elements: ");
- LinkedList_add_at_end(list, "foo");
- LinkedList_add_at_end(list, "bar");
- LinkedList_add_at_end(list, "baz");
- LinkedList_print_string_list(list);
- printf("adding Ted at front: ");
- LinkedList_add_at_front(list, "Ted");
- LinkedList_print_string_list(list);
- printf("iterating over list:\n");
- LinkedListIterator iterator = LinkedList_iterator(list);
- while (LinkedListIterator_hasNext(iterator)) {
- void *data = LinkedListIterator_next(iterator);
- char *str = (char*)data;
- printf("%s\n", str);
- }
- free(iterator);
- // Test remove in middle
- printf("removing bar from middle: ");
- LinkedList_remove(list, "bar");
- LinkedList_print_string_list(list);
- // Test remove at head
- printf("removing Ted from start: ");
- LinkedList_remove(list, "Ted");
- LinkedList_print_string_list(list);
- // Test remove in end
- printf("removing baz from end: ");
- LinkedList_remove(list, "baz");
- LinkedList_print_string_list(list);
- // Test remove only element
- void *elt = LinkedList_elementAt(list, 0);
- printf("element 0 is \"%s\"\n", (char*)elt);
- printf("removing only remaining element: ");
- LinkedList_remove(list, elt);
- LinkedList_print_string_list(list);
- printf("list isEmpty: %d\n", LinkedList_isEmpty(list));
- printf("freeing list\n");
- LinkedList_free(list, false);
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement