-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
130 lines (119 loc) · 3.3 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STR_MAX 256
typedef struct information {
char name[STR_MAX];
char main_info[STR_MAX];
struct information *next;
} information_t;
typedef struct ring {
information_t *first;
information_t *last;
} ring_t;
void str_input(char *information, char *input_text, int max_number_of_symbols);
int int_input(char *information, int Min, int Max);
void ring_fill(ring_t point);
int ring_add(ring_t *point);
void ring_output(char *which, ring_t *point, int elements_number);
void ring_inverse(ring_t *point, int elements_number);
int main(int argc, char **argv)
{
ring_t point;
point.first = NULL;
point.last = NULL;
ring_fill(point);
return 0;
}
void ring_fill(ring_t point)
{
int counter = 0;
puts ("\n----- Enter info about appliances('stop' for input end): -----\n");
printf ("%d)",counter + 1);
while (ring_add(&point)) {
printf ("%d)",++counter + 1);
}
point.last->next = point.first;
ring_output("normal", &point, counter);
ring_inverse(&point, counter);
ring_output("inversed", &point, counter);
}
int ring_add(ring_t *point)
{
information_t *new_elem;
new_elem = (information_t*)malloc(sizeof(*new_elem));
new_elem->next = NULL;
str_input(" name: ", new_elem->name, STR_MAX);
if (!strcmp(new_elem->name, "stop")) {
return 0;
}
str_input(" info: ", new_elem->main_info, STR_MAX);
if (point->first == NULL) {
point->first = new_elem;
}
else {
point->last->next = new_elem;
}
point->last = new_elem;
return 1;
}
void ring_inverse(ring_t *point, int counter)
{ int i,j;
information_t *info, *element;
info = point->last;
for (i = counter - 2; i > 0; i--) {
element = point->first;
for (j = 0; j < i; j++) {
element = element->next;
}
point->last->next = element;
point->last = element;
}
point->last->next = point->first;
point->last = point->first;
point->first = info;
point->last->next = point->first;
}
void ring_output(char *manner, ring_t *point, int counter)
{
int i;
information_t *info;
info = point->first;
printf ("The ring in %s manner\n", manner);
for(i = 0; i < counter; i++) {
printf ("%d) Name: %s\n Info:%s\n",
i + 1, info->name, info->main_info);
info = info->next;
}
}
void str_input(char *information, char *input_text, int max_number_of_symbols)
{
while (1) {
printf("%s", information);
fgets(input_text, max_number_of_symbols, stdin);
if (input_text[0] == '\n' || input_text[0] == ' ') {
puts("Invalid input, try again.");
}
else {
input_text[strlen(input_text)-1]='\0';
break;
}
}
}
int int_input(char *information, int Min, int Max)
{
int input_text;
char input_buffer[128];
printf("%s (min - %d, max - %d): ", information, Min, Max);
while (1) {
fgets(input_buffer, 128, stdin);
input_buffer[strlen(input_buffer)-1]='\0';
if (!(input_text = atoi(input_buffer)) || input_text < Min || input_text > Max) {
puts("Invalid input, try again.");
}
else {
break;
}
}
return input_text;
}