Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // -------------- By: Anisha KC | ID: 805536562---------------- //
  5.  
  6.  
  7.  
  8. // ---------------- Creating the structs --------------------------- //
  9. // Cell struct (node)
  10. typedef struct cell {
  11. char *data;
  12. struct cell *left;
  13. struct cell *right;
  14. } cell;
  15.  
  16. // Moving commands struct (state machines)
  17. typedef struct stateMachine {
  18. int currentState;
  19. char readVal;
  20. char writeVal;
  21. char moveDirection;
  22. int newState;
  23. } stateMachine;
  24. // ----------------- Done creating structs ------------------------- //
  25.  
  26.  
  27.  
  28. // ----------------------------- Main function ------------------------------ //
  29. int main() {
  30. // Variables
  31. char *pInitialTape, initialTape[50];pInitialTape = initialTape;
  32. int *pNumOfStates, numOfStates; pNumOfStates = &numOfStates;
  33. int *pStartState, startState; pStartState = &startState;
  34. int *pEndState, endState; pEndState = &endState;
  35.  
  36. // Ask for input file
  37. FILE *fp;
  38. char fileName [500];
  39. printf("Input file path: ");
  40. scanf("%s", &fileName);
  41.  
  42. // Open the input file
  43. fp = fopen(fileName,"r");
  44.  
  45. // File is not found
  46. if(fp == NULL) {
  47. printf("File not found \n");
  48. return (1);
  49. }
  50.  
  51. // Read the file
  52. // Get initial tape, number of states, start state and end state
  53. fscanf(fp, "%s \n %d \n %d \n %d \n", pInitialTape, &pNumOfStates, &pStartState, &pEndState);
  54. cell start = {"A", NULL, NULL};
  55. printf("Initial tape: %s, # of States: %d, Start State: %d, End State: %d \n", pInitialTape, pNumOfStates, pStartState, pEndState);
  56.  
  57. // Read rest of the file and get state machines (if any)
  58. char * line = NULL;
  59. size_t len = 0;
  60. ssize_t read;
  61. stateMachine *pTempSM, tempSM;
  62. pTempSM = &tempSM;
  63. while ((read = getline(&line, &len, fp)) != -1) {
  64. fscanf(fp, "(%d,%c)->(%c,%c,%d", &pTempSM->currentState, &pTempSM->readVal, &pTempSM->writeVal, &pTempSM->moveDirection, &pTempSM->newState);
  65. printf("(%d,%c)->(%c,%c,%d)\n",pTempSM->currentState, pTempSM->readVal, pTempSM->writeVal, pTempSM->moveDirection, pTempSM->newState);
  66.  
  67. }
  68.  
  69. // Closing the file
  70. fclose(fp);
  71.  
  72. return 0;
  73. }
  74. // ----------------------------- End of main function ------------------------------ //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement