Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Из текстового файла вводится символьная строка, состоящая из чисел,
- //разделенных пробелом. Составить программу, которая вводит строку, организует из ее
- //чисел однонаправленный список, отсортированный по возрастанию их значения, считает
- // общее количество чисел в списке и количество чисел кратных трем. Вывести на экран
- // созданный список и всю найденную информацию с соответствующими комментариями.
- //Если искомых чисел нет вывести соответствующее сообщение.
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <locale>
- #include <fstream>
- #include <stdlib.h>
- using namespace std;
- struct numbs
- {
- int value;
- numbs *next;
- };
- numbs *list_add(numbs **phead, int num) {
- numbs *head = *phead;
- numbs *new_word = new struct numbs;
- new_word->value = num;
- new_word->next = nullptr;
- if (head)
- {
- while (head->next)
- head = head->next;
- head->next = new_word;
- }
- else
- *phead = new_word;
- return new_word;
- }
- void print_numbs(numbs *phead) {
- while (phead)
- {
- cout << " " << phead->value;
- phead = phead->next;
- }
- puts(" ");
- }
- int count_num(numbs *phead) {
- int count = 0;
- while (phead->next) {
- ++count;
- phead = phead->next;
- }
- ++count;
- return count;
- }
- int count_crat_num(numbs *phead) {
- int count = 0;
- cout << "ELements kratnie 3" << endl;
- while (phead->next) {
- if ((phead->value % 3) == 0) {
- ++count;
- cout << " "<<phead->value;
- }
- phead = phead->next;
- }
- if ((phead->value % 3) == 0) {
- ++count;
- cout << " "<<phead->value;
- }
- cout << endl;
- if (count == 0)
- cout << "No numbers kratnih 3" << endl;
- return count;
- }
- void sort_numbs(numbs **phead) {
- numbs *head = *phead;
- int ptr;
- int c = count_num(head);
- for (int i = 0; i <= c; i++) {
- while (head->next)
- {
- if (head->value > head->next->value)
- {
- ptr = head->value;
- head->value = head->next->value;
- head->next->value = ptr;
- }
- head = head->next;
- }
- head = *phead;
- }
- }
- int main()
- {
- setlocale(0, "Rus");
- char val[120];
- char buf;
- numbs *head;
- int g, i = 0;
- FILE *f = fopen("numb.txt", "w+");
- cout<<"Enter numbers"<<endl;
- gets_s(val);
- while (*(val + i) != '\0')
- {
- fprintf(f, "%c", val[i]);
- ++i;
- }
- i = 0;
- _fcloseall();
- f = fopen("numb.txt", "r+");
- fscanf(f, "%d", &g);
- head = new numbs;
- head->value = g;
- head->next = nullptr;
- while (!feof(f))
- {
- fscanf(f, "%d", &g);
- list_add(&head, g);
- }
- _fcloseall();
- cout << "List from file:"<<endl;
- print_numbs(head);
- sort_numbs(&head);
- cout << "Sorted list:"<<endl;
- print_numbs(head);
- cout << "Amount of elements in the list: "<< count_num(head) <<endl;
- cout << "Amount of elements kratnih 3: "<< count_crat_num(head)<<endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement