-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task 1.c
147 lines (126 loc) · 4.23 KB
/
Task 1.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
struct izdatel {
int god;
char naz[100];
char gor[100];
};
struct Book {
char title[100];
int price;
int numPages;
char author[100];
struct izdatel publisherInfo;
};
void check_number(const char name[], int *s) {
int ret_code;
do {
printf("Input %s:\n", name);
ret_code = scanf("%d", s);
while (getchar() != '\n'); // чистим буфер (потому что без fgets)
} while (ret_code != 1 || *s <= 0);
}
void check_year(const char name[], int *s) {
int ret_code;
do {
printf("Input %s (between 1458 and 2024):\n", name);
ret_code = scanf("%d", s);
while (getchar() != '\n');
} while (ret_code != 1 || *s < 1458 || *s > 2024);
}
void check_string(const char name[], char s[]) {
int invalid_input;
do {
invalid_input = 0;
printf("Input %s:\n", name);
scanf("%s", s);
while (getchar() != '\n');
for (int i = 0; i < strlen(s); i++) {
if (!isalpha(s[i]) && !isspace(s[i])) {
invalid_input = 1;
printf("Invalid input. Please enter a string without numbers or special characters.\n");
break;
}
}
} while (invalid_input || strlen(s) == 0);
}
void input(struct Book *books, const int numStructs) {
for (int i = 0; i < numStructs; i++) {
printf("Book #%d\n", i + 1);
check_string("Title", books[i].title);
check_number("Price", &books[i].price);
check_number("Number of pages", &books[i].numPages);
check_string("Author", books[i].author);
printf("Publishing company:\n");
check_year("Year of publication", &books[i].publisherInfo.god);
check_string("Publisher name", books[i].publisherInfo.naz);
check_string("Publishing city", books[i].publisherInfo.gor);
}
}
void output(struct Book *books, const int numStructs) {
printf("\nReceived books:\n");
for (int i = 0; i < numStructs; i++) {
printf("\nBook %d:\n", i + 1);
printf("Title: %s\n", books[i].title);
printf("Price: %d\n", books[i].price);
printf("Number of pages: %d\n", books[i].numPages);
printf("Author: %s\n", books[i].author);
printf("Publisher Info:\n");
printf("Year of publication: %d\n", books[i].publisherInfo.god);
printf("Publisher name: %s\n", books[i].publisherInfo.naz);
printf("Publishing city: %s\n", books[i].publisherInfo.gor);
}
}
char** solution(struct Book *books, const int numStructs, int *numResults){
float minPrice = books[0].price;
for (int i = 0; i < numStructs; i++) {
if (books[i].price < minPrice) {
minPrice = books[i].price;
}
}
char **result = (char **)malloc(numStructs * sizeof(char *));
if (result == NULL) {
printf("Memory failed.\n");
*numResults = 0;
return NULL;
}
*numResults = 0;
for (int i = 0; i < numStructs; i++) {
if (books[i].price == minPrice) {
int found = 0;
for (int j = 0; j < *numResults; j++) {
if (strcmp(books[i].publisherInfo.gor, result[j]) == 0) {
found = 1;
break;
}
}
if (!found) {
result[*numResults] = strdup(books[i].publisherInfo.gor);
(*numResults)++;
}
}
}
return result;
}
int main() {
int numStructs;
printf("Enter the number of books: ");
scanf("%d", &numStructs);
while (getchar() != '\n');
struct Book books[numStructs];
input(books, numStructs);
output(books, numStructs);
int numResults;
char **result = solution(books, numStructs, &numResults);
if (result != NULL) {
printf("\nCities where the cheapest books are published:\n");
for (int i = 0; i < numResults; i++) {
printf("%s\n", result[i]);
free(result[i]);
}
free(result);
}
return 0;
}