Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. //Из текстового файла вводится символьная строка, состоящая из чисел,
  2. //разделенных пробелом. Составить программу, которая вводит строку, организует из ее
  3. //чисел однонаправленный список, отсортированный по возрастанию их значения, считает
  4. // общее количество чисел в списке и количество чисел кратных трем. Вывести на экран
  5. // созданный список и всю найденную информацию с соответствующими комментариями.
  6. //Если искомых чисел нет вывести соответствующее сообщение.
  7.  
  8. #include <iostream>
  9. #include <cstdio>
  10. #include <string>
  11. #include <locale>
  12. #include <fstream>
  13. #include <stdlib.h>
  14. using namespace std;
  15.  
  16. struct numbs
  17. {
  18. int value;
  19. numbs *next;
  20. };
  21.  
  22. numbs *list_add(numbs **phead, int num) {
  23. numbs *head = *phead;
  24. numbs *new_word = new struct numbs;
  25. new_word->value = num;
  26. new_word->next = nullptr;
  27. if (head)
  28. {
  29. while (head->next)
  30. head = head->next;
  31. head->next = new_word;
  32. }
  33. else
  34. *phead = new_word;
  35. return new_word;
  36. }
  37.  
  38. void print_numbs(numbs *phead) {
  39. while (phead)
  40. {
  41. cout << " " << phead->value;
  42. phead = phead->next;
  43. }
  44. puts(" ");
  45. }
  46.  
  47. int count_num(numbs *phead) {
  48. int count = 0;
  49. while (phead->next) {
  50. ++count;
  51. phead = phead->next;
  52. }
  53. ++count;
  54. return count;
  55. }
  56.  
  57. int count_crat_num(numbs *phead) {
  58. int count = 0;
  59. cout << "ELements kratnie 3" << endl;
  60. while (phead->next) {
  61. if ((phead->value % 3) == 0) {
  62. ++count;
  63. cout << " "<<phead->value;
  64. }
  65. phead = phead->next;
  66. }
  67. if ((phead->value % 3) == 0) {
  68. ++count;
  69. cout << " "<<phead->value;
  70. }
  71. cout << endl;
  72. if (count == 0)
  73. cout << "No numbers kratnih 3" << endl;
  74. return count;
  75. }
  76.  
  77.  
  78. void sort_numbs(numbs **phead) {
  79. numbs *head = *phead;
  80. int ptr;
  81. int c = count_num(head);
  82. for (int i = 0; i <= c; i++) {
  83. while (head->next)
  84. {
  85. if (head->value > head->next->value)
  86. {
  87. ptr = head->value;
  88. head->value = head->next->value;
  89. head->next->value = ptr;
  90. }
  91. head = head->next;
  92. }
  93. head = *phead;
  94. }
  95. }
  96.  
  97. int main()
  98. {
  99. setlocale(0, "Rus");
  100. char val[120];
  101. char buf;
  102. numbs *head;
  103. int g, i = 0;
  104. FILE *f = fopen("numb.txt", "w+");
  105. cout<<"Enter numbers"<<endl;
  106. gets_s(val);
  107. while (*(val + i) != '\0')
  108. {
  109. fprintf(f, "%c", val[i]);
  110. ++i;
  111. }
  112. i = 0;
  113. _fcloseall();
  114.  
  115. f = fopen("numb.txt", "r+");
  116. fscanf(f, "%d", &g);
  117. head = new numbs;
  118. head->value = g;
  119. head->next = nullptr;
  120. while (!feof(f))
  121. {
  122. fscanf(f, "%d", &g);
  123. list_add(&head, g);
  124. }
  125. _fcloseall();
  126.  
  127. cout << "List from file:"<<endl;
  128. print_numbs(head);
  129. sort_numbs(&head);
  130. cout << "Sorted list:"<<endl;
  131. print_numbs(head);
  132. cout << "Amount of elements in the list: "<< count_num(head) <<endl;
  133. cout << "Amount of elements kratnih 3: "<< count_crat_num(head)<<endl;
  134. system("pause");
  135. return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement