Форум программистов, компьютерный форум, киберфорум
C для начинающих
Войти
Регистрация
Восстановить пароль
Карта форума Темы раздела Блоги Сообщество Поиск Заказать работу  
 
0 / 0 / 0
Регистрация: 13.11.2020
Сообщений: 64
1

Массив структур

04.03.2021, 13:21. Показов 336. Ответов 0
Метки нет (Все метки)

Author24 — интернет-сервис помощи студентам
Код разбит на 3 файла, нужна помощь с заполнением недостающих функций. Сроки поджимают и нет времени все написать. Буду благодарен любой помощи.

исходный файл Main.c
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "Header.h"
int main()
{
    const int n = 2;
    int choice=0;
    struct Apteka* a = 0;
 
    printf("\n Menu: \n");
    printf("1. Dinamic memory allocation for a array of struct: \n");
    printf("2. Enter array elements from the keyboard: \n");
    printf("3. Writing an array of structures to a file .txt: \n");
    printf("4. Reading an array of structures from a file .txt: \n");
    printf("5. Display array elements on the screen: \n");
    printf("6. Sort array elements : \n");
    printf("7. Search for an array elements: \n");
    printf("8. Adding a new elementsto the end: \n");
    printf("9. Editing an array elements : \n");
    printf("10. Insert a new elements: \n");
    printf("11. Reading an array of structures from a file .txt: \n");
    printf("12. Delete memory of array: \n");
    printf("13. End of the program: \n");
    do
    {
        printf("\n \n Choose a number from the menu: ");
        scanf_s("%d", &choice);
        switch (choice)
        {
        case 1:
            a = Memory_Allocation(a, n);
            break;
        case 2:
            Enter_array_elements(a, n);
            break;
        case 3:
            Writing_an_array_of_structures_to_a_file(a, n);
            break;
        case 4:
            Reading_an_array_of_structures_from_a_file(a, n);
            break;
        case 5:
            Display_array_elements_on_the_screen(a, n);
            break;
        case 6:
            Sort_array_elements(a, n);
            break;
        case 7:
            Search_for_an_array_elements(a, n);
            break;
        case 8:
            Adding_a_new_elements_to_the_end(a, n);
            break;
        case 9:
            Editing_an_array_elements(a, n);
            break;
        case 10:
            Insert_a_new_elements(a, n);
            break;
        case 11:
            Reading_an_array_of_structures_from_a_file(a, n);
            break;
        case 12:
            Delete_memory_of_array(a);
            break;
        case 13:
            if (a != NULL)
                Delete_memory_of_array(a);
                printf("\n Succesful exit from the program");
                return 0;
        default:
            printf("\n Invalid input. Select a number for the menu ");
            break;
        }
    } while (choice < 13);
    return 0;
}
исходный файл function.c
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "Header.h"
//1
struct Apteka* Memory_Allocation(struct Apteka* a, int r)
{
    a = (struct Apteka*)malloc(r * sizeof(struct Apteka));
 
    if (a == NULL)
    {
        printf("\n Dinamic memory is not allocated for the array ");
        exit(1);
    }
    else
    {
        printf("\n allocated memory: %d bytes ", r * sizeof(struct Apteka));
    }
    return a;
}
//2
void Enter_array_elements(struct Apteka * a, int r)
{
    int i = 0;
    for (int i = 0; i < r; i++)
    {
        printf("\n Input Apteka's name: ");
        scanf("%s", a[i].name);
        printf("\n Input Apteka's adress: ");
        scanf("%s", a[i].adress);
        printf("\n Input Apteka's telephone: ");
        scanf("%d", &a[i].telephone);
        printf("\n Input when Apteka open: ");
        scanf("%d", &a[i].open);
        printf("\n Input when Apteka close: ");
        scanf("%d", &a[i].close);
    }
}
//3
int Writing_an_array_of_structures_to_a_file(struct Apteka* a, const int r)
{
    FILE* outFile;
    if ((outFile = fopen("Apteka.txt", "w")) == NULL)
    {
        printf("Error opening file \n");
        return 1;
    }
    for (int i = 0; i < r; i++)
    {
        fprintf(outFile, " %s %s %d %d %d \n", a[i].name, a[i].adress, a[i].telephone, a[i].open, a[i].close);
    }
    fclose(outFile);
    return 0;
}
//4
 
int Reading_an_array_of_structures_from_a_file(struct Apteka* a, const int r)
{
    FILE* outFile;
    outFile = fopen("Apteka.txt", "r");
    for (int i = 0; i < r; i++)
    {
        fprintf(outFile, "%s %s %d %d %d \n", a[i].name, a[i].adress, a[i].telephone, a[i].open, a[i].close);
    }
    fclose(outFile);
    return 0;
}
//5  
void Display_array_elements_on_the_screen(struct Apteka * a,  int r)
{
    int i=0;
    printf("\n Apteka info \n");
    printf("\n----------------------------------------------------------------------------------\n");
    printf("\n|   #  |       Name      |     Adress    |   Telephone   |  Open  |  Close  | \n");
    printf("\n----------------------------------------------------------------------------------\n");
    for (int i = 0; i < r; i++)
    {
        printf("| %4d | %-15s | %-13s | %11d | %10d | %10d | \n" , i+1,  a[i].name, a[i].adress, a[i].telephone, a[i].open, a[i].close);
        printf("\n----------------------------------------------------------------------------------\n");
    }
    printf("\n----------------------------------------------------------------------------------\n");
}
 
//6
void Sort_array_elements(struct Apteka * a, const int r) 
{
    int i, j;
    Apteka* a = new Apteka * [r];
    Apteka* tmp;
    for (int i = 0; i < r; i++)
    {
 
    }
    for (int j = 0; j < r; j++) 
    {
        for (int i = 0; i < r - j - 1; i++) 
        {
            if (a[i].open > a[i + 1].open) 
            {
                tmp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = tmp;
            }
        }
    }
    printf("\n------------------\n");
    printf("Array was successfuly sorted!");
    printf("\n------------------\n");
 
}
 
//12//
 
void Delete_memory_of_array(struct Apteka* a)
{
    if (a!=NULL)
    {
        free(a);
            a = NULL;
        printf("\n %p Memory freed", a);
    }
    return 0;
}
заголовочный файл Header.h
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#include<stdio.h>
#include<stdlib.h>
 
struct Apteka
{
    char name[40];
    char adress[60];
    int telephone;
    int open;
    int close;
};
//1//
struct Apteka* Memory_Allocation(struct Apteka* a, int r);
//2//
void Enter_array_elements(struct Apteka * a, int r);
//3//
int Writing_an_array_of_structures_to_a_file(struct Apteka* a, const int r);
//4//
int Reading_an_array_of_structures_from_a_file(struct Apteka* a, const int r);
//5//
void Display_array_elements_on_the_screen(struct Apteka* a, const int r);
//6//
void Sort_array_elements(struct Apteka* a, const int r);
//7//
void Search_for_an_array_elements(struct Apteka* a, const int r);
//8//
void Adding_a_new_elements_to_the_end(struct Apteka* a, const int r);
//9//
void Editing_an_array_elements(struct Apteka* a, const int r);
//10//
void Insert_a_new_elements(struct Apteka* a, const int r);
//11//
void Reading_an_array_of_structures_from_a_file(struct Apteka* a, const int r);
//12//
void Delete_memory_of_array(struct Apteka * a);
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
04.03.2021, 13:21
Ответы с готовыми решениями:

Составить массив структур состоящий из имени и цены,по введенному имени изменить цену,вывести обновленный массив структур
составить массив структур состоящий из имени и цены,по введенному имени изменить цену,вывести...

Массив структур, элементом которой является массив
В задачке нужно сформировать массив структур студентов (Фамилия, имя, группа, 5 оценок) и вычислить...

Передать массив указателей на массив структур в функцию
Нужно передать массив указателей на массив структур в функцию. Можешь подсказать, что нужно...

Массив структур
Может кто-нибудь помочь с этим? Ввод данных проходит вроде нормально, но выводит какие-то левые...

0
04.03.2021, 13:21
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
04.03.2021, 13:21
Помогаю со студенческими работами здесь

Массив структур
нужно сделать отдельно функцию с циклом который будет заносить данные всех полей в текстовый файл...

Сделать динамический массив структур, причем в структуре так же существует динамический массив
Добрый день. Есть такая пзадача: нужно сделать динамический массив структур, причем в структуре...

структуры, массив структур
Помогите пожалуйста, как обнулить массив структур? А именно, при выборе числа от 1 до n, необходимо...

Составить массив структур
Составить массив структур для описания квартир с полями: улица, дом, номер, стоимость. Вывести на...

Динамический массив структур
Здравствуйте,мне нужно сделать динамический массив из структур struct student { char FIO;...

Указатель на массив структур
Собственно вопрос, как реализовать? Что бы можно было потом обращаться через указатель к какому-то...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
1
Ответ Создать тему
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru