Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. if (strcmp(tmp, "AND") == 0) { // ========================== AND Case ==============================
  2. if (validLine[j] == 0) {
  3. char temp[100];
  4. char Input1[100];
  5. char Input2[100];
  6. fscanf(fp, "%s %s %s\n", Input1, Input2, temp);
  7. if (isValid(Input1) && isValid(Input2)) {
  8. Operate(Input1, Input2, temp, MAX, 'A');
  9. validLine[j] = 1;
  10. }
  11. } else fgets(NA, 10000, fp);
  12. } else if (strcmp(tmp, "OR") == 0) { // ========================== OR Case ==============================
  13. if (validLine[j] == 0) {
  14. char temp[100];
  15. char Input1[100];
  16. char Input2[100];
  17. fscanf(fp, "%s %s %s\n", Input1, Input2, temp);
  18. if (isValid(Input1) && isValid(Input2)) {
  19. Operate(Input1, Input2, temp, MAX, 'O');
  20. validLine[j] = 1;
  21. }
  22. } else fgets(NA, 10000, fp);
  23. } else if (strcmp(tmp, "NOT") == 0) { // ========================== NOT Case ==============================
  24. if (validLine[j] == 0) {
  25. char temp[100];
  26. char Input1[100];
  27. fscanf(fp, "%s %s\n", Input1, temp);
  28. if (isValid(Input1)) {
  29. Operate(Input1, NULL, temp, MAX, 'T');
  30. validLine[j] = 1;
  31. }
  32. } else fgets(NA, 10000, fp);
  33. } else if (strcmp(tmp, "XOR") == 0) { // ========================== XOR Case ==============================
  34. if (validLine[j] == 0) {
  35. char temp[100];
  36. char Input1[100];
  37. char Input2[100];
  38. fscanf(fp, "%s %s %s\n", Input1, Input2, temp);
  39. if (isValid(Input1) && isValid(Input2)) {
  40. Operate(Input1, Input2, temp, MAX, 'X');
  41. validLine[j] = 1;
  42. }
  43. } else fgets(NA, 10000, fp);
  44. } else if (strcmp(tmp, "NAND") == 0) { // ========================== NAND Case ==============================
  45. if (validLine[j] == 0) {
  46. char temp[100];
  47. char Input1[100];
  48. char Input2[100];
  49. fscanf(fp, "%s %s %s\n", Input1, Input2, temp);
  50. if (isValid(Input1) && isValid(Input2)) {
  51. Operate(Input1, Input2, temp, MAX, 'D');
  52. validLine[j] = 1;
  53. }
  54. } else fgets(NA, 10000, fp);
  55. } else if (strcmp(tmp, "NOR") == 0) { // ========================== NOR Case ==============================
  56. if (validLine[j] == 0) {
  57. char temp[100];
  58. char Input1[100];
  59. char Input2[100];
  60. fscanf(fp, "%s %s %s\n", Input1, Input2, temp);
  61. if (isValid(Input1) && isValid(Input2)) {
  62. Operate(Input1, Input2, temp, MAX, 'R');
  63. validLine[j] = 1;
  64. }
  65. } else fgets(NA, 10000, fp);
  66. } else if (strcmp(tmp, "DECODER") == 0) { // ========================== Decoder Case ==============================
  67. if (validLine[j] == 0) {
  68. int nInputs, VALID = 0;
  69. fscanf(fp, "%d ", &nInputs);
  70. int nOutputs = pow(2, nInputs);
  71. char **inputs = malloc(sizeof(char*) * 5000);
  72. for (i = 0; i < 5000; i++) inputs[i] = malloc(sizeof(char) * 500);
  73. char **outputs = malloc(sizeof(char*) * 10000);
  74. for (i = 0; i < 5000; i++) outputs[i] = malloc(sizeof(char) * 500);
  75. for (i = 0; i < nInputs; i++) fscanf(fp, "%s ", &inputs[i][0]);
  76. for (i = 0; i < nOutputs; i++) (i == nOutputs-1) ? fscanf(fp, "%s\n", &outputs[i][0]) : fscanf(fp, "%s ", &outputs[i][0]);
  77. for (i = 0; i < nInputs; i++) {
  78. if (isValid(inputs[i])) {
  79. VALID = 1;
  80. } else {
  81. VALID = 0;
  82. break;
  83. }
  84. }
  85. if (VALID == 1) {
  86. Decoder(nInputs, nOutputs, inputs, outputs, MAX);
  87. validLine[j] = 1;
  88. }
  89. } else fgets(NA, 10000, fp);
  90. } else if (strcmp(tmp, "MULTIPLEXER") == 0) { // ======================= Multiplexer Case ==============================
  91. if (validLine[j] == 0) {
  92. int nInputs, nSelectors, VALID = 0;
  93. fscanf(fp, "%d ", &nInputs);
  94. if (power2(nInputs) == 0) {
  95. printf("error");
  96. return 0;
  97. }
  98. nSelectors = log(nInputs)/log(2);
  99. char **inputs = malloc(sizeof(char*) * 5000);
  100. for (i = 0; i < 5000; i++) inputs[i] = malloc(sizeof(char) * 500);
  101. char **selectors = malloc(sizeof(char*) * 5000);
  102. for (i = 0; i < 5000; i++) selectors[i] = malloc(sizeof(char) * 500);
  103. char output[500];
  104. for (i = 0; i < nInputs; i++) fscanf(fp, "%s ", &inputs[i][0]);
  105. for (i = 0; i < nSelectors; i++) fscanf(fp, "%s ", &selectors[i][0]);
  106. fscanf(fp, "%s\n", output);
  107. for (i = 0; i < nInputs; i++) {
  108. if (isValid(inputs[i])) {
  109. VALID = 1;
  110. } else {
  111. VALID = 0;
  112. break;
  113. }
  114. }
  115. if (VALID == 1) {
  116. for (i = 0; i < nSelectors; i++) {
  117. if (isValid(selectors[i])) {
  118. VALID = 1;
  119. } else {
  120. VALID = 0;
  121. break;
  122. }
  123. }
  124. }
  125. if (VALID == 1) {
  126. Multiplexer(nInputs, nSelectors, inputs, selectors, output, MAX);
  127. validLine[j] = 1;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement