Задача со структурами турбопролог - Prolog
Формулировка задачи:
нужно решить такую задачу.
Найти всех матерей, имеющих менее трех детей.
Как это сделать?
Решение задачи: «Задача со структурами турбопролог»
textual
Листинг программы
- // ConsoleApplication2.cpp: главный файл проекта.
- #include "stdafx.h"
- using namespace std;
- struct product{
- char Name[40];
- int weigh = 0;
- int number = 0;
- char material[40];
- product *next;
- };
- void List_New_Elem(product* &my_food, product* to_food); // добавляет элемент в список
- int Kol_Elem_List(product* my_food); // возвращает количество элементов
- bool List_NULL(product* my_food); // проверяет на пустоту
- void Print_List(product* my_food); // вывод информации о продуктах на экран
- product* pow_Stal(product* my_food); // получает массив продуктов из стали
- product* pow_plastics(product* my_food); // получает массив продуктов из пластмассы
- product* pow_aluminum(product* my_food); // получаем массив продуктов из алюминия
- void torn(product *to_fStal, product *to_food); // создает отдельный элемент
- product *End_Sort(product* my_food); // заключительная сортировка
- void TopToTop(product *to_elem, product *mass_food, int j); // переставляет элементы местами
- product *post_elemMass_to_stract(product *mass_food, int n); // перенос элементов из массива в структуру
- int main(array<System::String ^> ^args)
- {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- product *my_food = NULL;
- product *to_food = NULL;
- product *my_fStal = NULL; // продукты из стали
- product *my_fplastics = NULL; // продукты из пластмассы
- product *my_aluminum = NULL; // продукты из алюминия
- cout << " Введите количество продуктив: ";
- int k = 0;
- cin >> k;
- cout << " <br>МЕНЮ<br>" << endl;
- cout << " 1 - Добавить информацию о продуктах с клавиатуры" << endl;
- cout << " 2 - Добавить информацию о продуктах из текстового файла" << endl;
- cout << " 0 - Выход" << endl;
- cout << " <br>" << endl;
- cout << " Введите команду: ";
- int clic = 0;
- cin >> clic;
- switch (clic)
- {
- case 1:{
- for (int i = 0; i < k; i++){
- to_food = new product;
- cout << " Введите наименование продукта: ";
- cin >> to_food->Name;
- cout << " Введите вес продукта: ";
- cin >> to_food->weigh;
- cout << " Введите серийный номер продукта: ";
- cin >> to_food->number;
- cout << " Введите материал продукта: ";
- cin >> to_food->material;
- cout << "<br>ПРОДУКТ ДОБАВЛЕН<br>" << endl;
- to_food->next = NULL;
- List_New_Elem(my_food, to_food);
- }
- cout << "Полученный список продуктов: "<<endl;
- Print_List(my_food);
- break;
- }
- case 2:{
- char str[40]; // файл
- cout << " Введите имя файла: ";
- cin >> str;
- ifstream in(str); // открытие файла по заданному файлу
- for (int i = 0; i < k; i++){
- to_food = new product;
- in >> to_food->Name >> to_food->weigh >> to_food->number >> to_food->material;
- to_food->next = NULL;
- List_New_Elem(my_food, to_food);
- }
- cout << "Полученный список продуктов: " << endl;
- Print_List(my_food);
- break;
- }
- case 0:{
- return 0;
- }
- default:
- cout << "Неверная команда!" << endl;
- break;
- }
- system("pause");
- cout << endl << " ==============Отсортированные продукты============== " << endl<<endl;
- cout << " =======Полученный список продуктов из стали:======== " << endl;
- my_fStal = pow_Stal(my_food);
- Print_List(my_fStal);
- cout << " ==================================================== " << endl << endl;
- cout << " =====Полученный список продуктов из пластмассы:===== " << endl;
- my_fplastics = pow_plastics(my_food);
- Print_List(my_fplastics);
- cout << " ==================================================== " << endl << endl;
- cout << " ======Полученный список продуктов из алюминия:====== " << endl;
- my_aluminum = pow_aluminum(my_food);
- Print_List(my_aluminum);
- cout << endl << " =====================СОРТИРОВКА===================== " << endl;
- my_fStal = End_Sort(my_fStal);
- Print_List(my_fStal);
- cout << " ==================================================== " << endl;
- my_fplastics = End_Sort(my_fplastics);
- Print_List(my_fplastics);
- cout << " ==================================================== " << endl;
- my_aluminum = End_Sort(my_aluminum);
- Print_List(my_aluminum);
- system("pause");
- }
- // Добавление элемента
- void List_New_Elem(product* &my_food, product* to_food) {
- int kol = Kol_Elem_List(my_food);
- if (kol == 0){
- my_food = to_food;
- }
- else if (kol == 1){
- my_food->next = to_food;
- }
- else {
- product *my_f = my_food;
- for (int i = 1; i < kol && my_f->next != NULL; i++) {
- my_f = my_f->next;
- }
- my_f->next = to_food;
- }
- }
- // Количество элементов
- int Kol_Elem_List(product* my_food) {
- int kol = 0;
- product *to_food = my_food;
- // Проходим весь список
- while (!List_NULL(to_food)) {
- to_food = to_food->next;
- kol++;
- }
- return kol;
- }
- // Проверка на пустоту списка
- bool List_NULL(product* my_food) {
- if (my_food == NULL){
- return true; // список пуст
- }
- else{
- return false; //список не пуст
- }
- }
- // Вывод на экран
- void Print_List(product* my_food) {
- product *to_food = my_food;
- while (!List_NULL(to_food)) {
- cout << " " << to_food->Name << " " << to_food->weigh << " " << to_food->number << " " << to_food->material << endl;
- to_food = to_food->next;
- }
- }
- // получаем сталь
- product* pow_Stal(product* my_food){
- product *to_food = my_food;
- product *my_fStal = NULL;
- while (!List_NULL(to_food)){
- if (to_food->material[0] == 'С'&&to_food->material[1] == 'т'&&to_food->material[2] == 'а'&&to_food->material[3] == 'л'&&to_food->material[4] == 'ь'){
- product *to_fStal = new product;
- torn(to_fStal, to_food);
- List_New_Elem(my_fStal, to_fStal);
- }
- to_food = to_food->next;
- }
- return my_fStal;
- }
- // получаем пластмассу
- product* pow_plastics(product* my_food){
- product *to_food = my_food;
- product *my_fplastics = NULL;
- while (!List_NULL(to_food)){
- if (to_food->material[0] == 'П'&&to_food->material[1] == 'л'&&to_food->material[2] == 'а'&&to_food->material[3] == 'с'&&to_food->material[4] == 'т'&&to_food->material[5] == 'м'&&to_food->material[6] == 'а'&&to_food->material[7] == 'с'&&to_food->material[8] == 'с'&&to_food->material[9] == 'а'){
- product *to_fplastics = new product;
- torn(to_fplastics, to_food);
- List_New_Elem(my_fplastics, to_fplastics);
- }
- to_food = to_food->next;
- }
- return my_fplastics;
- }
- // получаем алюминий
- product* pow_aluminum(product* my_food){
- product *to_food = my_food;
- product *my_aluminum = NULL;
- while (!List_NULL(to_food)){
- if (to_food->material[0] == 'А'&&to_food->material[1] == 'л'&&to_food->material[2] == 'ю'&&to_food->material[3] == 'м'&&to_food->material[4] == 'и'&&to_food->material[5] == 'н'&&to_food->material[6] == 'и'&&to_food->material[7] == 'й'){
- product *to_aluminum = new product;
- torn(to_aluminum, to_food);
- List_New_Elem(my_aluminum, to_aluminum);
- }
- to_food = to_food->next;
- }
- return my_aluminum;
- }
- void torn(product *to_f, product *to_food){
- strcpy(to_f->Name, to_food->Name);
- to_f->number = to_food->number;
- to_f->weigh = to_food->weigh;
- strcpy(to_f->material, to_food->material);
- to_f->next = NULL;
- }
- product *End_Sort(product* my_food){
- product *to_food = my_food;
- product *mass_food;
- product *to_elem = new product; // буферная переменная
- // выделение памяти
- mass_food = new product[Kol_Elem_List(my_food)];
- // цикл переноса элементов структуры в массив
- for (int i = 0; i < Kol_Elem_List(my_food); i++){
- product *to_f = new product;
- torn(to_f, to_food);
- strcpy((mass_food[i]).Name, to_f->Name);
- mass_food[i].number = to_f->number;
- mass_food[i].weigh = to_f->weigh;
- strcpy((mass_food[i].material), to_f->material);
- mass_food[i].next = NULL;
- if (!List_NULL(to_food)){
- to_food = to_food->next;
- }
- }
- // сортировка массива
- for (int i = Kol_Elem_List(my_food) - 1;i > 0; i--){
- for (int j = 0; j<i; j++){
- if (mass_food[j].number > mass_food[j + 1].number){
- TopToTop(to_elem,mass_food, j);
- }
- if (mass_food[j].number == mass_food[j + 1].number){
- if (mass_food[j].weigh < mass_food[j + 1].weigh){
- TopToTop(to_elem,mass_food, j);
- }
- }
- }
- }
- my_food = post_elemMass_to_stract(mass_food, Kol_Elem_List(my_food));
- return my_food;
- }
- void TopToTop(product *to_elem, product *mass_food, int j){
- // копируем элемент
- strcpy(to_elem->Name, mass_food[j].Name);
- to_elem->number = mass_food[j].number;
- to_elem->weigh = mass_food[j].weigh;
- strcpy(to_elem->material, mass_food[j].material);
- to_elem->next = NULL;
- // переносим значение из следующего элемента
- mass_food[j] = mass_food[j + 1];
- // следующий элемент становится предыдущем
- strcpy((mass_food[j + 1]).Name, to_elem->Name);
- mass_food[j + 1].number = to_elem->number;
- mass_food[j + 1].weigh = to_elem->weigh;
- strcpy((mass_food[j + 1].material), to_elem->material);
- mass_food[j + 1].next = NULL;
- }
- product *post_elemMass_to_stract(product *mass_food,int n){
- product *to_food = new product;
- to_food = NULL;
- for (int j = 0; j < n; j++){
- product *to_elem = new product;
- strcpy(to_elem->Name, mass_food[j].Name);
- to_elem->number = mass_food[j].number;
- to_elem->weigh = mass_food[j].weigh;
- strcpy(to_elem->material, mass_food[j].material);
- to_elem->next = NULL;
- List_New_Elem(to_food, to_elem);
- }
- return to_food;
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д