Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct queueNode {
  6. char *string;
  7. struct queueNode *next;
  8. };
  9.  
  10. struct queue {
  11. struct queueNode *head;
  12. struct queueNode *tail;
  13. };
  14.  
  15. char* stringInput() {
  16. int i = 0;
  17. int key = 0;
  18. char *string = nullptr;
  19. if (!(string = (char*)malloc(sizeof(char)))) {
  20. printf(" Memory error.\n");
  21. return NULL;
  22. }
  23. while ((key = getchar()) != '\n') {
  24. if (!(string = (char*)realloc(string, sizeof(char) * (i + 1)))) {
  25. printf(" Memory error.\n");
  26. return NULL;
  27. }
  28. string[i++] = key;
  29. }
  30. string[i] = '\0';
  31. return string;
  32. }
  33.  
  34. int numberInput() {
  35. int number = 0;
  36. char buffer = 0;
  37. printf(" ");
  38. while ((!(scanf_s(" %d%c", &number, &buffer, 1))) || (buffer != '\n') || (number <= 0)) {
  39. printf(" You've entered the wrong value. Please, enter again: ");
  40. rewind(stdin);
  41. }
  42. return number;
  43. }
  44.  
  45.  
  46. struct queueNode *createQueue(struct queue *queuePoints) {
  47. struct queueNode *node = nullptr;
  48. if (!(node = (struct queueNode*)malloc(sizeof(struct queueNode)))) {
  49. return nullptr;
  50. }
  51. node->string = stringInput();
  52. node->next = NULL;
  53. if (queuePoints->tail == NULL) {
  54. queuePoints->head = queuePoints->tail = node;
  55. return node;
  56. }
  57. queuePoints->tail->next = node;
  58. queuePoints->tail = node;
  59. return node;
  60. }
  61.  
  62. bool isSymmetric(struct queueNode *string) {
  63. int i = 0;
  64. int j = 0;
  65. int counter = 0;
  66. for (i = 0, j = strlen(string->string) - 1; i < (strlen(string->string) / 2); i++, j--) {
  67. if (string->string[i] == string->string[j]) {
  68. counter++;
  69. }
  70. }
  71. if (counter == 0) {
  72. return false;
  73. }
  74. if (counter == strlen(string->string) / 2) {
  75. return true;
  76. } else {
  77. return false;
  78. }
  79. }
  80.  
  81. bool showQueue(struct queue *queuePoints) {
  82. struct queueNode *temp = queuePoints->head;
  83. bool flag = false;
  84. while (temp) {
  85. if (isSymmetric(temp)) {
  86. if (!flag) {
  87. printf(" These strings are symmetric:\n\n");
  88. flag = true;
  89. }
  90. printf(" %s\n\n", temp->string);
  91. }
  92. temp = temp->next;
  93. }
  94. if (!flag)
  95. return false;
  96. return true;
  97. }
  98.  
  99. int main() {
  100. int switchOption = 0;
  101. char buffer = 0;
  102. int size = 0;
  103. struct queue *queuePoints = nullptr;
  104. if (!(queuePoints = (struct queue*)malloc(sizeof(struct queue)))) {
  105. return 0;
  106. }
  107. queuePoints->head = queuePoints->tail = NULL;
  108. printf(" Nice to meet you. Please, choose the required option:\n\n");
  109. do {
  110. printf(" 1. Queue elements input. \n 2. Queue elements output. \n 3. Exit. \n ");
  111. while (!(scanf_s("%d%c", &switchOption, &buffer, 1)) || switchOption < 1 || switchOption > 3 || buffer != '\n') {
  112. printf(" You've entered the wrong value. Please, enter again:\t");
  113. rewind(stdin);
  114. }
  115. system("CLS");
  116. switch (switchOption) {
  117. case 1:
  118. system("CLS");
  119. createQueue(queuePoints);
  120. break;
  121. case 2:
  122. system("CLS");
  123. if (!queuePoints->head) {
  124. printf(" Queue is empty.\n\n");
  125. break;
  126. }
  127. if (!showQueue(queuePoints)) {
  128. printf(" None are symmetric.\n\n");
  129. }
  130. break;
  131. case 3:
  132. system("CLS");
  133. free(queuePoints);
  134. printf("Thank you for using this programm!\nHave a good day!\n\n");
  135. system("exit");
  136. }
  137. } while (switchOption != 3);
  138. return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement